An open source design system that's fully customizable and agent ready
TypeScript
fix(BottomSheet): yield to a pinch whichever finger lands first (#7198)
Cindy Zhang59b2863924f9
diff --git a/.changeset/bottom-sheet-pinch-zoom.md b/.changeset/bottom-sheet-pinch-zoom.mdindex d8ede668a6..e9cf15fb3a 100644--- a/.changeset/bottom-sheet-pinch-zoom.md+++ b/.changeset/bottom-sheet-pinch-zoom.md@@ -2,6 +2,6 @@'@astryxdesign/core': patch----[fix] BottomSheet no longer blocks pinch-zoom. A pinch that started on an open sheet (its handle, its content, or across both) did nothing, because the sheet claimed every touch gesture: `touch-action: none` on the sheet and handle, `pan-y` on the content. The sheet now leaves pinch to the browser (`pinch-zoom`, and `pan-y pinch-zoom` on the content), and a second finger ends any sheet drag in flight, so the page zooms and the sheet stays at its detent. One-finger drag and scroll are unchanged.+[fix] BottomSheet no longer blocks pinch-zoom. A pinch that started on an open sheet (its handle, its content, or across both) did nothing, because the sheet claimed every touch gesture: `touch-action: none` on the sheet and handle, `pan-y` on the content. The sheet now leaves pinch to the browser (`pinch-zoom`, and `pan-y pinch-zoom` on the content), and a second finger (on the handle, on the content, or off the sheet, whichever lands first) ends any sheet drag in flight or about to start, so the page zooms and the sheet stays at its detent. One-finger drag and scroll are unchanged.@cixzhangdiff --git a/packages/core/src/BottomSheet/__tests__/BottomSheetPinchZoom.a11y.chromium.spec.ts b/packages/core/src/BottomSheet/__tests__/BottomSheetPinchZoom.a11y.chromium.spec.tsindex bb33b68d34..e99865bbd5 100644--- a/packages/core/src/BottomSheet/__tests__/BottomSheetPinchZoom.a11y.chromium.spec.ts+++ b/packages/core/src/BottomSheet/__tests__/BottomSheetPinchZoom.a11y.chromium.spec.ts@@ -34,7 +34,8 @@ test.use({deviceScaleFactor: 3,});-const sleep = async (ms: number) => new Promise(resolve => setTimeout(resolve, ms));+const sleep = async (ms: number) =>+ new Promise(resolve => setTimeout(resolve, ms));async function openSheet(page: Page) {await page.goto(@@ -107,7 +108,16 @@ async function trackSheetTravel(page: Page) {});}-const PINCHES: {name: string; fingers: (box: Box) => [Point, Point]}[] = [+// `maxTravel` is how far the sheet may move while the fingers are down. A pinch+// is not a drag, so it is 2px except in one order: a finger that lands off the+// sheet is invisible to the sheet until the handle finger's next touch event,+// and the browser delivers that finger's pointermove first. That one input+// sample can move the sheet before it yields and returns to its detent.+const PINCHES: {+ name: string;+ fingers: (box: Box) => [Point, Point];+ maxTravel?: number;+}[] = [{name: 'across the handle and the body',fingers: box => [@@ -122,6 +132,28 @@ const PINCHES: {name: string; fingers: (box: Box) => [Point, Point]}[] = [{x: box.x + box.width / 2 + 10, y: box.y + 220},],},+ {+ name: 'across the body and the handle, body finger first',+ fingers: box => [+ {x: box.x + box.width / 2 + 10, y: box.y + 90},+ {x: box.x + box.width / 2, y: box.y + 12},+ ],+ },+ {+ name: 'at the top of the body, lower finger first',+ fingers: box => [+ {x: box.x + box.width / 2 + 10, y: box.y + 220},+ {x: box.x + box.width / 2, y: box.y + 160},+ ],+ },+ {+ name: 'from the handle and off the sheet above it',+ fingers: box => [+ {x: box.x + box.width / 2, y: box.y + 12},+ {x: box.x + box.width / 2 + 10, y: box.y - 70},+ ],+ maxTravel: 16,+ },{name: 'in the body, spreading sideways',fingers: box => [@@ -132,7 +164,7 @@ const PINCHES: {name: string; fingers: (box: Box) => [Point, Point]}[] = [];type Box = {x: number; y: number; width: number; height: number};-for (const {name, fingers} of PINCHES) {+for (const {name, fingers, maxTravel = 2} of PINCHES) {test(`a pinch ${name} zooms the page and leaves the sheet at rest`, async ({page,}) => {@@ -145,12 +177,12 @@ for (const {name, fingers} of PINCHES) {expect(await page.evaluate(() => window.visualViewport?.scale ?? 1),).toBeGreaterThan(1.2);- // A pinch is not a drag: the sheet never left its detent.+ // A pinch is not a drag: the sheet holds its detent.expect(await page.evaluate(() => (window as unknown as {__travel: number}).__travel,),- ).toBeLessThan(2);+ ).toBeLessThan(maxTravel);await expect(dialog).toBeVisible();// Layout-viewport coordinates: the zoom itself does not move them.expect(diff --git a/packages/core/src/BottomSheet/useSheetGestures.test.ts b/packages/core/src/BottomSheet/useSheetGestures.test.tsindex ba4ef609d4..a500f205d0 100644--- a/packages/core/src/BottomSheet/useSheetGestures.test.ts+++ b/packages/core/src/BottomSheet/useSheetGestures.test.ts@@ -617,6 +617,64 @@ describe('useSheetGestures', () => {up(hook, 300, 400, target);expect(onDismiss).not.toHaveBeenCalled();});++ it('returns to the detent when the browser cancels a handle drag to take a pinch', () => {+ const onSnap = vi.fn();+ const {hook, onDismiss} = setup({snapHeights: () => [200], onSnap});+ const target = makeTarget();+ down(hook, 0, 0, target);+ // Fast enough that settling from here would throw the sheet away.+ move(hook, 60, 20, target);+ act(() =>+ hook.result.current.handleProps.onPointerCancel(+ pointerEvent(60, 22, target),+ ),+ );+ expect(hook.result.current.isDragging).toBe(false);+ expect(hook.result.current.contentProps.style.transform).toBeUndefined();+ expect(onDismiss).not.toHaveBeenCalled();+ expect(onSnap).not.toHaveBeenCalled();+ });++ it('returns a handle drag to its detent when a second finger lands off the sheet', () => {+ const {hook, onDismiss} = setup({snapHeights: () => [200]});+ const target = makeTarget();+ down(hook, 0, 0, target);+ move(hook, 40, 100, target);+ expect(hook.result.current.isDragging).toBe(true);+ act(() =>+ hook.result.current.handleProps.onTouchMove({+ currentTarget: target,+ touches: [+ {identifier: 1, clientY: 40},+ {identifier: 2, clientY: -60},+ ],+ } as unknown as React.TouchEvent),+ );+ expect(hook.result.current.isDragging).toBe(false);+ expect(hook.result.current.contentProps.style.transform).toBeUndefined();+ expect(onDismiss).not.toHaveBeenCalled();+ });++ it('drops an armed body pull when a second pointer lands on the body', () => {+ const {hook} = setup({snapHeights: () => [200]});+ const body = makeTarget();+ act(() => hook.result.current.sheetRef(body));+ act(() =>+ hook.result.current.bodyProps.onPointerDown(pointerEvent(0, 0, body)),+ );+ act(() =>+ hook.result.current.bodyProps.onPointerDown(+ pointerEvent(80, 10, body, 2, 0, false),+ ),+ );+ act(() =>+ hook.result.current.bodyProps.onPointerMove(+ pointerEvent(60, 100, body),+ ),+ );+ expect(hook.result.current.isDragging).toBe(false);+ });});it('does not start a drag from a secondary pointer button', () => {@@ -958,6 +1016,60 @@ describe('useSheetGestures', () => {expect(onDismiss).not.toHaveBeenCalled();});+ it('leaves a pinch to the browser when the body finger lands first and the second lands on the handle', () => {+ const {hook} = setup({snapHeights: () => [200]});+ const el = makeScroller({+ scrollTop: 0,+ clientHeight: 200,+ scrollHeight: 800,+ });+ const handle = makeTarget();+ act(() => hook.result.current.sheetRef(el));+ act(() => hook.result.current.bodyProps.ref(el));+ let pull: Event | undefined;+ act(() => {+ touch(el, 'touchstart', 100);+ hook.result.current.handleProps.onPointerDown(+ pointerEvent(20, 10, handle, 2, 0, false),+ );+ // The spread carries the body finger down past the promotion slop.+ pull = touch(el, 'touchmove', 160);+ });+ expect(pull?.defaultPrevented).toBe(false);+ expect(hook.result.current.isDragging).toBe(false);+ });++ it('leaves a pinch to the browser when the second finger lands off the body', () => {+ const {hook} = setup({snapHeights: () => [200]});+ const el = makeScroller({+ scrollTop: 0,+ clientHeight: 200,+ scrollHeight: 800,+ });+ act(() => hook.result.current.sheetRef(el));+ act(() => hook.result.current.bodyProps.ref(el));+ let pull: Event | undefined;+ act(() => {+ touch(el, 'touchstart', 100);+ // No listener here saw the second finger arrive; `touches` lists it.+ const ev = new Event('touchmove', {bubbles: true, cancelable: true});+ Object.defineProperty(ev, 'changedTouches', {+ value: [{identifier: 1, clientY: 160}],+ });+ Object.defineProperty(ev, 'touches', {+ value: [+ {identifier: 1, clientY: 160},+ {identifier: 2, clientY: 20},+ ],+ });+ Object.defineProperty(ev, 'currentTarget', {value: el});+ el.dispatchEvent(ev);+ pull = ev;+ });+ expect(pull?.defaultPrevented).toBe(false);+ expect(hook.result.current.isDragging).toBe(false);+ });+it('leaves native scrolling alone in the middle of the content', () => {const {hook} = setup({snapHeights: () => [200]});const el = makeScroller({diff --git a/packages/core/src/BottomSheet/useSheetGestures.ts b/packages/core/src/BottomSheet/useSheetGestures.tsindex 9444ad534a..46ba8fb7e1 100644--- a/packages/core/src/BottomSheet/useSheetGestures.ts+++ b/packages/core/src/BottomSheet/useSheetGestures.ts@@ -54,8 +54,10 @@** Every gesture here is one finger. A second finger is a pinch, and pinch-zoom* belongs to the browser (WCAG 1.4.4): the surfaces declare `pinch-zoom` in- * their `touch-action`, and a second finger landing ends any drag in flight- * or armed — the sheet returns to its detent and the pinch zooms the page.+ * their `touch-action`, and a second finger — on either surface, in either+ * order — ends any drag in flight or armed (yieldToPinch), as does the+ * browser cancelling a handle drag to take the pinch. The sheet returns to+ * its detent and the pinch zooms the page.** Kept private to BottomSheet: a dismiss edge + detents on a bottom-anchored* surface are inherently sheet concepts. It is not a general primitive and is@@ -80,6 +82,7 @@ import {type MouseEvent as ReactMouseEvent,type PointerEvent as ReactPointerEvent,type RefObject,+ type TouchEvent as ReactTouchEvent,type UIEvent as ReactUIEvent,} from 'react';import {useMediaQuery} from '../hooks';@@ -346,6 +349,8 @@ export interface SheetHandleProps {onPointerMove: (event: ReactPointerEvent) => void;onPointerUp: (event: ReactPointerEvent) => void;onPointerCancel: (event: ReactPointerEvent) => void;+ onTouchStart: (event: ReactTouchEvent) => void;+ onTouchMove: (event: ReactTouchEvent) => void;}export interface SheetBodyProps {@@ -1082,12 +1087,7 @@ export function useSheetGestures({const handlePointerDown = useCallback((event: ReactPointerEvent) => {- if (!event.isPrimary) {- // A second finger is a pinch: give it to the browser.- cancelDrag(event.currentTarget as HTMLElement);- return;- }- if (event.button !== 0) {+ if (event.button !== 0 || !event.isPrimary) {return;}// The handle has no native focus action. Prevent pointer-down from@@ -1096,7 +1096,7 @@ export function useSheetGestures({event.preventDefault();beginDrag(event, measureHeight());},- [beginDrag, cancelDrag, measureHeight],+ [beginDrag, measureHeight],);const handleContextMenu = useCallback(@@ -1358,14 +1358,29 @@ export function useSheetGestures({end: (e: TouchEvent) => void;} | null>(null);+ // A second finger is a pinch, and pinch-zoom is the browser's. Whichever+ // surface it lands on and whichever finger came first, drop every armed+ // hand-off and any drag in flight so nothing claims (or preventDefault()s)+ // the gesture; the sheet returns to its detent.+ const yieldToPinch = useCallback(+ (target?: HTMLElement) => {+ touchDragRef.current = null;+ armedBodyRef.current = null;+ cancelDrag(target);+ },+ [cancelDrag],+ );+const beginDragRef = useRef(beginDrag);const cancelDragRef = useRef(cancelDrag);+ const yieldToPinchRef = useRef(yieldToPinch);const pointerMoveRef = useRef(handlePointerMove);const endDragRef = useRef(endDrag);const measureHeightRef = useRef(measureHeight);useEffect(() => {beginDragRef.current = beginDrag;cancelDragRef.current = cancelDrag;+ yieldToPinchRef.current = yieldToPinch;pointerMoveRef.current = handlePointerMove;endDragRef.current = endDrag;measureHeightRef.current = measureHeight;@@ -1386,10 +1401,7 @@ export function useSheetGestures({const onTouchStart = (event: TouchEvent) => {const body = event.currentTarget as HTMLElement;if (event.touches.length > 1) {- // A second finger is a pinch. Disarm, and drop a drag already in- // flight, so no later touchmove is cancelled out from under the zoom.- touchDragRef.current = null;- cancelDragRef.current(body);+ yieldToPinchRef.current(body);return;}const touch = event.changedTouches[0];@@ -1426,6 +1438,13 @@ export function useSheetGestures({const onTouchMove = (event: TouchEvent) => {const scroller = event.currentTarget as HTMLElement;+ // `touches` lists every finger on the screen, so this also catches a+ // second finger that landed outside the body (on the handle, or off the+ // sheet), where no listener here saw it arrive.+ if (event.touches.length > 1) {+ yieldToPinchRef.current(scroller);+ return;+ }const armed = touchDragRef.current;if (dragStateRef.current) {const t = [...event.changedTouches].find(@@ -1595,6 +1614,53 @@ export function useSheetGestures({[reconcileScrollPreservationInset],);+ // A second pointer on either surface is a pinch: yield before either+ // surface's primary-pointer handling sees it.+ const handleHandlePointerDown = useCallback(+ (event: ReactPointerEvent) => {+ if (!event.isPrimary) {+ yieldToPinch(event.currentTarget as HTMLElement);+ return;+ }+ handlePointerDown(event);+ },+ [handlePointerDown, yieldToPinch],+ );+ const handleBodyPointerDownOrPinch = useCallback(+ (event: ReactPointerEvent) => {+ if (!event.isPrimary) {+ yieldToPinch(event.currentTarget as HTMLElement);+ return;+ }+ handleBodyPointerDown(event);+ },+ [handleBodyPointerDown, yieldToPinch],+ );+ // A finger that started on the handle reports every finger on the screen,+ // so a second one landing anywhere (off the sheet included) is seen here,+ // usually before the browser gets round to cancelling the handle pointer.+ const handleHandleTouch = useCallback(+ (event: ReactTouchEvent) => {+ if (event.touches.length > 1) {+ yieldToPinch(event.currentTarget as HTMLElement);+ }+ },+ [yieldToPinch],+ );+ // The handle allows only pinch-zoom, so the browser cancels a handle drag's+ // pointer when it takes a pinch. That is not a release: return to the+ // detent rather than settling from wherever the finger had got to.+ const handleHandlePointerCancel = useCallback(+ (event: ReactPointerEvent) => {+ const state = dragStateRef.current;+ if (state?.pointerId !== event.pointerId || state.syntheticTouch) {+ return;+ }+ cancelDrag(event.currentTarget as HTMLElement);+ },+ [cancelDrag],+ );+// The resting transform; a drag in flight writes its own to the element// (writeLiveTransform) and a host that owns the element's transform must not// render this over it while `isDragging`.@@ -1626,16 +1692,20 @@ export function useSheetGestures({style: {touchAction: 'pinch-zoom', cursor: 'grab'},onContextMenu: handleContextMenu,onLostPointerCapture: handleLostPointerCapture,- onPointerDown: handlePointerDown,+ onPointerDown: handleHandlePointerDown,onPointerMove: handlePointerMove,onPointerUp: endDrag,- onPointerCancel: endDrag,+ onPointerCancel: handleHandlePointerCancel,+ onTouchStart: handleHandleTouch,+ onTouchMove: handleHandleTouch,}),[endDrag,handleContextMenu,+ handleHandlePointerCancel,+ handleHandlePointerDown,+ handleHandleTouch,handleLostPointerCapture,- handlePointerDown,handlePointerMove,],);@@ -1645,7 +1715,7 @@ export function useSheetGestures({ref: bodyRef,onContextMenu: handleContextMenu,onLostPointerCapture: handleLostPointerCapture,- onPointerDown: handleBodyPointerDown,+ onPointerDown: handleBodyPointerDownOrPinch,onPointerMove: handleBodyPointerMove,onPointerUp: handleBodyEnd,onPointerCancel: handleBodyEnd,@@ -1654,7 +1724,7 @@ export function useSheetGestures({[bodyRef,handleBodyEnd,- handleBodyPointerDown,+ handleBodyPointerDownOrPinch,handleBodyPointerMove,handleBodyScroll,handleContextMenu,