TypeaheadSearch
TypeaheadSearch is a search input that provides a menu of options based on the current search query. It is meant to help users search for and navigate to content.
Guidelines
When to use typeahead searches
TypeaheadSearch consists of a search input and a submit button. As users type, predictive search results are displayed in a menu. Additionally, there's a final option at the end of the search results list, enabling users to navigate to the search page.
Use the TypeaheadSearch component when you need a predictive list of options in a menu that updates as users type within the input field. Avoid using TypeaheadSearch if your goal is to enable users to perform text-based searches to find specific content, such as locating results on a page. In this case use the SearchInput component instead.
Specifications
TypeaheadSearch may include the following elements:
- Input
A search input where users can type their searches. The input features the 'search' icon for clarity and should also include a placeholder to clarify its purpose. - Button (optional)
The input can be accompanied with a button, in order to trigger the search action. It's crucial not to customize the label of the search button: consistently employ the term "Search" or its appropriate translation. Additionally, avoid using long text within this button. - Menu
When the user types within the input, a menu component with results is displayed. By default, menu items will feature a label and an optional description. Thumbnails can also be included. - Footer
The final menu item at the end of the search results menu list allows users to navigate to the search page.
Component limitations
The menu displayed will show a maximum of 10 items, adjusting if there are fewer matching results.
Menu labels will expand to fit text length, possibly wrapping to multiple lines, while descriptions will use an ellipsis if exceeding one line.
The fixed menu footer may also wrap to multiple lines for lengthy text.
Refer to the TypeaheadSearch component in Codex Figma.
Types
As with SearchInput, TypeaheadSearch can be categorized based on the visibility and type of button it contains:
- Without button
The TypeaheadSearch can consist of the input field alone or include the decorative icon. In this scenario, using the icon is suggested to emphasize that the input serves as a search input, distinguishing it from a simple text input. - With text button
The TypeaheadSearch also has the option to feature a text button to initiate the search process.
Interaction states
TypeaheadSearch has the following visually separate states:
- Default
- Hover
- Focus
- Loading
- Active with results
- Active with no results
Best practices
Consider the following recommendations when using the TypeaheadSearch.
- Use the 'search' icon to emphasize search functionality.
- Customize the placeholder text as needed.
- Ensure the copy is concise and includes the term "Search" or an appropriate translation.
- Customize or remove the 'search' icon.
- Customize the copy of the search button or make it lengthy.
Keyboard navigation
Key | Function |
---|---|
Down arrow / Up arrow | When the menu is displayed, it navigates through the menu items. |
Enter | If the focus is placed in any of the options within the menu, the focused option is selected. |
Esc | This key closes the menu when it is open. |
Demos
Search Wikipedia articles
This implementation of TypeaheadSearch fetches articles from English Wikipedia. Note that the input expands on focus via the autoExpandWidth
prop, thumbnails are enabled via the showThumbnail
prop, and the "Search" button is added via the useButton
prop. Open the console to see emitted events.
Name | Value |
---|---|
View | |
Reading direction |
Search Wikidata items
In this example, results are fetched from Wikidata. Thumbnails are disabled, and the input doesn't expand on focus. There is no button, because the useButton
prop is set to false
. Open the console to see emitted events.
With initial input value
The initialInputValue
prop can be used to pass in the initial value of the TextInput. This is useful when replacing a server-rendered UI where the user may have started typing a search query, or for pre-populating the search term when a user navigates back to a page where they had previously entered one.
On mount, TypeaheadSearch will fetch search results for the initial input value if it's provided. After that, the input value is tracked internally and will be emitted up to the parent component when the value changes.
Pending state
Pending state indicators, including an inline progress bar and a message stating that results are pending, can be displayed to users with slower connections while search results are being fetched. To enable this, provide content in the search-results-pending
slot.
The pending state indicators will display when a search takes longer than half a second, so you may need to throttle your connection to see them in the demo below.
No results message
A message prompt that no search results were found. To enable this, provide content in the search-no-results-text
slot.
Vue usage
TypeaheadSearch contains a form with a text input, a submit button, and a slot for hidden inputs. The parent component must listen for changes in the search query (which are debounced by default), fetch or calculate search results, then provide them as an array of search results for display to the user in a dropdown menu.
At the end of the list of search results, a final option to go to the search page for the current search query is provided.
Events are emitted to the parent when a search result is selected and when the form is submitted, with data about the selected item (e.g. for analytics).
TextInput props apply
This component contains a TextInput component. You can bind TextInput props to this component and they will be passed to the TextInput within.
Attributes passed to input
This component will pass any HTML attributes applied to it, except for CSS class, to the <input>
element within the component.
Props
Prop name | Description | Type | Default |
---|---|---|---|
id (required) | ID attribute for the form. | string | |
formAction (required) | Action attribute for form. | string | |
searchResults (required) | List of search results. See the SearchResult type. | SearchResult[] | |
useButton | Whether to display a submit button. | boolean | false |
buttonLabel | Custom label for the submit button. Omit this prop to use the default value, "Search". | string | '' |
initialInputValue | Initial value for the text input. Triggers an initial input event on mount. | string | '' |
searchFooterUrl | Link for the final menu item. This will typically be a link to the search page for the current search query. | string | '' |
debounceInterval | Time interval for debouncing input events, in ms. | number | DebounceInterval |
highlightQuery | Whether the search query should be highlighted within a search result's title. | boolean | false |
showThumbnail | Whether to show search results' thumbnails (or a placeholder icon). | boolean | false |
autoExpandWidth | Contract the width of the input when unfocused and expand the width of the input when focused to accommodate the extra width of the thumbnails. This prop is ignored if showThumbnail is false. | boolean | false |
visibleItemLimit | Limit the number of menu items to display before scrolling. Setting this prop to anything falsy will show all menu items. By default, all menu items are shown. | number|null | null |
Methods
Method name | Description | Signature |
---|---|---|
focus | Focus the component's input element. | Returns: void |
Events
Event name | Properties | Description |
---|---|---|
load-more | When the user scrolls towards the bottom of the menu. If it is possible to add or load more menu items, then now would be a good moment so that the user can experience infinite scrolling. | |
input | value string - The new input value | When the text input value changes. Debounced by default. |
search-result-click | event SearchResultClickEvent - Data for the selected result | When a search result is selected. |
submit | event SearchResultClickEvent - Data for the selected result | When the form is submitted. |
Slots
Name | Description | Bindings |
---|---|---|
search-results-pending | A slot for a translated "Loading search results" message. | |
search-no-results-text | A slot for passing in a translated "no results" message. | |
search-footer-text | A slot for passing in translated search footer text. | search-query string - Input text entered by the user |
default | A slot for passing hidden inputs, i.e. |
CSS-only version
Markup structure
The CSS-only version of TypeaheadSearch is simply a SearchInput component styled to look like the Vue version of TypeaheadSearch. It will have no menu of results and is meant to be replaced by the Vue component once JavaScript has loaded. If you just need a SearchInput, check out the CSS-only version of the SearchInput component. Note that the search icon is automatically added for you.
With thumbnails
When your CSS-only TypeaheadSearch component will be swapped out for a Vue version that shows thumbnails (see the "With initial input value" demo above), apply the .cdx-typeahead-search--show-thumbnail
class to the wrapper <div>
to expand the start icon width.
With thumbnails and auto-expand width
When your CSS-only TypeaheadSearch component will be swapped out for a Vue version that shows thumbnails and expands when results are shown (see the "Search Wikipedia articles" demo above), apply the .cdx-typeahead-search--show-thumbnail
and .cdx-typeahead-search--auto-expand-width
classes to the wrapper <div>
to reduce the starting size of the input.