An open source design system that's fully customizable and agent ready
TypeScript
refactor(AppShell): main's ref is the one owner of its overflow observation and keyboard delegation (#9262)
Cindy Zhang853d94a02aec
diff --git a/.changeset/appshell-main-release-on-detach.md b/.changeset/appshell-main-release-on-detach.mdnew file mode 100644index 0000000000..95ddac03c0--- /dev/null+++ b/.changeset/appshell-main-release-on-detach.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[chore] AppShell drops an unmount Effect that repeated the release main's ref already performs; main's overflow observation and keyboard delegation are still released on unmount. No behavior changes.++@cixzhangdiff --git a/packages/core/src/AppShell/AppShell.test.tsx b/packages/core/src/AppShell/AppShell.test.tsxindex ac92df125e..a7c059e2c1 100644--- a/packages/core/src/AppShell/AppShell.test.tsx+++ b/packages/core/src/AppShell/AppShell.test.tsx@@ -1234,6 +1234,45 @@ describe('AppShell main keyboard access', () => {expect(main).toHaveAttribute('tabindex', '-1');});+ // Main's ref owns both: React detaches it on unmount, and the detach+ // releases the overflow observation and the keyboard delegation. Each+ // listener is matched by its exact handler, so another owner's cleanup+ // (the focus-ring Effect listens for the same event types) cannot stand in.+ it('releases the overflow observation and the keyboard delegation on unmount', () => {+ const windowAdd = vi.spyOn(window, 'addEventListener');+ const {unmount} = render(+ <AppShell topNav={<div>Nav</div>}>+ <p>Long content</p>+ </AppShell>,+ );+ const overflowListeners = windowAdd.mock.calls.filter(+ ([type]) => type === 'resize',+ );+ expect(overflowListeners.length).toBeGreaterThan(0);++ const main = screen.getByRole('main');+ const documentAdd = vi.spyOn(document, 'addEventListener');+ setMainGeometry(main, 1200);+ remeasure();+ // Main became a scroll region, so the delegation attached its listeners.+ expect(main).toHaveAttribute('tabindex', '0');+ const delegationListeners = [...documentAdd.mock.calls];+ expect(delegationListeners.map(([type]) => type)).toEqual(+ expect.arrayContaining(['keydown', 'keyup', 'pointerdown', 'focusin']),+ );++ const documentRemove = vi.spyOn(document, 'removeEventListener');+ const windowRemove = vi.spyOn(window, 'removeEventListener');+ unmount();++ for (const [type, listener, options] of delegationListeners) {+ expect(documentRemove).toHaveBeenCalledWith(type, listener, options);+ }+ for (const [type, listener] of overflowListeners) {+ expect(windowRemove).toHaveBeenCalledWith(type, listener);+ }+ });+it('observes main only with fill, where it can become a keyboard stop', () => {const observed: Element[] = [];vi.stubGlobal(diff --git a/packages/core/src/AppShell/AppShell.tsx b/packages/core/src/AppShell/AppShell.tsxindex e8f9438a1a..ab45141e4d 100644--- a/packages/core/src/AppShell/AppShell.tsx+++ b/packages/core/src/AppShell/AppShell.tsx@@ -728,15 +728,6 @@ export function AppShell({isMainScrollRegionRef.current = isMainScrollRegion;syncMainDelegation();}, [isMainScrollRegion, syncMainDelegation]);- useEffect(- () => () => {- mainOverflowRef.current?.stop();- mainOverflowRef.current = null;- mainDelegationRef.current?.release();- mainDelegationRef.current = null;- },- [],- );// Nav style derived valuesconst hasBanner = isRenderable(banner);