Skip to content

Commit

Permalink
fix: mouse status is now checked when entering document
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zurg committed Jun 18, 2020
1 parent da9100d commit ebc11ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback } from 'react';
import { BeginDragCallback, ClientPosition } from '../effects/useDragState';
import { ChildPane } from '../useSplitPaneResize';
import { SplitPaneHooks, SplitType } from '../..';
import { SplitPaneHooks } from '../..';
import { useGetCurrentPaneSizes } from './useGetCurrentPaneSizes';

/**
Expand All @@ -10,16 +9,12 @@ import { useGetCurrentPaneSizes } from './useGetCurrentPaneSizes';
export function useHandleDragStart({
isReversed,
hooks,
childPanes,
split,
beginDrag,
setSizes,
getCurrentPaneSizes,
}: {
isReversed: boolean;
childPanes: Omit<ChildPane, 'size'>[];
hooks?: SplitPaneHooks;
split: SplitType;
beginDrag: BeginDragCallback<any>;
setSizes: React.Dispatch<React.SetStateAction<number[]>>;
getCurrentPaneSizes: ReturnType<typeof useGetCurrentPaneSizes>;
Expand All @@ -31,6 +26,6 @@ export function useHandleDragStart({
beginDrag(pos, { index: isReversed ? index - 1 : index });
setSizes(clientSizes);
},
[beginDrag, childPanes, hooks, isReversed, split]
[beginDrag, getCurrentPaneSizes, hooks, isReversed, setSizes]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function useUpdateCollapsedSizes({
return movedSizes[idx]; // when collapsed store current size
}
if (!isCollapsed && size !== null) {
console.log(`uncollapsing index ${idx} with size ${size}`);
unCollapseSize({ idx, size }); // when un-collapsed clear size
hooks?.onChange?.(sizes);
return null;
Expand Down
28 changes: 20 additions & 8 deletions src/components/SplitPane/hooks/effects/useDragState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface DragStateHandlers<T> {
onMouseMove?: (event: ClientPosition) => void;
onTouchMove?: (event: TouchEvent) => void;
onMouseUp?: () => void;
onMouseEnter?: (event: MouseEvent) => void;
}

function useDragStateHandlers<T>(
Expand Down Expand Up @@ -55,7 +56,7 @@ function useDragStateHandlers<T>(
return [dragState, onMouseUp];
}, [current, dragging, onDragFinished]);

const [onMouseMove, onTouchMove] = useMemo(() => {
const [onMouseMove, onTouchMove, onMouseEnter] = useMemo(() => {
if (!dragging) {
return [undefined, undefined];
}
Expand All @@ -68,25 +69,36 @@ function useDragStateHandlers<T>(
const onTouchMove = (event: TouchEvent): void => {
onMouseMove(event.touches[0]);
};
const onMouseEnter = (event: MouseEvent): void => {
const isPrimaryPressed = (event.buttons & 1) === 1;
if (!isPrimaryPressed) {
onMouseUp?.();
}
};

return [onMouseMove, onTouchMove];
}, [dragging, split]);
return [onMouseMove, onTouchMove, onMouseEnter];
}, [dragging, onMouseUp, split]);

return { beginDrag, dragState, onMouseMove, onTouchMove, onMouseUp };
return { beginDrag, dragState, onMouseMove, onTouchMove, onMouseUp, onMouseEnter };
}

export function useDragState<T>(
split: SplitType,
onDragFinished: (dragState: DragState<T>) => void
): [DragState<T> | null, (pos: ClientPosition, extraState: T) => void] {
const { beginDrag, dragState, onMouseMove, onTouchMove, onMouseUp } = useDragStateHandlers<T>(
split,
onDragFinished
);
const {
beginDrag,
dragState,
onMouseMove,
onTouchMove,
onMouseUp,
onMouseEnter,
} = useDragStateHandlers<T>(split, onDragFinished);

useEventListener('mousemove', onMouseMove);
useEventListener('touchmove', onTouchMove);
useEventListener('mouseup', onMouseUp);
useEventListener('mouseenter', onMouseEnter);

return [dragState, beginDrag];
}
2 changes: 0 additions & 2 deletions src/components/SplitPane/hooks/useSplitPaneResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ export function useSplitPaneResize(options: SplitPaneResizeOptions): SplitPaneRe
setSizes,
isReversed,
hooks,
split,
childPanes,
beginDrag,
getCurrentPaneSizes,
});
Expand Down

0 comments on commit ebc11ae

Please sign in to comment.