Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Search

Start with a search bar, then explore suggestions in a docked or fullscreen view.

FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

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.

Vanilla
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);
Web Components
<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>
React
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>
  );
}
Vue
<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>
Svelte
<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>
SolidJS
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.

Vanilla
import { createSearch } from 'material';

const search = createSearch({
  placeholder: 'Search places',
  variant: 'divided',
  viewMode: 'fullscreen',
});
document.body.append(search.element);
Web Components
<m-search placeholder="Search places" variant="divided" view-mode="fullscreen"></m-search>
React
import { Search } from 'material/react';

export function Example() {
  return (
    <Search placeholder="Search places" variant="divided" viewMode="fullscreen" />
  );
}
Vue
<script setup lang="ts">
import { MSearch } from 'material/vue';
</script>

<template>
  <MSearch placeholder="Search places" variant="divided" view-mode="fullscreen" />
</template>
Svelte
<script lang="ts">
  import { Search } from 'material/svelte';
</script>

<Search placeholder="Search places" variant="divided" viewMode="fullscreen" />
SolidJS
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.

Vanilla
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);
Web Components
<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>
React
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>
  );
}
Vue
<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>
Svelte
<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>
SolidJS
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.

Vanilla
import { createSearch } from 'material';

const search = createSearch({ placeholder: 'Search messages', value: 'Invoices' });
document.body.append(search.element);

function reset() {
  search.setValue('');
}
Web Components
<m-search value="Invoices" placeholder="Search messages"></m-search>

<script type="module">
  const search = document.querySelector('m-search');

  function reset() {
    search.value = "";
  }
</script>
React
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" />
  );
}
Vue
<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>
Svelte
<script lang="ts">
  import { Search } from 'material/svelte';

  let value = $state("Invoices");

  function reset() {
    value = "";
  }
</script>

<Search bind:value placeholder="Search messages" />
SolidJS
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

variant

The M3 style: a filled pill bar with results in their own container, or a bar that squares off above a divider

Type'contained' | 'divided'
Default'contained'
viewMode

Where the view opens: under the bar, or over the whole screen. Compact windows open docked as full screen

Type'docked' | 'fullscreen'
Default'docked'
initialState

Whether it starts open

Type'bar' | 'view'
Default'bar'
placeholder

The hinted text, and the input's accessible name

Typestring
Default'Search'
value

The initial query

Typestring
Default''
name

Submits the query with a surrounding form

Typestring
suggestions

The list in the view: { text, value?, icon?, supportingText?, group? }; supportingText makes a two-line item, a change of group adds a divider

TypeSearchSuggestion[] | string[]
Default[]
leadingIcon

SVG for the leading icon (the view shows a back arrow)

Typestring
Defaultsearch icon
trailingItems

Up to two trailing icon buttons, or one and an avatar: { id, type: 'icon' | 'avatar', content, ariaLabel?, onClick? }

TypeSearchTrailingItem[]
showClearButton

A clear button while there is text

Typeboolean
Defaulttrue
expandOnFocus

Open the view when the bar is focused

Typeboolean
Defaulttrue
collapseOnBlur

Close the view when focus leaves the search

Typeboolean
Defaulttrue
collapseDelay

Milliseconds before closing on blur

Typenumber
Default150
minWidth / maxWidth

The bar's width range, in pixels

Typenumber
Default360 / 720
fullWidth

Fill the container instead

Typeboolean
Defaultfalse
disabled

Whether it starts disabled

Typeboolean
Defaultfalse
class

Additional CSS classes

Typestring
prefix

Prefix for CSS class names

Typestring
Default'mtrl'
onInput / onSubmit / onClear / onSuggestionSelect

{ component, value, originalEvent, suggestion?, preventDefault, defaultPrevented }; suggestion is set for onSuggestionSelect

TypeSearchEvent
onExpand / onCollapse

{ component, state, viewMode }

TypeSearchStateEvent
on

Event handlers, by event name

Type{ [event]: handler }

Methods

getValue() / setValue(value, triggerEvent?)

The query

Returnsstring / SearchComponent
getPlaceholder() / setPlaceholder(text)

The hinted text

Returnsstring / SearchComponent
expand() / collapse()

Opens or closes the view

ReturnsSearchComponent
isExpanded() / getState()

Whether it is open

Returnsboolean / 'bar' | 'view'
setViewMode(mode) / getViewMode()

Docked or full screen, also while open

ReturnsSearchComponent / SearchViewMode
setVariant(variant) / getVariant()

Contained or divided

ReturnsSearchComponent / SearchVariant
setSuggestions(list) / getSuggestions() / clearSuggestions()

The list in the view

ReturnsSearchComponent / SearchSuggestion[]
setLeadingIcon(svg)

The bar's leading icon

ReturnsSearchComponent
setTrailingItems(items) / addTrailingItem(item) / removeTrailingItem(id)

Trailing icons and avatar

ReturnsSearchComponent
focus() / blur() / clear() / submit()

Input actions

ReturnsSearchComponent
enable() / disable() / isDisabled()

Disabled state

ReturnsSearchComponent / boolean
on(event, handler) / off(event, handler)

Events

ReturnsSearchComponent
destroy()

Closes the view and removes the search

Returnsvoid

Events

input

The query changed: typing, or the clear button or Escape emptying it

submit

Enter was pressed with a query

clear

The clear button or Escape cleared the query, after its input

suggestionSelect

A suggestion was chosen; suggestion carries it

expand / collapse

The view opened or closed

focus / blur

The 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