# Filter (`<z-filter>`)

> Filters and sorts a list of items from declarative controls, with animated show/hide, grouped filters, and empty-state handling.

- **Kind:** controller
- **Category:** data
- **Inherits:** `Controller`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `target` | `string` | `''` | — | Selector of the container(s) whose children are filtered. Defaults to owned `[data-filter-target]` descendants; a selector with no internal match falls back to the document, so external lists work too. |
| `sel-active` | `string` | `''` | — | Selector seeding the initial state from matching controls (e.g. `.z-active`); `false` disables seeding. Without it, controls carrying the active class seed the state. |
| `animation` | `FilterAnimation` | `'slide'` | — | Transition style when items show/hide: `slide` (FLIP move/resize), `fade`, `delayed-fade` (staggered fade), or `none`/`false`/empty for instant updates. |
| `no-animation` | `boolean` | `false` | — | Disables all transition animation regardless of `animation`. |
| `duration` | `number` | `250` | — | Transition duration in milliseconds (default 250). |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `active`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `active` | `z-active` | ✓ | — | Class toggled on controls whose filter/sort configuration matches the current state (default `z-active`). |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-filter:after-filter` | — | `{ state: FilterState; matchingCount: number; totalCount: number }` | Fires after the state is applied with `state`, `matchingCount`, and `totalCount`. |
| `z-filter:before-filter` | ✓ | `{ state: { filter: { [x: string]: string; }; sort: string[]; }; previousState: FilterState }` | Cancelable; fires before a state change with `state` and `previousState` in detail. `preventDefault()` keeps the current state. |
| `z-filter:change` | — | `{ state: FilterState; matchingCount: number; totalCount: number }` | Fires after every applied state change, with the same detail as `after-filter`. |
| `z-filter:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `apply()` | `controlEl: HTMLElement`, `animate?: boolean = true` | `Promise<void>` | Applies a control element's configuration (as if it were clicked), optionally without animation. |
| `getState()` | — | `FilterState` | Returns a copy of the current state: `filter` (group → selector map) and `sort` (`[key, order]` or `[]`). |
| `refresh()` | — | `void` | Re-indexes items, re-syncs control classes/ARIA, and re-applies the current state without events. |
| `reset()` | `animate?: boolean = true` | `Promise<void>` | Clears all filters and sorting (back to the original order). |
| `setState()` | `state: Partial<FilterState>`, `animate?: boolean = true` | `Promise<void>` | Sets the state directly, firing `before-filter`/`after-filter`/`change`; resolves when transitions finish. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-filter` | — | `descendant` | `config` | On a control: the CSS selector an item must match to stay visible. Empty value shows all items. |
| `data-filter-control` | — | `descendant` | `config` | Marks a clickable control. The value is a filter selector, or `filter: …; group: …; sort: …; order: …` options; separate `data-filter`/`data-group`/`data-sort`/`data-order` attributes override the parsed keys. |
| `data-filter-empty` | — | `descendant` | `config` | Marks an empty-state element; shown only when no items match. |
| `data-filter-target` | — | `descendant` | `config` | Marks a container whose children are filtered when `target` is unset. |
| `data-fn` | `template` | `descendant` | `config` | Marks a deferred `<template>` child whose content is mounted into the container on demand. |
| `data-group` | — | `descendant` | `config` | On a control: names an independent filter group, so several groups can be active at once (AND-combined). |
| `data-order` | `asc`, `desc` | `descendant` | `trigger` | Sort direction on a sort control: `asc` (default) or `desc`. |
| `data-sort` | — | `descendant` | `config` | On a control: the attribute to sort by (`data-<key>` is also checked), or `:text` for text content. Empty value clears sorting. Numeric values sort numerically. |

## Keyboard

| Keys | Action | When |
| --- | --- | --- |
| `Enter` / `Space` | Activates the focused filter control (native buttons/links keep their default behavior). | — |

## Accessibility

> Controls get `aria-pressed` (or `aria-selected` for `role="tab"`) reflecting whether their configuration is active, and are kept button-accessible.

> Hidden items receive both `hidden` and `display: none` so they leave the layout and accessibility tree.

## Notes

- Sorting reorders the DOM; clearing the sort restores the original item order.
- A MutationObserver re-applies the state when items are added or removed, including in external targets.
- Items marked `data-filter-empty` and `<template>` children are never treated as filterable items.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)

## Related

- [Sortable](https://next.0build.dev/docs/components/sortable.md)
