Skip to content

Commit 4675375

Browse files
committed
use the new useRefocusableInput hook
1 parent 8d8bb9b commit 4675375

File tree

1 file changed

+10
-5
lines changed
  • packages/@headlessui-react/src/components/combobox

1 file changed

+10
-5
lines changed

packages/@headlessui-react/src/components/combobox/combobox.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { useIsoMorphicEffect } from '../../hooks/use-iso-morphic-effect'
3232
import { useLatestValue } from '../../hooks/use-latest-value'
3333
import { useOutsideClick } from '../../hooks/use-outside-click'
3434
import { useOwnerDocument } from '../../hooks/use-owner'
35+
import { useRefocusableInput } from '../../hooks/use-refocusable-input'
3536
import { useResolveButtonType } from '../../hooks/use-resolve-button-type'
3637
import { useSyncRefs } from '../../hooks/use-sync-refs'
3738
import { useTrackedPointer } from '../../hooks/use-tracked-pointer'
@@ -1381,6 +1382,8 @@ function ButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>(
13811382
} = props
13821383
let d = useDisposables()
13831384

1385+
let refocusInput = useRefocusableInput(data.inputRef)
1386+
13841387
let handleKeyDown = useEvent((event: ReactKeyboardEvent<HTMLUListElement>) => {
13851388
switch (event.key) {
13861389
// Ref: https://www.w3.org/WAI/ARIA/apg/patterns/menu/#keyboard-interaction-12
@@ -1392,7 +1395,7 @@ function ButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>(
13921395
actions.openCombobox()
13931396
}
13941397

1395-
return d.nextFrame(() => data.inputRef.current?.focus({ preventScroll: true }))
1398+
return d.nextFrame(() => refocusInput())
13961399

13971400
case Keys.ArrowUp:
13981401
event.preventDefault()
@@ -1405,7 +1408,7 @@ function ButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>(
14051408
}
14061409
})
14071410
}
1408-
return d.nextFrame(() => data.inputRef.current?.focus({ preventScroll: true }))
1411+
return d.nextFrame(() => refocusInput())
14091412

14101413
case Keys.Escape:
14111414
if (data.comboboxState !== ComboboxState.Open) return
@@ -1414,7 +1417,7 @@ function ButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>(
14141417
event.stopPropagation()
14151418
}
14161419
actions.closeCombobox()
1417-
return d.nextFrame(() => data.inputRef.current?.focus({ preventScroll: true }))
1420+
return d.nextFrame(() => refocusInput())
14181421

14191422
default:
14201423
return
@@ -1430,7 +1433,7 @@ function ButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>(
14301433
actions.openCombobox()
14311434
}
14321435

1433-
d.nextFrame(() => data.inputRef.current?.focus({ preventScroll: true }))
1436+
d.nextFrame(() => refocusInput())
14341437
})
14351438

14361439
let labelledBy = useLabelledBy([id])
@@ -1629,6 +1632,8 @@ function OptionFn<
16291632
let data = useData('Combobox.Option')
16301633
let actions = useActions('Combobox.Option')
16311634

1635+
let refocusInput = useRefocusableInput(data.inputRef)
1636+
16321637
let active = data.virtual
16331638
? data.activeOptionIndex === data.calculateIndex(value)
16341639
: data.activeOptionIndex === null
@@ -1701,7 +1706,7 @@ function OptionFn<
17011706
// But right now this is still an experimental feature:
17021707
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/virtualKeyboard
17031708
if (!isMobile()) {
1704-
requestAnimationFrame(() => data.inputRef.current?.focus({ preventScroll: true }))
1709+
requestAnimationFrame(() => refocusInput())
17051710
}
17061711

17071712
if (data.mode === ValueMode.Single) {

0 commit comments

Comments
 (0)