useSearchAutocomplete
Provides the behavior and accessibility implementation for a search autocomplete component. A search autocomplete combines a combobox with a searchfield, allowing users to filter a list of options to items matching a query.
| install | yarn add @react-aria/autocomplete |
|---|---|
| version | 3.0.0-alpha.0 |
| usage | import {useSearchAutocomplete} from '@react-aria/autocomplete' |
API#
useSearchAutocomplete<T>(
(props: AriaSearchAutocompleteProps<T>,
, state: ComboBoxState<T>
)): SearchAutocompleteAria<T>Features#
Autocomplete for search fields can be implemented using the <datalist> HTML element, but this has limited functionality and behaves differently across browsers.
useSearchAutocomplete helps achieve accessible search field and autocomplete components that can be styled as needed.
- Support for filtering a list of options by typing
- Support for selecting a single option
- Support for disabled options
- Support for groups of items in sections
- Support for custom user input values
- Support for controlled and uncontrolled options, selection, input value, and open state
- Support for custom filter functions
- Async loading and infinite scrolling support
- Support for virtualized scrolling for performance with long lists
- Exposed to assistive technology as a
comboboxwith ARIA - Labeling support for accessibility
- Required and invalid states exposed to assistive technology via ARIA
- Support for mouse, touch, and keyboard interactions
- Keyboard support for opening the list box using the arrow keys, including automatically focusing the first or last item accordingly
- Support for opening the list box when typing, on focus, or manually
- Handles virtual clicks on the input from touch screen readers to toggle the list box
- Virtual focus management for list box option navigation
- Hides elements outside the input and list box from assistive technology while the list box is open in a portal
- Custom localized announcements for option focusing, filtering, and selection using an ARIA live region to work around VoiceOver bugs
Anatomy#

A search autocomplete consists of a label, an input which displays the current value, and a list box popup. Users can type within the input
to see search suggestions within the list box. The list box popup may be opened by a variety of input field interactions specified
by the menuTrigger prop provided to useSearchAutocomplete. useSearchAutocomplete handles exposing
the correct ARIA attributes for accessibility for each of the elements comprising the search autocomplete. It should be combined
with useListBox, which handles the implementation of the popup list box.
useSearchAutocomplete returns props that you should spread onto the appropriate elements:
| Name | Type | Description |
labelProps | HTMLAttributes<HTMLElement> | Props for the label element. |
inputProps | InputHTMLAttributes<HTMLInputElement> | Props for the search input element. |
listBoxProps | AriaListBoxOptions<T> | Props for the list box, to be passed to useListBox. |
clearButtonProps | AriaButtonProps | Props for the search input's clear button. |
State is managed by the useComboBoxState hook from @react-stately/combobox.
The state object should be passed as an option to useSearchAutocomplete.
If the search field does not have a visible label, an aria-label or aria-labelledby prop must be provided instead to
identify it to assistive technology.
State management#
useSearchAutocomplete requires knowledge of the options in order to handle keyboard
navigation and other interactions. It does this using the Collection
interface, which is a generic interface to access sequential unique keyed data. You can
implement this interface yourself, e.g. by using a prop to pass a list of item objects,
but useComboBoxState from
@react-stately/combobox implements a JSX based interface for building collections instead.
See Collection Components for more information,
and Collection Interface for internal details.
In addition, useComboBoxState
manages the state necessary for single selection and exposes
a SelectionManager,
which makes use of the collection to provide an interface to update the selection state.
It also holds state to track if the popup is open, if the search field is focused, and the current input value.
For more information about selection, see Selection.
Example#
This example uses an <input> element for the search field.
A "contains" filter function is obtained from useFilter and is passed to useComboBoxState so
that the list box can be filtered based on the option text and the current input text.
The list box popup should use the same Popover and ListBox components created with useOverlay
and useListBox that you may already have in your component library or application. These can be shared with other
components such as a Select created with useSelect or a Dialog popover created with useDialog.
The code for these components is also included below in the collapsed sections.
This example does not do any advanced popover positioning or portaling to escape its visual container.
See useOverlayTrigger for an example of how to implement this
using useOverlayPosition.
In addition, see useListBox for examples of sections (option groups), and more complex options.
import {Item} from '@react-stately/collections';
import {useButton} from '@react-aria/button';
import {useComboBoxState} from '@react-stately/combobox';
import {useSearchAutocomplete} from '@react-aria/autocomplete';
import {useFilter} from '@react-aria/i18n';
// Reuse the ListBox and Popover from your component library. See below for details.
import {ListBox Popover} from 'your-component-library';
function SearchAutocomplete(props) {
// Setup filter function and state.
let {contains} = useFilter({sensitivity: 'base'});
let state = useComboBoxState({...props defaultFilter: contains});
// Setup refs and get props for child elements.
let inputRef = ReactuseRef(null);
let listBoxRef = ReactuseRef(null);
let popoverRef = ReactuseRef(null);
let {
inputProps
listBoxProps
labelProps
clearButtonProps
} = useSearchAutocomplete(
{
...props
popoverRef
listBoxRef
inputRef
}
state
);
let {buttonProps} = useButton(clearButtonProps);
return (
<div style={display: 'inline-flex' flexDirection: 'column'}>
<label ...labelProps>propslabel</label>
<div style={position: 'relative' display: 'inline-block'}>
<input
...inputProps
ref=inputRef
style={
height: 24
boxSizing: 'border-box'
marginRight: 0
fontSize: 16
}
/>
stateinputValue !== '' && <button ...buttonProps>❎</button>
stateisOpen && (
<Popover
popoverRef=popoverRef
isOpen=stateisOpen
onClose=stateclose>
<ListBox ...listBoxProps listBoxRef=listBoxRef state=state />
</Popover>
)
</div>
</div>
);
}
<SearchAutocomplete label="Search Animals">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete>import {Item} from '@react-stately/collections';
import {useButton} from '@react-aria/button';
import {useComboBoxState} from '@react-stately/combobox';
import {useSearchAutocomplete} from '@react-aria/autocomplete';
import {useFilter} from '@react-aria/i18n';
// Reuse the ListBox and Popover from your component library. See below for details.
import {ListBox Popover} from 'your-component-library';
function SearchAutocomplete(props) {
// Setup filter function and state.
let {contains} = useFilter({sensitivity: 'base'});
let state = useComboBoxState({
...props
defaultFilter: contains
});
// Setup refs and get props for child elements.
let inputRef = ReactuseRef(null);
let listBoxRef = ReactuseRef(null);
let popoverRef = ReactuseRef(null);
let {
inputProps
listBoxProps
labelProps
clearButtonProps
} = useSearchAutocomplete(
{
...props
popoverRef
listBoxRef
inputRef
}
state
);
let {buttonProps} = useButton(clearButtonProps);
return (
<div
style={
display: 'inline-flex'
flexDirection: 'column'
}>
<label ...labelProps>propslabel</label>
<div
style={
position: 'relative'
display: 'inline-block'
}>
<input
...inputProps
ref=inputRef
style={
height: 24
boxSizing: 'border-box'
marginRight: 0
fontSize: 16
}
/>
stateinputValue !== '' && (
<button ...buttonProps>❎</button>
)
stateisOpen && (
<Popover
popoverRef=popoverRef
isOpen=stateisOpen
onClose=stateclose>
<ListBox
...listBoxProps
listBoxRef=listBoxRef
state=state
/>
</Popover>
)
</div>
</div>
);
}
<SearchAutocomplete label="Search Animals">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete>import {Item} from '@react-stately/collections';
import {useButton} from '@react-aria/button';
import {useComboBoxState} from '@react-stately/combobox';
import {useSearchAutocomplete} from '@react-aria/autocomplete';
import {useFilter} from '@react-aria/i18n';
// Reuse the ListBox and Popover from your component library. See below for details.
import {
ListBox
Popover
} from 'your-component-library';
function SearchAutocomplete(
props
) {
// Setup filter function and state.
let {
contains
} = useFilter({
sensitivity: 'base'
});
let state = useComboBoxState(
{
...props
defaultFilter: contains
}
);
// Setup refs and get props for child elements.
let inputRef = ReactuseRef(
null
);
let listBoxRef = ReactuseRef(
null
);
let popoverRef = ReactuseRef(
null
);
let {
inputProps
listBoxProps
labelProps
clearButtonProps
} = useSearchAutocomplete(
{
...props
popoverRef
listBoxRef
inputRef
}
state
);
let {
buttonProps
} = useButton(
clearButtonProps
);
return (
<div
style={
display:
'inline-flex'
flexDirection:
'column'
}>
<label
...labelProps>
propslabel
</label>
<div
style={
position:
'relative'
display:
'inline-block'
}>
<input
...inputProps
ref=inputRef
style={
height: 24
boxSizing:
'border-box'
marginRight: 0
fontSize: 16
}
/>
stateinputValue !==
'' && (
<button
...buttonProps>
❎
</button>
)
stateisOpen && (
<Popover
popoverRef=
popoverRef
isOpen=
stateisOpen
onClose=
stateclose
>
<ListBox
...listBoxProps
listBoxRef=
listBoxRef
state=
state
/>
</Popover>
)
</div>
</div>
);
}
<SearchAutocomplete label="Search Animals">
<Item key="red panda">
Red Panda
</Item>
<Item key="cat">
Cat
</Item>
<Item key="dog">
Dog
</Item>
<Item key="aardvark">
Aardvark
</Item>
<Item key="kangaroo">
Kangaroo
</Item>
<Item key="snake">
Snake
</Item>
</SearchAutocomplete>Popover#
The Popover component is used to contain the popup listbox for the SearchAutocomplete.
It can be shared between many other components, including Select,
Menu, Dialog, and others.
See useOverlayTrigger for more examples of popovers.
Show code
import {useOverlay DismissButton} from '@react-aria/overlays';
import {FocusScope} from '@react-aria/focus';
function Popover(props) {
let ref = ReactuseRef();
let {popoverRef = ref isOpen onClose children} = props;
// Handle events that should cause the popup to close,
// e.g. blur, clicking outside, or pressing the escape key.
let {overlayProps} = useOverlay(
{
isOpen
onClose
shouldCloseOnBlur: true
isDismissable: true
}
popoverRef
);
// Add a hidden <DismissButton> component at the end of the popover
// to allow screen reader users to dismiss the popup easily.
return (
<FocusScope restoreFocus>
<div
...overlayProps
ref=popoverRef
style={
position: 'absolute'
width: '100%'
border: '1px solid gray'
background: 'lightgray'
marginTop: 4
}>
children
<DismissButton onDismiss=onClose />
</div>
</FocusScope>
);
}
import {
useOverlay
DismissButton
} from '@react-aria/overlays';
import {FocusScope} from '@react-aria/focus';
function Popover(props) {
let ref = ReactuseRef();
let {popoverRef = ref isOpen onClose children} = props;
// Handle events that should cause the popup to close,
// e.g. blur, clicking outside, or pressing the escape key.
let {overlayProps} = useOverlay(
{
isOpen
onClose
shouldCloseOnBlur: true
isDismissable: true
}
popoverRef
);
// Add a hidden <DismissButton> component at the end of the popover
// to allow screen reader users to dismiss the popup easily.
return (
<FocusScope restoreFocus>
<div
...overlayProps
ref=popoverRef
style={
position: 'absolute'
width: '100%'
border: '1px solid gray'
background: 'lightgray'
marginTop: 4
}>
children
<DismissButton onDismiss=onClose />
</div>
</FocusScope>
);
}
import {
useOverlay
DismissButton
} from '@react-aria/overlays';
import {FocusScope} from '@react-aria/focus';
function Popover(props) {
let ref = ReactuseRef();
let {
popoverRef = ref
isOpen
onClose
children
} = props;
// Handle events that should cause the popup to close,
// e.g. blur, clicking outside, or pressing the escape key.
let {
overlayProps
} = useOverlay(
{
isOpen
onClose
shouldCloseOnBlur: true
isDismissable: true
}
popoverRef
);
// Add a hidden <DismissButton> component at the end of the popover
// to allow screen reader users to dismiss the popup easily.
return (
<FocusScope
restoreFocus>
<div
...overlayProps
ref=popoverRef
style={
position:
'absolute'
width: '100%'
border:
'1px solid gray'
background:
'lightgray'
marginTop: 4
}>
children
<DismissButton
onDismiss=
onClose
/>
</div>
</FocusScope>
);
}
ListBox#
The ListBox and Option components are used to show the filtered list of options as the
user types in the SearchAutocomplete. They can also be shared with other components like a Select. See
useListBox for more examples, including sections and more complex items.
Show code
import {useListBox useOption} from '@react-aria/listbox';
function ListBox(props) {
let ref = ReactuseRef();
let {listBoxRef = ref state} = props;
let {listBoxProps} = useListBox(props state listBoxRef);
return (
<ul
...listBoxProps
ref=listBoxRef
style={
margin: 0
padding: 0
listStyle: 'none'
maxHeight: '150px'
overflow: 'auto'
}>
[...statecollection]map((item) => (
<Option key=itemkey item=item state=state />
))
</ul>
);
}
function Option({item state}) {
let ref = ReactuseRef();
let {optionProps isSelected isFocused isDisabled} = useOption(
{key: itemkey}
state
ref
);
let backgroundColor;
let color = 'black';
if (isSelected) {
backgroundColor = 'blueviolet';
color = 'white';
} else if (isFocused) {
backgroundColor = 'gray';
} else if (isDisabled) {
backgroundColor = 'transparent';
color = 'gray';
}
return (
<li
...optionProps
ref=ref
style={
background: backgroundColor
color: color
padding: '2px 5px'
outline: 'none'
cursor: 'pointer'
}>
itemrendered
</li>
);
}
import {useListBox useOption} from '@react-aria/listbox';
function ListBox(props) {
let ref = ReactuseRef();
let {listBoxRef = ref state} = props;
let {listBoxProps} = useListBox(props state listBoxRef);
return (
<ul
...listBoxProps
ref=listBoxRef
style={
margin: 0
padding: 0
listStyle: 'none'
maxHeight: '150px'
overflow: 'auto'
}>
[...statecollection]map((item) => (
<Option key=itemkey item=item state=state />
))
</ul>
);
}
function Option({item state}) {
let ref = ReactuseRef();
let {
optionProps
isSelected
isFocused
isDisabled
} = useOption({key: itemkey} state ref);
let backgroundColor;
let color = 'black';
if (isSelected) {
backgroundColor = 'blueviolet';
color = 'white';
} else if (isFocused) {
backgroundColor = 'gray';
} else if (isDisabled) {
backgroundColor = 'transparent';
color = 'gray';
}
return (
<li
...optionProps
ref=ref
style={
background: backgroundColor
color: color
padding: '2px 5px'
outline: 'none'
cursor: 'pointer'
}>
itemrendered
</li>
);
}
import {
useListBox
useOption
} from '@react-aria/listbox';
function ListBox(props) {
let ref = ReactuseRef();
let {
listBoxRef = ref
state
} = props;
let {
listBoxProps
} = useListBox(
props
state
listBoxRef
);
return (
<ul
...listBoxProps
ref=listBoxRef
style={
margin: 0
padding: 0
listStyle:
'none'
maxHeight:
'150px'
overflow: 'auto'
}>
[
...statecollection
]map((item) => (
<Option
key=itemkey
item=item
state=state
/>
))
</ul>
);
}
function Option({
item
state
}) {
let ref = ReactuseRef();
let {
optionProps
isSelected
isFocused
isDisabled
} = useOption(
{key: itemkey}
state
ref
);
let backgroundColor;
let color = 'black';
if (isSelected) {
backgroundColor =
'blueviolet';
color = 'white';
} else if (isFocused) {
backgroundColor =
'gray';
} else if (
isDisabled
) {
backgroundColor =
'transparent';
color = 'gray';
}
return (
<li
...optionProps
ref=ref
style={
background: backgroundColor
color: color
padding:
'2px 5px'
outline: 'none'
cursor: 'pointer'
}>
itemrendered
</li>
);
}
Usage#
The following examples show how to use the SearchAutocomplete component created in the above example.
Uncontrolled#
The following example shows how you would create an uncontrolled SearchAutocomplete. The input value, selected option, and open state is completely uncontrolled.
<SearchAutocomplete label="Search Animals">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete label="Search Animals">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete label="Search Animals">
<Item key="red panda">
Red Panda
</Item>
<Item key="cat">
Cat
</Item>
<Item key="dog">
Dog
</Item>
<Item key="aardvark">
Aardvark
</Item>
<Item key="kangaroo">
Kangaroo
</Item>
<Item key="snake">
Snake
</Item>
</SearchAutocomplete>Dynamic collections#
SearchAutocomplete follows the Collection Components API, accepting both static and dynamic collections. The examples above show static collections, which can be used when the full list of options is known ahead of time. Dynamic collections, as shown below, can be used when the options come from an external data source such as an API call, or update over time.
As seen below, an iterable list of options is passed to the SearchAutocomplete using the defaultItems prop. The input's value is passed to the
onSubmit handler, along with a key if the event was triggered by selecting an item from the listbox.
function Example() {
let options = [
{id: 1 name: 'Aerospace'}
{id: 2 name: 'Mechanical'}
{id: 3 name: 'Civil'}
{id: 4 name: 'Biomedical'}
{id: 5 name: 'Nuclear'}
{id: 6 name: 'Industrial'}
{id: 7 name: 'Chemical'}
{id: 8 name: 'Agricultural'}
{id: 9 name: 'Electrical'}
];
let [major setMajor] = ReactuseState();
let onSubmit = (value key) => {
if (value) {
setMajor(value);
} else if (key) {
setMajor(optionsfind((o) => oid === key)name);
}
};
return (
<>
<SearchAutocomplete
label="Search engineering majors"
defaultItems=options
onSubmit=onSubmit>
(item) => <Item>itemname</Item>
</SearchAutocomplete>
<p>Results for: major</p>
</>
);
}
function Example() {
let options = [
{id: 1 name: 'Aerospace'}
{id: 2 name: 'Mechanical'}
{id: 3 name: 'Civil'}
{id: 4 name: 'Biomedical'}
{id: 5 name: 'Nuclear'}
{id: 6 name: 'Industrial'}
{id: 7 name: 'Chemical'}
{id: 8 name: 'Agricultural'}
{id: 9 name: 'Electrical'}
];
let [major setMajor] = ReactuseState();
let onSubmit = (value key) => {
if (value) {
setMajor(value);
} else if (key) {
setMajor(optionsfind((o) => oid === key)name);
}
};
return (
<>
<SearchAutocomplete
label="Search engineering majors"
defaultItems=options
onSubmit=onSubmit>
(item) => <Item>itemname</Item>
</SearchAutocomplete>
<p>Results for: major</p>
</>
);
}
function Example() {
let options = [
{
id: 1
name: 'Aerospace'
}
{
id: 2
name: 'Mechanical'
}
{
id: 3
name: 'Civil'
}
{
id: 4
name: 'Biomedical'
}
{
id: 5
name: 'Nuclear'
}
{
id: 6
name: 'Industrial'
}
{
id: 7
name: 'Chemical'
}
{
id: 8
name:
'Agricultural'
}
{
id: 9
name: 'Electrical'
}
];
let [
major
setMajor
] = ReactuseState();
let onSubmit = (
value
key
) => {
if (value) {
setMajor(value);
} else if (key) {
setMajor(
optionsfind(
(o) =>
oid === key
)name
);
}
};
return (
<>
<SearchAutocomplete
label="Search engineering majors"
defaultItems=
options
onSubmit=
onSubmit
>
(item) => (
<Item>
itemname
</Item>
)
</SearchAutocomplete>
<p>
Results for:' '
major
</p>
</>
);
}
Custom filtering#
By default, useComboBoxState uses the filter function passed to the defaultFilter prop (in the above example, a
"contains" function from useFilter). The filter function can be overridden by users of the SearchAutocomplete component by
using the items prop to control the filtered list. When items is provided rather than defaultItems, useComboBoxState
does no filtering of its own.
The following example makes the inputValue controlled, and updates the filtered list that is passed to the items
prop when the input changes value.
function Example() {
let options = [
{id: 1 email: 'fake@email.com'}
{id: 2 email: 'anotherfake@email.com'}
{id: 3 email: 'bob@email.com'}
{id: 4 email: 'joe@email.com'}
{id: 5 email: 'yourEmail@email.com'}
{id: 6 email: 'valid@email.com'}
{id: 7 email: 'spam@email.com'}
{id: 8 email: 'newsletter@email.com'}
{id: 9 email: 'subscribe@email.com'}
];
let {startsWith} = useFilter({sensitivity: 'base'});
let [filterValue setFilterValue] = ReactuseState('');
let filteredItems = ReactuseMemo(
() => optionsfilter((item) => startsWith(itememail filterValue))
[options filterValue]
);
return (
<SearchAutocomplete
label="To:"
items=filteredItems
inputValue=filterValue
onInputChange=setFilterValue
placeholder="Enter recipient email">
(item) => <Item>itememail</Item>
</SearchAutocomplete>
);
}
function Example() {
let options = [
{id: 1 email: 'fake@email.com'}
{id: 2 email: 'anotherfake@email.com'}
{id: 3 email: 'bob@email.com'}
{id: 4 email: 'joe@email.com'}
{id: 5 email: 'yourEmail@email.com'}
{id: 6 email: 'valid@email.com'}
{id: 7 email: 'spam@email.com'}
{id: 8 email: 'newsletter@email.com'}
{id: 9 email: 'subscribe@email.com'}
];
let {startsWith} = useFilter({sensitivity: 'base'});
let [filterValue setFilterValue] = ReactuseState('');
let filteredItems = ReactuseMemo(
() =>
optionsfilter((item) =>
startsWith(itememail filterValue)
)
[options filterValue]
);
return (
<SearchAutocomplete
label="To:"
items=filteredItems
inputValue=filterValue
onInputChange=setFilterValue
placeholder="Enter recipient email">
(item) => <Item>itememail</Item>
</SearchAutocomplete>
);
}
function Example() {
let options = [
{
id: 1
email:
'fake@email.com'
}
{
id: 2
email:
'anotherfake@email.com'
}
{
id: 3
email:
'bob@email.com'
}
{
id: 4
email:
'joe@email.com'
}
{
id: 5
email:
'yourEmail@email.com'
}
{
id: 6
email:
'valid@email.com'
}
{
id: 7
email:
'spam@email.com'
}
{
id: 8
email:
'newsletter@email.com'
}
{
id: 9
email:
'subscribe@email.com'
}
];
let {
startsWith
} = useFilter({
sensitivity: 'base'
});
let [
filterValue
setFilterValue
] = ReactuseState('');
let filteredItems = ReactuseMemo(
() =>
optionsfilter(
(item) =>
startsWith(
itememail
filterValue
)
)
[
options
filterValue
]
);
return (
<SearchAutocomplete
label="To:"
items=
filteredItems
inputValue=
filterValue
onInputChange=
setFilterValue
placeholder="Enter recipient email">
(item) => (
<Item>
itememail
</Item>
)
</SearchAutocomplete>
);
}
Fully controlled#
The following example shows how you would create a controlled SearchAutocomplete, by controlling the input value (inputValue)
and the autocomplete options (items). By passing in inputValue and items to the SearchAutocomplete you can control
exactly what your SearchAutocomplete should display. For example, note that the item filtering for the controlled SearchAutocomplete below now follows a "starts with"
filter strategy, accomplished by controlling the exact set of items available to the SearchAutocomplete whenever the input value updates.
function ControlledSearchAutocomplete() {
let optionList = [
{name: 'Red Panda' id: '1'}
{name: 'Cat' id: '2'}
{name: 'Dog' id: '3'}
{name: 'Aardvark' id: '4'}
{name: 'Kangaroo' id: '5'}
{name: 'Snake' id: '6'}
];
// Store SearchAutocomplete input value, selected option, open state, and items
// in a state tracker
let [fieldState setFieldState] = ReactuseState({
inputValue: ''
items: optionList
});
// Implement custom filtering logic and control what items are
// available to the SearchAutocomplete.
let {startsWith} = useFilter({sensitivity: 'base'});
// Specify how each of the SearchAutocomplete values should change when an
// option is selected from the list box
let onSubmit = (value key) => {
setFieldState((prevState) => {
let selectedItem = prevStateitemsfind((option) => optionid === key);
return {
inputValue: selectedItem?name ?? ''
items: optionListfilter((item) =>
startsWith(itemname selectedItem?name ?? '')
)
};
});
};
// Specify how each of the SearchAutocomplete values should change when the input
// field is altered by the user
let onInputChange = (value) => {
setFieldState((prevState) => ({
inputValue: value
items: optionListfilter((item) => startsWith(itemname value))
}));
};
// Show entire list if user opens the menu manually
let onOpenChange = (isOpen menuTrigger) => {
if (menuTrigger === 'manual' && isOpen) {
setFieldState((prevState) => ({
inputValue: prevStateinputValue
items: optionList
}));
}
};
// Pass each controlled prop to useSearchAutocomplete along with their
// change handlers
return (
<SearchAutocomplete
label="Search Animals"
items=fieldStateitems
inputValue=fieldStateinputValue
onOpenChange=onOpenChange
onSubmit=onSubmit
onInputChange=onInputChange>
(item) => <Item>itemname</Item>
</SearchAutocomplete>
);
}
<ControlledSearchAutocomplete />function ControlledSearchAutocomplete() {
let optionList = [
{name: 'Red Panda' id: '1'}
{name: 'Cat' id: '2'}
{name: 'Dog' id: '3'}
{name: 'Aardvark' id: '4'}
{name: 'Kangaroo' id: '5'}
{name: 'Snake' id: '6'}
];
// Store SearchAutocomplete input value, selected option, open state, and items
// in a state tracker
let [fieldState setFieldState] = ReactuseState({
inputValue: ''
items: optionList
});
// Implement custom filtering logic and control what items are
// available to the SearchAutocomplete.
let {startsWith} = useFilter({sensitivity: 'base'});
// Specify how each of the SearchAutocomplete values should change when an
// option is selected from the list box
let onSubmit = (value key) => {
setFieldState((prevState) => {
let selectedItem = prevStateitemsfind(
(option) => optionid === key
);
return {
inputValue: selectedItem?name ?? ''
items: optionListfilter((item) =>
startsWith(itemname selectedItem?name ?? '')
)
};
});
};
// Specify how each of the SearchAutocomplete values should change when the input
// field is altered by the user
let onInputChange = (value) => {
setFieldState((prevState) => ({
inputValue: value
items: optionListfilter((item) =>
startsWith(itemname value)
)
}));
};
// Show entire list if user opens the menu manually
let onOpenChange = (isOpen menuTrigger) => {
if (menuTrigger === 'manual' && isOpen) {
setFieldState((prevState) => ({
inputValue: prevStateinputValue
items: optionList
}));
}
};
// Pass each controlled prop to useSearchAutocomplete along with their
// change handlers
return (
<SearchAutocomplete
label="Search Animals"
items=fieldStateitems
inputValue=fieldStateinputValue
onOpenChange=onOpenChange
onSubmit=onSubmit
onInputChange=onInputChange>
(item) => <Item>itemname</Item>
</SearchAutocomplete>
);
}
<ControlledSearchAutocomplete />function ControlledSearchAutocomplete() {
let optionList = [
{
name: 'Red Panda'
id: '1'
}
{
name: 'Cat'
id: '2'
}
{
name: 'Dog'
id: '3'
}
{
name: 'Aardvark'
id: '4'
}
{
name: 'Kangaroo'
id: '5'
}
{
name: 'Snake'
id: '6'
}
];
// Store SearchAutocomplete input value, selected option, open state, and items
// in a state tracker
let [
fieldState
setFieldState
] = ReactuseState({
inputValue: ''
items: optionList
});
// Implement custom filtering logic and control what items are
// available to the SearchAutocomplete.
let {
startsWith
} = useFilter({
sensitivity: 'base'
});
// Specify how each of the SearchAutocomplete values should change when an
// option is selected from the list box
let onSubmit = (
value
key
) => {
setFieldState(
(prevState) => {
let selectedItem = prevStateitemsfind(
(option) =>
optionid ===
key
);
return {
inputValue:
selectedItem?name ??
''
items: optionListfilter(
(item) =>
startsWith(
itemname
selectedItem?name ??
''
)
)
};
}
);
};
// Specify how each of the SearchAutocomplete values should change when the input
// field is altered by the user
let onInputChange = (
value
) => {
setFieldState(
(prevState) => ({
inputValue: value
items: optionListfilter(
(item) =>
startsWith(
itemname
value
)
)
})
);
};
// Show entire list if user opens the menu manually
let onOpenChange = (
isOpen
menuTrigger
) => {
if (
menuTrigger ===
'manual' &&
isOpen
) {
setFieldState(
(prevState) => ({
inputValue:
prevStateinputValue
items: optionList
})
);
}
};
// Pass each controlled prop to useSearchAutocomplete along with their
// change handlers
return (
<SearchAutocomplete
label="Search Animals"
items=
fieldStateitems
inputValue=
fieldStateinputValue
onOpenChange=
onOpenChange
onSubmit=onSubmit
onInputChange=
onInputChange
>
(item) => (
<Item>
itemname
</Item>
)
</SearchAutocomplete>
);
}
<ControlledSearchAutocomplete />Menu trigger behavior#
useComboBoxState supports three different menuTrigger prop values:
input(default): SearchAutocomplete menu opens when the user edits the input text.focus: SearchAutocomplete menu opens when the user focuses the SearchAutocomplete input.manual: SearchAutocomplete menu only opens when the user presses the trigger button or uses the arrow keys.
The example below has menuTrigger set to focus.
<SearchAutocomplete label="Search Animals" menuTrigger="focus">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete
label="Search Animals"
menuTrigger="focus">
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete
label="Search Animals"
menuTrigger="focus">
<Item key="red panda">
Red Panda
</Item>
<Item key="cat">
Cat
</Item>
<Item key="dog">
Dog
</Item>
<Item key="aardvark">
Aardvark
</Item>
<Item key="kangaroo">
Kangaroo
</Item>
<Item key="snake">
Snake
</Item>
</SearchAutocomplete>Disabled options#
You can disable specific options by providing an array of keys to useComboBoxState
via the disabledKeys prop. This will prevent options with matching keys from being pressable and
receiving keyboard focus as shown in the example below. Note that you are responsible for the styling of disabled options.
<SearchAutocomplete label="Search Animals" disabledKeys=['cat' 'kangaroo']>
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete
label="Search Animals"
disabledKeys=['cat' 'kangaroo']>
<Item key="red panda">Red Panda</Item>
<Item key="cat">Cat</Item>
<Item key="dog">Dog</Item>
<Item key="aardvark">Aardvark</Item>
<Item key="kangaroo">Kangaroo</Item>
<Item key="snake">Snake</Item>
</SearchAutocomplete><SearchAutocomplete
label="Search Animals"
disabledKeys=[
'cat'
'kangaroo'
]>
<Item key="red panda">
Red Panda
</Item>
<Item key="cat">
Cat
</Item>
<Item key="dog">
Dog
</Item>
<Item key="aardvark">
Aardvark
</Item>
<Item key="kangaroo">
Kangaroo
</Item>
<Item key="snake">
Snake
</Item>
</SearchAutocomplete>Asynchronous loading#
This example uses the useAsyncList hook to handle asynchronous loading and filtering of data from a server. You may additionally want to display a spinner to indicate the loading state to the user, or support features like infinite scroll to load more data.
import {useAsyncList} from '@react-stately/data';
function AsyncLoadingExample() {
let list = useAsyncList({
async load({signal filterText}) {
let res = await fetch(
`https://swapi.dev/api/people/?search=`
{signal}
);
let json = await resjson();
return {
items: jsonresults
};
}
});
return (
<SearchAutocomplete
label="Search Star Wars Characters"
items=listitems
inputValue=listfilterText
onInputChange=listsetFilterText>
(item) => <Item key=itemname>itemname</Item>
</SearchAutocomplete>
);
}
import {useAsyncList} from '@react-stately/data';
function AsyncLoadingExample() {
let list = useAsyncList({
async load({signal filterText}) {
let res = await fetch(
`https://swapi.dev/api/people/?search=`
{signal}
);
let json = await resjson();
return {
items: jsonresults
};
}
});
return (
<SearchAutocomplete
label="Search Star Wars Characters"
items=listitems
inputValue=listfilterText
onInputChange=listsetFilterText>
(item) => <Item key=itemname>itemname</Item>
</SearchAutocomplete>
);
}
import {useAsyncList} from '@react-stately/data';
function AsyncLoadingExample() {
let list = useAsyncList(
{
async load({
signal
filterText
}) {
let res = await fetch(
`https://swapi.dev/api/people/?search=`
{signal}
);
let json = await resjson();
return {
items:
jsonresults
};
}
}
);
return (
<SearchAutocomplete
label="Search Star Wars Characters"
items=listitems
inputValue=
listfilterText
onInputChange=
listsetFilterText
>
(item) => (
<Item
key=
itemname
>
itemname
</Item>
)
</SearchAutocomplete>
);
}
Internationalization#
useSearchAutocomplete handles some aspects of internationalization automatically.
For example, the item focus, count, and selection VoiceOver announcements are localized.
You are responsible for localizing all labels and option
content that is passed into the autocomplete.
| Name | Type | Default | Description |
inputRef | RefObject<HTMLInputElement> | — | The ref for the input element. |
popoverRef | RefObject<HTMLDivElement> | — | The ref for the list box popover. |
listBoxRef | RefObject<HTMLElement> | — | The ref for the list box. |
children | CollectionChildren<T> | — | The contents of the collection. |
keyboardDelegate | KeyboardDelegate | — | An optional keyboard delegate implementation, to override the default. |
defaultItems | Iterable<T> | — | The list of SearchAutocomplete items (uncontrolled). |
items | Iterable<T> | — | The list of SearchAutocomplete items (controlled). |
onOpenChange | (
(isOpen: boolean,
, menuTrigger: MenuTriggerAction
)) => void | — | Method that is called when the open state of the menu changes. Returns the new open state and the action that caused the opening of the menu. |
inputValue | string | — | The value of the SearchAutocomplete input (controlled). |
defaultInputValue | string | — | The default value of the SearchAutocomplete input (uncontrolled). |
onInputChange | (
(value: string
)) => void | — | Handler that is called when the SearchAutocomplete input value changes. |
menuTrigger | MenuTriggerAction | 'input' | The interaction required to display the SearchAutocomplete menu. |
onSubmit | (
(value: string,
, key: Key
| | null
)) => void | — | |
disabledKeys | Iterable<Key> | — | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
onClear | () => void | — | Handler that is called when the clear button is pressed. |
isDisabled | boolean | — | Whether the input is disabled. |
isReadOnly | boolean | — | Whether the input can be selected but not changed by the user. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | — | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
description | ReactNode | — | A description for the field. Provides a hint such as specific requirements for what to choose. |
errorMessage | ReactNode | — | An error message for the field. |
autoFocus | boolean | — | Whether the element should receive focus on render. |
onFocus | (
(e: FocusEvent
)) => void | — | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | — | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | — | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is released. |
placeholder | string | — | Temporary text that occupies the text input when it is empty. |
value | string | — | The current value (controlled). |
defaultValue | string | — | The default value (uncontrolled). |
onChange | (
(value: T
)) => void | — | Handler that is called when the value changes. |
label | ReactNode | — | The content to display as the label. |
'focus'
| 'input'
| 'manual'Properties
| Name | Type | Description |
inputValue | string | The current value of the combo box input. |
isFocused | boolean | Whether the select is currently focused. |
selectedKey | Key | The key for the currently selected item. |
selectedItem | Node<T> | The value of the currently selected item. |
collection | Collection<Node<T>> | A collection of items in the list. |
disabledKeys | Set<Key> | A set of items that are disabled. |
selectionManager | SelectionManager | A selection manager to read and update multiple selection state. |
focusStrategy | FocusStrategy | Controls which item will be auto focused when the menu opens. |
isOpen | boolean | Whether the overlay is currently open. |
Methods
| Method | Description |
setInputValue(
(value: string
)): void | Sets the value of the combo box input. |
commit(): void | Selects the currently focused item and updates the input value. |
open(
(focusStrategy: FocusStrategy
| | null,
, trigger: MenuTriggerAction
)): void | Opens the menu. |
toggle(
(focusStrategy: FocusStrategy
| | null,
, trigger: MenuTriggerAction
)): void | Toggles the menu. |
revert(): void | Resets the input value to the previously selected item's text if any and closes the menu. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
close(): void | Closes the overlay. |
An interface for reading and updating multiple selection state.
Properties
| Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectionBehavior | SelectionBehavior | The selection behavior for the collection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
childFocusStrategy | FocusStrategy | Whether the first or last child of the focused key should receive focus. |
selectedKeys | Set<Key> | The currently selected keys in the collection. |
rawSelection | Selection | The raw selection value for the collection. Either 'all' for select all, or a set of keys. |
isEmpty | boolean | Whether the selection is empty. |
isSelectAll | boolean | Whether all items in the collection are selected. |
firstSelectedKey | Key | null | |
lastSelectedKey | Key | null |
Methods
| Method | Description |
setSelectionBehavior(
(selectionBehavior: SelectionBehavior
)): void | Sets the selection behavior for the collection. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key,
, childFocusStrategy: FocusStrategy
)): void | Sets the focused key. |
isSelected(
(key: Key
)): void | Returns whether a key is selected. |
extendSelection(
(toKey: Key
)): void | Extends the selection to the given key. |
toggleSelection(
(key: Key
)): void | Toggles whether the given key is selected. |
replaceSelection(
(key: Key
)): void | Replaces the selection with only the given key. |
setSelectedKeys(
(keys: Iterable<Key>
)): void | Replaces the selection with the given keys. |
selectAll(): void | Selects all items in the collection. |
clearSelection(): void | Removes all keys from the selection. |
toggleSelectAll(): void | Toggles between select all and an empty selection. |
select(
(key: Key,
, e: PressEvent
| LongPressEvent
| PointerEvent
)): void | |
isSelectionEqual(
(selection: Set<Key>
)): void | Returns whether the current selection is equal to the given selection. |
canSelectItem(
(key: Key
)): void |
Properties
| Name | Type | Description |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
selectionBehavior | SelectionBehavior | The selection behavior for the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | Selection | The currently selected keys in the collection. |
disabledKeys | Set<Key> | The currently disabled keys in the collection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
childFocusStrategy | FocusStrategy | Whether the first or last child of the focused key should receive focus. |
Methods
| Method | Description |
setSelectionBehavior(
(selectionBehavior: SelectionBehavior
)): void | Sets the selection behavior for the collection. |
setSelectedKeys(
(keys: Selection
| | (
(v: Selection
)) => Selection
)): void | Sets the selected keys in the collection. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key,
, child: FocusStrategy
)): void | Sets the focused key, and optionally, whether the first or last child of that key should receive focus. |
| Name | Type | Description |
labelProps | HTMLAttributes<HTMLElement> | Props for the label element. |
inputProps | InputHTMLAttributes<HTMLInputElement> | Props for the search input element. |
listBoxProps | AriaListBoxOptions<T> | Props for the list box, to be passed to useListBox. |
clearButtonProps | AriaButtonProps | Props for the search input's clear button. |
| Name | Type | Description |
isVirtualized | boolean | Whether the listbox uses virtual scrolling. |
keyboardDelegate | KeyboardDelegate | An optional keyboard delegate implementation for type to select, to override the default. |
shouldUseVirtualFocus | boolean | Whether the listbox items should use virtual focus instead of being focused directly. |
shouldSelectOnPressUp | boolean | Whether selection should occur on press up instead of press down. |
shouldFocusOnHover | boolean | Whether options should be focused when the user hovers over them. |
label | ReactNode | An optional visual label for the listbox. |
autoFocus | boolean | FocusStrategy | Whether to auto focus the listbox or an option. |
shouldFocusWrap | boolean | Whether focus should wrap around when the end/start is reached. |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
selectionMode | SelectionMode | The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys: Selection
)) => any | Handler that is called when the selection changes. |
onFocus | (
(e: FocusEvent
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
id | string | The element's unique identifier. See MDN. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Provides state management for a combo box component. Handles building a collection of items from props and manages the option selection state of the combo box. In addition, it tracks the input value, focus state, and other properties of the combo box.
useComboBoxState<T>(
(props: ComboBoxStateProps<T>
)): ComboBoxState<T>| Name | Type | Default | Description |
children | CollectionChildren<T> | — | The contents of the collection. |
defaultFilter | FilterFn | — | The filter function used to determine if a option should be included in the combo box list. |
allowsEmptyCollection | boolean | — | Whether the combo box allows the menu to be open when the collection is empty. |
shouldCloseOnBlur | boolean | — | Whether the combo box menu should close on blur. |
defaultItems | Iterable<T> | — | The list of ComboBox items (uncontrolled). |
items | Iterable<T> | — | The list of ComboBox items (controlled). |
onOpenChange | (
(isOpen: boolean,
, menuTrigger: MenuTriggerAction
)) => void | — | Method that is called when the open state of the menu changes. Returns the new open state and the action that caused the opening of the menu. |
inputValue | string | — | The value of the ComboBox input (controlled). |
defaultInputValue | string | — | The default value of the ComboBox input (uncontrolled). |
onInputChange | (
(value: string
)) => void | — | Handler that is called when the ComboBox input value changes. |
allowsCustomValue | boolean | — | Whether the ComboBox allows a non-item matching input value to be set. |
menuTrigger | MenuTriggerAction | 'input' | The interaction required to display the ComboBox menu. |
shouldFocusWrap | boolean | — | Whether keyboard navigation is circular. |
disabledKeys | Iterable<Key> | — | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
disallowEmptySelection | boolean | — | Whether the collection allows empty selection. |
selectedKey | Key | — | The currently selected key in the collection (controlled). |
defaultSelectedKey | Key | — | The initial selected key in the collection (uncontrolled). |
onSelectionChange | (
(key: Key
)) => any | — | Handler that is called when the selection changes. |
isDisabled | boolean | — | Whether the input is disabled. |
isReadOnly | boolean | — | Whether the input can be selected but not changed by the user. |
placeholder | string | — | Temporary text that occupies the text input when it is empty. |
id | string | — | The element's unique identifier. See MDN. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | — | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
autoFocus | boolean | — | Whether the element should receive focus on render. |
onFocus | (
(e: FocusEvent
)) => void | — | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | — | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | — | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is released. |
label | ReactNode | — | The content to display as the label. |
description | ReactNode | — | A description for the field. Provides a hint such as specific requirements for what to choose. |
errorMessage | ReactNode | — | An error message for the field. |
(
(textValue: string,
, inputValue: string
)) => booleanProvides localized string search functionality that is useful for filtering or matching items in a list. Options can be provided to adjust the sensitivity to case, diacritics, and other parameters.
useFilter(
(options: Intl.CollatorOptions
)): Filter| Method | Description |
startsWith(
(string: string,
, substring: string
)): boolean | Returns whether a string starts with a given substring. |
endsWith(
(string: string,
, substring: string
)): boolean | Returns whether a string ends with a given substring. |
contains(
(string: string,
, substring: string
)): boolean | Returns whether a string contains a given substring. |
Handles positioning overlays like popovers and menus relative to a trigger element, and updating the position when the window resizes.
useOverlayPosition(
(props: AriaPositionProps
)): PositionAria| Name | Type | Default | Description |
targetRef | RefObject<HTMLElement> | — | The ref for the element which the overlay positions itself with respect to. |
overlayRef | RefObject<HTMLElement> | — | The ref for the overlay element. |
boundaryElement | HTMLElement | document.body | Element that that serves as the positioning boundary. |
scrollRef | RefObject<HTMLElement> | overlayRef | A ref for the scrollable region within the overlay. |
shouldUpdatePosition | boolean | true | Whether the overlay should update its position automatically. |
onClose | () => void | — | Handler that is called when the overlay should close. |
placement | Placement | 'bottom' | The placement of the element with respect to its anchor element. |
containerPadding | number | 12 | The placement padding that should be applied between the element and its surrounding container. |
offset | number | 0 | The additional offset applied along the main axis between the element and its anchor element. |
crossOffset | number | 0 | The additional offset applied along the cross axis between the element and its anchor element. |
shouldFlip | boolean | true | Whether the element should flip its orientation (e.g. top to bottom or left to right) when there is insufficient room for it to render completely. |
isOpen | boolean | — | Whether the element is rendered. |
'bottom'
| 'bottom left'
| 'bottom right'
| 'bottom start'
| 'bottom end'
| 'top'
| 'top left'
| 'top right'
| 'top start'
| 'top end'
| 'left'
| 'left top'
| 'left bottom'
| 'start'
| 'start top'
| 'start bottom'
| 'right'
| 'right top'
| 'right bottom'
| 'end'
| 'end top'
| 'end bottom'Properties
| Name | Type | Description |
overlayProps | HTMLAttributes<Element> | Props for the overlay container element. |
arrowProps | HTMLAttributes<Element> | Props for the overlay tip arrow if any. |
placement | PlacementAxis | Placement of the overlay with respect to the overlay trigger. |
Methods
| Method | Description |
updatePosition(): void | Updates the position of the overlay. |
Axis | 'center''top'
| 'bottom'
| 'left'
| 'right'