Repository navigation
Fix dcc.Dropdown Shift+Tab focus trap on hidden focus-target input #4207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SalmanBarkat
wants to merge
4
commits into
plotly:dev
Choose a base branch
from
SalmanBarkat:fix/4191-dropdown-shift-tab-focus
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7be986c
Fix dcc.Dropdown Shift+Tab focus trap on hidden focus-target input
SalmanBarkat fcde243
Merge branch 'dev' into fix/4191-dropdown-shift-tab-focus
T4rk1n 0b65917
Add Shift+Tab integration test after Escape and Enter and drop redund…
SalmanBarkat 2ca45a0
Merge branch 'fix/4191-dropdown-shift-tab-focus' of https://github.co…
SalmanBarkat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
90 changes: 90 additions & 0 deletions
90
components/dash-core-components/tests/unit/dropdown/focusTarget.test.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import React from 'react'; | ||
| import {fireEvent, render, screen, waitFor} from '@testing-library/react'; | ||
| import Dropdown from '../../../src/components/Dropdown'; | ||
|
|
||
| const options = [ | ||
| {label: 'Apple', value: 'apple'}, | ||
| {label: 'Banana', value: 'banana'}, | ||
| ]; | ||
|
|
||
| async function findDropdownElements(container: HTMLElement) { | ||
| const trigger = await waitFor(() => { | ||
| const button = container.querySelector('button.dash-dropdown'); | ||
| expect(button).toBeInstanceOf(HTMLButtonElement); | ||
| return button as HTMLButtonElement; | ||
| }); | ||
| const focusTarget = container.querySelector('.dash-dropdown-focus-target'); | ||
| expect(focusTarget).toBeInstanceOf(HTMLInputElement); | ||
| return { | ||
| trigger, | ||
| focusTarget: focusTarget as HTMLInputElement, | ||
| }; | ||
| } | ||
|
|
||
| describe('Dropdown focus target', () => { | ||
| it('removes focus target from tab order when trigger button is focused directly and restores it on blur', async () => { | ||
| const {container} = render( | ||
| <div> | ||
| <button type="button">Before</button> | ||
| <Dropdown | ||
| id="test-dropdown" | ||
| options={options} | ||
| setProps={jest.fn()} | ||
| /> | ||
| <button type="button">After</button> | ||
| </div> | ||
| ); | ||
|
|
||
| const {trigger, focusTarget} = await findDropdownElements(container); | ||
| expect(focusTarget.tabIndex).toBe(0); | ||
|
|
||
| // Simulates clicking the trigger or Radix returning focus after Escape/selection. | ||
| fireEvent.focus(trigger); | ||
| expect(focusTarget.tabIndex).toBe(-1); | ||
|
|
||
| // Moving focus out restores the focus target so Safari Tab navigation still works. | ||
| fireEvent.blur(trigger); | ||
| expect(focusTarget.tabIndex).toBe(0); | ||
| }); | ||
|
|
||
| it('keeps focus target untabbable when disabled', async () => { | ||
| const {container} = render( | ||
| <Dropdown | ||
| id="disabled-dropdown" | ||
| options={options} | ||
| disabled | ||
| setProps={jest.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| const {trigger, focusTarget} = await findDropdownElements(container); | ||
| expect(focusTarget.tabIndex).toBe(-1); | ||
|
|
||
| fireEvent.focus(trigger); | ||
| expect(focusTarget.tabIndex).toBe(-1); | ||
|
|
||
| fireEvent.blur(trigger); | ||
| expect(focusTarget.tabIndex).toBe(-1); | ||
| }); | ||
|
|
||
| it('delegates focus from focus target to trigger button when entering from another element', async () => { | ||
| const {container} = render( | ||
| <div> | ||
| <button type="button">Before</button> | ||
| <Dropdown | ||
| id="tab-dropdown" | ||
| options={options} | ||
| setProps={jest.fn()} | ||
| /> | ||
| </div> | ||
| ); | ||
|
|
||
| const beforeButton = screen.getByRole('button', {name: 'Before'}); | ||
| const {trigger, focusTarget} = await findDropdownElements(container); | ||
|
|
||
| fireEvent.focus(focusTarget, {relatedTarget: beforeButton}); | ||
|
|
||
| expect(focusTarget.tabIndex).toBe(-1); | ||
| expect(document.activeElement).toBe(trigger); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fireEvent.focusonly 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 restorestabIndex=0, and that ordering is what this bug is about. Can you add an integration test intests/integration/dropdown/test_a11y.pynext totest_a11y012c? Click the dropdown, press Escape, Shift+Tab, then assertdocument.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 ondevand pass here.