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

s-typeahead

v2.2.1

Published

Native v1 web typeahead component

Downloads

45

Readme

s-typeahead

This is a native v1 web component which provides a traditional typeahead or autocomplete.

Use

<s-typeahead options="options"></s-typeahead>

or

// Use with  list
let component = document.querySelector('s-typeahead');
let states = [
  {name: "Alabama", abbreviation: 'AL'},
  {name: "Alaska", abbreviation: 'AK'},
]; //
component.options = {
  list: states,
  propertyInObjectArrayToUse: 'name',
  requireSelectionFromList: true
};
// Use with a function to get data. The function must be return a promise, and options
// must be set using a property.
let component = document.querySelector('s-typeahead');
component.options = {
  propertyInObjectArrayToUse: 'id',
  requireSelectionFromList: true,
  makeRequest: (val) => {
    let url = '/path?q=val';
    return fetch(url).then(response => response.json());
  }
};

Options

"options" is an object that can have the following properties:

  • uid: (optional) Unique identified for the component. If provided, it is used in selectionChangedEvent, providing a way to identify a component when multiple components are used.
  • list: A list of items to use for matching input. The list can be a flat array or an object array.
  • placeholder: (optional) Text to be used as the placeholder for the text input.\
  • initial value: the initial value of the input.
  • propertyInObjectArrayToUse: If the list is an object array, the name of the property within the array to use for searching.
  • requireSelectionFromList: Whether to force the user to select one of the choices in the list.
  • makeRequest: function returning a promise to fetch list. If list is provided, makeRequest is not. makeRequest must return a promise with a JSON response of either a flat array or an object array, and propertyInObjectArrayToUse must be provided if an object array is returned.
{
  searchParam: 'query', // name of parameter uses for searching for matches
  otherParams: {foo: 'bar', bar: 'baz'}
}

Custom CSS Properties

The component exposes the follow css custom properties:

  • bold-color: The color of the bold element placed around matching text in the dropdown. Defaults to blue.
  • border: The border style used by the component. Default is: 1px solid #ddd.
  • dropdown-padding: The padding used for the dropdown list items. Defaults to 10px.
  • dropdown-text-color: The text color of the dropdown items. Defaults to #555.
  • font-family: The font family to be used. Defaults to arial.
  • font-size: The font size used in the component. Defaults to 20px;
  • highlight: The background-color for selected items in the dropdown.
  • hover: The background-color used when hovering over dropdown items.
  • input-padding: The padding used in the input element. Defaults to 10px;
  • input-text-color: The color to use for the input element text. Defaults to #444;
  • radius: The radius to be used for the top right and left of the input element. Defaults to 3px.

Events

Fires a selectionChangedEvent when the selection changes. If options.uid is provided, the event signature and detail include it:

// assuming options.uid = 'foo';
document.addEventListener(`foo:selectionChangedEvent${options.uid}`, (evt) => {
  console.log("evt", evt)
  let uid = evt.detail.uid;
  let value = evt.detail.value;
})

Installation

In Frontier apps, use bower. Otherwise you can use npm. When installed, the component adds the v1 polyfills to the dist directory so you don't have to figure out how to get those loaded in Frontier since the polyfills do not currently support bower.

Demo, Development & Testing

To see a demo of the component and to build it, run npm run dev. This will build the assets and run a development server. Access it at http://localhost:8000.

To run the tests, run npm run karma.