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

@loicblascos/autocomplete

v1.0.2

Published

A flexible, accessible, customizable and efficient Autocomplete input control with zero dependencies.

Downloads

6

Readme

AutoComplete

A flexible, accessible, customizable and efficient autocomplete input control with zero dependencies

See loicblascos.github.io/autocomplete for complete docs and demos.

Installation

Install AutoComplete from npm package manager:

npm install @loicblascos/autocomplete --save

Basic Usage

You can import polyfills, style and main class from package as follows:

import '@loicblascos/autocomplete/lib/polyfills'; // Optional
import '@loicblascos/autocomplete/lib/style.css'; // Optional
import AutoComplete from '@loicblascos/autocomplete';

AutoComplete script works with search and text input elements.
Explicit and implicit input labels are supported by AutoComplete script to be fully accessible.

<!-- Explicit label -->
<label for="my-input">Input Label</label>
<input type="text" id="my-input" placeholder="type to search">
<!-- Implicit label -->
<label>
    Input Label
    <input type="text" id="my-input" placeholder="type to search">
</label>

The AutoComplete() constructor accepts two arguments: the input element and an options object.

const input = document.querySelector( '#my-input' );
const acplt = new AutoComplete(
    input,
    {
        source: [
            { label: 'label 1', value: 'value 1' },
            { label: 'label 2', value: 'value 2' },
            { label: 'label 3', value: 'value 3' },
        ],
        minLength: 1,
        maxResults: 50,
        autoSearch: true,
    }
);

Options

{
    // Holds list of suggestions to search for.
    source: [],
    // Minimum number of characters to trigger a search.
    minLength: 1,
    // Maximum number of suggestions to display in the list.
    maxResults: -1,
    // Expression used to match string(s) in suggestion.
    regExp: '($1)',
    // Function used to filter suggestions in the list.
    filterResults: exists => exists,
    // Function used to sort suggestions in the list.
    sortResults: ( a, b ) => (
        a.index - b.index ||
        a.label.localeCompare( b.label ) ||
        a.label.length - b.label.length
    ),
    // Element used to append matched string(s).
    highlighter: document.createElement( 'mark' ),
    // Function used to render suggestion in list item.
    renderItem: item => item.content,
    // Element used to append autocomplete menu.
    menuTarget: document.body,
    // Only match accents from searched string.
    smartAccent: false,
    // Highlight all matches string in suggestion.
    matchAll: false,
    // Automatically focus first suggestion in the list.
    autoFocus: true,
    // Automatically searches for suggestions on focus.
    autoSearch: false,
    // Inline auto complete first suggestion while typing.
    autoComplete: false,
    // Display a loader while searching in source (async).
    loader: false,
    // Display a button with cross icon to clear the field.
    clearable: true,
    // Accessible button label used in clear button.
    clearLabel: 'Clear field',
    // Class names used in AutoComplete elements.
    classes: {
        wrapper: 'acplt',
        menu: 'acplt-menu',
        list: 'acplt-list',
        item: 'acplt-item',
        clear: 'acplt-clear',
        loader: 'acplt-loader',
        message: 'acplt-message',
    },
    // Accessible texts appended during user interactions.
    messages: {
        open: 'Use Up and Down to choose suggestions and press Enter to select suggestion.',
        input: 'Type to search or press Escape to clear the input.',
        clear: 'Field cleared.',
        select: '%s suggestion was selected.',
        noResults: 'No suggestions found.',
        loading: 'Loading suggestions...',
    },
}

License

AutoComplete is released under the MIT License. See LICENSE file for details.