An open source design system that's fully customizable and agent ready
TypeScript
fix(usePanGesture): write the onPan ref at commit (#10090)
Christopher Chedeauecd652301fc4
diff --git a/.changeset/pan-gesture-committed-on-pan.md b/.changeset/pan-gesture-committed-on-pan.mdnew file mode 100644index 0000000000..d2a5d2bbc5--- /dev/null+++ b/.changeset/pan-gesture-committed-on-pan.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[chore] Internal: `usePanGesture` copies `onPan` into its ref at commit, not during render, before the hook's first release. A render React discards, as a suspended transition does, no longer hands the next drag to its `onPan`.++@vjeuxdiff --git a/packages/core/src/gestures/__tests__/createPanGesture.test.tsx b/packages/core/src/gestures/__tests__/createPanGesture.test.tsxindex 3a2a6689d8..5701429700 100644--- a/packages/core/src/gestures/__tests__/createPanGesture.test.tsx+++ b/packages/core/src/gestures/__tests__/createPanGesture.test.tsx@@ -11,7 +11,14 @@*/import {act, render} from '@testing-library/react';-import {StrictMode, useRef, useState} from 'react';+import {+ StrictMode,+ Suspense,+ startTransition,+ use,+ useRef,+ useState,+} from 'react';import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';import {createPanGesture,@@ -401,6 +408,43 @@ describe('usePanGesture (spec:AST-107 FR1, FR6)', () => {expect(events.at(-1)?.translation).toEqual({x: 30, y: 0});});+ it('keeps the committed onPan when React discards a render, as a suspended transition does', async () => {+ const committed: PanGestureEvent[] = [];+ const discarded: PanGestureEvent[] = [];+ const pending = new Promise<never>(() => {});+ function Pending(): null {+ use(pending);+ return null;+ }+ function Screen({+ onPan,+ isPending,+ }: {+ onPan: (event: PanGestureEvent) => void;+ isPending: boolean;+ }) {+ return (+ <Suspense fallback={null}>+ <Draggable axis="x" onPan={onPan} />+ {isPending ? <Pending /> : null}+ </Suspense>+ );+ }+ const {getByTestId, rerender} = render(+ <Screen onPan={event => committed.push(event)} isPending={false} />,+ );+ // The transition renders the new onPan, then suspends for good, so+ // React keeps the committed tree and never commits that render.+ await act(async () => {+ startTransition(() => {+ rerender(<Screen onPan={event => discarded.push(event)} isPending />);+ });+ });+ drag(getByTestId('surface'), 40, 0);+ expect(discarded).toEqual([]);+ expect(phases(committed)).toEqual(['began', 'changed', 'ended']);+ });+it('calls onPan from no effect: a drag in progress at unmount ends without a cancelled event (architecture:react-component-runtime INV2)', () => {const events: PanGestureEvent[] = [];const {getByTestId, unmount} = render(diff --git a/packages/core/src/hooks/usePanGesture.ts b/packages/core/src/hooks/usePanGesture.tsindex f4d2899fc9..95338dc1b3 100644--- a/packages/core/src/hooks/usePanGesture.ts+++ b/packages/core/src/hooks/usePanGesture.ts@@ -10,8 +10,9 @@* createPanGesture, through Core's attachment owner (useAttachment). It* keeps one pan for the element behind `ref`, re-created when the node or* the axis changes, and destroyed at unmount. `onPan` is read through a- * ref, so a new function each render re-creates nothing. A drag costs no- * React render.+ * ref, so a new function each render re-creates nothing. The hook writes+ * the ref at commit, so a render React discards leaves no listener. A drag+ * costs no React render.* SYNC: When modified, update usePanGesture.doc.mjs and* ../gestures/__tests__/createPanGesture.test.tsx.*/@@ -22,6 +23,7 @@ import {type PanGestureOptions,} from '../gestures/createPanGesture';import {useAttachment} from './useAttachment';+import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';/*** Report drags on the element behind `ref` as values.@@ -43,8 +45,12 @@ export function usePanGesture(): void {const {axis, onPan} = options;// Keep the latest listener without re-creating the pan on every render.+ // Write it at commit: a render React discards, as a suspended transition+ // does, must not leave its listener behind.const onPanRef = useRef(onPan);- onPanRef.current = onPan;+ useIsomorphicLayoutEffect(() => {+ onPanRef.current = onPan;+ });// An effect never calls `onPan`. So a drag in progress ends with no event// when the node or the axis changes, or at unmount.useAttachment(