Skip to main content

Downshift

Introduction​

The Downshift component has been developed in order to provide accessibility and functionality to a combobox or autocomplete input, described by its corresponding ARIA accessibility pattern.

This is a component that controls user interactions and state for you so you can create autocomplete/combobox or select components. It uses a render prop which gives you maximum flexibility with a minimal API because you are responsible for the rendering of everything and you simply apply props to what you're rendering.

This differs from other solutions which render things for their use case and then expose many options to allow for extensibility resulting in a bigger API that is less flexible as well as making the implementation more complicated and harder to contribute to.

Breaking Changes in v8​

Downshift has been affected by breaking changes in v8, so check out the migration page.

Props used in examples​

In the examples below, we use the Downshift component and destructure from its result the getter props and state variables. The props returned by Downshift are used as follows:

Returned propElementComments
getLabelProps<label>Call and destructure its returned object on the label element.
getToggleButtonProps<button>Call and destructure its returned object on the toggle button (if any).
getRootProps<div>Call and destructure its returned object on the input and toggle button parent.
getInputProps<input>Call and destructure its returned object on the input element.
getMenuProps<ul>Call and destructure its returned object on the menu element.
getItemProps<li>Pass the item and its optional index, then spread the result onto the option.
isOpenState value with the open state of the menu. Used below for conditionally showing the items.
highlightedIndexState value with the index of the highlighted menu item. Used below for styling.
selectedItemState value with the item that is selected. Used below for styling.
inputValueState value with the search query. Used below for filtering the items.

For a complete documentation on all the returned props, component props and more information check out the Github Page.

Important​

If you are new to Downshift then maybe you should first check useCombobox which should provide the same functionality but as a hook. Also, if you need just a select dropdown without a text input, then check useSelect. To display selected items as removable tags, pair either hook with useTagGroup.

The first example applies getRootProps to the wrapper around the input and toggle button. Its listbox is a sibling of that wrapper. The second example shows the older structure where Downshift attaches the root props to its only child and the listbox sits inside the combobox.

Usage with getRootProps​

It is possible to achieve the correct HTML structure only if you use getRootProps getter prop from Downshift. You apply getRootProps on the wrapper element that contains the input and optionally the trigger button.

CodeSandbox for usage with getRootProps.

Live Editor
Result
Loading...

Usage without getRootProps​

Using Downshift without the getRootProps will add the combobox role to the child element rendered. This way forces your widget into having all elements (menu, input, button, label) as children of the combobox, which is not compatible with the ARIA combobox HTML structure. It will still work but this structure is not supported by all screen readers. Since this is how the usage was advertised in the past, we are still supporting it, but we strongly suggest to move either to the structure with the getRootProps or even better to use the useCombobox hook.

CodeSandbox for usage without getRootProps.

Live Editor
Result
Loading...

React Native​

The component can be used with React Native as well. The HTML elements and styles are replaced with React Native equivalents, but the Downshift usage is exactly the same as on React web.

Other usage examples​

To see more cool stuff you can build with Downshift, explore the examples repository.