Skip to content

Commit 69309ed

Browse files
author
Johannes Weber
committed
chore: Use separate drag handle hooks for the drag/resize handle.
1 parent ecb6416 commit 69309ed

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/internal/item-container/index.tsx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,18 @@ function ItemContainerComponent(
321321
// When drag- or resize handle on palette or board item loses focus the transition must be submitted with two exceptions:
322322
// 1. If the last interaction is not "keyboard" (the user clicked on another handle issuing a new transition);
323323
// 2. If the item is acquired by the board (in that case the focus moves to the board item which is expected, palette item is hidden and all events handlers must be muted).
324-
dragInteractionHook.processBlur();
324+
selectedHook.current.processBlur();
325325
if (acquired || (transition && transition.interactionType === "keyboard" && !muteEventsRef.current)) {
326326
draggableApi.submitTransition();
327327
}
328328
}
329329

330330
function handleGlobalPointerMove(event: PointerEvent) {
331-
dragInteractionHook.processPointerMove(event);
331+
selectedHook.current.processPointerMove(event);
332332
}
333333

334334
function handleGlobalPointerUp(event: PointerEvent) {
335-
dragInteractionHook.processPointerUp(event);
335+
selectedHook.current.processPointerUp(event);
336336
// Clean up global listeners after interaction ends
337337
window.removeEventListener("pointermove", handleGlobalPointerMove);
338338
window.removeEventListener("pointerup", handleGlobalPointerUp);
@@ -346,7 +346,13 @@ function ItemContainerComponent(
346346
return;
347347
}
348348

349-
dragInteractionHook.processPointerDown(event.nativeEvent, operation);
349+
if (operation === "drag") {
350+
selectedHook.current = dragInteractionHook;
351+
} else {
352+
selectedHook.current = resizeInteractionHook;
353+
}
354+
selectedHook.current.processPointerDown(event.nativeEvent);
355+
350356
// If pointerdown is on our button, start listening for global move and up
351357
window.addEventListener("pointermove", handleGlobalPointerMove);
352358
window.addEventListener("pointerup", handleGlobalPointerUp);
@@ -435,30 +441,28 @@ function ItemContainerComponent(
435441
},
436442
}));
437443

438-
const hookProps: UseInternalDragHandleInteractionStateProps<HandleOperation> = {
439-
onDndStartAction: (event, handleOperation) => {
440-
handlePointerInteractionStart(event, handleOperation!);
441-
},
442-
onDndActiveAction: (event) => {
443-
onHandleDndTransitionActive(event);
444-
},
445-
onDndEndAction: () => {
446-
if (transition) {
447-
draggableApi.submitTransition();
448-
}
449-
},
450-
onUapActionStartAction: (handleOperation) => {
451-
handleIncrementalTransition(handleOperation!);
452-
},
444+
const dragHookProps: UseInternalDragHandleInteractionStateProps = {
445+
onDndStartAction: (event) => handlePointerInteractionStart(event, "drag"),
446+
onDndActiveAction: onHandleDndTransitionActive,
447+
onDndEndAction: () => transition && draggableApi.submitTransition(),
448+
onUapActionStartAction: () => handleIncrementalTransition("drag"),
453449
};
454-
455-
const dragInteractionHook = useInternalDragHandleInteractionState<HandleOperation>(hookProps);
450+
const resizeHookProps: UseInternalDragHandleInteractionStateProps = {
451+
onDndStartAction: (event) => handlePointerInteractionStart(event, "resize"),
452+
onDndActiveAction: onHandleDndTransitionActive,
453+
onDndEndAction: () => transition && draggableApi.submitTransition(),
454+
onUapActionStartAction: () => handleIncrementalTransition("resize"),
455+
};
456+
const dragInteractionHook = useInternalDragHandleInteractionState(dragHookProps);
457+
const resizeInteractionHook = useInternalDragHandleInteractionState(resizeHookProps);
458+
// We use a ref to the hook for the handle which is currently active. Distinguishment is managed in the handle button's onPointerDown callback.
459+
const selectedHook = useRef(dragInteractionHook);
456460

457461
const isActive = (!!transition && !isHidden) || !!acquired;
458462
const shouldUsePortal =
459463
transition?.operation === "insert" &&
460464
transition?.interactionType === "pointer" &&
461-
dragInteractionHook.interaction.value === "dnd-active";
465+
selectedHook.current.interaction.value === "dnd-active";
462466
const childrenRef = useRef<ReactNode>(null);
463467
if (!inTransition || isActive) {
464468
childrenRef.current = children(!!transition?.hasDropTarget);
@@ -497,7 +501,7 @@ function ItemContainerComponent(
497501
activeState: determineHandleActiveState({
498502
isHandleActive: isActive,
499503
currentTransition: transition,
500-
interactionHookValue: dragInteractionHook.interaction.value,
504+
interactionHookValue: resizeInteractionHook.interaction.value,
501505
targetOperation: "resize",
502506
}),
503507
onDirectionClick: handleDirectionalMovement,

0 commit comments

Comments
 (0)