> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Facet drop-down menus

> Learn how to display your facets as dropdowns with React InstantSearch.

<Note>
  This is the **React InstantSearch v7** documentation.
  If you're upgrading from v6, see the [upgrade guide](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-v6-to-react-instantsearch-v7).
  If you were using React InstantSearch Hooks,
  this v7 documentation applies—just check for [necessary changes](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-hooks-to-react-instantsearch-v7).
  To continue using v6, you can find the [archived documentation](https://algolia.com/old-docs/deprecated/instantsearch/react/v6/api-reference/instantsearch/).
</Note>

All Search and Discovery user interfaces display, in addition to a search box, a series of filters that allow users to narrow down their search.
These onscreen filters are called [facets](/doc/guides/managing-results/refine-results/faceting).
A typical facet is an attribute like "brand" or "price", and facet values are the individual brands and prices.
By clicking on a facet value, users can include and exclude whole categories of products.
For example, by selecting "Blue" in the "Color" facet filter, a user can exclude every product except blue ones.

For a long time, ecommerce websites have been displaying facets on the left side of the screen, providing direct access.
But this placement reduces the space available to display products.
With the rise of mobile-first design and touchscreen, online businesses have started to display facets at the top of their product listing.
That's the case of [Lacoste](https://www.lacoste.com/gb/lacoste-polo.html#query=Polo) which [increased its sales by +150% sales from Algolia's search](https://resources.algolia.com/customer-stories/casestudy-lacoste-en).

<img src="https://mintcdn.com/algolia/0u_XqgAn7MC5F_qG/images/guides/facet-dropdown/lacoste-dropdown.png?fit=max&auto=format&n=0u_XqgAn7MC5F_qG&q=85&s=5e1459ce1a701d315084b77e4806186f" alt="Lacoste search interface with filters in drop-down menus" width="1280" height="820" data-path="images/guides/facet-dropdown/lacoste-dropdown.png" />

Facet drop-down menus offer two benefits:

* They increase facet visibility and accessibility, encouraging more usage.
* They simplify the screen, leaving more room for products and creating more on-screen breathing space, an important UX design choice.

This guide shows how to turn a facet filter, commonly represented by a [`RefinementList`](/doc/api-reference/widgets/refinement-list/react) or a [`HierarchicalMenu`](/doc/api-reference/widgets/hierarchical-menu/react) widget, into a drop-down menu layout.

<Columns>
  <Card title="Open CodeSandbox" icon="codesandbox" href="https://codesandbox.io/s/github/algolia/doc-code-samples/tree/master/react-instantsearch/facet-dropdown">
    Run and edit the Facet drop-down menus example in CodeSandbox.
  </Card>

  <Card title="Explore source code" icon="github" href="https://github.com/algolia/doc-code-samples/tree/master/react-instantsearch/facet-dropdown">
    Browse the source for the Facet drop-down menus example on GitHub.
  </Card>
</Columns>

## Widget elements

By design, each facet drop-down menu widget has two elements:

* A button with a label and the number of active refinements.
* A panel (that can be toggled) containing the widget.

Both can be [customized](/doc/guides/building-search-ui/ui-and-ux-patterns/facet-dropdown/react#customize-the-ui).

<Note>
  The facet drop-down menu widget can only contain one widget and is compatible with [`DynamicWidgets`](/doc/api-reference/widgets/dynamic-facets/react)
</Note>

<img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-explained.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=809f7dfee8ab966b34d455ee02b470d3" alt="Screenshot of a 'Brand' facet menu with filter options like 'Insignia™', 'Samsung', and 'HP' and their counts." width="417" height="287" data-path="images/guides/facet-dropdown/rish/facet-dropdown-explained.jpg" />

### Quickstart

<Steps>
  <Step title="Clone the code sample">
    To get started, clone the [code sample](https://github.com/algolia/doc-code-samples/tree/master/react-instantsearch/facet-dropdown)
    and follow the instructions to install the dependencies, or fork the following [CodeSandbox](https://codesandbox.io/s/github/algolia/doc-code-samples/tree/master/react-instantsearch/facet-dropdown).

    In the project, you see two components:

    * [`Panel.tsx`](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/components/Panel.tsx) used as the drop-down menu that can be toggled. It also displays a header, wraps your provided widget and adds a footer on mobile.
    * [`FacetDropdown.tsx`](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/components/FacetDropdown.tsx) used to get the number of active refinements for the provided widget and close the panel when the search state has changed.

    <Info>
      To use the facet drop-down menu widget in your own project:
      install or duplicate the dependencies for each widget (like the [`hooks` folder](https://github.com/algolia/doc-code-samples/tree/master/react-instantsearch/facet-dropdown/src/hooks) and [`utils.ts` file](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/utils.ts)).
    </Info>
  </Step>

  <Step title="Use the facet drop-down menu widget">
    A basic usage of the facet drop-down menu widget:

    ```jsx React icon=code theme={"system"}
    <FacetDropdown>
      <RefinementList attribute="type" />
    </FacetDropdown>;
    ```
  </Step>
</Steps>

The `FacetDropdown` widget automatically retrieves the attribute of the first provided child widget to display the number of current refinements in the drop-down menu toggle button.

<img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-current-refinements.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=36ccb173ee17f9cd1771042cdfd37e53" alt="Facet drop-down menu showing the number of current refinements" width="253" height="415" data-path="images/guides/facet-dropdown/rish/facet-dropdown-current-refinements.jpg" />

## Props

### `buttonText`

**Type:** `string | function`

You can use the `buttonText` prop to customize the text displayed in the drop-down menu button.
It can be a `string` or a `function`.
By default, it shows the number of active refinement for the provided widget.

Here's an example showing how you can get the current `refinements` to display the current bounds of a [`RangeInput`](/doc/api-reference/widgets/range-input/react) widget.

```jsx React icon=code theme={"system"}
<FacetDropdown
  buttonText={({ refinements }) => {
    const [start, end] = refinements;
    return start || end
      ? `Price (${(start || end).label}${end ? " & " + end.label : ""})`
      : `Price`;
  }}
>
  <RangeInput attribute="price" />
</FacetDropdown>;
```

<img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-range-input.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=f3ae4fb09892c8937fcfda78b4719703" alt="Screenshot of a price range facet with 'Price (≥ 50 and ≤ 100)' button text and input fields for '50' to '100' with a 'Go' button." width="312" height="130" data-path="images/guides/facet-dropdown/rish/facet-dropdown-range-input.jpg" />

### `closeOnChange`

**Type:** `boolean | function`

You can use the `closeOnChange` prop to close the drop-down menu as soon as users selects a facet value using a `boolean` or `function` that returns `true`.
If it's `false`, the drop-down menu isn't closed automatically, allowing users to select more than one facet value.

Here's an example where the drop-down menu is automatically closed only if users are using a device with a width greater than or equal to 375px.

```jsx React icon=code theme={"system"}
const MOBILE_WIDTH = 375;
/* ... */
<FacetDropdown closeOnChange={() => window.innerWidth >= MOBILE_WIDTH}>
  <RefinementList
    attribute="brand"
    searchable={true}
    searchablePlaceholder="Search..."
  />
</FacetDropdown>;
```

<video controls>
  <source src="https://mintcdn.com/algolia/0u_XqgAn7MC5F_qG/images/guides/facet-dropdown/facet-dropdown-close-on-change.webm?fit=max&auto=format&n=0u_XqgAn7MC5F_qG&q=85&s=e3fd8bda1a360d39c7f4134e10298d7d" type="video/webm" data-path="images/guides/facet-dropdown/facet-dropdown-close-on-change.webm" />

  <source src="https://mintcdn.com/algolia/0u_XqgAn7MC5F_qG/images/guides/facet-dropdown/facet-dropdown-close-on-change.mp4?fit=max&auto=format&n=0u_XqgAn7MC5F_qG&q=85&s=6767a54d7bafe410349d177c71af1ab5" type="video/mp4" data-path="images/guides/facet-dropdown/facet-dropdown-close-on-change.mp4" />
</video>

### `cssClasses`

**Type:** `Object`

You can use the `classNames` prop to add CSS classes on multiple elements of the facet drop-down menu widget.

Here's an example that adds custom CSS classes.

```jsx React icon=code theme={"system"}
<FacetDropdown
  classNames={{
    button: "MyButton",
    buttonRefined: "MyButton--refined",
    closeButton: "MyCloseButton",
    mobileTitle: "MyMobileTitle",
  }}
>
  <RefinementList attribute="type" />
</FacetDropdown>;
```

## Customize the UI

The generated markup lets you customize the look and feel to your needs,
to change the default aspect of the drop-down menu with a few lines of CSS.
Here's the default version:

<img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-customize-ui.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=2f5aa66b5aae31046257776847f80098" alt="Screenshot of a 'Type' facet drop-down menu with checkboxes and counts for options like 'Trend cases' and 'Ink cartridges.'" width="243" height="377" data-path="images/guides/facet-dropdown/rish/facet-dropdown-customize-ui.jpg" />

You can customize two aspects you can customize to your needs:

* **Inline facet values**

  The demo's brand drop-down menu widget uses an inline list.
  See the code in the [`App.css`](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/App.css) file.

  <img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-inlined.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=4905fd86c5752cacec113daeb2c11158" alt="An example of a drop-down menu with inline facet values" width="375" height="294" data-path="images/guides/facet-dropdown/rish/facet-dropdown-inlined.jpg" />

```css CSS icon=paintbrush theme={"system"}
  .my-BrandDropdown .ais-RefinementList-list {
    width: 20rem;
    display: flex;
    flex-wrap: wrap;
  }
```

* **Fixed height drop-down menu**

  The demo's category drop-down menu widget is hierarchical and is a fixed height list.
  See the code in the [`App.css`](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/App.css) file.

  <img src="https://mintcdn.com/algolia/AACpGv43fS_1tsyy/images/guides/facet-dropdown/rish/facet-dropdown-fixed-height.jpg?fit=max&auto=format&n=AACpGv43fS_1tsyy&q=85&s=c7ff3c9d60071f5285a4928e865ad84e" alt="An example of facets with a fixed height drop-down menu" width="260" height="285" data-path="images/guides/facet-dropdown/rish/facet-dropdown-fixed-height.jpg" />

```css CSS icon=paintbrush theme={"system"}
  .my-CategoryDropdown .ais-HierarchicalMenu {
    height: 185px;
    overflow: auto;
  }
```

## Mobile support

Mobile devices are already supported by the widget.
Below 375px (this breaking point can be changed, see below):

* The drop-down menu position is fixed and taking the full page size.
* The page scroll is locked when the drop-down menu is open.
* The button text is added as a title at the top of the drop-down menu.
* A button to apply the changes is added at the bottom of the drop-down menu.

<video controls>
  <source src="https://mintcdn.com/algolia/0u_XqgAn7MC5F_qG/images/guides/facet-dropdown/facet-dropdown-mobile.webm?fit=max&auto=format&n=0u_XqgAn7MC5F_qG&q=85&s=60e299e6f45ee48a7a16a1a640d32082" type="video/webm" data-path="images/guides/facet-dropdown/facet-dropdown-mobile.webm" />

  <source src="https://mintcdn.com/algolia/0u_XqgAn7MC5F_qG/images/guides/facet-dropdown/facet-dropdown-mobile.mp4?fit=max&auto=format&n=0u_XqgAn7MC5F_qG&q=85&s=3118ea5a15db9d891d39b557883a14a2" type="video/mp4" data-path="images/guides/facet-dropdown/facet-dropdown-mobile.mp4" />
</video>

To customize the default behavior,
the facet drop-down menu widget uses the following [CSS media query](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/components/FacetDropdown.css#L72-L99):

```css CSS icon=paintbrush theme={"system"}
@media only screen and (max-width: 375px) {
  /* ... */
}
```

It also uses the following logic to [prevent page scrolling](https://github.com/algolia/doc-code-samples/blob/master/react-instantsearch/facet-dropdown/src/components/FacetDropdown.tsx#L99-L101) when the drop-down menu is open:

```ts TypeScript icon=code theme={"system"}
const isMobile = useMediaQuery("(max-width: 375px)");
useLockedBody(isOpened && isMobile);
```

This code sample gets you started by providing a basic look and feel for mobile devices.
Refactor the provided code to match your current design and offer the best experience to your mobile users.
