Profile
Back to NewsBack
GitHub Trending 12 min
Reader Mode
alphagov/accessible-autocomplete: An autocomplete component, built to be accessible.

alphagov/accessible-autocomplete: An autocomplete component, built to be accessible.

15 hours ago

Accessible autocomplete

The accessible autocomplete is a component that helps users choose answers from a list you provide. You can also use it to make the answers you get from users more consistent.

If you're asking users to provide their country or territory, the govuk-country-and-territory-autocomplete might be more appropriate.

npm version</a> JavaScript Style Guide</a> gzip size</a>

Sauce Labs Build Status</a>

accessible-autocomplete is a JavaScript autocomplete built from the ground up to be accessible. The design goals are:

Try out the examples!

Support

The GOV.UK Design System team maintains the accessible autocomplete as a standalone component. However, we’re only able to put in minimal work to support it.

Read about our plans to maintain this component.

Read more about the types of support we can provide.


Installation / usage

Using npm and a module system

Install it by running:

npm install --save accessible-autocomplete

The accessibleAutocomplete function will render an autocomplete and its accompanying suggestions and aria-live region. Your page should provide a 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 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:

  • Place an autocomplete input field after the specified 's id
  • Default the autocomplete name attribute to '' to prevent it being included in form submissions
  • Default the autocomplete source to use existing
  • Set the
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 for accessibleAutocomplete. If your use case doesn't fit the above defaults, try reading the source and seeing if you can write your own.

Null options

If your