Repository navigation
Conversation
…le-selection mode Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
snowystinger
left a comment
There was a problem hiding this comment.
I went back to look at the WAI Aria pattern guidelines, there's nothing about focus going to the first selected item https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/. I think this logic should only be in the useToggleButtonGroup, do you have ideas on how to accomplish that?
|
Good point @snowystinger . Keeping We can keep all of this inside
(One small trade-off of returning early without calling I'll update the PR to revert |
… of useToolbar Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
Closes #11059
Summary
Problem & Intent
When keyboard focus enters a standalone single-selection
ToggleButtonGroup(including S2SegmentedControl, which rendersToggleButtonGroupwithselectionMode="single"anddisallowEmptySelection), focus should land on the currently selected item (role="radio"witharia-checked="true") on bothTabandShift+Tab, matching the WAI-ARIA APG Radio Group pattern. Arrow keys should continue moving focus across items without changing selection, and re-entering the control after tabbing away should again focus the selected item rather than an unselected one.Previously,
useToggleButtonGroupdelegated all focus management touseToolbar. On initial keyboard entry,lastFocused.currentwasnull, so focus landed on the first or last item instead of the selected item. After moving focus with arrow keys and tabbing out,useToolbarrestored focus to the unselectedlastFocuseditem instead of the selected item.Implementation
useToolbarInternalhelper inpackages/react-aria/src/toolbar/useToolbar.tsaccepting an optionalgetInitialFocusTargetcallback, while keeping the publicuseToolbarsignature and return type unchanged.useToolbarInternal'sonFocushandler, when keyboard modality enters the root from outside, it checksoptions?.getInitialFocusTarget?.() ?? lastFocused.currentand focuses that target if it differs from the event target, then clearslastFocused.current.packages/react-aria/src/button/useToggleButtonGroup.ts, whenstate.selectionMode === 'single',useToggleButtonGrouppassesgetInitialFocusTargetto find the first focusable descendant matching[role="radio"][aria-checked="true"](excludingaria-disabled="true") viagetFocusableTreeWalker.useToolbarInternalrather than switching to rovingtabIndexkeeps existingToolbarandselectionMode="multiple"behavior untouched, automatically avoids hijacking arrow navigation when a single-selectionToggleButtonGroupis nested inside a<Toolbar>(whereisInToolbardisablesonFocusCapture), and gracefully falls back to standard toolbar behavior when no item is selected or the selected item is disabled.📝 Test Instructions
Regression Tests (RED → GREEN)
packages/react-aria-components/test/ToggleButtonGroup.test.jscovering:TabandShift+Tabentry focusing the selected item in single-selection mode, and re-entry afterArrowRight+Tabout still focusing the selected item.virtualinteraction modality not redirecting focus.selectionMode="multiple".ToggleButtonGroupinside<Toolbar>not overriding arrow navigation.packages/@react-spectrum/s2/test/SegmentedControl.test.tsx(focuses the selected item when tabbing into the control).ToggleButtonGroup.test.jsandSegmentedControl.test.tsxfailed with focus landing onDay(aria-checked="false") instead ofMonth(aria-checked="true").Lint, Formatting & Typecheck
yarn format:check: passed (0 issues across 6591 files).oxlint packages: passed (0 warnings, 0 errors across 2865 files).node scripts/lint-packages.jsandyarn constraints: passed (exit code 0).yarn check-types: workspace dependencies were installed with--mode=skip-build(skippingpostinstall), sotsc --noEmitreported only missing generated@spectrum-icons/*modules and unpatchednode_modulesdefinitions; 0 type errors in any non-icon package source or test files (including all modified files).Untested / Inapplicable
yarn test:ssr packages/react-aria-components/test/ToggleButtonGroup.test.jsfound no matching.ssr.test.*files forToggleButtonGroup/SegmentedControl/Toolbar.yarn test,yarn test:browser(Playwright), and manual browser / screen reader testing were not run locally.Release impact
✅ PR Checklist
AI Assistance Disclosure: Used Antigravity (Gemini) to analyze the root cause in
useToggleButtonGroup/useToolbar, draft the surgical hook changes and regression tests, and format the PR description. All diffs and test outputs were reviewed.🧢 Your Project
Open-source contribution