Skip to content

Commit 7403a47

Browse files
authored
Improve initial useCombobox initialization process (#2818)
* Improve initial combobox initialization process * Create .changeset/olive-glasses-listen.md
1 parent 81447a1 commit 7403a47

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

.changeset/olive-glasses-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Improve initial `useCombobox` initialization process to avoid race conditions

src/drafts/hooks/useCombobox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export const useCombobox = <T>({
9797
if (!list.getAttribute('role')) list.setAttribute('role', 'listbox')
9898

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

102101
// By using state instead of a ref here, we trigger the toggleKeyboardEventHandling
103102
// effect. Otherwise we'd have to depend on isOpen in this effect to start the instance
@@ -116,7 +115,9 @@ export const useCombobox = <T>({
116115
useEffect(
117116
function toggleKeyboardEventHandling() {
118117
const wasOpen = isOpenRef.current
119-
isOpenRef.current = isOpen
118+
119+
// It cannot be open if the instance hasn't yet been initialized
120+
isOpenRef.current = isOpen && comboboxInstance !== null
120121

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

0 commit comments

Comments
 (0)