Skip to content

Fix dcc.Dropdown Shift+Tab focus trap on hidden focus-target input - #4207

Open
SalmanBarkat wants to merge 4 commits into
plotly:devfrom
SalmanBarkat:fix/4191-dropdown-shift-tab-focus
Open

SalmanBarkat wants to merge 4 commits into
plotly:devfrom
SalmanBarkat:fix/4191-dropdown-shift-tab-focus

Conversation

@SalmanBarkat

Copy link
Copy Markdown

Fixes #4191

Root Cause

\Dropdown\ renders a hidden <input className=dash-dropdown-focus-target aria-hidden=true>\ with \ abIndex={0}\ right before the trigger <button tabIndex={-1}>\ so Safari includes the dropdown in the \Tab\ order.

Previously, \dummyInput.tabIndex = -1\ was only cleared inside the hidden input's own \onFocus\ handler, while the trigger button's \onBlur\ always restored \dummyInput.tabIndex = 0. Whenever focus landed directly on the trigger <button>\ without first passing through the hidden input (for example after clicking the trigger, closing the popover with \Escape, or selecting an option with \closeOnSelect), the hidden input remained at \ abIndex={0}. Pressing \Shift+Tab\ from the trigger <button>\ then moved focus back onto the hidden \�ria-hidden\ input (with
elatedTarget === dropdownContainerRef.current), trapping keyboard focus there.

What Changed

  • Added \onFocus\ on the trigger <button>\ in \components/dash-core-components/src/fragments/Dropdown.tsx\ to set the preceding focus target's \ abIndex\ to -1\ whenever the trigger receives focus directly.
  • Updated the trigger <button>'s \onBlur\ to restore \ abIndex\ to \disabled ? -1 : 0\ via a shared \setFocusTargetTabIndex\ helper so disabled dropdowns do not re-enable the focus target on blur.
  • Added unit tests in \components/dash-core-components/tests/unit/dropdown/focusTarget.test.tsx\ covering direct trigger focus/blur, disabled state, and Tab focus delegation.

Verification

  • Ran
    pm run test:jest\ in \components/dash-core-components: 10 passed test suites, 96 passed tests (93 existing + 3 new tests in \ ocusTarget.test.tsx).
  • Ran
    px eslint src/fragments/Dropdown.tsx tests/unit/dropdown/focusTarget.test.tsx: 0 errors.

Contributor Checklist

  • This PR targets \dev\ (fix or chore) or
    ext\ (feature, new API, breaking change), see Which branch to target
  • I have broken down my PR scope into the following TODO tasks
    • Clear hidden focus target \ abIndex\ when trigger button gains focus directly
    • Preserve \ abIndex = -1\ on blur when dropdown is disabled
    • Add Jest unit tests covering focus-target \ abIndex\ transitions
  • I have run the tests locally and they passed. (refer to testing section in contributing)
  • I have added tests, or extended existing tests, to cover any new features or bugs fixed in this PR

@T4rk1n T4rk1n left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Setting the hidden input to tabIndex=-1 when the trigger gets focus covers the click, Escape and pick-an-option cases from #4191. I checked the focus order in headless Chrome: Shift+Tab now goes straight to the previous element, and Tab still goes through the dropdown normally both ways.

One ask before merge: the jest tests fire synthetic focus/blur events, so they never run the browser's actual Tab order, which is where this bug lives. See the inline comment.

Small cleanup, not blocking: now that the button's onFocus sets tabIndex = -1, the same line in the input's onFocus handler is redundant and can go.

expect(focusTarget.tabIndex).toBe(0);

// Simulates clicking the trigger or Radix returning focus after Escape/selection.
fireEvent.focus(trigger);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fireEvent.focus only calls the handler. It doesn't show that a real Shift+Tab skips the hidden input. These assertions would pass even if the browser chose where focus goes after the blur restores tabIndex=0, and that ordering is what this bug is about. Can you add an integration test in tests/integration/dropdown/test_a11y.py next to test_a11y012c? Click the dropdown, press Escape, Shift+Tab, then assert document.activeElement.id == "before". A second case that picks an option with Enter before the Shift+Tab would cover the other path in the issue. That test should fail on dev and pass here.

@sonarqubecloud

Copy link
Copy Markdown

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.

[BUG] dcc.Dropdown: Shift+Tab traps focus on the hidden focus-target input

2 participants