Our npm namespace has changed. Developers should update their projects to continue accessing our components.

Search autocomplete

Use autocomplete with search to give users suggestions, making searching faster and easier.

  • Content:
    @use 'sass:math';
    @use '@ongov/ontario-design-system-global-styles/dist/styles/scss/1-variables/global.variables' as globalVariables;
    @use '@ongov/ontario-design-system-global-styles/dist/styles/scss/1-variables/spacing.variables' as spacing;
    @use '@ongov/ontario-design-system-global-styles/dist/styles/scss/1-variables/colours.variables' as colours;
    @use '@ongov/ontario-design-system-global-styles/dist/styles/scss/1-variables/breakpoints.variables' as breakpoints;
    @use '@ongov/ontario-design-system-global-styles/dist/styles/scss/1-variables/z-index.variables' as zIndex;
    
    $ontario-search-autosuggest-box-shadow: #0000001a;
    
    .ontario-search-autocomplete__suggestion-list {
    	background-color: colours.$ontario-colour-white;
    	border-radius: 0 0 globalVariables.$global-radius globalVariables.$global-radius;
    	border-top: 2px solid $ontario-search-autosuggest-box-shadow;
    	box-shadow: 0px 3px 8px 1px $ontario-search-autosuggest-box-shadow;
    	clip-path: inset(0px -8px -8px -8px);
    	display: none;
    	height: auto;
    	margin: (-0.75 * spacing.$spacing-1) spacing.$spacing-7 spacing.$spacing-7 (0.5 * spacing.$spacing-1);
    	overflow: hidden;
    	position: absolute;
    	width: calc(100% - spacing.$spacing-1);
    	z-index: zIndex.$ontario-z-index-above-high;
    
    	li {
    		list-style-type: none;
    		padding: spacing.$spacing-2 spacing.$spacing-4;
    
    		&:active {
    			box-shadow: 0 0 0 4px colours.$ontario-colour-focus inset;
    		}
    
    		&:last-child {
    			padding-bottom: spacing.$spacing-2;
    		}
    	}
    }
    
    .ontario-search-autocomplete__suggestion-list--selected {
    	background-color: colours.$ontario-colour-button-tertiary--hover;
    }
    
    .ontario-search-autocomplete__suggestion-list__list-item:focus {
    	outline: none;
    }
    
    .ontario-search__overlay {
    	position: fixed;
    	top: 0;
    	left: 0;
    	width: 100%;
    	height: 100%;
    	background-color: rgba(colours.$ontario-colour-white, 0.95);
    	z-index: zIndex.$ontario-z-index-above-medium;
    }
    
    .ontario-search__overlay-open {
    	.ontario-search__input-container,
    	.ontario-search-autocomplete__suggestion-list {
    		z-index: zIndex.$ontario-z-index-above-high;
    		position: absolute;
    		width: 100%;
    	}
    
    	.ontario-search__input-container {
    		top: spacing.$spacing-4;
    	}
    
    	.ontario-search-autocomplete__suggestion-list {
    		top: 4.06rem; /* Position below the search input box */
    		left: 0;
    		width: calc(100% - spacing.$spacing-6);
    	}
    }
    
    .ontario-search__overlay-open.ontario-search__input-suggestion-container {
    	position: fixed;
    	width: 100%;
    	top: 0;
    	z-index: zIndex.$ontario-z-index-above-high;
    }
    
    .ontario-search-autocomplete__suggestion-list-open {
    	.ontario-search__submit {
    		z-index: zIndex.$ontario-z-index-above-high;
    		border-radius: 0 globalVariables.$global-radius 0 0;
    		bottom: 0.0625rem;
    	}
    }
    
    .ontario__visually-hidden {
    	position: absolute;
    	width: 1px;
    	height: 1px;
    	margin: -1px;
    	padding: 0;
    	border: 0;
    	clip: rect(0, 0, 0, 0);
    	overflow: hidden;
    }
    
  • URL: /components/raw/search-autocomplete/search-autocomplete.scss
  • Filesystem Path: fractal/components/components/search-autocomplete/search-autocomplete.scss
  • Size: 2.9 KB
  • Content:
    .ontario-search-autocomplete__suggestion-list{background-color:#fff;border-radius:0 0 4px 4px;border-top:2px solid #0000001a;box-shadow:0px 3px 8px 1px #0000001a;-webkit-clip-path:inset(0px -8px -8px -8px);clip-path:inset(0px -8px -8px -8px);display:none;height:auto;margin:-0.1875rem 2.5rem 2.5rem .125rem;overflow:hidden;position:absolute;width:calc(100% - .25rem);z-index:4}.ontario-search-autocomplete__suggestion-list li{list-style-type:none;padding:.5rem 1rem}.ontario-search-autocomplete__suggestion-list li:active{box-shadow:0 0 0 4px #009adb inset}.ontario-search-autocomplete__suggestion-list li:last-child{padding-bottom:.5rem}.ontario-search-autocomplete__suggestion-list--selected{background-color:#e8e8e8}.ontario-search-autocomplete__suggestion-list__list-item:focus{outline:none}.ontario-search__overlay{position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(255,255,255,.95);z-index:2}.ontario-search__overlay-open .ontario-search__input-container,.ontario-search__overlay-open .ontario-search-autocomplete__suggestion-list{z-index:4;position:absolute;width:100%}.ontario-search__overlay-open .ontario-search__input-container{top:1rem}.ontario-search__overlay-open .ontario-search-autocomplete__suggestion-list{top:4.06rem;left:0;width:calc(100% - 2rem)}.ontario-search__overlay-open.ontario-search__input-suggestion-container{position:fixed;width:100%;top:0;z-index:4}.ontario-search-autocomplete__suggestion-list-open .ontario-search__submit{z-index:4;border-radius:0 4px 0 0;bottom:.0625rem}.ontario__visually-hidden{position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;clip:rect(0, 0, 0, 0);overflow:hidden}
  • URL: /components/raw/search-autocomplete/search-autocomplete.css
  • Filesystem Path: fractal/components/components/search-autocomplete/search-autocomplete.css
  • Size: 1.7 KB

Guiding principle: Give users control.


When to use this component

Search autocomplete shows the user search suggestions based on the characters they are typing in a search field.

This allows the user to browse search suggestions and gives them an overview of the website’s content.

This is also known as search autosuggest, typeahead, predictive search, search-as-you-type and query suggestions.

Successful implementation of search autocomplete can lead to:

  • a reduction in the user’s mental load while typing
  • fewer spelling or input errors
  • better navigational accuracy

When to suggest

A list of search suggestions should appear when a user enters a valid character in the search input field.

Valid characters are defined depending on the application’s search parameters. The suggestions should appear or change as the user adds or removes characters.

If no search suggestions match the user’s input, then hide the menu list and let the user manually complete their entry.


Component features

The search autocomplete component includes:

A. divider separating the suggestion menu list from the search box
B. hover and active press states for each search suggestion
C. search suggestion with bolded text that completes the suggestion
D. menu list with up to 6 search suggestions
E. drop shadow contrasting the suggestion menu list with the page background

Example


A diagram of the features that make up the search autocomplete, including the divider, hover state, menu list, and drop shadow on the menu list.

This component does not currently include previous search history or a categorised scoped search (for example “[search term] in [category]”).


Search menu interaction states

Active typing

Search suggestions should appear once the user enters a valid character. The suggestions given will depend on:

  • what the user types
  • how many matches there are in the site’s content

Hover and selection

Users should be able to hover through the list of search suggestions with their keyboard or mouse on desktop. They can choose a suggestion by either clicking on it, or by pressing enter when hovered over it. Choosing a suggestion then prompts the search to run, bringing the user to the search results page for the chosen term.

Example - Hover state


A diagram that shows three states of the search autocomplete: the hover state, the active selection state of a search suggestion and the submitted search state.

Example - Active select state


A diagram that shows three states of the search autocomplete: the hover state, the active selection state of a search suggestion and the submitted search state.

Example - Selected and submitted search


A diagram that shows three states of the search autocomplete: the hover state, the active selection state of a search suggestion and the submitted search state.

The full menu list should always appear above the fold and should be in the centre of focus.

Best practices

Do

  • include up to 4 to 6 search suggestions
  • show suggestions as soon as the user enters the first valid character
  • suggest terms or phrases that are short, and easy to read and scan
  • bold the portion of the suggestion that completes the user’s entered text
  • highlight the selected search suggestion with an active state
  • use a white page background to help show the contrast of the menu list
  • for header-based search, add a darkened page overlay to the menu list to increase its visibility

Don’t

  • bold the portion of text in the suggestions that matches what the user has typed
  • use scrollbars in the suggestion menu list
  • add extra visual elements like icons, padding, separators or alternating row colours
  • exceed 6 suggestions — users will ignore the suggestions and it will slow down the search process

Do this


A diagram showing one example of how to lay out the search autocomplete in a mobile responsive size and two examples showing what not to do including using a scroll bar in the suggestion menu list and bolding the search suggestions.

Don't do this


A diagram showing one example of how to lay out the search autocomplete in a mobile responsive size and two examples showing what not to do including using a scroll bar in the suggestion menu list and bolding the search suggestions.

Don't do this


A diagram showing one example of how to lay out the search autocomplete in a mobile responsive size and two examples showing what not to do including using a scroll bar in the suggestion menu list and bolding the search suggestions.

Technical specifications

Usage

To use the search autocomplete component in your project, follow these steps:

  1. Download the ontario-search.js file from the repository.
  2. Import the ontario-search.js file into your project using a <script> tag (for example <script src="path/to/ontario-search.js"></script>)
  3. Declare your custom suggestion list as an array within the customSuggestions variable in the JavaScript file.
  4. Call the function addSuggestions and add the customSuggestions as an argument to the function. Adding the addSuggestions(customSuggestions) function call within the load function ensures that the custom suggestions load properly into the autocomplete component when the page fully loads.

Example

The example shows one way to implement the component. You can replace the example with your own code.

 window.addEventListener('load', () => {
       const customSuggestions = [
    "Suggestion 1",
           "Suggestion 2",
           "Suggestion 3",
       ];
       addSuggestions(customSuggestions);
 });

Accessibility

Users can interact with the autocomplete component in multiple ways:

  • up and down arrow keys move focus between items when the suggestion menu list is expanded
  • pressing enter or clicking the mouse selects the search suggestion, adding it to the search field and completing the search
  • esc key closes the drop-down menu

Focus Management

The search autocomplete component has a built-in focus management system that will highlight suggestions as the user navigates through them with their keyboard (up and down arrows) or mouse (clicking and hovering).

The focus management system responds and adapts to the user’s choice of interaction. This means the user can switch back and forth between using their keyboard or their mouse to choose a suggestion.

Attributes

In HTML, the role of a Searchbox is often designated as a combobox for accessibility purposes because it combines features of both a text input field and a dropdown list. A combobox allows users to interact with it using both keyboard and mouse. This provides flexibility for different interaction preferences and accessibility needs.

Aria-Controls

Use aria-controls to establish a connection between an input field and the element it influences or owns. This attribute helps assistive technologies understand the relationship between the input field and its associated content.

Aria-autocomplete

The aria-autocomplete property is crucial for defining the behaviour of the search autocomplete feature. For search autocomplete, use the list model (aria-autocomplete=”list”).

This presents a collection of possible values in a separate element that appears below the search input field, helping users choose a suggestion.

Autocomplete

Set the autocomplete attribute to false to make sure that the browser’s autocomplete functionality does not interfere with the design system autocomplete component.

Aria-haspopup

Include aria-haspopup=”list” to show that the search input field triggers a list with suggestions.

Aria-expanded

Utilise the aria-expanded state on the search input field to communicate the display status of the suggestion menu list.

Example

<input
    type="search"
    name="search"
    id=""
    autocomplete="off"
    aria-autocomplete="list"
    aria-controls="suggestion-list"
    aria-expanded="false"
    role="combobox"
    class="ontario-input ontario-search__input"
/>

The example shows the search input field configured with aria-autocomplete="list" and aria-haspopup="list". This indicates that it provides an autocomplete suggestion list, displayed in a separate popup.

To denote that the input field manages the list of autocomplete suggestions:

  • set aria-controls=”suggestion-list’ on the search input field to show that it controls the suggestion menu list with the ID "suggestion-list"
  • structure the associated popup element as a <ul> containing individual suggestion options, represented as <li> elements
  • give each suggestion <li> element role="option" to indicate its purpose as an option within the list

In addition, set aria-expanded="false" on the search input field to show that the suggestion menu list is currently hidden or collapsed. When the suggestion menu list is expanded, aria-expanded should dynamically change to "true".

If you have any questions or feedback, please get in touch.