An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a sheet hosting another sheet in its content leaves that sheet's gestures alone (#8820)
Xinzi Zhoud73908f6755b
diff --git a/.changeset/bottom-sheet-nested-sheet-drag.md b/.changeset/bottom-sheet-nested-sheet-drag.mdnew file mode 100644index 0000000000..fe2e95f00b--- /dev/null+++ b/.changeset/bottom-sheet-nested-sheet-drag.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A BottomSheet opened from inside another sheet's content drags down to dismiss on its own. The covered sheet underneath no longer takes the gesture, so dragging the top sheet's grabber no longer moves or dismisses the sheet beneath it.++@imdreamrunnerdiff --git a/packages/core/src/BottomSheet/BottomSheet.test.tsx b/packages/core/src/BottomSheet/BottomSheet.test.tsxindex 2cc246afa3..d2a22412b1 100644--- a/packages/core/src/BottomSheet/BottomSheet.test.tsx+++ b/packages/core/src/BottomSheet/BottomSheet.test.tsx@@ -1045,6 +1045,52 @@ describe('BottomSheet', () => {});describe('swipe to dismiss', () => {+ it("a sheet opened from inside another sheet's content drags to dismiss by its own handle, and the covered host neither drags nor takes the pointer (FR6, FR7)", () => {+ const onInnerOpenChange = vi.fn();+ const onOuterOpenChange = vi.fn();+ render(+ <BottomSheet isOpen onOpenChange={onOuterOpenChange} label="Outer">+ Outer content+ <BottomSheet isOpen onOpenChange={onInnerOpenChange} label="Inner">+ Inner content+ </BottomSheet>+ </BottomSheet>,+ );+ const dialogs = screen.getAllByRole('dialog', {hidden: true});+ expect(dialogs).toHaveLength(2);+ const [outerDialog, innerDialog] = dialogs;+ // The inner sheet's dialog is rendered inside the outer sheet's body,+ // so its events bubble through the outer body's gesture listeners.+ expect(outerDialog.contains(innerDialog)).toBe(true);+ const outerPanel = outerDialog.querySelector<HTMLElement>(+ ':scope > .astryx-bottom-sheet',+ )!;+ const outerBody = outerPanel.querySelector<HTMLElement>(+ ':scope > :not([aria-hidden])',+ )!;+ const innerHandle = innerDialog.querySelector<HTMLElement>(+ '.astryx-bottom-sheet > [aria-hidden="true"]',+ )!;+ // `setPointerCapture` is a prototype stub here, so record who captures.+ const captured: Element[] = [];+ const capture = vi+ .spyOn(Element.prototype, 'setPointerCapture')+ .mockImplementation(function (this: Element) {+ captured.push(this);+ });+ drag(innerHandle, [{y: 0}, {y: 40}, {y: 120}], 'touch');+ capture.mockRestore();+ // The inner sheet's own drag ran to its dismissal.+ expect(onInnerOpenChange).toHaveBeenCalledWith(false);+ // The covered host saw the same pointer bubble up and did nothing with+ // it: no drag of its own (its panel keeps its resting transform) and no+ // pointer capture, which would have cancelled the inner handle's drag.+ expect(onOuterOpenChange).not.toHaveBeenCalled();+ expect(captured).toContain(innerHandle);+ expect(captured).not.toContain(outerBody);+ expect(outerPanel.style.transform).toBe('');+ });+it('requests close when dragged past the dismiss threshold', () => {const onOpenChange = vi.fn();render(diff --git a/packages/core/src/BottomSheet/useSheetGestures.test.ts b/packages/core/src/BottomSheet/useSheetGestures.test.tsindex a500f205d0..e975e9626a 100644--- a/packages/core/src/BottomSheet/useSheetGestures.test.ts+++ b/packages/core/src/BottomSheet/useSheetGestures.test.ts@@ -12,6 +12,7 @@import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';import {act, renderHook} from '@testing-library/react';import {+ isInsideNestedLayer,useSheetGestures,type UseSheetGesturesOptions,} from './useSheetGestures';@@ -106,6 +107,27 @@ function up(hook: Hook, y: number, t: number, target: HTMLElement) {);}+describe('isInsideNestedLayer', () => {+ it("is true for a target inside a <dialog> rendered within the body, and false for the body's own content", () => {+ const body = document.createElement('div');+ const own = document.createElement('button');+ body.append(own);+ const nested = document.createElement('dialog');+ const nestedHandle = document.createElement('div');+ nested.append(nestedHandle);+ body.append(nested);+ expect(isInsideNestedLayer(own, body)).toBe(false);+ expect(isInsideNestedLayer(body, body)).toBe(false);+ expect(isInsideNestedLayer(nestedHandle, body)).toBe(true);+ expect(isInsideNestedLayer(nested, body)).toBe(true);+ // A dialog above the body (the sheet's own) does not count.+ const ownDialog = document.createElement('dialog');+ ownDialog.append(body);+ expect(isInsideNestedLayer(own, body)).toBe(false);+ expect(isInsideNestedLayer(null, body)).toBe(false);+ });+});+describe('useSheetGestures', () => {it('dismisses on a fast downward flick (swipe-to-close)', () => {const {hook, onDismiss} = setup({snapHeights: () => [200]});@@ -1213,6 +1235,36 @@ describe('useSheetGestures', () => {expect(hook.result.current.isDragging).toBe(true);});+ it('leaves a touch that starts inside a nested <dialog> to that layer', () => {+ // A sheet opened from this sheet's content renders its <dialog> inside+ // the body, so its touches bubble here. The same pull-down from the+ // body's own content promotes (see above); from the nested layer it+ // must not.+ const {hook} = setup({snapHeights: () => [200]});+ const {body} = makeNested({+ bodyScrollTop: 0,+ innerScrollTop: 0,+ innerScrollHeight: 200,+ });+ const nested = document.createElement('dialog');+ const nestedHandle = document.createElement('div');+ nested.appendChild(nestedHandle);+ body.appendChild(nested);+ act(() => hook.result.current.sheetRef(body));+ act(() => hook.result.current.bodyProps.ref(body));+ let start: Event | undefined;+ let pull: Event | undefined;+ act(() => {+ start = touchOn(nestedHandle, body, 'touchstart', 100);+ pull = touchOn(nestedHandle, body, 'touchmove', 160);+ });+ expect(start?.defaultPrevented).toBe(false);+ expect(pull?.defaultPrevented).toBe(false);+ expect(hook.result.current.isDragging).toBe(false);+ expect(liveOffset(hook)).toBe(0);+ expect(body.style.transform).toBe('');+ });+it('leaves a pull-up to a nested scroller with content below', () => {const {hook} = setup({snapHeights: () => [200]});const {body, tile} = makeNested({bodyScrollTop: 0, innerScrollTop: 100});diff --git a/packages/core/src/BottomSheet/useSheetGestures.ts b/packages/core/src/BottomSheet/useSheetGestures.tsindex b012cf9e84..e4b0a72bd8 100644--- a/packages/core/src/BottomSheet/useSheetGestures.ts+++ b/packages/core/src/BottomSheet/useSheetGestures.ts@@ -235,6 +235,33 @@ function isBlockScroller(element: HTMLElement): boolean {return overflowY === 'auto' || overflowY === 'scroll';}+/**+ * Whether a gesture that lands on `target` began inside another layer+ * rendered in this sheet's content — a `<dialog>` between the target and the+ * body. A sheet opened from inside a sheet's content, a Dialog opened there,+ * renders its dialog as a descendant of the host's body, so its pointer and+ * touch events bubble through the host's gesture listeners. That gesture is+ * the layer's own: the host must neither arm a drag on it nor take pointer+ * capture for it, which would cancel the drag the layer's own handle began+ * (`component:BottomSheet` FR7: a swipe reaches only the top sheet of a+ * stack; FR6: a covered sheet is blocked).+ */+export function isInsideNestedLayer(+ target: EventTarget | null,+ body: HTMLElement,+): boolean {+ for (+ let element = target instanceof Element ? target : null;+ element != null && element !== body;+ element = element.parentElement+ ) {+ if (element.localName === 'dialog') {+ return true;+ }+ }+ return false;+}+/*** The scrollers a gesture that lands on `target` can scroll, innermost first* and the body last: every scrollable box between the target and the body,@@ -1328,10 +1355,13 @@ export function useSheetGestures({if (event.button !== 0 || !event.isPrimary) {return;}- const scrollers = scrollChainUnder(- event.target,- event.currentTarget as HTMLElement,- );+ const body = event.currentTarget as HTMLElement;+ // A gesture inside a layer this sheet hosts belongs to that layer.+ if (isInsideNestedLayer(event.target, body)) {+ armedBodyRef.current = null;+ return;+ }+ const scrollers = scrollChainUnder(event.target, body);if (!chainAtTop(scrollers)) {armedBodyRef.current = null;return;@@ -1465,8 +1495,9 @@ export function useSheetGestures({// At the top, a pull DOWN hands off (collapse); at the bottom, a pull UP// hands off (expand). A gesture that starts mid-content is an ordinary// scroll, but it is tracked all the same: the scroller can run out of- // content while the finger is still down (see onTouchMove).- if (!touch) {+ // content while the finger is still down (see onTouchMove). A touch+ // inside a layer this sheet hosts is that layer's, never tracked here.+ if (!touch || isInsideNestedLayer(event.target, body)) {touchDragRef.current = null;return;}