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

@spectrum-web-components/combobox

v0.42.3

Published

Web component implementation of a Spectrum design Combobox

Downloads

1,114

Readme

Description

An <sp-combobox> allows users to filter lists to only the options matching a query. It's composed of a textfield, a picker button, and child menu items.

Usage

See it on NPM! How big is this package in your project?

yarn add @spectrum-web-components/combobox

Import the side effectful registration of <sp-combobox> via:

import '@spectrum-web-components/combobox/sp-combobox.js';

When looking to leverage the Combobox base class as a type and/or for extension purposes, do so via:

import { Combobox } from '@spectrum-web-components/combobox';

Sizes

<sp-combobox size="s" label="Color">
    <sp-menu-item value="red">Red</sp-menu-item>
    <sp-menu-item value="green">Green</sp-menu-item>
    <sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>
<sp-combobox size="m" label="Color">
    <sp-menu-item value="red">Red</sp-menu-item>
    <sp-menu-item value="green">Green</sp-menu-item>
    <sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>
<sp-combobox size="l" label="Color">
    <sp-menu-item value="red">Red</sp-menu-item>
    <sp-menu-item value="green">Green</sp-menu-item>
    <sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>
<sp-combobox size="xl" label="Color">
    <sp-menu-item value="red">Red</sp-menu-item>
    <sp-menu-item value="green">Green</sp-menu-item>
    <sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>

Labeling

A combobox must be labeled. Typically, you should render a visible label via <sp-field-label>. For exceptional cases, provide an accessible label via the label attribute.

<sp-field-label for="color">Color</sp-field-label>
<sp-combobox id="color">
    <sp-menu-item value="red">Red</sp-menu-item>
    <sp-menu-item value="green">Green</sp-menu-item>
    <sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>

Providing options

Combobox options are presented as a popup menu. Menu items can be provided via markup as <sp-menu-item> children, or by assigning an array to the options property of an <sp-combobox>.

Options property

Instead of providing <sp-menu-item> children, you can assign an array of ComboboxOptions to the options property, and <sp-combobox> will create matching menu items:

<sp-combobox id="color" label="Color"></sp-combobox>

<script>
    document.getElementById('color').options = [
        { value: "red", itemText: "Red" },
        { value: "green", itemText: "Green" },
        { value: "blue", itemText: "Blue" }
    ];
</script>

Dynamic options

When you replace the options Array, or add/remove <sp-menu-item> children, the <sp-combobox> will detect that change and update its popup menu contents. For example, using Lit:

render() {
    return html`<sp-combobox label="Color" .options=${this.colorOptions}></sp-combobox>`;
}

mutate() {
    this.colorOptions = [
        ...this.colorOptions,
        { value: 'purple', itemText: 'Purple' }
    ];
}

Autocomplete

The text in an <sp-combobox> is editable, and the string the user has typed in will become the value of the combobox unless the user selects a different value in the popup menu.

None

autocomplete="none"

The suggested popup menu items will remain the same regardless of the currently-input value. Whenever the currently-typed input exactly matches the value of a popup menu item, that item is automatically selected.

List

autocomplete="list"

The popup menu items are filtered to only those completing the currently-input value.

Focus and Accessibility

The combobox supports both mouse and keyboard navigation. Mobile behavior is currently unspecified.

When an <sp-combobox> is focused, pressing the down arrow moves focus to the first menu item in the popup menu. The up and down arrows then move between available menu items.

The escape key dismisses the popup menu if open. Otherwise, it clears the combobox's textfield.

The enter key sets the value of the focused <sp-combobox>. If the popup menu is open, the value is set to the value of the selected menu item, returning focus back to the combobox's textfield.