FormsFilters
Searchastronomic.dev# Filters
To build your own search filters: a buy or rent switch, a text box, choices and ranges from your fields list, kept in the address bar.
Interactive: inline script.
## Usage
```astro
import Filters from "@astronomic-dev/ui/components/Filters.astro";
```
## Examples
### The shell
The parts in their order, with no recipe: the switch, the text box, price and rent ranges, beds and baths, the home type as option chips, then Clear and the active-filter chips. Pick a few filters and watch the address bar.
```astro
---
import Filters from "@astronomic-dev/ui/components/Filters.astro";
import { defaults, fields, modes, sorts } from "../_data/vega.ts";
---
<Filters label="Filter homes" fields={fields} modes={modes} modesLabel="Buy or rent" sorts={sorts} defaults={defaults} textLabel="Where" textPlaceholder="Street, neighborhood or listing number" />
```
### Dropdowns
The same fields with dropdown: labels above values, the home type in the row as a dropdown of checkboxes.
```astro
---
import Filters from "@astronomic-dev/ui/components/Filters.astro";
import { defaults, fields, modes, sorts } from "../_data/vega.ts";
---
<Filters dropdown label="Filter homes" fields={fields} modes={modes} modesLabel="Buy or rent" sorts={sorts} defaults={defaults} textLabel="Where" textPlaceholder="Street, neighborhood or listing number" />
```
## Props
### Filters
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | None | The search's accessible name, for role="search", such as "Filter homes". |
| `fields` | `ListingField[]` | None | Your fields list (lib/listing.ts). Each field with a kind gets a filter: range (Min and Max selects, or one "at least" select with bound "min"), choice (one select, or option chips with multiple) or toggle (a checkbox). |
| `modes` | `{ value: string; label: string }[]` | None | The modes of the two-part switch, such as Buy and Rent. Without them, no switch. |
| `modesLabel` | `string` | None | The switch's name, read before its options ("Buy or rent"). Required with modes: the kit has no default, since the modes are yours. |
| `sorts` | `{ value: string; label: string }[]` | None | The sorts ResultsHead offers. The filters keep the picked one in the address bar. |
| `defaults` | `{ mode?: string; sort: string }` | None | The mode and sort with nothing chosen. Neither is written in the address bar. |
| `textLabel` | `string` | None | The text box's visible label ("Where"). Without it, no text box. |
| `textPlaceholder` | `string` | None | The text box's placeholder, such as "Street or neighborhood". |
| `pageHref` | `(n: number) => string` | None | The path of each server page, as your list's pager builds them ("/search/page/2/"), so a page is a real file. With pages. |
| `pages` | `number` | None | How many server pages there are. Unset: one page. |
| `clearText` | `string` | `"Clear"` | The button that removes the text and every filter (shown only when something is set). |
| `anyText` | `string` | `FILTERS_TEXTS.anyText` | A select's empty first option: a choice field's and a minimum-only range's ("Any"). |
| `minText` | `string` | `FILTERS_TEXTS.minText` | A range's empty Min option ("No min"). |
| `maxText` | `string` | `FILTERS_TEXTS.maxText` | A range's empty Max option ("No max"). |
| `minLabel` | `string` | None | A range's Min select name for screen readers, after its legend. Default "Min". |
| `maxLabel` | `string` | None | A range's Max select name for screen readers, after its legend. Default "Max". |
| `dropdown` | `boolean` | `false` | Draws the fields as dropdowns: one choice and a range with bound "min" as a Select with picker, several choices as checkboxes in a Popover, a range as Min and Max radio lists in one, all in the row. ResultsHead's sort has its own prop, sortPicker. The names, the values, the address bar and filters:change stay the same. |
| `rangeText` | `string` | None | With dropdown: a range's words for both ends. Default "{min} to {max}". |
| `fromText` | `string` | None | With dropdown: a range's words for its Min alone. Default "{min}+". |
| `upToText` | `string` | None | With dropdown: a range's words for a maximum only. Default "Up to {max}". |
| `countText` | `string` | None | With dropdown: the words for two or more choices. A field's own countText wins. Default "{n} selected". |
| `removeLabel` | `string` | `FILTERS_TEXTS.removeLabel` | An active-filter chip's name, with {text} filled in ("Remove: {text}"). |
| `barClass` | `string` | None | For recipes. Classes for the bar: the row, the option-chip fields, Clear and the actions. |
| `rowClass` | `string` | None | For recipes. Classes for the row: the switch, the text box and the one-line fields. |
| `fieldClass` | `string` | None | For recipes. Classes for each field (the text box, each range and choice). An option-chip field has data-filter="options". |
| `chipsClass` | `string` | None | For recipes. Classes for the list of active-filter chips. |
| `class` | `string` | None | More classes for the <form>, merged with cn(). The form is the filters container (@container/filters). |
| Slot | Description |
| --- | --- |
| `actions` | After Clear: your own buttons, such as "Save search". Optional. |
Any other attribute goes on the <form>; id names it (ResultsHead's filtersId), else a page-unique id. It fires filters:change on the form, detail { state, kind, source, url }, once per change; a pager sends filters:page with { page }. filtersState(form) in lib/filters.ts gives the state now.
## Accessibility
| Key | Action |
| --- | --- |
| Tab and Shift+Tab | Move through the switch, the text box, each filter, Clear and the active-filter chips, in the order drawn |
| Enter or Space | With dropdown, on a trigger: opens and closes its panel |
| Up and Down arrows | With dropdown, in a radio list: move and pick, each move a filter change |
| Escape | With dropdown: closes the panel and returns focus to the trigger |
| Left and Right arrows | Move between the switch's options (Buy, Rent), as in any radio group |
| Space | Checks or unchecks an option chip |
| Enter | In the text box, applies the text at once; on an active-filter chip, removes that filter |
- The form is a search landmark (role="search") named by label. Every control is native: radios, a search box, selects and checkboxes, each with a label.
- The switch is a fieldset of radios with a visually hidden legend (modesLabel), so a screen reader says "Buy or rent" before the options. It takes one Tab stop.
- A range is a fieldset: its legend is the field's label for the mode ("Price", or "Rent"), and each select has its own name ("Min", "Max").
- Option chips are native checkboxes, each its own Tab stop ("House, checkbox, checked"); checked shows a check icon, not only a color. No aria-pressed buttons.
- Each active-filter chip is a button named "Remove: 3+ beds". After one is removed, focus moves to the next chip, else to the first filter.
- Every edit writes the address bar, so a search can be shared, reloaded and walked with Back and Forward. A mode change is its own history step; filter edits replace the current one.
- With dropdown, each trigger is a button named by its label and its words ("Price Any price"), and opens a named dialog. Its options are native radios or checkboxes ("Min, group; $750K, radio"). Enter or Space picks; radios pick as the arrows move, as native radios do; Escape closes and goes back to the trigger. After Clear or the last chip is removed, focus goes to the first filter, which for a dropdown is its trigger.
- With no JavaScript the filters are hidden (nothing would listen), so the page's list shows every listing.
- Every control is at least 44px on a touch screen. The switch's thumb slides only when motion is allowed (not under reduced motion or the theme's calm setting).
Filters
FreeTo build your own search filters: a buy or rent switch, a text box, choices and ranges from your fields list, kept in the address bar.
- File
- Filters.astro
- Group
- Forms
- Kind
- Interactive: inline script
- Examples
- 2
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
The shell
The parts in their order, with no recipe: the switch, the text box, price and rent ranges, beds and baths, the home type as option chips, then Clear and the active-filter chips. Pick a few filters and watch the address bar.
This preview did not load.
main.astro
examples/filters/main.astro
---
import Filters from "@astronomic-dev/ui/components/Filters.astro";
import { defaults, fields, modes, sorts } from "../_data/vega.ts";
---
<Filters label="Filter homes" fields={fields} modes={modes} modesLabel="Buy or rent" sorts={sorts} defaults={defaults} textLabel="Where" textPlaceholder="Street, neighborhood or listing number" />Dropdowns
The same fields with dropdown: labels above values, the home type in the row as a dropdown of checkboxes.
This preview did not load.
dropdown.astro
examples/filters/dropdown.astro
---
import Filters from "@astronomic-dev/ui/components/Filters.astro";
import { defaults, fields, modes, sorts } from "../_data/vega.ts";
---
<Filters dropdown label="Filter homes" fields={fields} modes={modes} modesLabel="Buy or rent" sorts={sorts} defaults={defaults} textLabel="Where" textPlaceholder="Street, neighborhood or listing number" />Minimal · Light