Skip to content

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

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

SalmanBarkat wants to merge 1 commit 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

@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

1 participant