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

select-pure

v2.1.4

Published

Custom JavaScript <select> component. Easy-to-use, accessible, mobile friendly and super efficient

Downloads

3,011

Readme

Custom JavaScript <select> component. Easy-to-use, accessible, mobile friendly and super efficient.

npm version gzip size brotli size npm Published on webcomponents.org

Description

SelectPure is a Web Component (Custom Element) which makes it super easy to use and customize. Main goal is to use extended API of the native HTML <select> element and provide additional features, like autocomplete, custom styling and many more. The package itself is stable to be used, however, if you've found any issues, please report them here or create a PR of your own.

If you want to use older version, please refer to this README.

Demo

Interactive demo with many examples is available here.

Usage

SelectPure is very easy to use. At first you have to install a package

yarn add select-pure

or

npm i select-pure --save

then include it in your JavaScript file:

import 'select-pure';

Important! If you want to use a minified ES2015 version, include the package as following:

import 'select-pure/dist/index.js';

Use in the similar way as you would use a native HTML <select>.

<select-pure name="country" id="country">
  <option-pure value="" disabled hidden>-- Please select a country --</option-pure>
  <option-pure value="UA">Ukraine</option-pure>
  <option-pure value="PL">Poland</option-pure>
  <option-pure value="DE">Germany</option-pure>
  <option-pure value="US">USA</option-pure>
  <option-pure value="RU" disabled>Russia</option-pure>
</select-pure>

SelectPure instance

In order to call API methods of the SelectPure, subscribe to callbacks or use it's properties, you can simple use querySelector.

const selectPure = document.querySelector("select-pure");

console.log(selectPure.selectedIndex);

selectPure.disable();

Attributes

<select-pure> supports the following attributes: name, id, multiple, default-label and disabled.

<option-pure> supports value, label, disabled, selected and hidden attributes.

Available properties

| property | description | |-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | selectedIndex | Returns index of the selected option. You can also manipulate selected value with changing this property (selectPure.selectedIndex = 2). Just like in the native <select>. | | value | Returns selected value. | | selectedOptions | Returns and array of selected <option>s. Works only in multiple mode. | | values | Returns and array of selected values. Works only in multiple mode. |

Callbacks

If you want to set a callback for when the new value is selected, you can just use the traditional addEventListener.

const selectPure = document.querySelector("select-pure");
selectPure.addEventListener("change", (event) => {
  // You can use
  // event.target.value or
  // event.currentTarget.value
});

API methods

| method | description | |-----------|--------------------| | disable() | Disables select. | | enable() | Enables select. | | open() | Opens a dropdown. | | close() | Closes a dropdown. |

Custom styles

SelectPure offers high level of customisation. You can match any design you want by just providing a simple set of css variables. Below you can find their names and default values that are included in the package.

select-pure {
  --select-height: 44px;
  --select-width: 100%;
  --border-radius: 4px;
  --border-width: 1px;
  --border-color: #000;
  --padding: 0 10px;
  --dropdown-z-index: 2;
  --disabled-background-color: #bdc3c7;
  --disabled-color: #ecf0f1;
  --background-color: #fff;
  --color: #000;
  --hover-background-color: #e3e3e3;
  --hover-color: #000;
  --selected-background-color: #e3e3e3;
  --selected-color: #000;
  --dropdown-gap: 0;
  --font-size: 14px;
  --font-family: inherit;
  --font-weight: 400;
  --select-outline: 2px solid #e3e3e3;
  --dropdown-items: 4;
}

Example of the custom styles.

<select-pure name="country" id="country" class="dark">
  <option-pure value="" disabled hidden>-- Please select a country --</option-pure>
  <option-pure value="UA">Ukraine</option-pure>
  <option-pure value="PL">Poland</option-pure>
  <option-pure value="DE">Germany</option-pure>
  <option-pure value="US">USA</option-pure>
  <option-pure value="RU" disabled>Russia</option-pure>
</select-pure>

<form> support

If you place <select-pure> inside a <form> and specify a name or id attribute, it will then append a hidden input with a given name inside a <form> and trigger change event, when value is selected.

TODO

  • [ ] Autocomplete input
  • [ ] Callback for autocomplete input
  • [ ] Custom matching/filter pattern
  • [ ] Fill README with React/Angular/Vue/Svelte/Purejs examples of usage;
  • [ ] Precommit hooks;
  • [ ] Add destroy method;
  • [ ] Make sure Select behaves correctly when attributes are changed or options are removed from the DOM (MutationObserver);
  • [ ] Sometimes one select scrolls dropdown in the others?
  • [ ] Support keyboard arrows;

2.1

  • [ ] Option groups;
  • [ ] Required attribute support
  • [ ] Contribution guide;
  • [ ] Position of the dropdown;

License

MIT