Skip to content

fix(selection): prevent empty ListBox from stealing focus in Select Popover#9808

Open
albertdugba wants to merge 1 commit intoadobe:mainfrom
albertdugba:fix/empty-listbox-focus-steal
Open

fix(selection): prevent empty ListBox from stealing focus in Select Popover#9808
albertdugba wants to merge 1 commit intoadobe:mainfrom
albertdugba:fix/empty-listbox-focus-steal

Conversation

@albertdugba
Copy link
Contributor

This fixes issue #9803

Closes

✅ 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:

🧢 Your Project:

@github-actions github-actions bot added the RAC label Mar 18, 2026
const didAutoFocusRef = useRef(false);
useEffect(() => {
if (autoFocusRef.current) {
// Don't autofocus an empty collection. Wait until items are added.
Copy link
Member

Choose a reason for hiding this comment

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

I think we can still autoFocus an empty collection, but it's hard to tell the difference between an intentionally empty collection and one that is waiting for the first set of items to appear due to the double render nature of collections.

In addition, this may now steal focus unintentionally if you go from 0 items to N items much later.

I think the solution to this would typically be, if the user knows that it's intentionally empty and they don't want to autoFocus the empty collection, they set autoFocus to false.

I realise this may be harder inside a Select, you could intercept the ListBoxContext and override that property.

Will have to think on this some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants