An open source design system that's fully customizable and agent ready
TypeScript
fix(MobileNav): Tab wraps inside the open drawer through the shared focus-region module (#9999)
Cindy Zhang15a95e7d24ef
diff --git a/.changeset/mobilenav-focus-region-tab-wrap.md b/.changeset/mobilenav-focus-region-tab-wrap.mdnew file mode 100644index 0000000000..ed0d877817--- /dev/null+++ b/.changeset/mobilenav-focus-region-tab-wrap.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] MobileNav: Tab now wraps inside the open navigation drawer. Tab from the last control moves to the first, and Shift+Tab from the first moves to the last, so keyboard users no longer move from inside the drawer to the browser's toolbar. The drawer uses the same focus-region module as Dialog, BottomSheet and Lightbox.++@cixzhangdiff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsxindex 58e3d529af..9592be734c 100644--- a/packages/core/src/MobileNav/MobileNav.tsx+++ b/packages/core/src/MobileNav/MobileNav.tsx@@ -80,6 +80,7 @@ import {themeProps} from '../utils/themeProps';import {useMergedRefs} from '../hooks/useMergedRefs';import {useScrollLock} from '../hooks/useScrollLock';+import {useFocusRegionContainment} from '../hooks/useFocusTrap';import {registerEnglishFallback,useComponentTranslator,@@ -515,6 +516,10 @@ export function MobileNav({);const dialogRef = useRef<HTMLDialogElement>(null);+ // Native showModal() lets Tab leave for the browser's own UI, so the shared+ // module wraps it. A closing drawer stops containing as soon as it starts to+ // slide out.+ useFocusRegionContainment(dialogRef, isOpen);const drawerRef = useRef<HTMLDivElement>(null);const closeTimeoutRef = useRef<ReturnType<typeof setTimeout>>(null);// The input hold on the drawer's layer while it slides in under a coarsediff --git a/packages/core/src/MobileNav/MobileNavFocusRegion.test.tsx b/packages/core/src/MobileNav/MobileNavFocusRegion.test.tsxnew file mode 100644index 0000000000..fb12a915fc--- /dev/null+++ b/packages/core/src/MobileNav/MobileNavFocusRegion.test.tsx@@ -0,0 +1,81 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file MobileNavFocusRegion.test.tsx+ * @input vitest, @testing-library/react, MobileNav+ * @output Tab and Shift+Tab wrap inside the open drawer+ * @position Testing; MobileNav applies the shared focus-region module+ */++import {describe, it, expect, beforeAll} from 'vitest';+import {render, screen, fireEvent} from '@testing-library/react';+import {MobileNav} from './MobileNav';+import {getFocusableElements} from '../hooks/useFocusTrap';++// jsdom doesn't implement showModal/close on <dialog>.+beforeAll(() => {+ HTMLDialogElement.prototype.showModal =+ HTMLDialogElement.prototype.showModal ||+ function (this: HTMLDialogElement) {+ this.setAttribute('open', '');+ };+ HTMLDialogElement.prototype.close =+ HTMLDialogElement.prototype.close ||+ function (this: HTMLDialogElement) {+ this.removeAttribute('open');+ };+});++function Drawer({isOpen = true}: {isOpen?: boolean}) {+ return (+ <MobileNav isOpen={isOpen} onOpenChange={() => {}} header="Menu">+ <a href="#home">Home</a>+ <a href="#settings">Settings</a>+ </MobileNav>+ );+}++describe('MobileNav focus region', () => {+ it('wraps Tab from the last control to the first and back', () => {+ render(<Drawer />);+ const drawer = screen.getByRole('dialog');+ const tabbables = getFocusableElements(drawer);+ expect(tabbables.length).toBeGreaterThan(1);+ const first = tabbables[0];+ const last = tabbables[tabbables.length - 1];++ last.focus();+ expect(fireEvent.keyDown(last, {key: 'Tab'})).toBe(false);+ expect(first).toHaveFocus();++ expect(fireEvent.keyDown(first, {key: 'Tab', shiftKey: true})).toBe(false);+ expect(last).toHaveFocus();+ });++ it('wraps Shift+Tab from the focused drawer panel to the last control', () => {+ render(<Drawer />);+ const drawer = screen.getByRole('dialog');+ const tabbables = getFocusableElements(drawer);+ const last = tabbables[tabbables.length - 1];+ // Opening focuses the drawer panel itself, which is not a Tab stop.+ const panel = drawer.querySelector<HTMLElement>('[tabindex="-1"]');+ expect(panel).not.toBeNull();+ panel!.focus();++ expect(fireEvent.keyDown(panel!, {key: 'Tab', shiftKey: true})).toBe(false);+ expect(last).toHaveFocus();+ });++ it('stops containing Tab as soon as the drawer starts to close', () => {+ const {rerender} = render(<Drawer />);+ rerender(<Drawer isOpen={false} />);+ // The dialog stays open through its slide-out; Tab from its last link is+ // no longer wrapped.+ const drawer = document.querySelector('dialog[open]') as HTMLElement;+ expect(drawer).not.toBeNull();+ const tabbables = getFocusableElements(drawer);+ const last = tabbables[tabbables.length - 1];+ last.focus();+ expect(fireEvent.keyDown(last, {key: 'Tab'})).toBe(true);+ });+});