react-weekly-table
Version:
React weekly scheduler <br/> By default build time ranges for a week, supports up to 31 days <br/> Can work with different timezones, data always return to UTC+0
86 lines (85 loc) • 2.55 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useCallback, useContext, useRef, useState, } from 'react';
import { ACTION } from '../common';
const PointerLockContext = createContext({});
/**
* Pointer lock provider context hook
* @returns [PointerLockProps]{@link PointerLockProps}
*/
export const usePointerLock = () => useContext(PointerLockContext);
/**
* This provides used pointer-lock browser api
* It using with all timeblock moving actions
*/
const PointerLockProvider = ({ children }) => {
const moveRef = useRef(null);
const resTopRef = useRef(null);
const resBotRef = useRef(null);
/**
* Cursor locking state
*/
const [isLocking, setIsLocking] = useState(false);
/**
* Current movement state
*/
const [movement, setMovement] = useState(0);
/**
* Action code
*/
const [action, setAction] = useState(ACTION.MOVE);
/**
* Cursor locking callback
*/
const startDrawing = useCallback((action) => {
if (!moveRef.current || !resTopRef.current || !resBotRef.current)
return;
setAction(action);
setIsLocking(true);
switch (action) {
case ACTION.MOVE: {
moveRef.current.requestPointerLock();
break;
}
case ACTION.RESIZE_BOT: {
resBotRef.current.requestPointerLock();
break;
}
case ACTION.RESIZE_TOP: {
resTopRef.current.requestPointerLock();
break;
}
default:
setIsLocking(false);
break;
}
}, []);
/**
* Cursor unlocking callback
*/
const finishDrawing = useCallback(() => {
setIsLocking(false);
document.exitPointerLock();
}, []);
/**
* Save current movement
*/
const draw = useCallback(({ nativeEvent }) => {
if (!isLocking) {
return;
}
const { movementY } = nativeEvent;
setMovement(movementY);
}, [isLocking]);
return (_jsx(PointerLockContext.Provider, Object.assign({ value: {
draw,
moveRef,
resBotRef,
resTopRef,
isLocking,
movement,
startDrawing,
finishDrawing,
action,
} }, { children: children })));
};
export default PointerLockProvider;