Web component / Examples

<list-filter>

Filter ordered (ol), unordered (ul), and grouped definition (dl) lists without replacing their content. Add a search field, a category dropdown, or both. See the README for the full API.

Loading filter controls. The original lists remain available.

Search a reading list

Use the for attribute on <list-filter> to reference the list, and add search to enable a labeled search field. Try “CSS” or “progressive”. Matching is case-insensitive and literal. Reset brings back the full list without changing its order.

<list-filter for="reading" search></list-filter>
<ol id="reading">
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS">CSS reference</a></li>
  <li>Progressive enhancement handbook</li>
  <li>HTML forms guide</li>
</ol>
  1. CSS reference
  2. Progressive enhancement handbook
  3. HTML forms guide

Filter by category

Add the categories attribute to show a dropdown without a search field. Give each direct list item a data-category value. The dropdown uses those values as its option labels, with repeated values appearing only once. Choose “cold” or “hot” to filter the drinks; choose “All” to show the full list again.

<list-filter for="raw-drinks" categories></list-filter>
<ul id="raw-drinks">
  <li data-category="cold">Sparkling water</li>
  <li data-category="hot">Lemon tea</li>
  <li data-category="cold">Lemonade</li>
</ul>
  • Sparkling water
  • Lemon tea
  • Lemonade

Give category keys readable labels

Use the categories-map attribute when the stored category keys need different display labels. Supply a JSON object mapping each key to its label. This example displays “Cold drinks” and “Hot drinks” but still matches the original cold and hot values in the list. Unmapped values keep their raw labels.

<list-filter for="mapped-drinks" categories
  categories-map='{"cold":"Cold drinks","hot":"Hot drinks"}'></list-filter>
<ul id="mapped-drinks">
  <li data-category="cold">Sparkling water</li>
  <li data-category="hot">Lemon tea</li>
  <li data-category="cold">Lemonade</li>
</ul>
  • Sparkling water
  • Lemon tea
  • Lemonade

Combine search and categories

Add both search and categories to offer both controls. An item must match the search term and the selected category to stay visible. Try searching for “lemon” and choosing “Hot drinks”. Choose “All” to clear only the category, or use Reset to clear both filters. Search for “coffee” to try a zero-result state.

Customize the legend with title, and use search-placeholder to give a search hint without replacing the field’s label.

<list-filter for="drinks" search categories
  title="Find a drink"
  categories-map='{"cold":"Cold drinks","hot":"Hot drinks"}'
  search-placeholder="Try lemon"></list-filter>
<ul id="drinks">
  <li data-category="cold">Sparkling water</li>
  <li data-category="hot">Lemon tea</li>
  <li data-category="cold">Lemonade</li>
</ul>
  • Sparkling water
  • Lemon tea
  • Lemonade

Nothing shown? Clear the category, edit the search term, or reset.

Translate a definition-list filter

Keep each term with its definition by wrapping them in a direct div child of the dl. Override the generated labels to translate the controls, and use output-label with output-label-* variants for translated counts. Set lang to select the language’s plural rules.

<list-filter for="glossary" categories lang="fr"
  title="Filtrer le glossaire"
  categories-label="Par catégorie"
  categories-placeholder="Toutes"
  categories-map='{"html":"HTML","css":"CSS"}'
  reset-label="Réinitialiser"
  output-label="{count} termes"
  output-label-one="{count} terme"></list-filter>
<dl id="glossary" lang="fr">
  <div data-category="html">
    <dt>Élément</dt><dd>Une partie du document HTML.</dd>
  </div>
  <div data-category="css">
    <dt>Sélecteur</dt><dd>Une règle pour cibler des éléments.</dd>
  </div>
</dl>
Élément
Une partie du document HTML.
Sélecteur
Une règle pour cibler des éléments.

Supply your own controls

Supply a template as a direct child of <list-filter> to use your own controls. Mark each enabled field, reset button, and output with its data-list-filter-ref value so the component can wire them up. Provide native labels and a legend.

The component clones your template into light DOM; the original template stays inert. This example groups the field and button in a wrapping row, with the result count below.

<list-filter for="fruit" search>
  <template>
    <fieldset class="fruit-controls">
      <legend>Find fruit</legend>
      <div class="fruit-fields">
        <label>Fruit name
          <input type="search" data-list-filter-ref="search">
        </label>
        <button type="button" data-list-filter-ref="reset">Clear</button>
      </div>
      <output data-list-filter-ref="output"></output>
    </fieldset>
  </template>
</list-filter>
<ul id="fruit"><li>Apple</li><li>Pear</li></ul>
  • Apple
  • Pear

Recover from a missing list reference

Give for the ID of an existing list. If that ID is missing, the component leaves the list unfiltered and adds no controls or visible message. Use the button below to correct this example’s reference and enable its filter.

<list-filter id="recovery" for="missing-list" search></list-filter>
<ul id="recovery-list">
  <li>Original content stays available.</li>
</ul>
<!-- Correct the reference to recover: for="recovery-list". -->
  • Original content stays available.