Skip to content

Commit

Permalink
Fix a bug with touch event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Jan 30, 2024
1 parent 4729287 commit 6fb837b
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
background-color: var(--color-resize-bar-drag);
}

@media (max-width: 500px) {
@media (pointer: coarse) {
.ResizeHandle {
flex: 0 0 1rem;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useLayoutEffect } from "react";
import styles from "./VisibleCursor.module.css";
import { getResizeEventCoordinates } from "../../../react-resizable-panels/src/utils/events/getResizeEventCoordinates";

export function VisibleCursor() {
useLayoutEffect(() => {
Expand All @@ -13,9 +14,10 @@ export function VisibleCursor() {
element.setAttribute("data-state", "down");
};

const onMouseMove = (event: MouseEvent) => {
element.style.left = event.pageX + "px";
element.style.top = event.pageY + "px";
const onMouseMove = (event: MouseEvent | TouchEvent) => {
const { x, y } = getResizeEventCoordinates(event);
element.style.left = x + "px";
element.style.top = y + "px";
};

const onMouseUp = () => {
Expand All @@ -25,13 +27,21 @@ export function VisibleCursor() {
document.addEventListener("mousedown", onMouseDown, true);
document.addEventListener("mousemove", onMouseMove, true);
document.addEventListener("mouseup", onMouseUp, true);
document.addEventListener("touchcancel", onMouseUp, true);
document.addEventListener("touchend", onMouseUp, true);
document.addEventListener("touchmove", onMouseMove, true);
document.addEventListener("touchstart", onMouseDown, true);

return () => {
document.body.removeChild(element);

document.removeEventListener("mousedown", onMouseDown, true);
document.removeEventListener("mousemove", onMouseMove, true);
document.removeEventListener("mouseup", onMouseUp, true);
document.removeEventListener("touchcancel", onMouseUp, true);
document.removeEventListener("touchend", onMouseUp, true);
document.removeEventListener("touchmove", onMouseMove, true);
document.removeEventListener("touchstart", onMouseDown, true);
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
background-color: var(--color-button-background-hover);
}

@media (max-width: 500px) {
@media (pointer: coarse) {
.ResizeHandle,
.ResizeHandleCollapsed {
width: 1rem;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import debounce from "./utils/debounce";
import { determinePivotIndices } from "./utils/determinePivotIndices";
import { getResizeHandleElement } from "./utils/dom/getResizeHandleElement";
import { isKeyDown, isMouseEvent, isTouchEvent } from "./utils/events";
import { getResizeEventCursorPosition } from "./utils/getResizeEventCursorPosition";
import { getResizeEventCursorPosition } from "./utils/events/getResizeEventCursorPosition";
import { initializeDefaultStorage } from "./utils/initializeDefaultStorage";
import {
loadPanelGroupState,
Expand Down
33 changes: 7 additions & 26 deletions packages/react-resizable-panels/src/PanelResizeHandleRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Direction, ResizeEvent } from "./types";
import { resetGlobalCursorStyle, setGlobalCursorStyle } from "./utils/cursor";
import { isMouseEvent, isTouchEvent } from "./utils/events";
import { getResizeEventCoordinates } from "./utils/events/getResizeEventCoordinates";

export type ResizeHandlerAction = "down" | "move" | "up";
export type ResizeHandlerState = "drag" | "hover" | "inactive";
Expand Down Expand Up @@ -67,33 +67,14 @@ export function registerResizeHandle(
};
}

function getEventCoordinates(event: ResizeEvent) {
if (isMouseEvent(event)) {
return {
x: event.pageX,
y: event.pageY,
};
} else if (isTouchEvent(event)) {
const touch = event.touches[0];
if (touch && touch.pageX && touch.pageY) {
return {
x: touch.pageX,
y: touch.pageY,
};
}
}

return {
x: Infinity,
y: Infinity,
};
}

function handlePointerDown(event: ResizeEvent) {
const { x, y } = getResizeEventCoordinates(event);

isPointerDown = true;

updateResizeHandlerStates("down", event);

recalculateIntersectingHandles({ x, y });
updateListeners();

if (intersectingHandles.length > 0) {
Expand All @@ -102,7 +83,7 @@ function handlePointerDown(event: ResizeEvent) {
}

function handlePointerMove(event: ResizeEvent) {
const { x, y } = getEventCoordinates(event);
const { x, y } = getResizeEventCoordinates(event);

if (isPointerDown) {
intersectingHandles.forEach((data) => {
Expand All @@ -125,7 +106,7 @@ function handlePointerMove(event: ResizeEvent) {
}

function handlePointerUp(event: ResizeEvent) {
const { x, y } = getEventCoordinates(event);
const { x, y } = getResizeEventCoordinates(event);

panelConstraintFlags.clear();
isPointerDown = false;
Expand Down Expand Up @@ -232,11 +213,11 @@ function updateListeners() {

if (count > 0) {
body.addEventListener("contextmenu", handlePointerUp);
body.addEventListener("mouseleave", handlePointerMove);
body.addEventListener("mousemove", handlePointerMove);
body.addEventListener("touchmove", handlePointerMove, {
passive: false,
});
body.addEventListener("mouseleave", handlePointerMove);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Direction } from "../types";
import { assert } from "./assert";
import { getPanelGroupElement } from "./dom/getPanelGroupElement";
import { getResizeHandleElement } from "./dom/getResizeHandleElement";
import { getResizeEventCursorPosition } from "./getResizeEventCursorPosition";
import { getResizeEventCursorPosition } from "./events/getResizeEventCursorPosition";

export function calculateDragOffsetPercentage(
event: ResizeEvent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ResizeEvent } from "../../types";
import { isMouseEvent, isTouchEvent } from ".";

export function getResizeEventCoordinates(event: ResizeEvent) {
if (isMouseEvent(event)) {
return {
x: event.pageX,
y: event.pageY,
};
} else if (isTouchEvent(event)) {
const touch = event.touches[0];
if (touch && touch.pageX && touch.pageY) {
return {
x: touch.pageX,
y: touch.pageY,
};
}
}

return {
x: Infinity,
y: Infinity,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ResizeEvent } from "../../PanelGroupContext";
import { Direction } from "../../types";
import { getResizeEventCoordinates } from "./getResizeEventCoordinates";

export function getResizeEventCursorPosition(
direction: Direction,
event: ResizeEvent
): number {
const isHorizontal = direction === "horizontal";

const { x, y } = getResizeEventCoordinates(event);

return isHorizontal ? x : y;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResizeEvent } from "../PanelGroupContext";
import { ResizeEvent } from "../../PanelGroupContext";

export function isKeyDown(event: ResizeEvent): event is KeyboardEvent {
return event.type === "keydown";
Expand Down

This file was deleted.

0 comments on commit 6fb837b

Please sign in to comment.