Skip to content

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

By name or description, across the 34 props below

  • idtype string

    The root element's id.

  • valuetype string

    Controlled query.

  • defaultValuetype stringdefault ""

    Initial query when uncontrolled.

  • expandedtype boolean

    Controlled expansion of the search view.

  • defaultExpandedtype booleandefault false

    Whether the search view starts expanded (uncontrolled).

  • viewtype "auto" | "docked" | "full-screen"default "auto"

    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.

  • placeholdertype string

    Hint text shown in the empty input, e.g. "Search mail".

  • leadingIcontype SearchBarIcon

    Leading icon, e.g. a search icon, or a back button while expanded.

  • trailingIcontype SearchBarIcon

    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).

  • textAligntype "start" | "center"default "start"

    Centre the placeholder and the query while the bar is collapsed (M3's search app bar supports centred text).

  • resultsAnnouncementtype string

    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.

  • childrentype ReactNode

    The expanded content: suggestions or results, usually a list. ↓ from the input moves focus into it.

  • inputReftype Ref<HTMLInputElement>

    The input element (collapsed, or expanded while the view is open).

  • inputPropstype SearchBarInputProps

    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.

  • reftype Ref<HTMLDivElement>

    Ref to the root <div> element.

Events

  • onChangetype ((value: string) => void)

    Called with the query as it changes.

  • onSubmittype ((value: string) => void)

    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.

  • onExpandedChangetype ((expanded: boolean) => void)

    Called when the search view expands or collapses.

Text

  • aria-describedbytype string

    The ids of elements that describe the search, for both inputs (the bar's and the open view's), before any inputProps['aria-describedby'].

  • aria-detailstype string

    The id of an element with more details about the search, for both inputs.

  • aria-labeltype string

    Accessible name for the search bar and its input. Required unless aria-labelledby is set.

  • aria-labelledbytype string

    The id of the element that names the search bar and its input. Required unless aria-label is set.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

SearchAppBar 9 props

  • childrentype ReactNodeRequired

    The SearchBar. It fills the free space, up to 720px, centred.

  • navigationIcontype ReactNode

    A leading control, e.g. a menu IconButton.

  • actionstype ReactNode

    Trailing controls, e.g. an avatar or IconButtons.

  • scrollBehaviortype "pinned" | "enter-always"

    Respond to content scrolling under the bar: it sticks to the top and turns surface-container (its search bar surface-container-highest); enter-always also hides it on scroll down and brings it back on any scroll up. Leave it out for a static bar positioned by the consumer.

  • scrollReftype RefObject<HTMLElement | null>

    The scroll container the bar responds to. Defaults to the window.

  • reftype Ref<HTMLElement>

    Ref to the root element.

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.

Search tokens (md-comp-search-bar)
VariableDefaultPaintsCompose
--md-comp-search-bar-container-colorvar(--md-sys-color-surface-container-high)The field.SearchBarTokens.ContainerColor
--md-comp-search-bar-container-shapevar(--md-sys-shape-corner-full)The corners.SearchBarTokens.ContainerShape
--md-comp-search-bar-input-text-colorvar(--md-sys-color-on-surface)The query.SearchBarTokens.InputTextColor
--md-comp-search-bar-leading-icon-colorvar(--md-sys-color-on-surface)The leading icon.SearchBarTokens.LeadingIconColor
--md-comp-search-bar-trailing-icon-colorvar(--md-sys-color-on-surface-variant)The trailing icons or avatar.SearchBarTokens.TrailingIconColor
Search tokens (md-comp-search-view)
VariableDefaultPaintsCompose
--md-comp-search-view-container-colorvar(--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-shapevar(--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.

  • 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.

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 kB
  • SearchAppBar: 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-low surface that grows from the bar's bounds via a clip-path on the slow spatial spring, with the filled field 16px from each side.
  • In a SearchAppBar the field is surface-container and 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 consumer w-* class wins.
  • Not yet supported: predictive back, the divided (baseline) style, and the docked view without a gap.