Skip to content

Commit

Permalink
Improve initial useCombobox initialization process (#2818)
Browse files Browse the repository at this point in the history
* Improve initial combobox initialization process

* Create .changeset/olive-glasses-listen.md
  • Loading branch information
iansan5653 authored Jan 26, 2023
1 parent 81447a1 commit 7403a47
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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

0 comments on commit 7403a47

Please sign in to comment.