Search lets people enter a keyword or phrase and find what they are looking for. It starts as a search bar; focusing it opens the search view, with suggestions or results under the bar, docked on larger screens or full screen on compact ones.
Usage
Focusing the bar opens the view. input fires as the query changes, including when the clear
button or Escape empties it; Enter submits, and a chosen suggestion fills the bar and closes
the view.
import { createSearch } from 'material';
const search = createSearch({
placeholder: 'Search messages',
suggestions: ['Invoices', 'Travel', 'Team lunch'],
});
search.on('input', ({ value }) => runSearch(value));
document.body.append(search.element);
<m-search placeholder="Search messages">
<m-search-suggestion>Invoices</m-search-suggestion>
<m-search-suggestion>Travel</m-search-suggestion>
<m-search-suggestion>Team lunch</m-search-suggestion>
</m-search>
<script type="module">
const search = document.querySelector('m-search');
search.addEventListener('input', (event) => runSearch(event.detail.value));
</script>
import { Search, SearchSuggestion } from 'material/react';
import { runSearch } from './app';
export function Example() {
return (
<Search onInput={(event) => runSearch(event.detail.value)} placeholder="Search messages">
<SearchSuggestion>Invoices</SearchSuggestion>
<SearchSuggestion>Travel</SearchSuggestion>
<SearchSuggestion>Team lunch</SearchSuggestion>
</Search>
);
}
<script setup lang="ts">
import { MSearch, MSearchSuggestion } from 'material/vue';
import { runSearch } from './app';
</script>
<template>
<MSearch @input="runSearch($event.detail.value)" placeholder="Search messages">
<MSearchSuggestion>Invoices</MSearchSuggestion>
<MSearchSuggestion>Travel</MSearchSuggestion>
<MSearchSuggestion>Team lunch</MSearchSuggestion>
</MSearch>
</template>
<script lang="ts">
import { Search, SearchSuggestion } from 'material/svelte';
import { runSearch } from './app';
</script>
<Search oninput={(event) => runSearch(event.detail.value)} placeholder="Search messages">
<SearchSuggestion>Invoices</SearchSuggestion>
<SearchSuggestion>Travel</SearchSuggestion>
<SearchSuggestion>Team lunch</SearchSuggestion>
</Search>
import { Search, SearchSuggestion } from 'material/solid';
import { runSearch } from './app';
export function Example() {
return (
<Search onInput={(event) => runSearch(event.detail.value)} placeholder="Search messages">
<SearchSuggestion>Invoices</SearchSuggestion>
<SearchSuggestion>Travel</SearchSuggestion>
<SearchSuggestion>Team lunch</SearchSuggestion>
</Search>
);
}
Examples
Style and view mode
variant is contained, M3 Expressive's style and the default, or divided, the
baseline. viewMode: 'fullscreen' opens the view over the whole screen; compact windows open
a docked view full screen anyway.
import { createSearch } from 'material';
const search = createSearch({
placeholder: 'Search places',
variant: 'divided',
viewMode: 'fullscreen',
});
document.body.append(search.element);
<m-search placeholder="Search places" variant="divided" view-mode="fullscreen"></m-search>
import { Search } from 'material/react';
export function Example() {
return (
<Search placeholder="Search places" variant="divided" viewMode="fullscreen" />
);
}
<script setup lang="ts">
import { MSearch } from 'material/vue';
</script>
<template>
<MSearch placeholder="Search places" variant="divided" view-mode="fullscreen" />
</template>
<script lang="ts">
import { Search } from 'material/svelte';
</script>
<Search placeholder="Search places" variant="divided" viewMode="fullscreen" />
import { Search } from 'material/solid';
export function Example() {
return (
<Search placeholder="Search places" variant="divided" viewMode="fullscreen" />
);
}
Grouped suggestions
A suggestion is a string or { text, value, icon, group }. A change of group between two
suggestions draws a divider.
import { createSearch } from 'material';
const search = createSearch({
placeholder: 'Search places',
suggestions: [
{ text: 'Lisbon', group: 'Recent' },
{ text: 'Tokyo', group: 'Recent' },
{ text: 'Paris', group: 'Suggested' },
],
});
document.body.append(search.element);
<m-search placeholder="Search places">
<m-search-suggestion group="Recent">Lisbon</m-search-suggestion>
<m-search-suggestion group="Recent">Tokyo</m-search-suggestion>
<m-search-suggestion group="Suggested">Paris</m-search-suggestion>
</m-search>
import { Search, SearchSuggestion } from 'material/react';
export function Example() {
return (
<Search placeholder="Search places">
<SearchSuggestion group="Recent">Lisbon</SearchSuggestion>
<SearchSuggestion group="Recent">Tokyo</SearchSuggestion>
<SearchSuggestion group="Suggested">Paris</SearchSuggestion>
</Search>
);
}
<script setup lang="ts">
import { MSearch, MSearchSuggestion } from 'material/vue';
</script>
<template>
<MSearch placeholder="Search places">
<MSearchSuggestion group="Recent">Lisbon</MSearchSuggestion>
<MSearchSuggestion group="Recent">Tokyo</MSearchSuggestion>
<MSearchSuggestion group="Suggested">Paris</MSearchSuggestion>
</MSearch>
</template>
<script lang="ts">
import { Search, SearchSuggestion } from 'material/svelte';
</script>
<Search placeholder="Search places">
<SearchSuggestion group="Recent">Lisbon</SearchSuggestion>
<SearchSuggestion group="Recent">Tokyo</SearchSuggestion>
<SearchSuggestion group="Suggested">Paris</SearchSuggestion>
</Search>
import { Search, SearchSuggestion } from 'material/solid';
export function Example() {
return (
<Search placeholder="Search places">
<SearchSuggestion group="Recent">Lisbon</SearchSuggestion>
<SearchSuggestion group="Recent">Tokyo</SearchSuggestion>
<SearchSuggestion group="Suggested">Paris</SearchSuggestion>
</Search>
);
}
Setting the query
An action sets the query; here, it empties it.
import { createSearch } from 'material';
const search = createSearch({ placeholder: 'Search messages', value: 'Invoices' });
document.body.append(search.element);
function reset() {
search.setValue('');
}
<m-search value="Invoices" placeholder="Search messages"></m-search>
<script type="module">
const search = document.querySelector('m-search');
function reset() {
search.value = "";
}
</script>
import { useState } from 'react';
import { Search } from 'material/react';
export function Example() {
const [value, setValue] = useState("Invoices");
const reset = () => setValue("");
return (
<Search value={value} onInput={(event) => setValue(event.detail.value)} placeholder="Search messages" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSearch } from 'material/vue';
const value = ref("Invoices");
function reset() {
value.value = "";
}
</script>
<template>
<MSearch v-model="value" placeholder="Search messages" />
</template>
<script lang="ts">
import { Search } from 'material/svelte';
let value = $state("Invoices");
function reset() {
value = "";
}
</script>
<Search bind:value placeholder="Search messages" />
import { createSignal } from 'solid-js';
import { Search } from 'material/solid';
export function Example() {
const [value, setValue] = createSignal("Invoices");
const reset = () => setValue("");
return (
<Search value={value()} onInput={(event) => setValue(event.detail.value)} placeholder="Search messages" />
);
}
The factory also takes trailingItems, up to two trailing icon buttons or one and an avatar,
each { id, type, content, ariaLabel, onClick }; the web component takes one icon and one
avatar, as trailing-icon and avatar, and dispatches action with the item's id on a click.
API
Options
variantThe M3 style: a filled pill bar with results in their own container, or a bar that squares off above a divider
'contained' | 'divided''contained'viewModeWhere the view opens: under the bar, or over the whole screen. Compact windows open docked as full screen
'docked' | 'fullscreen''docked'initialStateWhether it starts open
'bar' | 'view''bar'placeholderThe hinted text, and the input's accessible name
string'Search'valueThe initial query
string''nameSubmits the query with a surrounding form
stringsuggestionsThe list in the view: { text, value?, icon?, supportingText?, group? }; supportingText makes a two-line item, a change of group adds a divider
SearchSuggestion[] | string[][]leadingIconSVG for the leading icon (the view shows a back arrow)
stringtrailingItemsUp to two trailing icon buttons, or one and an avatar: { id, type: 'icon' | 'avatar', content, ariaLabel?, onClick? }
SearchTrailingItem[]showClearButtonA clear button while there is text
booleantrueexpandOnFocusOpen the view when the bar is focused
booleantruecollapseOnBlurClose the view when focus leaves the search
booleantruecollapseDelayMilliseconds before closing on blur
number150minWidth / maxWidthThe bar's width range, in pixels
number360 / 720fullWidthFill the container instead
booleanfalsedisabledWhether it starts disabled
booleanfalseclassAdditional CSS classes
stringprefixPrefix for CSS class names
string'mtrl'onInput / onSubmit / onClear / onSuggestionSelect{ component, value, originalEvent, suggestion?, preventDefault, defaultPrevented }; suggestion is set for onSuggestionSelect
SearchEventonExpand / onCollapse{ component, state, viewMode }
SearchStateEventonEvent handlers, by event name
{ [event]: handler }Methods
getValue() / setValue(value, triggerEvent?)The query
string / SearchComponentgetPlaceholder() / setPlaceholder(text)The hinted text
string / SearchComponentexpand() / collapse()Opens or closes the view
SearchComponentisExpanded() / getState()Whether it is open
boolean / 'bar' | 'view'setViewMode(mode) / getViewMode()Docked or full screen, also while open
SearchComponent / SearchViewModesetVariant(variant) / getVariant()Contained or divided
SearchComponent / SearchVariantsetSuggestions(list) / getSuggestions() / clearSuggestions()The list in the view
SearchComponent / SearchSuggestion[]setLeadingIcon(svg)The bar's leading icon
SearchComponentsetTrailingItems(items) / addTrailingItem(item) / removeTrailingItem(id)Trailing icons and avatar
SearchComponentfocus() / blur() / clear() / submit()Input actions
SearchComponentenable() / disable() / isDisabled()Disabled state
SearchComponent / booleanon(event, handler) / off(event, handler)Events
SearchComponentdestroy()Closes the view and removes the search
voidEvents
inputThe query changed: typing, or the clear button or Escape emptying it
submitEnter was pressed with a query
clearThe clear button or Escape cleared the query, after its input
suggestionSelectA suggestion was chosen; suggestion carries it
expand / collapseThe view opened or closed
focus / blurThe input gained or lost focus
input, submit, clear, suggestionSelect, focus and blur receive the SearchEvent:
{ component, value, originalEvent, suggestion?, preventDefault(), defaultPrevented }.
expand and collapse receive { component, state, viewMode }. The web component's events are input, change (a submit), select (a
suggestion), each with { value }, then open, close and action.
Styling
.mtrl-search { } /* the root; keeps the bar's place */
.mtrl-search--bar, .mtrl-search--view { } /* closed, open */
.mtrl-search--docked, .mtrl-search--fullscreen { }
.mtrl-search--contained, .mtrl-search--divided { }
.mtrl-search--populated, .mtrl-search--focused, .mtrl-search--disabled, .mtrl-search--full-width { }
.mtrl-search__surface { } /* the bar and results; in the top layer while open */
.mtrl-search__container, .mtrl-search__input-wrapper, .mtrl-search__input { }
.mtrl-search__leading-icon, .mtrl-search__clear-button, .mtrl-search__trailing, .mtrl-search__trailing-icon, .mtrl-search__avatar { }
.mtrl-search__divider, .mtrl-search__content, .mtrl-search__suggestions { }
.mtrl-search__suggestion-list, .mtrl-search__suggestion-item, .mtrl-search__suggestion-item--selected { }
.mtrl-search__suggestion-icon, .mtrl-search__suggestion-text, .mtrl-search__suggestion-divider { }
.inbox-header .mtrl-search {
--mtrl-search-min-width: 240px; /* the bar's width range */
--mtrl-search-max-width: 480px;
}
Measurements
From the M3 search specs (SearchBarTokens, SearchViewTokens):
| Attribute | Value |
|---|---|
| Bar | 56dp, full corners, surface-container-high; 360–720dp wide |
| Input | Body Large, on-surface; hinted text on-surface-variant |
| Icons | 24dp in 48dp targets, 4dp from the ends; text at 56dp; a 30dp avatar |
| States | Hover 8%, focus and pressed 10%; a 3dp secondary focus ring |
| Contained, docked | Results 2dp below the pill, in a surface-container-high container with 12dp corners, 240dp tall at least and 2/3 of the screen at most |
| Contained, full screen | surface-container-low, the bar inset 12dp |
| Divided, docked | The bar's lower corners square off above a 1dp outline divider; 28dp corners overall |
| Divided, full screen | A 72dp header, no corners, surface-container-high |
| Scrim | 0.32, docked |
| Suggestions | 56dp one-line items, Body Large, 16dp padding |
| Motion | The results reveal over 300ms, emphasized decelerate; none with reduced motion |