Navigation

v2.1.0
Application blocks

Filter bar

Conditions to narrow a list, staged, applied and shown as chips.

A filter bar narrows a list by conditions: a field, a way to compare and a value, such as "Status is Ready" or "Duration is less than 60 seconds". Conditions are written in a popover, applied together, and shown on the bar as chips that each take one away. The bar works over any list; here it narrows a list of deployments, and in the table view the same bar narrows a table.

Filter bar

<section class="sheet container-inline flex flex-col overflow-hidden" data-example="filter-bar" aria-labelledby="fb-title">  <header class="flex flex-wrap items-center justify-between gap-sm p-md">    <div class="flex flex-col gap-3xs">      <h2 class="heading heading-sm" id="fb-title">Deployments</h2>      <p class="m(0) fg-text-muted" id="fb-summary" aria-live="polite">30 deployments in the last two weeks</p>    </div>  </header>  <div class="flex flex-wrap items-center gap-xs px-md pb-md">    <button type="button" class="btn btn-sm" popovertarget="fb-panel" data-filter="open">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M4.5365 3L11.4635 3A1 1 0 0 1 12.2548 4.6114L9.4587 8.2299A1 1 0 0 0 9.25 8.8413L9.25 11.323A1 1 0 0 1 8.6214 12.2514L7.875 12.55A0.8203 0.8203 0 0 1 6.75 11.7883L6.75 8.8413A1 1 0 0 0 6.5413 8.2299L3.7452 4.6114A1 1 0 0 1 4.5365 3Z" />      </svg>Filters      <span data-filter="count" aria-hidden="true" hidden>        <span class="badge badge-sm badge-ink badge-pill">0</span>      </span>    </button>    <div data-filter="applied" hidden>      <div class="flex flex-wrap items-center gap-xs">        <ul class="flex flex-wrap gap-xs m(0) p(0)" aria-label="Applied filters" data-filter="chips"></ul>        <button type="button" class="btn btn-quiet btn-sm" data-filter="clear-all">Clear all</button>      </div>    </div>  </div>  <div class="popover p(0) w(560)" id="fb-panel" popover role="dialog" aria-labelledby="fb-panel-title" style="--popover-max-width: min(36rem, calc(100vw - 2rem))" data-filter="panel">    <form class="flex flex-col" data-filter="form" novalidate>      <header class="flex flex-col gap-3xs px-md pt-md pb-sm">        <h3 class="heading heading-xs" id="fb-panel-title">Filter deployments</h3>        <p class="m(0) fg-text-muted">A deployment shows when it meets every condition.</p>      </header>      <ul class="flex flex-col gap-xs m(0) px-md py-xs" data-filter="conditions"></ul>      <p class="m(0) px-md py-xs fg-text-muted" data-filter="empty">No conditions yet: every deployment shows.</p>      <p class="m(0) px-md py-xs fg-danger" id="fb-error" role="alert" data-filter="error" hidden></p>      <div class="px-md pb-md">        <button type="button" class="btn btn-quiet btn-sm" data-filter="add">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M3 8L13 8" />            <path d="M8 3L8 13" />          </svg>Add condition</button>      </div>      <footer class="flex flex-wrap items-center justify-between gap-xs p-md bt(1) bt-solid b-line-hair">        <button type="button" class="btn btn-quiet btn-sm" data-filter="clear">Clear</button>        <div class="flex gap-xs">          <button type="button" class="btn btn-sm" data-filter="cancel">Cancel</button>          <button type="submit" class="btn btn-primary btn-sm">Apply</button>        </div>      </footer>    </form>  </div>  <ul class="list px-md bt(1) bt-solid b-line-hair" id="fb-results" aria-label="Matching deployments"></ul>  <p class="m(0) px-md pb-md pt-xs fg-text-muted" id="fb-more" hidden></p>  <template data-filter="row">    <li class="flex flex-wrap items-center gap-xs">      <span class="w(44) shrink(0) fg-text-muted text-end" data-part="joiner">Where</span>      <select class="select select-sm" aria-label="Field" data-part="field"></select>      <select class="select select-sm" aria-label="Comparison" data-part="operator"></select>      <input class="input input-sm grow(1) basis(128) min-w(0)" aria-label="Value" data-part="value" required>      <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove condition" data-part="remove">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M4 4L12 12" />          <path d="M4 12L12 4" />        </svg>      </button>    </li>  </template>  <template data-filter="choice">    <select class="select select-sm grow(1) basis(128)" aria-label="Value" data-part="value"></select>  </template>  <template data-filter="chip">    <li>      <span class="chip">        <span class="chip-label"></span>        <button type="button" class="chip-remove" aria-label="Remove the filter">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4 4L12 12" />            <path d="M4 12L12 4" />          </svg>        </button>      </span>    </li>  </template>  <template id="fb-row">    <ul class="list">      <li class="list-row">        <div class="list-body">          <p class="list-title"></p>          <p class="list-description"></p>        </div>        <span class="list-end flex items-center gap-sm">          <span class="fg-text-muted"></span>          <span class="badge badge-dot"></span>        </span>      </li>    </ul>  </template>  <template id="fb-none">    <ul class="list">      <li class="list-row">        <div class="list-body">          <p class="list-title">No deployments match</p>          <p class="list-description">Remove a filter, or clear them all to see every deployment.</p>        </div>      </li>    </ul>  </template></section>
Open Filters to change a condition or add one, then Apply. A chip's cross takes its condition away.

Blocks are markup to copy. They take the page's own text color and size; the previews set fg-text-body text-sm on the body. Each block is a size container, so it reflows by its own width: in a wide page, a sidebar's worth of room or a dialog.

#What is in it

  • One button opens the conditions. "Filters" with a count of what is applied in an ink badge, so a closed bar still says that the list is narrowed. Its accessible name says it too: "Filters, 2 applied".
  • The conditions are a draft. The popover opens on a copy of what is applied. Add condition, a change of field, a change of value: nothing happens to the list until Apply. Cancel, Escape or a click outside drop the draft; Clear empties it, so Apply then shows every row.
  • Every condition is one row, "Where" before the first and "and" before the others: a row meets all of them, and there is one level, no groups and no "or". Each row is a select for the field, one for the comparison, the value and a quiet button that removes it.
  • The field decides the rest. A text field compares with contains, is, is not or starts with and takes words; a choice compares with is or is not and takes one of its values; a number is more or less and takes a number field; a date is on, before or after and takes a date field. Changing the field starts its row over, so a comparison never meets a value it cannot read.
  • A condition that cannot work is never applied. A missing value, letters in a number or half a date mark the value aria-invalid="true", which the field draws as its danger edge, and a message under the rows says which condition and why. Focus goes to it, and the list does not change.
  • Applied conditions are chips. Each says its condition in words and has a remove button named for it, "Remove the filter Status is Ready". Clear all, at the end of the row, takes them all away at once.
  • The list says what it shows. The summary under the title counts the matches against the whole, "12 of 30 deployments match all 2 filters", and the list says when nothing matches and how to get back.

#What needs a script

The example is three modules. query.js holds the conditions: the comparisons each type offers, what is wrong with a condition, whether a row meets one, and search, sort and pages; it reads plain rows and touches no element. filters.js runs the bar: it copies the templates the markup holds for a condition's row, a choice and a chip, fills them in, and calls onChange with the applied conditions. main.js draws the list from them. Your application asks its own data source the same questions, and the bar does not change: send the applied conditions to the server instead of to filter().

Without a script the bar has nothing to stage: give the list a <form method="get"> with a select for each field instead, and the server answers the query string.

#Accessibility

  • The popover is a role="dialog" named by its heading, "Filter deployments". Opening it puts focus on the first condition's field, or on Add condition when there is none; closing it gives focus back to Filters.
  • Each control of a row is named with its condition's number: "Field of condition 2", "Value of condition 2", "Remove condition 2". A number's value says its unit.
  • The message about a condition that cannot be applied is a role="alert" and is tied to its value with aria-describedby, so it is read when it appears and again from the value.
  • Removing a chip moves focus to the next chip, or to Filters when it was the last; Clear all moves it to Filters. The summary is a polite live region, so the new count is read after a change.
  • A condition is words in its chip, never a color: a status keeps its badge tone in the list, beside its name.