Skip to content

Filter Panel

A Filter button that counts the filters on and opens a panel of filter groups. Each group is a heading over checkboxes, or over radio choices when only one value may be picked.

Decision

When Use
A list that narrows by several criteria at once, such as status, kind, and owner Filter Panel, above the list
A few filters that fit in the list's own toolbar The filters of a Filtered List, Data Table, or Paginated Table
A long searchable list of picks Checkbox List
A filter the reader writes Rule Builder

Usage

import { createFilterPanel } from './configurationpage?name=myplugin_stolon.js';

var filter = createFilterPanel({
    groups: [
        { name: 'status', label: 'Status', options: [{ value: 'active', label: 'Active' }, { value: 'paused', label: 'Paused' }] },
        { name: 'kind', label: 'Kind', single: true, options: [{ value: 'movie', label: 'Movies' }, { value: 'show', label: 'Shows' }] }
    ],
    value: { status: ['active'] },
    onChange: function (value) { reload(value.status, value.kind[0]); }
});
toolbar.appendChild(filter.element);

Classes: stolon-filter-button, stolon-filter-count, stolon-filter-panel, stolon-filter-groups, stolon-filter-group, stolon-filter-actions, stolon-filter-dialog. The handle is { element, getValue, setValue, setGroups, open, close, isOpen, destroy }, where element is the button.

Parts: stolon-filter-text, stolon-filter-title, stolon-filter-group-name, stolon-filter-option, stolon-filter-input, stolon-filter-clear, stolon-filter-done. Each option row reuses stolon-check-row from the Checkbox List.

States and Modifiers

  • The value maps each group name to an array of picked values. getValue lists every group, with an empty array for a group with none. A value that names no group or option is dropped, and a single group keeps at most one.
  • The badge shows the count of picked values and hides at zero. The button takes stolon-active while any filter is on, and its aria-label reads "Filter, 3 filters on".
  • The defaults are "Filter" (buttonText), "Filters" (title), "Clear" (clearText), "Done" (doneText), "{value} filters on" (countLabel), and "{value} filter on" (countOneLabel).
  • applyOnChange (default true) applies and reports each change at once. With false, changes apply on Done, and Escape, an outside click, or leaving the panel drops them.
  • The panel is a popover under the button, or above it when there is no room. On a screen under 600px wide, or with asDialog: true, it opens as a Dialog with the Clear and Done buttons in its footer.
  • Opening moves focus to the first box. Escape and an outside click close the panel and return focus to the button, and Tab out of the popover closes it.
  • Clear unchecks every group. setValue and setGroups do not call onChange.
  • onChange reports a fresh copy of every group, so a list that filters by group name can take it as it is, and an empty array means no filter for that group.

Gotchas

  • The popover is placed in the page right after the button, so the button needs a parent for the Tab order to follow it.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createFilterPanel

function createFilterPanel(options?: FilterPanelOptions): FilterPanelHandle

A Filter button with a count of the filters on, which opens a panel of filter groups. The panel is a popover under the button, or a dialog on a screen under 600px wide or with asDialog.

Option Type What it does
groups FilterPanelGroup[] array of { name, label, single, options: [{ value, label }] } [optional]
value FilterPanelValue | null object from group name to an array of picked values, where a single group holds at most one [optional]
buttonText string text of the button (default 'Filter')
title string heading of the panel (default 'Filters')
clearText string text of the button that unchecks every group (default 'Clear')
doneText string text of the button that closes the panel (default 'Done')
countLabel string end of the button's aria-label while filters are on, where {value} is the count (default '{value} filters on')
countOneLabel string the same while one filter is on (default '{value} filter on')
applyOnChange boolean apply each change at once, or with false only on Done (default true)
asDialog boolean always open as a dialog (default false)
onChange (value: FilterPanelValue): void function(value) called each time the picked values apply [optional]
Handle member Type
element HTMLButtonElement
getValue (): FilterPanelValue
setValue (value: FilterPanelValue | null): void
setGroups (groups: FilterPanelGroup[] | null): void
open (): void
close (): void
isOpen (): boolean
destroy (): void
Type What it is
FilterPanelOption One choice in a filter group.
FilterPanelGroup One group of a filter panel, shown as a heading over checkboxes, or over radio choices when single is true.
FilterPanelValue The picked values of a filter panel, from group name to the values picked in it.
Class What it looks like
stolon-filter-button Filter button with a leading icon, its text, and a round accent badge counting the filters on
stolon-filter-count Small round badge with the count of filters on, hidden while none is on
stolon-filter-panel Floating panel of filter groups on the stolon-float surface, fixed to the viewport, placed by createFilterPanel under the button, or above it with no room
stolon-filter-title Bold heading at the top of the floating panel
stolon-filter-groups Scrolling stack of filter groups, in the floating panel or in the dialog body
stolon-filter-group One group of checkbox rows, or radio rows with single, under its name
stolon-filter-group-name Small dimmed name over a group's rows
stolon-filter-option One option row, which shows an accent ring while its box has focus
stolon-filter-actions Row of the Clear and Done buttons at the end of the floating panel

Example

Example Code

<!-- title: Filter Panel -->
<!-- min-height: 380 -->
<div id="mount"></div>
<p id="note"></p>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const note = document.getElementById('note');
const show = (value) => { note.textContent = 'Status ' + (value.status.join(', ') || 'any') + ', kind ' + (value.kind[0] || 'any') + '.'; };
const filter = base.createFilterPanel({
    groups: [
        { name: 'status', label: 'Status', options: [{ value: 'active', label: 'Active' }, { value: 'paused', label: 'Paused' }, { value: 'failed', label: 'Failed' }] },
        { name: 'kind', label: 'Kind', single: true, options: [{ value: 'movie', label: 'Movies' }, { value: 'show', label: 'Shows' }] }
    ],
    value: { status: ['active', 'failed'], kind: ['movie'] },
    onChange: show
});
mount.appendChild(filter.element);
show(filter.getValue());
</script>