Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function useDragHandle(args: Args): ?DragHandleProps {
// Capturing
const capturingRef = useRef(null);
const onCaptureStart = useCallbackOne((abort: () => void) => {
invariant(
!capturingRef.current,
'Cannot start capturing while something else is',
);
capturingRef.current = {
abort,
};
}, []);
const onCaptureEnd = useCallbackOne(() => {
invariant(
capturingRef.current,
'Cannot stop capturing while nothing is capturing',
);
capturingRef.current = null;
}, []);
const abortCapture = useCallbackOne(() => {
invariant(capturingRef.current, 'Cannot abort capture when there is none');
capturingRef.current.abort();
}, []);
const { canLift, style: styleContext }: AppContextValue = useRequiredContext(
AppContext,
);
const {
isDragging,
{
eventName: supportedPageVisibilityEventName,
fn: cancel,
},
];
return bindings;
}, [
callbacks,
cancel,
getIsCapturing,
getShouldRespectForcePress,
schedule,
stop,
]);
const bindWindowEvents = useCallbackOne(() => {
const win: HTMLElement = getWindow();
const options = { capture: true };
// setting up our unbind before we bind
unbindWindowEventsRef.current = () =>
unbindEvents(win, windowBindings, options);
bindEvents(win, windowBindings, options);
}, [getWindow, windowBindings]);
const startDragging = useCallbackOne(() => {
const pending: ?PendingDrag = pendingRef.current;
invariant(pending, 'Cannot start a drag without a pending drag');
isDraggingRef.current = true;
pendingRef.current = null;
schedule,
stop,
]);
const bindWindowEvents = useCallbackOne(() => {
const win: HTMLElement = getWindow();
const options = { capture: true };
// setting up our unbind before we bind
unbindWindowEventsRef.current = () =>
unbindEvents(win, windowBindings, options);
bindEvents(win, windowBindings, options);
}, [getWindow, windowBindings]);
const startDragging = useCallbackOne(() => {
const pending: ?PendingDrag = pendingRef.current;
invariant(pending, 'Cannot start a drag without a pending drag');
isDraggingRef.current = true;
pendingRef.current = null;
hasMovedRef.current = false;
callbacks.onLift({
clientSelection: pending.point,
movementMode: 'FLUID',
});
}, [callbacks]);
const startPendingDrag = useCallbackOne(
(event: TouchEvent) => {
invariant(!pendingRef.current, 'Expected there to be no pending drag');
draggableId,
callbacks,
getDraggableRef,
getShouldRespectForcePress,
canDragInteractiveElements,
} = args;
const lastArgsRef = usePreviousRef(args);
useValidation(isEnabled, getDraggableRef);
const getWindow = useCallbackOne(
(): HTMLElement => getWindowFromEl(getDraggableRef()),
[getDraggableRef],
);
const canStartCapturing = useCallbackOne(
(event: Event) => {
// Cannot lift when disabled
if (!isEnabled) {
return false;
}
// Something on this element might be capturing.
// A drag might not have started yet
// We want to prevent anything else from capturing
if (capturingRef.current) {
return false;
}
// Do not drag if anything else in the system is dragging
if (!canLift(draggableId)) {
return false;
}
AppContext,
);
const {
isDragging,
isEnabled,
draggableId,
callbacks,
getDraggableRef,
getShouldRespectForcePress,
canDragInteractiveElements,
} = args;
const lastArgsRef = usePreviousRef(args);
useValidation(isEnabled, getDraggableRef);
const getWindow = useCallbackOne(
(): HTMLElement => getWindowFromEl(getDraggableRef()),
[getDraggableRef],
);
const canStartCapturing = useCallbackOne(
(event: Event) => {
// Cannot lift when disabled
if (!isEnabled) {
return false;
}
// Something on this element might be capturing.
// A drag might not have started yet
// We want to prevent anything else from capturing
if (capturingRef.current) {
return false;
}
[],
);
const unregister = useCallbackOne((id: DroppableId) => {
delete cacheRef.current[id];
}, []);
const registration: DroppableResponderRegistration = useMemoOne(
() => ({
register,
unregister,
}),
[register, unregister],
);
const getDroppableResponders = useCallbackOne((id: DroppableId) => {
const getResponders: ?GetResponders = cacheRef.current[id];
invariant(getResponders, `Could not find droppable reponder for id: ${id}`);
return getResponders();
}, []);
const result: Result = useMemoOne(
() => ({
registration,
getDroppableResponders,
}),
[registration, getDroppableResponders],
);
return result;
invariant(
!capturingRef.current,
'Cannot start capturing while something else is',
);
capturingRef.current = {
abort,
};
}, []);
const onCaptureEnd = useCallbackOne(() => {
invariant(
capturingRef.current,
'Cannot stop capturing while nothing is capturing',
);
capturingRef.current = null;
}, []);
const abortCapture = useCallbackOne(() => {
invariant(capturingRef.current, 'Cannot abort capture when there is none');
capturingRef.current.abort();
}, []);
const { canLift, style: styleContext }: AppContextValue = useRequiredContext(
AppContext,
);
const {
isDragging,
isEnabled,
draggableId,
callbacks,
getDraggableRef,
getShouldRespectForcePress,
canDragInteractiveElements,
} = args;
const startDragging = useCallbackOne(() => {
const pending: ?PendingDrag = pendingRef.current;
invariant(pending, 'Cannot start a drag without a pending drag');
isDraggingRef.current = true;
pendingRef.current = null;
hasMovedRef.current = false;
callbacks.onLift({
clientSelection: pending.point,
movementMode: 'FLUID',
});
}, [callbacks]);
const startPendingDrag = useCallbackOne(
(event: TouchEvent) => {
invariant(!pendingRef.current, 'Expected there to be no pending drag');
const touch: Touch = event.touches[0];
const { clientX, clientY } = touch;
const point: Position = {
x: clientX,
y: clientY,
};
const longPressTimerId: TimeoutID = setTimeout(
startDragging,
timeForLongPress,
);
const pending: PendingDrag = {
point,
longPressTimerId,
export default function useTouchSensor(args: Args): OnTouchStart {
const {
callbacks,
getWindow,
canStartCapturing,
getShouldRespectForcePress,
onCaptureStart,
onCaptureEnd,
} = args;
const pendingRef = useRef(null);
const isDraggingRef = useRef(false);
const hasMovedRef = useRef(false);
const unbindWindowEventsRef = useRef<() => void>(noop);
const getIsCapturing = useCallbackOne(
() => Boolean(pendingRef.current || isDraggingRef.current),
[],
);
const postDragClickPreventer: EventPreventer = useMemoOne(
() => createPostDragEventPreventer(getWindow),
[getWindow],
);
const schedule = useMemoOne(() => {
invariant(
!getIsCapturing(),
'Should not recreate scheduler while capturing',
);
return createScheduler(callbacks);
}, [callbacks, getIsCapturing]);
export default function useResponders(props: Props) {
const { droppableId } = props;
const previousRef = usePrevious(props);
const context: AppContextValue = useRequiredContext(AppContext);
const registration: DroppableResponderRegistration =
context.droppableResponderRegistration;
const getResponders = useCallbackOne(
(): Responders => ({
onBeforeDragStart: previousRef.onBeforeDragStart,
onDragStart: previousRef.onDragStart,
onDragUpdate: previousRef.onDragUpdate,
onDragEnd: previousRef.onDragEnd,
}),
[],
);
useIsomorphicLayoutEffect(() => {
registration.register(droppableId, getResponders);
return () => registration.unregister(droppableId);
}, [droppableId, getResponders, registration]);
}