Open
Description
Most Ark Components use Component.Context to expose their state and functionality.
However, the Combobox Ark component uses a useCombobox
hook that isn't made available with the Park-UI component as you can see with
import { Combobox, createListCollection, useCombobox } from '@ark-ui/react/combobox'
...
const combobox = useCombobox({ collection: collection, onInputValueChange: handleInputChange })
...
<button onClick={() => combobox.focus()}>Focus</button>
From the full Ark component documentation example
Using the Root Provider
The RootProvider component provides a context for the combobox. It accepts the value of the useCombobox hook. You can leverage it to access the component state and methods from outside the combobox.
React
Solid
Vue
import { Combobox, createListCollection, useCombobox } from '@ark-ui/react/combobox'
import { Portal } from '@ark-ui/react/portal'
import { useMemo, useState } from 'react'
const initialItems = ['React', 'Solid', 'Vue']
export const RootProvider = () => {
const [items, setItems] = useState(initialItems)
const collection = useMemo(() => createListCollection({ items }), [items])
const handleInputChange = (details: Combobox.InputValueChangeDetails) => {
setItems(
initialItems.filter((item) => item.toLowerCase().includes(details.inputValue.toLowerCase())),
)
}
const combobox = useCombobox({ collection: collection, onInputValueChange: handleInputChange })
return (
<>
<button onClick={() => combobox.focus()}>Focus</button>
<Combobox.RootProvider value={combobox}>
<Combobox.Label>Framework</Combobox.Label>
<Combobox.Control>
<Combobox.Input />
<Combobox.Trigger>Open</Combobox.Trigger>
<Combobox.ClearTrigger>Clear</Combobox.ClearTrigger>
</Combobox.Control>
<Portal>
<Combobox.Positioner>
<Combobox.Content>
<Combobox.ItemGroup>
<Combobox.ItemGroupLabel>Frameworks</Combobox.ItemGroupLabel>
{collection.items.map((item) => (
<Combobox.Item key={item} item={item}>
<Combobox.ItemText>{item}</Combobox.ItemText>
<Combobox.ItemIndicator>✓</Combobox.ItemIndicator>
</Combobox.Item>
))}
</Combobox.ItemGroup>
</Combobox.Content>
</Combobox.Positioner>
</Portal>
</Combobox.RootProvider>
</>
)
}
If you're using the RootProvider component, you don't need to use the Root component.
I need the useCombobox available so that I can use Park-UI and not have to bypass to import directly from Ark so that I can for example focus the combobox on click
Metadata
Metadata
Assignees
Labels
No labels