diff --git a/.changeset/mobile-nav-close-like-uikit.md b/.changeset/mobile-nav-close-like-uikit.md new file mode 100644 index 0000000000000..1970ca6c47ee6 --- /dev/null +++ b/.changeset/mobile-nav-close-like-uikit.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] Closing `MobileNav` now runs like an iOS dismissal from start to end. The scrim fades out over the whole slide-out instead of vanishing partway through, and inside `AppShell` the slide-out is no longer cut off at 300 ms. While the drawer slides out nothing takes a tap: not the page, and not the leaving drawer, whose links used to stay live. When the slide-out ends, the dialog closes, the page takes input again and focus returns to the element that opened the drawer. + +@vjeux diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d19b9322e0b0..58e29a50ff3dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1535,6 +1535,17 @@ jobs: --project chromium \ --output table-min-width-output + # Closing MobileNav works like a UIKit dismissal: the dialog stays modal + # and inert while the drawer slides out and the scrim fades, no tap + # lands until it ends, then focus returns to the opener. Top-layer + # modality, inertness and real taps only exist in a real engine. + - name: Run MobileNav close contract + run: | + pnpm exec playwright test \ + packages/core/src/MobileNav/__tests__/MobileNavClose.a11y.chromium.spec.ts \ + --project chromium \ + --output mobile-nav-close-output + # A CodeBlock diff row's tint has to follow a long line to the end of its # scroll width; jsdom has no layout, so only a real engine shows whether # the paint stops at the viewport edge when the reader scrolls. diff --git a/packages/core/src/AppShell/AppShell.tsx b/packages/core/src/AppShell/AppShell.tsx index 04fbb2808ac3e..bc67bd3e8ceac 100644 --- a/packages/core/src/AppShell/AppShell.tsx +++ b/packages/core/src/AppShell/AppShell.tsx @@ -43,7 +43,7 @@ import {LayoutHeader} from '../Layout/LayoutHeader'; import {LayoutPanel} from '../Layout/LayoutPanel'; import {LayoutContent} from '../Layout/LayoutContent'; import {MobileNavToggle} from '../MobileNav/MobileNavToggle'; -import {MAX_CLOSE_DELAY_MS as MOBILE_NAV_MAX_CLOSE_DELAY_MS} from '../MobileNav/MobileNav'; +import {MAX_EXIT_MS as MOBILE_NAV_MAX_EXIT_MS} from '../MobileNav/MobileNav'; import {SideNavRenderContext} from '../SideNav/SideNavRenderContext'; import {TopNavRenderContext} from '../TopNav/TopNavRenderContext'; import {TopNavMobileContentContext} from '../TopNav/TopNavMobileContentContext'; @@ -83,16 +83,16 @@ const ActivityWrapper = HasActivity const MAIN_CONTENT_ID = 'astryx-app-shell-main'; /** - * Extra time, on top of MobileNav's own worst-case close delay, that - * AppShell waits before flipping its `Activity` boundary to `'hidden'`. + * Extra time, on top of MobileNav's longest slide-out, that AppShell waits + * before flipping its `Activity` boundary to `'hidden'`. * * React's `Activity` hides its subtree with `display: none !important` * synchronously in the same commit as the mode flip, and re-runs the - * cleanup of every effect inside it — which would otherwise clear - * MobileNav's own scheduled `dialog.close()` before it fires, or cut off - * its slide-out CSS transition before it plays (#5701). Waiting strictly - * longer than MobileNav's own cap, rather than the same duration, means - * this never turns into a race decided by browser timer-firing order. + * cleanup of every effect inside it — which would cut the drawer's + * slide-out off and clear MobileNav's own `dialog.close()` at its end + * (#5701). Waiting strictly longer than MobileNav's own cap, rather than the + * same duration, means this never turns into a race decided by browser + * timer-firing order. */ const ACTIVITY_HIDE_BUFFER_MS = 50; @@ -540,10 +540,9 @@ export function AppShell({ const [uncontrolledMobileOpen, setUncontrolledMobileOpen] = useState(false); const isMobileNavOpen = mobileNavConfig?.isOpen ?? uncontrolledMobileOpen; - // Holds the mobile nav's `Activity` boundary visible for a beat after a - // close, so MobileNav's own close effect gets a real chance to run its - // slide-out transition and schedule its delayed `dialog.close()` before - // Activity hides the subtree (#5701). Only the CLOSE is deferred: opening + // Holds the mobile nav's `Activity` boundary visible after a close, so the + // drawer's slide-out plays to its end and MobileNav closes its own dialog + // before Activity hides the subtree (#5701). Only the CLOSE is deferred: opening // reads `isMobileNavOpen` directly below, so a controlled open is visible in // the commit that carries it instead of one effect later. const [isMobileNavCloseHeld, setIsMobileNavCloseHeld] = @@ -560,7 +559,7 @@ export function AppShell({ const delayMs = window.matchMedia('(prefers-reduced-motion: reduce)') .matches ? 0 - : MOBILE_NAV_MAX_CLOSE_DELAY_MS + ACTIVITY_HIDE_BUFFER_MS; + : MOBILE_NAV_MAX_EXIT_MS + ACTIVITY_HIDE_BUFFER_MS; const timeout = setTimeout(() => { setIsMobileNavCloseHeld(false); }, delayMs); diff --git a/packages/core/src/MobileNav/MobileNav.doc.mjs b/packages/core/src/MobileNav/MobileNav.doc.mjs index 26520441978e1..83a11b6354ff2 100644 --- a/packages/core/src/MobileNav/MobileNav.doc.mjs +++ b/packages/core/src/MobileNav/MobileNav.doc.mjs @@ -98,7 +98,7 @@ export const docs = { name: 'onOpenChange', type: '(isOpen: boolean) => void', description: - 'Called when the drawer visibility changes (backdrop click, Escape key, or close button). Inside AppShell, this is managed automatically via context.', + 'Called when the drawer visibility changes (backdrop click, Escape key, or close button). Inside AppShell, this is managed automatically via context. On close the drawer slides out before anything takes input again, as on iOS; focus then returns to the element that opened it.', }, { name: 'children', @@ -203,7 +203,7 @@ export const docsZh = { name: 'onOpenChange', type: '(isOpen: boolean) => void', description: - '当抽屉可见性变化时调用(点击背景遮罩、按 Escape 键或点击关闭按钮)。', + '当抽屉可见性变化时调用(点击背景遮罩、按 Escape 键或点击关闭按钮)。关闭时抽屉先滑出,期间页面不接受输入(与 iOS 相同);随后焦点回到打开它的元素。', required: true, }, { @@ -311,7 +311,7 @@ export const docsDense = { propDescriptions: { isOpen: 'whether drawer is open', onOpenChange: - 'called when drawer visibility changes (backdrop click, Escape, close button)', + 'called when drawer visibility changes (backdrop click, Escape, close button); on close nothing takes input until the drawer has slid out, then focus returns to the opener', children: 'drawer content; typically SideNavSection/SideNavItem or any ReactNode', header: diff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsx index f5736fb099820..508141c0c793a 100644 --- a/packages/core/src/MobileNav/MobileNav.tsx +++ b/packages/core/src/MobileNav/MobileNav.tsx @@ -25,15 +25,24 @@ * closes. The native `cancel` event still handles the dismissals the browser * starts itself, such as the Android back gesture. * + * Closing works like a UIKit dismissal: nothing takes input until the + * slide-out has finished (UIKit ignores touches for the whole transition, on + * the presenting view and on the one leaving). The native dialog stays open, + * and so keeps the page inert, until the slide-out ends, and the drawer panel + * is `inert` meanwhile, so a tap on the leaving drawer does nothing either. + * Not the dialog itself: an inert modal dialog stops blocking the page in + * Chromium. The scrim fades out over the whole slide-out. + * * Focus RESTORATION is not delegated to the browser. The drawer captures the * element that opened it and refocuses it once the native dialog has closed, - * mirroring Dialog: the implicit `` restore has been observed dropping - * focus to ``. The close itself is deferred past the slide-out, and the - * restore has to follow it. Until close() runs the rest of the document is - * inert and focus() on the opener is silently dropped. + * at the end of the slide-out, mirroring Dialog: the implicit `` + * restore has been observed dropping focus to ``. The order is + * load-bearing: until close() runs the rest of the document is inert and + * focus() on the opener is silently dropped. * * SYNC: When modified, update these files to stay in sync: * - /packages/core/src/MobileNav/index.ts (exports if types change) + * - /packages/core/src/AppShell/AppShell.tsx (MAX_EXIT_MS: Activity hold) * - /packages/core/src/Layer/useLayerDismissal.ts (dismissal stack) * - /packages/core/src/hooks/scrollbarGutter.ts (shared scroll-lock gutter) * - /packages/cli/assets/templates/blocks/components/MobileNav/ (showcase blocks) @@ -114,19 +123,11 @@ const styles = stylex.create({ outline: 'none', // Native uses display:none when closed. // Open state applied via isOpen prop to avoid :where([open]) specificity issues. - // `display` participates in the transition with allow-discrete so it flips - // to none only after the slide-out finishes. That also keeps the dialog - // rendered until close() has actually run: an open modal dialog that isn't - // rendered still blocks the whole document, and a browser that fails to - // un-block it on close leaves the page inert with no error (#4290). - // Deliberately not shortened under reduced motion: `display` is discrete, - // so a long hold animates nothing — it is only the window the close has to - // land inside. The visible transitions (the drawer's transform and the - // backdrop's opacity) are the ones that respect the preference. + // While the drawer slides out, `isExiting` keeps `open` applied until the + // close() at its end, so the dialog is never an open modal that isn't + // rendered: a browser that fails to un-block such a dialog on close + // leaves the page inert with no error (#4290). display: 'none', - transitionProperty: 'display', - transitionDuration: durationVars['--duration-medium'], - transitionBehavior: 'allow-discrete', }, open: { display: 'flex', @@ -261,19 +262,20 @@ const dynamicStyles = stylex.create({ // ============================================================================= /** - * Longest the drawer will wait before closing, however long the hold is. + * Longest the drawer slides out for, in ms, however long its transition is. + * The slide-out runs for the theme's `--duration-medium` (410 ms by default; + * the default scale's `--duration-medium-max` is 545 ms); a theme that sets a + * longer one has its slide-out cut here, so the page is never held longer. * - * Exported so `AppShell` can defer hiding this drawer's `Activity` boundary - * by at least this long too (`ACTIVITY_HIDE_BUFFER_MS` in AppShell.tsx) — - * otherwise Activity's `display: none !important` lands before this delay's - * own scheduled `dialog.close()` has a chance to run (#5701). + * Exported so `AppShell` keeps this drawer's `Activity` boundary visible for + * at least this long after a close (`ACTIVITY_HIDE_BUFFER_MS` in + * AppShell.tsx): Activity's `display: none !important` would otherwise cut + * the slide-out off and close the dialog early (#5701). */ -export const MAX_CLOSE_DELAY_MS = 250; -/** Fraction of the hold to close at, so the close never lands on its boundary. */ -const CLOSE_WITHIN_HOLD = 0.6; +export const MAX_EXIT_MS = 550; /** - * Shortest duration in a `transition-duration` list, in ms; null if unreadable. + * Longest duration in a `transition-duration` list, in ms; null if unreadable. * * Browsers serialise computed `