Filter bar
Removable filter conditions, a clear action and space for your filter controls.
Demo
Usage
import { useState } from 'react'
import { Button, FilterBar, type FilterCondition, type FilterView } from '@tuttn/slant'
const [conditions, setConditions] = useState<FilterCondition[]>([
{ id: 'state', label: 'State', value: 'In progress' },
{ id: 'owner', label: 'Owner', value: 'Me' },
])
const remove = (id: string) => setConditions(current =>
current.filter(condition => condition.id !== id))
<FilterBar label="Issue filters" conditions={conditions} onRemove={remove}
onClear={() => setConditions([])}>
<Button size="sm" onClick={openFilterPicker}>Add filter</Button>
</FilterBar>
// Saved views, controlled: your app swaps the conditions when a view is
// chosen, and says when they no longer match the view's saved ones.
const views: FilterView[] = [
{ id: 'mine', label: 'My open issues' },
{ id: 'urgent', label: 'Urgent' },
]
const [view, setView] = useState('mine')
<FilterBar label="Issue filters" conditions={conditions} onRemove={remove}
views={views} view={view}
onViewChange={id => { setView(id); setConditions(saved[id]) }}
modified={!sameConditions(conditions, saved[view])}
onResetView={() => setConditions(saved[view])}
onUpdateView={() => saveView(view, conditions)} />
// Uncontrolled: the picker keeps the current view and reports each choice.
<FilterBar label="Issue filters" conditions={conditions} onRemove={remove}
views={views} defaultView="mine" onViewChange={id => setConditions(saved[id])} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
conditions | readonly FilterCondition[] | — | The applied conditions. The caller owns their state and filtering logic. |
label | string | — | The group's accessible name. |
onRemove | (id: string) => void | — | Requests removal of one condition. Update conditions to complete it. |
onClear | () => void | — | Adds a clear-all button while there are conditions to remove. |
clearLabel | string | messages.clearFilters | The clear-all button's text. |
views | readonly FilterView[] | — | Saved views: adds a leading picker, a small Select named `messages.savedView`. Choosing a view calls onViewChange; your app swaps the conditions for the view's own. |
view | string | null | — | The current view's id, or null for none. Pass it with onViewChange to control the picker; leave it out to let it keep its own. |
defaultView | string | null | — | The view an uncontrolled picker starts with. Without one nothing is chosen and the picker shows its placeholder. |
onViewChange | (id: string) => void | — | Called with the view's id when the reader chooses a different one. |
modified | boolean | false | Whether the conditions differ from the current view's saved ones; your app decides. With `views`, it shows a "Modified" badge (`messages.viewModified`, announced politely) after the picker, then the Reset and Update buttons. |
onResetView | () => void | — | Adds a Reset button (`messages.resetView`) while modified: put the view's saved conditions back. |
onUpdateView | () => void | — | Adds an Update button (`messages.updateView`) while modified: save the current conditions to the view. |
children | ReactNode | — | An optional caller-owned add-filter control, menu trigger or other action. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref reach the outer group. |
Keyboard
| Tab | moves through the view picker, Reset and Update, each condition's remove button, your actions and Clear all. The group itself is not a stop. |
| Enter / Space / Backspace / Delete | on a remove button, requests that condition's removal; focus moves on once your app removes it |
| Enter / Space / ↓ / ↑ | on the view picker, opens the list of views; Select's keys move through it and choose one |
| Enter / Space | on Reset or Update, calls onResetView or onUpdateView; once modified turns false, focus moves to the picker |
Accessibility
- label names the group; remove buttons identify both the field and its value.
- Native buttons support Enter and Space; Chip also supports Backspace and Delete.
- Controlled removal preserves keyboard focus without stealing it after a delayed update.
- The view picker is a combobox named
messages.savedView("Saved view"); its text is the current view, never its name. - The "Modified" badge sits in a
role="status"region that is rendered wheneverviewsis, so its arrival is announced politely; while modified, it also describes the picker (aria-describedby). - When Reset or Update disappears under focus, focus moves to the picker instead of falling back to the page.
Controlled conditions
Give every condition a stable, unique id, a field label and a human-readable value. The bar displays the current conditions and requests removal; your app updates the array and applies the filtering. Use values such as “Open or in progress” to express combinations without exposing query syntax.
Pass onClear to offer a clear-all action while the array is nonempty. The button reads messages.clearFilters, or your clearLabel. With no conditions, the bar keeps its children so an add-filter control remains available. Pair it with Menu or Popover to supply your own condition picker.
Saved views
Pass views, each a FilterView with an id and a label, and the bar leads with a view picker: a small Select named messages.savedView, showing the current view. Control it with view and onViewChange (null shows the placeholder), or give it a defaultView and let it keep its own. Choosing a view only reports it: your app swaps in that view's conditions.
Your app also decides when the live conditions have drifted from the view's saved ones and passes modified. While it's true, a "Modified" badge (messages.viewModified) follows the picker, then a Reset button with onResetView (messages.resetView: put the saved conditions back) and an Update button with onUpdateView (messages.updateView: save the live ones into the view), both small. Renaming, deleting and saving a new view belong to your app, in a menu or a dialog of its own. Without views, modified and the two callbacks show nothing.
Keyboard and layout
Each remove button includes the field and value in its accessible name, using the kit's messages.remove. Enter, Space, Backspace and Delete request removal. Once the caller updates the array, focus moves to the next condition, then the previous one if necessary. After the last removal it moves to a remaining action after the conditions, then the view picker, or the group itself. Delayed updates respect focus that the user has moved elsewhere.
Reset and Update leave as soon as modified turns false. If one of them had focus, focus moves to the view picker rather than the page, whether the change came from the button or from elsewhere in your app; focus the user has already moved on stays where it is.
Conditions and actions wrap at narrow widths. Long labels and unbroken values wrap inside their chip so its remove button stays reachable; a long view name is cut short inside the picker, which fits its view's name rather than the bar's width. A hairline sets the view controls off from the conditions. The group is not an extra Tab stop; it takes programmatic focus only when there is no remaining control.