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

sl-tags-input

v0.12.0

Published

An autocomplete input featuring tags and dropdown components built with Shoelace

Downloads

11

Readme

sl-tags-input

A web component based on Shoelace + LitElement to provide an autocomplete input featuring selected tags and dropdown choices.

under construction

DEMO on CodePen

Usage

Make sure you've imported all necessary Shoelace components used by sl-tags-input:

import "@shoelace-style/shoelace/dist/components/dropdown/dropdown"
import "@shoelace-style/shoelace/dist/components/icon/icon"
import "@shoelace-style/shoelace/dist/components/icon-button/icon-button"
import "@shoelace-style/shoelace/dist/components/input/input"
import "@shoelace-style/shoelace/dist/components/menu/menu"
import "@shoelace-style/shoelace/dist/components/menu-item/menu-item"
import "@shoelace-style/shoelace/dist/components/tag/tag"

Then import sl-tags-input:

import "sl-tags-input"

In your HTML, add the component marktup with its necessary slot dependencies:

<sl-tags-input>
  <sl-input slot="input" placeholder="Start typing…"></sl-input>
  <sl-menu slot="menu">
  </sl-menu>
</sl-tags-input>

To populate the dropdown menu based on user input, listen for the sl-tags-search-input event (debouced so it only fires a few times a second). Then use the updateMenu method with value/label keys:

document.querySelector("sl-tags-input").addEventListener("sl-tags-search-input", (event) => {
  const value = event.detail.value // what the user typed so far
  // examples:
  event.target.updateMenu([
    {value: "item1", label: event.detail.value + (Math.random() * 300).toFixed()},
    {value: "item2", label: event.detail.value.toUpperCase()}
  ])
})

You can listen for the sl-tags-update event every time a tag is added or cleared.

Finally, the tags method is available on the element directly for retrieving the current list of tags. Use that for saving as JSON:

// get an array of value/label objects:
const tagsJSON = JSON.stringify(document.querySelector("sl-tags-input").tags)
// or just an array of values:
const valuesJSON = JSON.stringify(document.querySelector("sl-tags-input").tags.map(item => item.value))

You can also include a JSON string of tag objcts in the tags attribute of sl-tags-input to populate the component with tags on startup:

<sl-tags-input tags='[{"label": "Tag Here", "value": "tag-123"}]'>...</sl-tags-input>

License

MIT