Skip to content

Commit

Permalink
fix: replace mouse event with pointer event to support touch devices (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam authored Oct 4, 2023
1 parent 6e3ba05 commit db36620
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
20 changes: 13 additions & 7 deletions src/components/split-pane/SplitPane.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import {
MouseEvent as ReactMouseEvent,
PointerEvent as ReactPointerEvent,
createContext,
CSSProperties,
ReactNode,
Expand Down Expand Up @@ -114,7 +114,7 @@ export function SplitPane(props: SplitPaneProps) {
}, [closePane, closed, openPane]);

const splitterRef = useRef<HTMLDivElement>(null);
const { onMouseDown } = useSplitPaneSize({
const { onPointerDown } = useSplitPaneSize({
controlledSide,
direction,
splitterRef,
Expand Down Expand Up @@ -183,7 +183,7 @@ export function SplitPane(props: SplitPaneProps) {

<Splitter
onDoubleClick={handleToggle}
onMouseDown={isFinalClosed ? undefined : onMouseDown}
onPointerDown={isFinalClosed ? undefined : onPointerDown}
isFinalClosed={isFinalClosed}
direction={direction}
splitterRef={splitterRef}
Expand All @@ -196,19 +196,24 @@ export function SplitPane(props: SplitPaneProps) {

interface SplitterProps {
onDoubleClick: () => void;
onMouseDown?: (event: ReactMouseEvent) => void;
onPointerDown?: (event: ReactPointerEvent) => void;
direction: SplitPaneDirection;
isFinalClosed: boolean;
splitterRef: RefObject<HTMLDivElement>;
}

function Splitter(props: SplitterProps) {
const { onDoubleClick, onMouseDown, direction, isFinalClosed, splitterRef } =
props;
const {
onDoubleClick,
onPointerDown,
direction,
isFinalClosed,
splitterRef,
} = props;
return (
<div
onDoubleClick={onDoubleClick}
onMouseDown={onMouseDown}
onPointerDown={onPointerDown}
css={getSeparatorStyle(direction, !isFinalClosed)}
ref={splitterRef}
>
Expand Down Expand Up @@ -298,6 +303,7 @@ function getSeparatorStyle(direction: SplitPaneDirection, enabled: boolean) {
alignItems: 'center',
userSelect: 'none',
minWidth: 11,
touchAction: 'none',
':hover': {
backgroundColor: 'rgb(223, 223, 223)',
},
Expand Down
16 changes: 8 additions & 8 deletions src/components/split-pane/useSplitPaneSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export function useSplitPaneSize(options: UseSplitPaneSizeOptions) {
onResize,
} = options;

function mouseDownCallback() {
function downCallback() {
let lastSize: [number, SplitPaneType] | null = null;
function onMouseMove(event: MouseEvent) {
function moveCallback(event: PointerEvent) {
if (!splitterRef.current) return;
const { clientX, clientY } = event;
const parentDiv = splitterRef.current.parentElement as HTMLDivElement;
Expand Down Expand Up @@ -69,20 +69,20 @@ export function useSplitPaneSize(options: UseSplitPaneSizeOptions) {
}
}

function mouseUpCallback() {
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', mouseUpCallback);
function upCallback() {
window.removeEventListener('pointermove', moveCallback);
window.removeEventListener('pointerup', upCallback);
if (lastSize && onResize) {
onResize(`${lastSize[0]}${lastSize[1]}`);
}
}

window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', mouseUpCallback);
window.addEventListener('pointermove', moveCallback);
window.addEventListener('pointerup', upCallback);
}

return {
onMouseDown: mouseDownCallback,
onPointerDown: downCallback,
};
}

Expand Down

0 comments on commit db36620

Please sign in to comment.