Search
A search bar that opens a full-screen view on phones and a docked view on larger windows, and an app bar with search.
import { SearchBar, SearchAppBar } from '@windmorph/react';Props
SearchBar 25 props
The root element's id.
Controlled query.
Initial query when uncontrolled.
Controlled expansion of the search view.
Whether the search view starts expanded (uncontrolled).
How the bar expands:
docked, a dropdown below it over a scrim;full-screen, a view that fills the window;auto, full screen on compact windows (below 600px wide) and docked on larger ones, as M3 recommends ("Full-screen layout: Default for compact breakpoints"; docked is "Best for medium and expanded windows"). It follows the window as it resizes.Hint text shown in the empty input, e.g. "Search mail".
Leading icon, e.g. a search icon, or a back button while expanded.
Trailing content: one or two icon buttons (e.g. voice search and a clear button), or an avatar (an
img, drawn at 30px) with up to one icon button (M3).Centre the placeholder and the query while the bar is collapsed (M3's search app bar supports centred text).
What screen readers hear when the suggestions or results change while the view is open (M3: "the screen reader must announce the change"), e.g. "5 results". Announced politely whenever it changes.
The expanded content: suggestions or results, usually a list. ↓ from the input moves focus into it.
The input element (collapsed, or expanded while the view is open).
Attributes for both inputs, the collapsed one and the expanded view's. They replace the bar's defaults (
enterKeyHint="search",autoComplete="off",spellCheck={false}); event handlers run before the bar's own, and the bar's role, name, value and popup state always win.Ref to the root
<div>element.
Events
Called with the query as it changes.
Called with the query when the user presses Enter (the keyboard's search action) in the open search view. Enter in the collapsed bar opens the view.
Called when the search view expands or collapses.
Text
The ids of elements that describe the search, for both inputs (the bar's and the open view's), before any
inputProps['aria-describedby'].The id of an element with more details about the search, for both inputs.
Accessible name for the search bar and its input. Required unless
aria-labelledbyis set.The id of the element that names the search bar and its input. Required unless
aria-labelis set.
Classes for the root element, merged with
classNames.root; consumer classes win.Inline styles for the root element.
Classes for each part, by slot.
The outer container, merged with
className.The pill around the icons and the input (collapsed and expanded).
The text input (collapsed and expanded).
The leading icon slot.
The trailing icon slot.
The scrim behind the docked view.
The expanded view: the docked panel or the full-screen surface.
The expanded content: the docked dropdown or the full-screen content area.
Also accepts the root element's HTML attributes (id, data-*, event handlers).
SearchAppBar 9 props
The
SearchBar. It fills the free space, up to 720px, centred.Trailing controls, e.g. an avatar or
IconButtons.Respond to content scrolling under the bar: it sticks to the top and turns
surface-container(its search barsurface-container-highest);enter-alwaysalso hides it on scroll down and brings it back on any scroll up. Leave it out for a static bar positioned by the consumer.The scroll container the bar responds to. Defaults to the window.
Ref to the root element.
Classes for each part, by slot.
Inline styles for the root element.
Classes for the root element, merged with the component's own (yours win).
Also accepts the root element's HTML attributes (id, data-*, event handlers).
Tokens
M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-search-bar-container-color | var(--md-sys-color-surface-container-high) | The field. | SearchBarTokens.ContainerColor |
--md-comp-search-bar-container-shape | var(--md-sys-shape-corner-full) | The corners. | SearchBarTokens.ContainerShape |
--md-comp-search-bar-input-text-color | var(--md-sys-color-on-surface) | The query. | SearchBarTokens.InputTextColor |
--md-comp-search-bar-leading-icon-color | var(--md-sys-color-on-surface) | The leading icon. | SearchBarTokens.LeadingIconColor |
--md-comp-search-bar-trailing-icon-color | var(--md-sys-color-on-surface-variant) | The trailing icons or avatar. | SearchBarTokens.TrailingIconColor |
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-search-view-container-color | var(--md-sys-color-surface-container-high)Varies with view. | The view: the docked dropdown (`surface-container-high`) or the full-screen surface (`surface-container-low`). | SearchViewTokens.ContainerColor |
--md-comp-search-view-docked-container-shape | var(--md-sys-shape-corner-medium) | The docked dropdown's corners. | SearchViewTokens.DockedContainerShape |
Spec status
Built to the M3 Expressive search spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.
| Item | Status | Details |
|---|---|---|
| Search | Built | Variant · Docked |
| Style: contained | Built | Configuration · Docked, Full screen |
| Layout: docked | Built | Configuration · Docked |
| Layout: full-screen | Built | Configuration · Full screen |
| Search app bar | Built | Use case · App bar |
inputProps | Extension | Attributes for both inputs, the bar and the expanded view, such as enterKeyHint or inputMode, in place of the defaults (enterKeyHint="search", no autocomplete or spellcheck). |
| Style: divided | Not built | Not recommended in M3 Expressive; the contained style is used for both layouts. |
| Predictive back | Not built | The web has no predictive back gesture; Escape, a press outside or a back button closes the view. |
Search
BuiltVariant · Docked
Style: contained
BuiltConfiguration · Docked, Full screen
Layout: docked
BuiltConfiguration · Docked
Layout: full-screen
BuiltConfiguration · Full screen
Search app bar
BuiltUse case · App bar
ExtensioninputPropsAttributes for both inputs, the bar and the expanded view, such as
enterKeyHintorinputMode, in place of the defaults (enterKeyHint="search", no autocomplete or spellcheck).Style: divided
Not builtNot recommended in M3 Expressive; the contained style is used for both layouts.
Predictive back
Not builtThe web has no predictive back gesture; Escape, a press outside or a back button closes the view.
Bundle size
Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.
SearchBar: 19.2 kBSearchAppBar: 21.9 kB
M3 Expressive specs
- Variants
auto, docked, full-screen
Differences from Compose
- The docked field widens by 12px on each side when it opens (M3 Expressive's 24 → 12dp margins), and its dropdown slides down from half its height on the default spatial spring over a 32% scrim; with the field it is 240px to 2/3 of the window tall.
- The docked dropdown's height follows the results on the default spatial spring as they change; Compose lays it out again at once.
- The full-screen view uses M3 Expressive's contained style: a
surface-container-lowsurface that grows from the bar's bounds via aclip-pathon the slow spatial spring, with the filled field 16px from each side. - In a
SearchAppBarthe field issurface-containerand fills the free space up to 312px, then half of it (M3). - The field is 360px wide by default and at most 720px (Compose's
sizeIn); a consumerw-*class wins. - Not yet supported: predictive back, the divided (baseline) style, and the docked view without a gap.