Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-glasses-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Improve initial `useCombobox` initialization process to avoid race conditions
5 changes: 3 additions & 2 deletions src/drafts/hooks/useCombobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const useCombobox = <T>({
if (!list.getAttribute('role')) list.setAttribute('role', 'listbox')

const cb = new Combobox(input, list, {tabInsertsSuggestions, defaultFirstOption})
if (isOpenRef.current) cb.start()

// By using state instead of a ref here, we trigger the toggleKeyboardEventHandling
// effect. Otherwise we'd have to depend on isOpen in this effect to start the instance
Expand All @@ -116,7 +115,9 @@ export const useCombobox = <T>({
useEffect(
function toggleKeyboardEventHandling() {
const wasOpen = isOpenRef.current
isOpenRef.current = isOpen

// It cannot be open if the instance hasn't yet been initialized
isOpenRef.current = isOpen && comboboxInstance !== null

if (isOpen === wasOpen || !comboboxInstance) return

Expand Down