live-filterA Web Component for filtering items using a text input.
Demo | Further reading
General usage example:
<script type="module" src="live-filter.js"></script>
<live-filter>
<label>Filter: <input type="search" aria-controls="list" /></label>
<ul role="region" id="list" aria-live="polite">
<li>African Violet</li>
<li>Aloe Tiger Plant</li>
<li>Aralia Ming</li>
<li>Autograph Tree</li>
</ul>
</live-filter>Example setting the selector option to select dt elements:
<script type="module" src="live-filter.js"></script>
<live-filter selector="dt">
<label>Filter: <input type="search" aria-controls="data" /></label>
<dl role="region" id="data" aria-live="polite">
<dt>Beast of Bodmin</dt>
<dd>A large feline inhabiting Bodmin Moor.</dd>
<dt>Morgawr</dt>
<dd>A sea serpent.</dd>
<dt>Owlman</dt>
<dd>A giant owl-like creature.</dd>
</dl>
</live-filter>Example setting the case option to change the search to be case insensitive:
<script type="module" src="live-filter.js"></script>
<live-filter case="insensitive">
<label>Filter: <input type="search" aria-controls="list" /></label>
<ul role="region" id="list" aria-live="polite">
<li>African Violet</li>
<li>Aloe Tiger Plant</li>
<li>Aralia Ming</li>
<li>Autograph Tree</li>
</ul>
</live-filter>This Web Component allows you to:
data-live-filter-match="true" and data-live-filter-match="false"selector attribute on the live-filter element itselfcase attribute on the live-filter element itselfYou have a few options (choose one of these):
npm install @daviddarnes/live-filterMake sure you include the <script> in your project (choose one of these):
<!-- Host yourself -->
<script type="module" src="live-filter.js"></script><!-- 3rd party CDN, not recommended for production use -->
<script
type="module"
src="https://www.unpkg.com/@daviddarnes/[email protected]/live-filter.js"
></script><!-- 3rd party CDN, not recommended for production use -->
<script
type="module"
src="https://esm.sh/@daviddarnes/[email protected]"
></script>selector and caseBy default live-filter will select all the li elements within it to search through. However with the selector attribute you can pass in a different selector string to search through:
<live-filter selector="dt">
<!-- ... -->
</live-filter>By default live-filter will be case sensitive, which can be restrictive when searching through proper nouns and titles. This can be adjusted using the case attribute and setting the value to insensitive:
<live-filter case="insensitive">
<!-- ... -->
</live-filter>With thanks to the following people: