Skip to content

Fix: Enable tabbing to tag remove buttons #8002

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

Merged
merged 7 commits into from
Apr 11, 2025

Conversation

kirilMatsiuk
Copy link
Contributor

This PR fixes issue #7483 by improving keyboard accessibility for tag remove buttons.

Changes made:

  • Added keyboardNavigationBehavior: 'tab' to useTagGroup
  • Removed delete rowProps.onKeyDownCapture from useTag
  • Removed excludeFromTabOrder: true from useTag
  • Updated tests to verify correct keyboard navigation

These changes allow users to tab from a tag to its remove button, making the remove functionality accessible via keyboard. Arrow keys still work for navigating between tags.

Before this fix, keyboard users couldn't access the remove buttons via tab navigation, making it impossible to remove tags using only a keyboard.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

  1. Navigate to the Tag component example in the storybook
  2. Use keyboard navigation to focus on a tag
  3. Press Tab to verify focus moves to the remove button (X)
  4. Press Enter/Space to verify the tag can be removed
  5. Verify that arrow keys still work to navigate between different tags

🧢 Your Project:

Thomson Reuters - Contributing to improve React Spectrum accessibility

@yihuiliao
Copy link
Member

Thanks for the PR! You'll need to sign the CLA for it the PR to pass

snowystinger
snowystinger previously approved these changes Apr 7, 2025
Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

Looks pretty good, I added a few more higher level tests to verify RAC and interactions between the TagGroup and other expected components continue to work

One interesting thing I found is that a Tooltip around a Tag with a removable button will still show the tooltip even if focus is on the remove button instead. I am unsure how we want to handle this one. Will require further team discussion. It's not something you have done though, it's just come up as a result of the work. I'm still approving.

await user.tab({shift: true});
expect(document.activeElement).toBe(tags[1]);

// TODO: Fix, this is going to the previous tag's remove button instead of to the gridlist item
Copy link
Member

Choose a reason for hiding this comment

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

Found a test case that's broken, however, I don't think it's due to any of your work, I think this just surfaced it. If you want to have a look go ahead.

Comment on lines +187 to +189
<Tag key="1">Tag 1<Button slot="remove">X</Button></Tag>
<Tag key="2">Tag 2<Button slot="remove">X</Button></Tag>
<Tag key="3">Tag 3<Button slot="remove">X</Button></Tag>
Copy link
Member

Choose a reason for hiding this comment

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

Is it weird that a user tabbing into the Gridlist's TagGroup needs to tab through each of the "X" buttons before being able to tab out of the GridList entirely? I think ideally hitting Tab when focused on the "X" button would move you out of the GridList then

Copy link
Member

Choose a reason for hiding this comment

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

Ah looks like the same case as what @snowystinger mentioned https://github.com/adobe/react-spectrum/pull/8002/files#r2030387211, due to

case 'Tab': {
if (keyboardNavigationBehavior === 'tab') {
// If there is another focusable element within this item, stop propagation so the tab key
// is handled by the browser and not by useSelectableCollection (which would take us out of the list).
let walker = getFocusableTreeWalker(ref.current, {tabbable: true});
walker.currentNode = document.activeElement;
let next = e.shiftKey ? walker.previousNode() : walker.nextNode();
if (next) {
e.stopPropagation();
}
}

Pushed up a fix

Comment on lines +520 to +527
// For some reason "await user.tab({shift: true});"" doesn't seem to follow the correct behavior when coercing focus to the collection before
// letting the browser handle the tab event so we simulate this
fireEvent.keyDown(document.activeElement, {key: 'Tab', shiftKey: true});
let walker = getFocusableTreeWalker(document.body, {tabbable: true});
walker.currentNode = document.activeElement;
act(() => {walker.previousNode().focus();});
fireEvent.keyUp(document.activeElement, {key: 'Tab', shiftKey: true});
expect(document.activeElement).toBe(items[0]);
Copy link
Member

Choose a reason for hiding this comment

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

weird that I had to do this, open to any ideas

Copy link
Member

Choose a reason for hiding this comment

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

We've had to do that in a couple places before, I think it's ok, just a limitation of the libraries we can use. Might be better in a later jsdom

@devongovett devongovett added this pull request to the merge queue Apr 11, 2025
Merged via the queue into adobe:main with commit 271277c Apr 11, 2025
30 checks passed
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.

5 participants