Repository navigation
Adjust active index when Menu items and Listbox options are unregistered - #3918
Open
atsui-affinity wants to merge 2 commits into
Open
atsui-affinity wants to merge 2 commits into
atsui-affinity wants to merge 2 commits into
Conversation
Delegate the unregister reducers to adjustOrderedState, matching Combobox, so removing the active item no longer leaves a stale index that crashes on Enter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
@atsui-affinity is attempting to deploy a commit to the Tailwind Labs Team on Vercel. A member of the Team first needs to authorize it. |
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
atsui-affinity
marked this pull request as draft
November 9, 2026 16:51
atsui-affinity
marked this pull request as ready for review
November 9, 2026 17:03
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #3856
An item or option can be removed while a
MenuorListboxis open. TheUnregisterItemsandUnregisterOptionsreducers then remove it from the array. They do not changeactiveItemIndexoractiveOptionIndex. The index now points past the end of the array. The nextEnterthrowsCannot destructure property 'dataRef' of ... as it is undefined.Comboboxalready avoids this because its unregister reducer callsadjustOrderedState(). This PR makes theMenuandListboxreducers do the same. If the active item was removed, the active index becomesnull. If an earlier item was removed, the index moves so the same item stays active.This is a smaller take on #3857. It reuses the existing helper and adds no null guards, because the reducer fix makes them unreachable.
@headlessui/vueis not affected. ItsunregisterItemalready usesadjustOrderedState.How to reproduce issue
Minimal React Code
Here is a minimal repro, trimmed from the demo page I recorded. It uses @headlessui/react 2.2.10 and React 18 or 19.
To trigger it:
You get Uncaught TypeError: Cannot destructure property 'dataRef' of 'machine.state.items[machine.state.activeItemIndex]' as it is undefined. The menu stays open and Enter does nothing.
The timer stands in for real cases, such as a permission change or a filter that removes items while the menu is open. The Listbox version is the same, with Listbox.Option in place of Menu.Item.
Video
Before - error appears in menu item
headlessui-menu-before-crash.mp4
After - fixed menu
headlessui-menu-after-fixed.mp4
Tests
Two new tests each for
MenuandListbox:Entercloses the menu.All four fail without the fix and pass with it. The full React component suite,
tscand prettier pass.