
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
accessible-autocomplete
Advanced tools
accessible-autocomplete
is a JavaScript autocomplete built from the ground up to be accessible. The design goals are:
Install it by running:
npm install --save accessible-autocomplete
The accessibleAutocomplete
function will render an autocomplete <input>
and its accompanying suggestions and aria-live
region. Your page should provide a <label>
and a container element:
<label for="my-autocomplete">Select your country</label>
<div id="my-autocomplete-container"></div>
Then import it using a module system like Browserify / Webpack / Rollup, and call the accessibleAutocomplete
function, providing an array of values:
import accessibleAutocomplete from 'accessible-autocomplete'
const countries = [
'France',
'Germany',
'United Kingdom'
]
accessibleAutocomplete({
element: document.querySelector('#my-autocomplete-container'),
id: 'my-autocomplete', // To match it to the existing <label>.
source: countries
})
If you want to use it as a replacement for a <select>
element, read the Progressive enhancement section.
You can copy the dist/accessible-autocomplete.min.js file to your JavaScript folder and import it into the browser:
<script type="text/javascript" src="assets/js/accessible-autocomplete.min.js"></script>
A stylesheet is included with the package at dist/accessible-autocomplete.min.css.
You can copy it to your stylesheets folder and import it into the browser:
<link rel="stylesheet" href="assets/css/accessible-autocomplete.min.css" />
You can also import it using Sass:
@import "accessible-autocomplete";
If you already use Preact in your application, you can import a bundle that will use that:
import preact from 'preact'
import Autocomplete from 'accessible-autocomplete/preact'
preact.render(
<Autocomplete id='autocomplete' source={suggest} />,
document.querySelector('#container')
)
If you already use React in your application, you can import a bundle that will use that:
import React from 'react'
import ReactDOM from 'react-dom'
import Autocomplete from 'accessible-autocomplete/react'
ReactDOM.render(
<Autocomplete id='autocomplete' source={suggest} />,
document.querySelector('#container')
)
element
Type: HTMLElement
The container element in which the autocomplete will be rendered in.
id
Type: string
The id
to assign to the autocomplete input field, to use with a <label for=id>
. Not required if using enhanceSelectElement
.
source
Type: Array | Function
An array of values to search when the user types in the input field, or a function to take what the user types and call a callback function with the results to be displayed.
An example of an array of values:
const countries = [
'France',
'Germany',
'United Kingdom'
]
If source
is a function, the arguments are: query: string, populateResults: Function
Similar to the source
argument for typeahead.js, a backing data source for suggestions. query
is what gets typed into the input field, which will callback to populateResults
synchronously with the array of string results to display in the menu.
An example of a simple suggestion engine:
function suggest (query, populateResults) {
const results = [
'France',
'Germany',
'United Kingdom'
]
const filteredResults = results.filter(result => result.indexOf(query) !== -1)
populateResults(filteredResults)
}
autoselect
(default: false
)Type: Boolean
Set to true to highlight the first option when the user types in something and receives results. Pressing enter will select it.
confirmOnBlur
(default: true
)Type: Boolean
The autocomplete will confirm the currently selected option when the user clicks outside of the component. Set to false
to disable.
cssNamespace
(default: 'autocomplete'
)Type: string
Use this property to override the BEM block name that the JavaScript component will use. You will need to rewrite the CSS class names to use your specified block name.
defaultValue
(default: ''
)Type: string
Specify a string to prefill the autocomplete with.
displayMenu
(default: 'inline'
)Type: 'inline' | 'overlay'
You can set this property to specify the way the menu should appear, whether inline or as an overlay.
minLength
(default: 0
)Type: number
The minimum number of characters that should be entered before the autocomplete will attempt to suggest options. When the query length is under this, the aria status region will also provide helpful text to the user informing them they should type in more.
name
(default: 'input-autocomplete'
)Type: string
The name
for the autocomplete input field, to use with a parent <form>
.
onConfirm
(default: () => {}
)Type: Function
Arguments: confirmed: Object
This function will be called when the user confirms an option, with the option they've confirmed.
placeholder
(default: ''
) :warning: not recommended :warning:Type: string
This option will populate the placeholder
attribute on the input element.
We think placeholders have usability issues and that there are better alternatives to input placeholder text, so we do not recommend using this option.
required
(default: false
)Type: Boolean
The input field will be rendered with a required
attribute, see W3C required
attribute definition.
showNoResultsFound
(default: true
)Type: Boolean
The autocomplete will display a "No results found" template when there are no results. Set to false
to disable.
templates
(default: undefined
)Type:
{
inputValue: Function,
suggestion: Function
}
This object defines templates (functions) that are used for displaying parts of the autocomplete.
inputValue
is a function that receives one argument, the currently selected suggestion. It returns the string value to be inserted into the input.
suggestion
is a function that receives one argument, a suggestion to be displayed. It is used when rendering suggestions, and should return a string, which can contain HTML. :warning: Caution: because this function allows you to output arbitrary HTML, you should make sure it's trusted, and accessible.
If your autocomplete is meant to select from a small list of options (a few hundred), we strongly suggest that you render a <select>
menu on the server, and use progressive enhancement.
If you have the following HTML:
<select id="location-picker">
<option value="fr">France</option>
<option value="de">Germany</option>
<option value="gb">United Kingdom</option>
</select>
You can use the accessibleAutocomplete.enhanceSelectElement
function to enhance it into an autocomplete:
accessibleAutocomplete.enhanceSelectElement({
selectElement: document.querySelector('#location-picker')
})
This will:
<select>
autoselect
to true
defaultValue
to the select's option[selected]
id
to the <select>
's id
name
attribute to ''
to prevent it being included in form submissionssource
to use existing <option>
s from the <select>
<select>
using inline display: none
<select>
's id
to ${id}-select
to decouple from any <label>
<select>
This function takes the same options as accessibleAutocomplete
, with the only difference being that it uses selectElement
instead of element
, which needs to be an instance of HTMLSelectElement
.
Note: The
accessibleAutocomplete.enhanceSelectElement
function is fairly light and wraps the public API foraccessibleAutocomplete
. If your use case doesn't fit the above defaults, try reading the source and seeing if you can write your own.
If your <select>
element has a "null" option - a default option with no value - then you can pass a defaultValue
option to enhanceSelectElement
which will replace the label of this option when it is selected.
With the following HTML:
<select id="location-picker">
<option value="">Select a country</option>
<option value="fr">France</option>
<option value="de">Germany</option>
<option value="gb">United Kingdom</option>
</select>
Then passing a defaultValue
option of ''
will then leave the autocomplete blank if the null option is selected.
accessibleAutocomplete.enhanceSelectElement({
defaultValue: '',
selectElement: document.querySelector('#location-picker')
})
Any null options will also be filtered out of the options used to populate the source
of the autocomplete element. To preserve options with no value in the autcomplete then pass a preserveNullOptions
flag of true
to enhanceSelectElement
.
The following events get triggered on the input element during the life cycle of the autocomplete:
onConfirm
- This function will be called when the user confirms an option, with the option they've chosen.Example usage:
accessibleAutocomplete({
// additional options
onConfirm: (val) => {
track(val)
}
})
accessible-autocomplete
was built after studying many existing solutions and prototyping patches to fix user experience or accessibility issues. It draws heavy inspiration from the following (and a lot of others):
Check out the CONTRIBUTING guide for instructions.
If you want to help and want to get more familiar with the codebase, try starting with the "good for beginners" issues.
MIT.
1.3.1 - 2017-06-08
autoselect: false
not working when using enhanceSelectElement
.FAQs
An autocomplete component, built to be accessible.
The npm package accessible-autocomplete receives a total of 35,059 weekly downloads. As such, accessible-autocomplete popularity was classified as popular.
We found that accessible-autocomplete demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.