Skip to content

refactor: Build Select and Multiselect on a shared useListbox hook - #5239

Open
amanabiy wants to merge 8 commits into
refactor/share-listbox-helpersfrom
refactor/use-listbox-in-select
Open

amanabiy wants to merge 8 commits into
refactor/share-listbox-helpersfrom
refactor/use-listbox-in-select

Conversation

@amanabiy

@amanabiy amanabiy commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Description

Stacked on #5249.

Adds an internal useListbox hook, built from the shared listbox helpers in #5249, and builds useSelect on it, so Select and Multiselect share one list engine. No behavior change for Select or Multiselect.

useListbox owns:

  • highlight, keyboard navigation (useMenuKeyboard) and choosing an option
  • row props and mouse handlers
  • the live region announcement
  • scrolling the highlighted row into view and the active descendant
  • the base list props, through listRef and getListProps({ withKeyboard, embedded })
  • the focus and blur handlers of an embedded list (embedded Multiselect), moved from useSelect with the same rule: focus highlights the first option when nothing is highlighted, blur clears the highlight

Open state, the filter, type-ahead and the highlight on open stay in useSelect, because they differ per component.

Also:

  • gives the shared list types and props neutral names (menuProps → listProps)
  • renames the useMenuKeyboard option closeDropdown to onEscape

Related links, issue #, if available: n/a

How has this been tested?

  • New unit tests for useListbox (use-listbox.test.tsx), including the embedded focus handling.
  • Existing Select, Multiselect, Autosuggest, Property filter and listbox unit tests pass unchanged. No existing test file was edited.
  • Documenter and test-utils snapshots are unchanged.
  • Existing integration tests run in CI.
Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

New useListbox (src/internal/components/listbox/use-listbox.ts) owns the list state shared by list components: highlight, keyboard navigation, the choose guard, mouse handlers, row props and the selected-announcement flag. useSelect calls it and keeps the dropdown concerns: trigger, open state, filter, dialog, focus, Escape, the highlight on open and embedded focus. Its props and return values are unchanged.

No behavior change and no existing test changed.
…ultiselect

Moves the live region announcement (useAnnouncement) from Select and Multiselect into useListbox, and the scroll-to-highlighted ref and effect into useSelect. Both components pass the same getParent, selectedAriaLabel and renderHighlightedAriaLive as before. No behavior change.
@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.84%. Comparing base (e468024) to head (bae2000).

Additional details and impacted files
@@                        Coverage Diff                         @@
##           refactor/share-listbox-helpers    #5239      +/-   ##
==================================================================
+ Coverage                           97.77%   97.84%   +0.07%     
==================================================================
  Files                                1004     1008       +4     
  Lines                               32566    32629      +63     
  Branches                            12100    12108       +8     
==================================================================
+ Hits                                31841    31926      +85     
+ Misses                                719      697      -22     
  Partials                                6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

ScrollToIndexRef moves to listbox/interfaces.ts, SelectListProps is renamed to ListProps, and the PlainList/VirtualList prop menuProps is renamed to listProps, so the shared list code no longer uses Select names. useListbox imports the ref type from interfaces instead of a component file. Two use-listbox docstrings now say exactly when activeDescendantId is undefined and when scrollToIndex is called. No behavior change.
useListbox is the only caller of useMenuKeyboard and already calls the option onEscape, so the option now has one name end to end. It describes the key, not the dropdown. No behavior change.
Move the onFocus and onBlur handlers of the embedded Multiselect list
from useSelect into useListbox, behind getListProps({ embedded }). The
rule is the same as before: focus highlights the first option if
nothing is highlighted, and blur clears the highlight. No behavior
change.

This branch was successfully deployed

3 active deployments
dev-pages-react18 — bae20008 Deployed Nov 4, 2026 by amanabiy via deploy (React 18) / deploy #3171
dev-pages-react16 — bae20008 Deployed Nov 4, 2026 by amanabiy via deploy (React 16) / deploy #3171
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.

2 participants