# Filter drawer

Filters in a sheet from the bottom edge on a phone: drag it down, tap outside or press Esc to close.

Live variants: https://design.arcadia.is/components/filter-drawer

```tsx
import {
  FilterDrawer, FilterDrawerSection, FilterDrawerOption,
} from "@arcadia/ui";
```

Open the filters, turn one on, then drag the sheet down to close it.

```tsx
import { useEffect, useState } from "react";
import {
  Button, FilterDrawer, FilterDrawerOption, FilterDrawerSection,
} from "@arcadia/ui";

function Filters({ initiallyOpen = false, selected = ["high"], unavailable = false }: { initiallyOpen?: boolean; selected?: readonly string[]; unavailable?: boolean } = {}) {
  const [open, setOpen] = useState(false);
  const [on, setOn] = useState<ReadonlySet<string>>(() => new Set(selected));
  useEffect(() => { if (initiallyOpen) setOpen(true); }, [initiallyOpen]);
  const toggle = (id: string, next: boolean) => setOn((s) => { const n = new Set(s); if (next) n.add(id); else n.delete(id); return n; });
  return (
    <>
      <Button variant="secondary" icon="filter" aria-haspopup="dialog" onClick={() => setOpen(true)}>{on.size === 0 ? "Filters" : on.size === 1 ? "1 filter" : `${on.size} filters`}</Button>
      <FilterDrawer open={open} onOpenChange={setOpen} onClear={() => setOn(new Set())} clearable={on.size > 0}>
        {FILTER_GROUPS.map((g) => (
          <FilterDrawerSection key={g.label} label={g.label}>
            {g.options.map(([id, label, count]) => (
              <FilterDrawerOption key={id} label={label} end={count} checked={on.has(id)} disabled={unavailable && id === "agent"} onCheckedChange={(next) => toggle(id, next)} />
            ))}
          </FilterDrawerSection>
        ))}
      </FilterDrawer>
    </>
  )
```

## Accessibility

A modal `dialog` named by its title that rises from the bottom edge, for the filters a phone has no room to show in a bar. Drag the handle or the sheet down past a quarter of its height, or flick it, to close it. Above its top it resists. A list inside scrolls first and the sheet follows only from the list's top. Tapping the dimmed page closes it, and under reduced motion it fades instead of sliding.

- Escape: Closes it
- Tab: Moves round the filters and Done, never out of the sheet
- Space: Turns the focused filter on or off

## API reference

### FilterDrawer

- `title` (`string`, default `"Filters"`)
- `onClear` (`() => void`)
- `clearable` (`boolean`, default `true`)
- `doneLabel` (`string`, default `"Done"`)
- `container` (`PortalContainer`): Where a surface's portal mounts. Default: `document.body`. Pass a themed subtree to keep its `data-theme`, or a transformed stage to contain `position: fixed` overlays (previews): a modal set down in one stays in it, shown in place, out of the browser's top layer.
- `open` (`boolean`): Controlled open state.
- `defaultOpen` (`boolean`, default `false`): Initial open state when uncontrolled.
- `onOpenChange` (`(open: boolean, details: OpenChangeDetails) => void`): Called when the popup opens or closes, with why (`details.reason`) and the event.
- `onOpenChangeComplete` (`(open: boolean) => void`): Called once the open or close transition has finished.

### FilterDrawerSection

- `label` (`string`, required)

### FilterDrawerOption

- `label` (`string`, required)
- `checked` (`boolean`, required)
- `onCheckedChange` (`(checked: boolean) => void`, required)
- `end` (`ReactNode`)
- `disabled` (`boolean`)
