Filter
Filter loop for e-commerce list pages: sort options stay on the bar (featured / sales / price, price supports ascending–descending toggle), the criteria panel holds multiple filter groups (single-select / multi-select), and selected conditions are echoed as chips under the panel — undo per chip, reset all at once. The value contract is the conditions JSON dual channel: uncontrolled hosts get automatic write-back on user interaction (works out of the box), controlled hosts take over by listening to oas-change. The panel comes in dropdown and half-screen drawer forms; the drawer is recommended on narrow screens.
Basic usage
groups declares filter groups (multiple: true for multi-select); clicking a sort option, picking a condition, or removing a chip all emit oas-change (detail carries the current sort, selected values per group, and a composed summary text that can feed an aria-live region directly):
Sort + criteria groups + selected echo
Click a sort option, open the panel to pick conditions, or remove a chip to see oas-change feedback.
Sort options
sorts can replace the default trio; sort items with toggled: true support an ascending–descending toggle — first click sorts descending, the next click ascending (the e-commerce "price ↓" convention). The direction lands in the oas-change order field and the summary:
Custom sort options + price toggle
Multiple criteria groups
In single-select groups clicking the selected option cancels it (back to "any"); multi-select groups toggle freely; disabled options are greyed out. Selected conditions echo as chips whose aria-label is "Remove condition-name"; they are focusable and removable with Enter, and the trailing "Reset" clears everything (sort falls back to the default):
Single-select + multi-select groups + disabled option
Drawer form
panel="drawer" switches to the half-screen drawer (slides in from the bottom with a mask), recommended on narrow (mobile) screens; the mask, the "Done" button, and Esc all close it, restoring focus to the trigger. The default panel="dropdown" is an anchored dropdown that dismisses on outside click:
Drawer form
Controlled usage
The host writes conditions (or the property channel el.conditions = {...}) to sync the view directly (chip echo, sort selection, badge count) without emitting oas-change; the component writes conditions back after user interactions — controlled hosts listen to oas-change and write it again to take over. open follows the same dual channel (preset expanding emits no oas-open-change):
Preset conditions + host-side reset
Preset expansion and preset conditions
The open attribute presets the panel expanded (host write, no oas-open-change); combined with conditions this is the "remember the user's last filter" echo:
Preset expanded + preset conditions (open attribute)
Combined with the goods list
Pair it with oas-goods-list for a closed list-page loop: filter changes refill the list (this demo simulates filtering over static data):
filter + goods-list recipe
Condition persistence (capability subpath)
Persisting conditions goes through the capability subpath @oas-ui/ui/commerce/filter/persist (helper: import on demand, not shipped with the base component). Two backends — URL (query / hash) and Storage (localStorage etc.) — used individually or together: the URL backend powers share / deep-link restore, the Storage backend powers returning-visitor memory. Encoded strings escape CJK characters and reserved characters such as & = # % (safe to embed in a hash); decoding applies structural sanitization (garbage input → null, output can be fed straight into filter.conditions):
URL encode round-trip + localStorage memory (save / load / toQuery / fromQuery)
Pick a sort or a condition, then click "Save" to see the encoded string (CJK fully escaped) and the raw JSON stored in localStorage; change the conditions and click "Restore" to replay the deep-link restore.
Host glue (the demo's three steps):
import { toQuery, fromQuery, save, load } from '@oas-ui/ui/commerce/filter/persist'
// (1) On load: URL first, fall back to local memory
const restored = fromQuery(location.search) ?? load(localStorage, 'oas:filter')
if (restored) filter.conditions = restored
// (2) Persist on interaction (dual write; the URL write-back is the host's history.replaceState)
filter.addEventListener('oas-change', () => {
toQuery(new URLSearchParams(location.search), filter.conditions)
save(localStorage, 'oas:filter', filter.conditions)
})Keyboard and accessibility
- The sort row and single-select groups carry
radiogroupsemantics (arrow keys move focus within a group); multi-select groups usegroup+checkboxsemantics; every option is Tab-reachable. - The trigger carries
aria-haspopup/aria-expanded/aria-controls; its accessible name is derived from the visible content (sort label + count badge — what you see is what a screen reader reads). Opening the panel focuses the first condition, Esc closes and restores focus to the trigger. - Chips are focusable buttons whose
aria-labelis "Remove condition-name"; after removing, focus moves to the adjacent chip; the composed summary renders into anaria-live="polite"region. - The dropdown dismisses on outside click; the drawer is a modal dialog (
role="dialog"+aria-modal="true"on the panel) whose Tab / Shift+Tab focus loop stays inside the panel, closing via the mask / "Done" / Esc. Layout uses logical properties and mirrors automatically under RTL.
Styling notes
::part(bar) lays out both ends (sort row at the inline start, trigger at the end); ::part(panel) is absolutely positioned under the bar in dropdown form and position: fixed to the bottom in drawer form (distinguished by the data-form attribute); when the space below is insufficient, the dropdown adds data-flip="up" to the panel (it then opens above the bar), so panel-positioning overrides must handle ::part(panel)[data-flip='up'] as well; the panel carries max-height: 70vh with vertical scrolling. ::part(chips) is a flex wrap row. The selected option background is a 10% color-mix of the primary color — keep the token linkage when overriding.
API
oas-filter
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
conditions | Current value as JSON { sort?, order?, groups? } (order only matters for toggled sort items: asc / desc; groups maps group key to a value array, single-select groups keep at most 1). The component writes this back and emits oas-change on user interaction; host writes only sync the view | FilterConditions | string | — |
groups | Filter groups as a JSON array [{ key, label, multiple?, options: [{ value, label, disabled? }] }]; entries without options are ignored, invalid JSON warns once and clears (the trigger hides when no groups remain) | FilterGroup[] | string | — |
open | Panel expanded state: user interaction / setOpen() writes it back and emits oas-open-change; host writes only sync the view | boolean | — |
panel | Panel form: dropdown (default, anchored under the bar, dismisses on outside click) / drawer (half-screen sheet with a mask; recommended on narrow screens); invalid values fall back to dropdown and warn once | — | — |
sorts | Sort options as a JSON array [{ key, label, toggled? }]; defaults to the featured/sales/price trio (price toggled), invalid JSON warns once and falls back; toggled items flip descending on first click, ascending on the next | FilterSortOption[] | string | — |
Property (JS property; see each row for attribute reflection)
| Property | Description | Type | Default |
|---|---|---|---|
conditions | Current value: get+set dual channel (object or JSON string), writes reflect the conditions attribute (controlled writes do not emit oas-change) | FilterConditions | string | — |
groups | Filter groups: get+set dual channel (array or JSON string), writes reflect the groups attribute | FilterGroup[] | string | — |
open | Panel expanded state: get+set dual channel (boolean), writes reflect the open attribute (silent; only setOpen() emits) | boolean | — |
sorts | Sort options: get+set dual channel (array or JSON string), writes reflect the sorts attribute | FilterSortOption[] | string | — |
Methods
| Method | Signature | Description |
|---|---|---|
setOpen | setOpen(open: boolean): void | Programmatic open/close (equivalent to user interaction): reflects the open attribute + emits oas-open-change + focus management (opening focuses the first condition, closing restores the trigger) |
Events
| Event | Description |
|---|---|
oas-change | Emitted after sorting / picking conditions / removing a chip / resetting, detail: { sort, order, groups, summary } (summary is a composed text for host-side aria-live); host writes to conditions do not emit |
oas-open-change | Emitted after the panel opens / closes via user interaction or setOpen(), detail: { open }; host writes to open do not emit |