Skip to content

fix(ToggleButtonGroup): focus selected item on keyboard entry in single-selection mode - #11067

Open
peremunoz wants to merge 3 commits into
adobe:mainfrom
peremunoz:fix/single-toggle-button-group-focus
Open

peremunoz wants to merge 3 commits into
adobe:mainfrom
peremunoz:fix/single-toggle-button-group-focus

Conversation

@peremunoz

Copy link
Copy Markdown

Closes #11059

Summary

Problem & Intent

When keyboard focus enters a standalone single-selection ToggleButtonGroup (including S2 SegmentedControl, which renders ToggleButtonGroup with selectionMode="single" and disallowEmptySelection), focus should land on the currently selected item (role="radio" with aria-checked="true") on both Tab and Shift+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, useToggleButtonGroup delegated all focus management to useToolbar. On initial keyboard entry, lastFocused.current was null, so focus landed on the first or last item instead of the selected item. After moving focus with arrow keys and tabbing out, useToolbar restored focus to the unselected lastFocused item instead of the selected item.

Implementation

  • Added an internal useToolbarInternal helper in packages/react-aria/src/toolbar/useToolbar.ts accepting an optional getInitialFocusTarget callback, while keeping the public useToolbar signature and return type unchanged.
  • In useToolbarInternal's onFocus handler, when keyboard modality enters the root from outside, it checks options?.getInitialFocusTarget?.() ?? lastFocused.current and focuses that target if it differs from the event target, then clears lastFocused.current.
  • In packages/react-aria/src/button/useToggleButtonGroup.ts, when state.selectionMode === 'single', useToggleButtonGroup passes getInitialFocusTarget to find the first focusable descendant matching [role="radio"][aria-checked="true"] (excluding aria-disabled="true") via getFocusableTreeWalker.
  • Why this approach: Resolving the target on keyboard entry via useToolbarInternal rather than switching to roving tabIndex keeps existing Toolbar and selectionMode="multiple" behavior untouched, automatically avoids hijacking arrow navigation when a single-selection ToggleButtonGroup is nested inside a <Toolbar> (where isInToolbar disables onFocusCapture), 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)

  • Added tests in packages/react-aria-components/test/ToggleButtonGroup.test.js covering:
    • Initial Tab and Shift+Tab entry focusing the selected item in single-selection mode, and re-entry after ArrowRight + Tab out still focusing the selected item.
    • Pointer clicks and virtual interaction modality not redirecting focus.
    • Fallback to standard toolbar focus when the selected item is disabled or selection is empty.
    • Unchanged behavior in selectionMode="multiple".
    • Nested single-selection ToggleButtonGroup inside <Toolbar> not overriding arrow navigation.
  • Added reproduction test in packages/@react-spectrum/s2/test/SegmentedControl.test.tsx (focuses the selected item when tabbing into the control).
  • Verified RED before the fix: ToggleButtonGroup.test.js and SegmentedControl.test.tsx failed with focus landing on Day (aria-checked="false") instead of Month (aria-checked="true").
  • Verified GREEN after the fix:
yarn test packages/react-aria-components/test/ToggleButtonGroup.test.js packages/react-aria-components/test/Toolbar.test.tsx packages/@react-spectrum/s2/test/SegmentedControl.test.tsx packages/@react-spectrum/s2/test/ToggleButtonGroup.test.tsx packages/react-aria/test/button/useButton.test.js
-> Test Suites: 5 passed, 5 total (Tests: 43 passed, 43 total)

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.js and yarn constraints: passed (exit code 0).
  • yarn check-types: workspace dependencies were installed with --mode=skip-build (skipping postinstall), so tsc --noEmit reported only missing generated @spectrum-icons/* modules and unpatched node_modules definitions; 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.js found no matching .ssr.test.* files for ToggleButtonGroup / SegmentedControl / Toolbar.
  • Full monorepo yarn test, yarn test:browser (Playwright), and manual browser / screen reader testing were not run locally.

Release impact

  • Only changes docs, tests, CI, or development tools. Nothing shipped to users changes.
  • Fixes a bug or improves existing code without changing how apps use it.
  • Adds a feature or API while keeping existing usage working.
  • May break existing usage or require changes in apps that use this code.

✅ PR Checklist

  • I followed the contributing guide.
  • I tested this change locally and included the results in Test Instructions, or explained why testing does not apply.
  • I understand and can explain every change in this PR. If I used AI assistance, I followed the AI contribution guidance.

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

…le-selection mode

Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
@peremunoz peremunoz closed this Dec 1, 2026
@peremunoz peremunoz reopened this Dec 1, 2026
Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
@peremunoz
peremunoz marked this pull request as ready for review December 1, 2026 21:11

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@peremunoz

peremunoz commented Dec 4, 2026 •

Copy link
Copy Markdown
Author

Good point @snowystinger . Keeping useToolbar untouched makes sense since focusing on the selected item is specific to the radiogroup pattern in single-selection useToggleButtonGroup, not Toolbar.

We can keep all of this inside useToggleButtonGroup by reverting useToolbar.ts and wrapping toolbarProps.onFocusCapture only when state.selectionMode === 'single' and toolbarProps.onFocusCapture is defined (which preserves the !isInToolbar behavior when nested inside a <Toolbar>):

  • On external keyboard entry (!nodeContains(e.currentTarget, e.relatedTarget) && nodeContains(ref.current, getEventTarget(e)) with getInteractionModality() === 'keyboard'), find the focusable [role="radio"][aria-checked="true"] (excluding aria-disabled="true") via getFocusableTreeWalker.
  • If found, focus it (when different from the event target) and return early so useToolbar's onFocusCapture doesn't briefly move focus to lastFocused first.
  • Otherwise (empty selection or disabled selected item), fall back to toolbarProps.onFocusCapture(e).

(One small trade-off of returning early without calling useToolbar's handler is that its internal lastFocused ref isn't cleared on that re-entry, which only surfaces if selection is later cleared and the user blurs via pointer rather than Tab, not an issue for disallowEmptySelection / SegmentedControl.)

I'll update the PR to revert useToolbar.ts and move this logic into useToggleButtonGroup.ts.

… of useToolbar

Signed-off-by: Pere Muñoz <peremunozfiguerol33@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S2 SegmentedControl focuses an unselected item when tabbing into the group

2 participants