Repository navigation
feat(a11y): add focus trap with nested modal support - #1528
Merged
nanaf6203-bit merged 1 commit intoNov 24, 2026
Merged
Conversation
Extension dialogs had no shared focus management, so Tab escaped into the page behind a modal and focus was not restored to the trigger on close. Adds useFocusTrap/FocusTrap (DOM-depth resolution for nested modals, restores the trigger, re-reads focusables on every Tab) along with the three other accessibility primitives assigned to this contributor: reduced-motion handling, a throttled polite price live region, and a 7:1 high contrast theme with a Storybook story.
|
@precy41 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
nanaf6203-bit
approved these changes
Nov 24, 2026
nanaf6203-bit
left a comment
Contributor
There was a problem hiding this comment.
Looks solid — good addition. Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1367
Adds a reusable focus trap to
ui-extensions/and, in the same module family, the three other accessibility primitives assigned to this contributor: reduced-motion handling (#1371), a throttled polite live region for price updates (#1369), and a 7:1 high contrast theme (#1365). The most important design decision is that the trap resolves the owning dialog by DOM depth rather than by mount order — React runs child effects before parent effects, so a modal rendered inside another would otherwise be won by the outer trap — and it re-reads focusable descendants on every Tab press instead of caching them at mount.Also closes
Closes #1371, Closes #1369, Closes #1365
Why
ui-extensions/had no shared focus management.PropertyComparisonTableand the dialog-like panels could open, but Tab walked straight out of the overlay into the page behind it, and closing an overlay left focus ondocument.bodyrather than the trigger. There was also no way for an extension to honourprefers-reduced-motion, no live region for price ticks, and no high contrast palette.Rather than inventing a second mechanism, the new code follows the existing
matchMediasubscription pattern insrc/hooks/use-mobile.ts, reusesformatPricefromutils/formatters.tsfor locale-aware prices, and reusescheckContrastfromutils/searchA11yUtils.tsto prove the high contrast palette meets 7:1 instead of asserting it in prose.What was built
ui-extensions/hooks/useFocusTrap.tsuseFocusTrap,getFocusableElements,FOCUSABLE_SELECTOR. Registers each open trap, resolves the owning trap as the deepest one containingdocument.activeElement(falling back to most-recently activated), wraps Tab/Shift+Tab, re-queries focusables per keypress so dynamically added elements are included, skipshidden,[inert],aria-hidden,disabled,aria-disabledandtabindex="-1", keeps focus on the dialog when nothing is focusable, dispatchesonEscapeonly to the owning trap, and restores focus to the trigger on deactivation when it is still connected.ui-extensions/components/FocusTrap.tsxFocusTrapwrapper addingrole="dialog",aria-modal, optionalaria-label/aria-labelledby(fallback label only rendered when the caller supplied neither, so ids are never duplicated), and a pass-through forinitialFocus/restoreFocus/onEscape. Inactive renders children without a wrapper.ui-extensions/hooks/useReducedMotion.tsuseReducedMotion,prefersReducedMotion(SSR-safe),reducedMotionClass,useReducedMotionClass,motionDuration(collapses to0),REDUCED_MOTION_QUERY,REDUCED_MOTION_CLASS, andobserveReducedMotionfor non-React animation owners. UsesaddEventListenerwith aaddListenerfallback.ui-extensions/components/PriceUpdateAnnouncer.tsxPriceUpdateAnnouncer(role="status",aria-live="polite",aria-atomic,sr-only) plus the purepercentChange,isSignificantChange,formatPriceAnnouncementandcreatePriceAnnouncementGatehelpers. Threshold defaults to 5%, throttle to 5000 ms; the gate takes an injected clock so timing is testable. Updates already seen are ignored, so a re-render cannot re-announce.ui-extensions/components/highContrastTheme.tsHIGH_CONTRAST_LIGHT/HIGH_CONTRAST_DARKpalettes,HIGH_CONTRAST_PAIRS(9 foreground/background combinations),highContrastPairs,highContrastStyle(the--hc-*custom properties),HIGH_CONTRAST_MIN_RATIO = 7.ui-extensions/components/HighContrastTheme.tsxHighContrastThemewrapper (hc-root,data-hc-theme="high",data-hc-base) anduseHighContrastThemewithlocalStoragepersistence,prefers-contrast: moresupport and an opt-out.ui-extensions/styles/high-contrast.css--hc-*custom properties for light and dark base themes, plus overrides for text, links, buttons, inputs/selects/textareas, cards/tables/dialogs,:focus-visibleoutlines and disabled controls (dashed outline rather than opacity, so nothing is hidden).ui-extensions/styles/reduced-motion.cssprefers-reduced-motionfallback and the.motion-reducedclass, both collapsing animation/transition duration, iteration count and delay.ui-extensions/stories/HighContrastTheme.stories.tsxui-extensions/tests/focusTrap.test.tsxui-extensions/tests/useReducedMotion.test.tsui-extensions/tests/priceUpdateAnnouncer.test.tsxui-extensions/tests/highContrastTheme.test.tsxIntegration changes outside
<module>/No existing files modified, only additions, so regression risk is low. Every file added is inside
ui-extensions/, as the isolation statement requires.Acceptance criteria coverage (primary issue)
Closes #1367 — Focus Trap for Modal Dialogs
ui-extensions/tests/focusTrap.test.tsx: wraps on Tab / wraps on Shift+Tab)focusTrap.test.tsx: restores focus to the trigger element on close)focusTrap.test.tsx: picks up focusable elements added after mount)focusTrap.test.tsx:getFocusableElementsskips hidden, disabled and tabindex=-1 elements)focusTrap.test.tsx: traps focus in the innermost modal when modals are nested; calls onEscape only for the owning trap)focusTrap.test.tsx: focuses the configured initial focus element instead of the first one)Closes #1371 — Reduced Motion Support for Animations
useReducedMotion.test.ts: queries the reduced motion media query)useReducedMotion.test.ts: returns the current preference and reacts to changes)useReducedMotion.test.ts: exposes a CSS class name).motion-reducedcompiles the same media query asmotion-reduce:, so the two compose; this cannot be asserted in jsdom, CSS is instyles/reduced-motion.css)useReducedMotion.test.ts: collapses durations to zero when motion is reduced; CSS clamps duration to0.01ms)Closes #1369 — ARIA Live Regions for Real-Time Price Updates
priceUpdateAnnouncer.test.tsx: renders a visually hidden polite live region)priceUpdateAnnouncer.test.tsx: throttles rapid status changes to one announcement per window; drops changes below the threshold without consuming the throttle)priceUpdateAnnouncer.test.tsx: defaults to a five percent threshold)priceUpdateAnnouncer.test.tsx: supports other locales and currencies)priceUpdateAnnouncer.test.tsx: renders a visually hidden polite live region;sr-only)priceUpdateAnnouncer.test.tsx: stays silent below the configurable threshold; honours a custom threshold)Closes #1365 — High Contrast Theme for All Extension Components
highContrastTheme.test.tsx: meets 7:1 for every pair on the light/dark base theme, checked withcheckContrast)highContrastTheme.test.tsx: exposes CSS custom properties for both base themes)styles/high-contrast.css: text, links, buttons, inputs, cards, tables, dialogs, focus, disabled)styles/high-contrast.css:.hc-button,.hc-input,.hc-card,.hc-text*)highContrastTheme.test.tsx: uses different colours for the light and dark base themes;HighContrastThemescopes the palette to a wrapper with data attributes)display:none; disabled controls stay legible via dashed outline)stories/HighContrastTheme.stories.tsx: 4 stories)Deliberately deferred
None. All four issues assigned to this contributor are implemented in full; each
Closeskeyword above refers to work contained in this diff.Test plan
npx jest ui-extensions/tests/{focusTrap,useReducedMotion,priceUpdateAnnouncer,highContrastTheme}: 57/57 passing (57 new: 15 + 11 + 17 + 14)npx jest --watchAll=false --ci: 2372/2464 passing across 249 suites (before: 2315/2407 across 245 suites). The same 25 pre-existing suites / 92 pre-existing tests fail before and after; this PR adds 4 suites and 57 passing tests and no new failures.npm run typecheck(tsc --noEmit): 482 errors before and 482 after — zero new, and no error mentionsui-extensions. The baselinesrc/errors (e.g.src/utils/seo.ts,src/utils/security/*) are untouched by this PR.tsconfigextending the repo config withui-extensions/**included: 0 errors in the 7 new source files.npx eslint ui-extensions: 11 errors after vs 10 before. All are the sameParsing error: Unexpected token {inui-extensions/stories/*.stories.tsx, becauseeslint.config.mjsonly applies the TypeScript parser tosrc/**and.storybook/**; the new story inherits that pre-existing gap. See Notes.npm run build: cannot pass at base.npm run buildrunsnpm run typecheck && next build, andtsc --noEmitexits2onmain(482 errors) whilenext.config.tssetstypescript.ignoreBuildErrors: false. The gate is red before this PR and this PR adds no errors to it.FocusTrapwith a keyboard, press Tab/Shift+Tab at both ends, Escape, and confirm focus returns to the trigger.Env vars / Notes
No new environment variables.
Consumers must import the stylesheets they use; they are not imported by the components so that Jest does not need a CSS transform:
Known pre-existing gates (unchanged by this PR, documented for reviewers):
typescript,lintand thereforebuildalready fail onmainbecauseui-extensions/**is absent fromtsconfig.json'sincludeand from the TypeScript parserfileslist ineslint.config.mjs. Addingui-extensions/**/*.{ts,tsx}to both would fix all 10 existing story parse errors, but it is a repo-wide config change outside this issue's isolation boundary, so it is left out of this diff.