npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@github/filter-input-element

v0.1.1

Published

Display elements in a subtree that match filter input text.

Downloads

668

Readme

<filter-input> element

Display elements in a subtree that match filter input text.

Installation

$ npm install @github/filter-input-element

Usage

<filter-input aria-owns="robots">
  <label>
    Filter robots
    <input type="text" autofocus autocomplete="off">
  </label>
</filter-input>
<div id="robots">
  <ul data-filter-list>
    <li>Bender</li>
    <li>Hubot</li>
    <li>Wall-E</li>
    <li>BB-8</li>
    <li>R2-D2</li>
  </ul>
  <p data-filter-empty-state hidden>0 robots found.</p>
</div>

Elements and attributes

Required

  • filter-input[aria-owns] should point to the container ID that wraps all <filter-input> related elements.
  • filter-input should have one input child element that is used to filter.
  • [id] should be set on a container that either contains or has [data-filter-list] attribute.
  • [data-filter-list] should be set on the element whose direct child elements are to be filtered.

Optional

Specify filter text

Use [data-filter-item-text] to specify an element whose text should be used for filtering. In the following example, the text (current) would not be matched.

<div data-filter-list>
  <a href="/bender">Bender</a>
  <a href="/hubot">
    <span data-filter-item-text>Hubot</span>
    (current)
  </a>
</div>

Blankslate

Use [data-filter-empty-state] to specify an element to be displayed when no results were found. This element must be inside of the container aria-owns points to.

<div id="filter-list">
  <div data-filter-list>
    <a href="/bender">Bender</a>
    <a href="/hubot">Hubot</a>
  </div>
  <div data-filter-empty-state hidden>No results found.</div>
</div>

Create new item

Use [data-filter-new-item] to include an item to create a new instance when no exact match were found. The element with [data-filter-new-text]'s text content will be set to the input value. You can also use [data-filter-new-value] to set an input value to the query param.

<div id="filter-list">
  <div data-filter-list>
    <a href="/bender">Bender</a>
    <a href="/hubot">Hubot</a>
  </div>
  <form action="/new" data-filter-new-item hidden>
    <button name="robot" data-filter-new-item-value>
      Create robot "<span data-filter-new-item-text></span>"
    </button>
  </form>
</div>

Methods

filterInputElement.filter can be optionally set to provide an alternative filtering logic. The default is substring.

const fuzzyFilterInput = document.querySelector('.js-fuzzy-filter-input')
fuzzyFilterInput.filter = (element, elementText, query) => {
  // fuzzy filtering logic
  return {match: boolean, hideNew: boolean}
}

match(required) indicates whether the item should be shown. hideNew (optional) will determine whether the "Create new item" element should be hidden. For example, when an exact match is found, the "create new item" option should be hidden.

Events

  • filter-input-start (bubbles) - fired on <filter-input> when a filter action is about to start.
  • filter-input-updated (bubbles) - fired on <filter-input> when filter has finished. event.detail.count is the number of matches found, and event.detail.total is the total number of elements.

To ensure that the client side update is communicated to assistive technologies, filter-input-updated event can be used to update filter results to screen readers. For example:

const ariaLiveContainer = document.querySelector('[aria-live="assertive"]')
document.addEventListener('filter-input-updated', event => {
  ariaLiveContainer.textContent = `${event.detail.count} results found.`
})

For more details on this technique, check out Improving accessibility on GOV.UK search.

Browser support

Browsers without native custom element support require a polyfill.

  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.