Repository navigation
|
Hi there, I'm looking to create a combobox input that will be immediately focused on render and display a list of options underneath. In other words, I'd like to combine the functionality of The way it works now, however, is not the way that I expected. The input focuses immediately on render (apparently respecting the Here's a sandbox example of the behavior I'm talking about. Note that you have to refresh the browser frame in order to see the behavior - the input is auto-focused, but the options do not render until a change in the input. This is counterintuitive to me, and I'm sure there's a hack that would get me the outcome I'm looking for, but I was curious if it was intended behavior or merited the creation of an issue. Thanks! |
Replies: 2 comments
|
This sounds like a timing issue between the initial focus and the Combobox's open interaction.
A small workaround is to trigger focus after mount rather than relying on const inputRef = useRef<HTMLInputElement | null>(null)
useEffect(() => {
requestAnimationFrame(() => {
inputRef.current?.focus()
})
}, [])
<Combobox immediate>
<ComboboxInput ref={inputRef} />
<ComboboxOptions>
{/* options */}
</ComboboxOptions>
</Combobox>If that opens the list correctly, it would confirm this is mostly an ordering issue: mount-time autofocus happens before the Combobox can treat it like an immediate focus-open interaction. For UX, I would also check whether the options should open with the full list before any query is typed. If yes, this seems reasonable as either a docs note or an issue, because |
|
This looks like mount-time The workaround that has been most reliable for me is: don't use function Example() {
const inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
const id = requestAnimationFrame(() => {
inputRef.current?.focus()
})
return () => cancelAnimationFrame(id)
}, [])
return (
<Combobox immediate value={value} onChange={setValue}>
<ComboboxInput ref={inputRef} onChange={(e) => setQuery(e.target.value)} />
<ComboboxOptions anchor="bottom" className="w-[var(--input-width)]">
{filtered.map((item) => (
<ComboboxOption key={item.id} value={item}>
{item.label}
</ComboboxOption>
))}
</ComboboxOptions>
</Combobox>
)
}If you want “open immediately with the full list”, also make sure your filtered list returns all items when the query is empty: const filtered = query === '' ? items : items.filter(...)So I'd call this more of an ordering/timing edge case than misuse on your part. |
|
@yudin-s thanks for the reply. After a lot more messing around, I'm now almost certain this behavior is related to the double-rendering that happens in That could be something to note in documentation (some kind of warning that certain edge cases might come up in development that aren't anything to worry about in prod) but I bet not, so I'm closing the discussion for now. |
@yudin-s thanks for the reply. After a lot more messing around, I'm now almost certain this behavior is related to the double-rendering that happens in
React.StrictModeduring development. In production builds, everything works intuitively.That could be something to note in documentation (some kind of warning that certain edge cases might come up in development that aren't anything to worry about in prod) but I bet not, so I'm closing the discussion for now.