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

react-hook-combobox

v0.1.0

Published

Turn any text field and list into a WAI-ARIA compliant combobox

Downloads

8

Readme

React Hook Combobox

Turn any text field and list into a WAI-ARIA compliant combobox

A combobox is a widget made up of the combination of two distinct elements: 1) a single-line textbox, and 2) an associated pop-up element for helping users set the value of the textbox. – w3.org

React Hook Combobox is built to the WAI-ARIA specification for comboboxes. It follows a bring-your-own principle, which gives consumers full ownership of:

  • markup: render any UI you want
  • data: use any data structure you want
  • filter: use any function to filter your data

This approach offers ultimate flexibility with a tiny footprint ({TODO}kB).

Quickstart

Install:

npm i react-hook-combobox

Usage:

import React from "react";
import useCombobox from "react-hook-combobox";

const items = ["London", "Paris", "Rome"];

const Example = () => {
  const { primitives, value, activeIndex, isOpen } = useCombobox({
    name: "city",
    optionToString: index => items[index],
    onChange: value => console.log(value)
  });

  return (
    <div {...primitives.container}>
      <label {...primitives.label}>City</label>
      <input type="text" {...primitives.input} />

      {isOpen && (
        <ul {...primitives.listbox}>
          {items
            .filter(item => item.startsWith(value))
            .map((item, index) => (
              <li
                {...primitives.listboxOption(index)}
                key={index}
                style={index === activeIndex ? { background: "#D8E2EB" } : {}}
              >
                {item}
              </li>
            ))}
        </ul>
      )}
    </div>
  );
};

API

The useCombobox hook accepts a single argument containing the fields:

{
  name,
  optionToString,
  onChange,
  initialValue
}

name

string, required

A unique to the page identifier.

optionToString

function(index), required

Function that returns the display name of the selected listbox option.

onChange

function(value, index), required

Function called when the selected listbox option changes.

The index argument is undefined when the input primitive looses focus before a listbox option is selected. The value the user typed becomes the value of the input primitive.

initialValue

string, optional

Initial value of the input primitive.

The useCombobox hook returns an object containing the fields:

{
  primitives,
  value,
  activeIndex,
  isOpen,
  handleOpen,
  handleClear
}

primitives

object, { container, label, input, listbox, listboxOption }

Object containing primitives used to build a combobox:

  • container, object Top level primitive that contains or owns the other primitives.
  • label, object Primitive used to label the combobox.
  • input, object Primitive that serves as the textbox.
  • listbox, object Menu primitive allowing users to choose an option.
  • listboxOption, function(index) Primitive for a single option rendered in the menu.

value

string, defaults to an empty string or the value passed to initialValue

Value of the input primitive.

activeIndex

number, defaults to -1

Index of the active listbox option.

isOpen

boolean, defaults to false

Reflects whether the listbox menu primitive should be visible or not.

handleOpen

function

Function to programmatically set isOpen to true.

handleClear

function

Function to clear the textbox value.

Homage

React Hook Combobox started as a gist to explore patterns for building a) flexible and b) accessible comboboxes.

Three-quarters of the way into the implementation a friend showed me Downshift, which achieves both a, b and more. However I couldn't stop returning to the remaining quarter and found myself finishing it!

Whilst Downshift offers way more features I still think React Hook Search is useful to those wanting:

  • a simpler API (less can sometimes be more)
  • a smaller bundle size ({TODO}kB vs 40.8kB)