Search lets people enter a keyword or phrase to get relevant information. Use it where finding content is a primary task. For a field that takes other text, use a text field instead.
Search has two parts:
| Part | Use |
|---|---|
SearchField | The search bar, to search the contents of a view, like Search your messages |
SearchView | The focused search, for the suggestions or results that a search bar opens |
import { Icon, IconButton, List, ListItem, SearchField, SearchView } from '@material-web/react'
import { Autocomplete, Input, Text, useFilter } from 'react-aria-components'Basic search
Write a SearchField for the search bar in the page, and a SearchView for the focused search that it opens. In the SearchView, write a second SearchField and, below it, what you show, typically a List. Wrap the two in React Aria's Autocomplete to filter the list as the query is typed.
import { Icon, IconButton, List, ListItem, SearchField, SearchView } from '@material-web/react'
import { useRef, useState } from 'react'
import { Autocomplete, Input, Text, useFilter } from 'react-aria-components'
const suggestions = [
['Peanut butter recipes', 'history'],
['Pet adoption', 'history'],
['Movie marathon', 'history'],
['Weekend hike', 'search'],
['Book club', 'search'],
['Flight tickets', 'search'],
] as const
export default function Basic() {
const [query, setQuery] = useState('')
const [isOpen, setOpen] = useState(false)
const bar = useRef<HTMLDivElement>(null)
const { contains } = useFilter({ sensitivity: 'base' })
return (
<div className="h-64 w-full pt-space-300">
<SearchField
ref={bar}
aria-label="Search your messages"
value={query}
onChange={(value) => {
setQuery(value)
setOpen(true)
}}
onClick={() => setOpen(true)}
className="mx-space-300"
>
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
<IconButton slot="trailing" aria-label="Voice search">
<Icon name="mic" />
</IconButton>
</SearchField>
<SearchView
aria-label="Search your messages"
triggerRef={bar}
isOpen={isOpen}
onOpenChange={setOpen}
>
{({ state }) => (
<Autocomplete
inputValue={query}
onInputChange={setQuery}
filter={contains}
disableAutoFocusFirst
>
<SearchField autoFocus aria-label="Search your messages" onSubmit={state.close}>
<Icon slot="leading" name="search" />
<IconButton slot="leading" aria-label="Back" onPress={state.close}>
<Icon name="arrow_back" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<List
aria-label="Suggestions"
onAction={(key) => {
setQuery(String(key))
state.close()
}}
>
{suggestions.map(([label, icon]) => (
<ListItem key={label} id={label} textValue={label}>
<Icon slot="leading" name={icon} />
<Text slot="label">{label}</Text>
</ListItem>
))}
</List>
</Autocomplete>
)}
</SearchView>
</div>
)
}Search bar
Write the parts of the bar as the children of a SearchField. They are shown in the order written. Their places are listed under Slots.
import { Icon, IconButton, SearchField } from '@material-web/react'
import { Input } from 'react-aria-components'
const avatar = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><rect width="30" height="30" fill="#6750a4"/><text x="15" y="20" font-family="sans-serif" font-size="14" fill="#fff" text-anchor="middle">A</text></svg>',
)}`
export default function SearchBars() {
return (
<div className="flex w-full flex-col gap-space-300">
<SearchField aria-label="Search your messages">
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<SearchField aria-label="Search your messages">
<IconButton slot="leading" aria-label="Menu">
<Icon name="menu" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton slot="trailing" aria-label="Voice search">
<Icon name="mic" />
</IconButton>
<IconButton slot="trailing" aria-label="Account">
<img src={avatar} alt="" />
</IconButton>
</SearchField>
<SearchField aria-label="Search your messages">
<Input placeholder="Search your messages" />
</SearchField>
</div>
)
}The bar is 56dp tall and fills the width available to it, up to 720dp, and is at least 360dp wide where there is room. In a page, add its 24dp side margins yourself with mx-space-300.
Pressing the bar outside its controls focuses the input.
Slots
- leading
- trailing
| Slot | Child | Use |
|---|---|---|
leading | An Icon or an IconButton | Before the input |
trailing | An Icon or an IconButton | After the input |
A bar also takes these parts:
| Part | Use |
|---|---|
Input | The text field. Set its placeholder to the hinted search text. |
A button with no slot | The clear button. It is shown while there is text, and clears it. A button in the leading or trailing slot does not clear the text. |
<img>, on its own or in an IconButton | Shown as a round avatar |
Focused search
A SearchView is modal, like a dialog. Its props are isOpen, defaultOpen and onOpenChange, layout, variant, triggerRef and aria-label.
As its children, write a SearchField with autoFocus, then typically a List, both in an Autocomplete, as in the first example. children can also be a function of isEntering, isExiting and state. Call state.close() to close the view.
In the view's bar, write the icon of the page's bar first, before the back button, as the example does. The view shows that icon where the page's bar has it, and cross-fades it with the back button.
Opening
You decide what opens focused search, and when it shows suggestions or results. The first example opens it when the bar is pressed or text is typed. You can also open it on the Down arrow in onKeyDown, or in onSubmit once a query is entered.
A SearchView also opens from the button of a DialogTrigger, such as the Button in the search slot of a search app bar. Wrap the DialogTrigger around that button and the SearchView alone: any other button in the trigger would open the view too.
import {
AppBar,
Icon,
IconButton,
List,
ListItem,
SearchField,
SearchView,
} from '@material-web/react'
import { useState } from 'react'
import { Button, DialogTrigger, Input } from 'react-aria-components'
const recent = ['Peanut butter recipes', 'Pet adoption', 'Movie marathon']
export default function FromAnAppBar() {
const [query, setQuery] = useState('')
return (
<div className="h-64">
<AppBar className="text-center">
<IconButton slot="leading" aria-label="Menu">
<Icon name="menu" />
</IconButton>
<DialogTrigger>
<Button slot="search">
<Icon slot="leading" name="search" />
{query || 'Search your messages'}
</Button>
<SearchView aria-label="Search your messages">
{({ state }) => (
<>
<SearchField
autoFocus
aria-label="Search your messages"
value={query}
onChange={setQuery}
onSubmit={state.close}
>
<Icon slot="leading" name="search" />
<IconButton slot="leading" aria-label="Back" onPress={state.close}>
<Icon name="arrow_back" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<List
aria-label="Recent searches"
onAction={(key) => {
setQuery(String(key))
state.close()
}}
>
{recent.map((label) => (
<ListItem key={label} id={label}>
{label}
</ListItem>
))}
</List>
</>
)}
</SearchView>
</DialogTrigger>
<IconButton slot="trailing" aria-label="Account">
<Icon name="account_circle" />
</IconButton>
</AppBar>
</div>
)
}Set triggerRef to the element the view opens from, typically the page's search bar. The view takes its position and width from that element. In a DialogTrigger without a triggerRef, the view opens from the trigger's button. With neither, it fades in and out, docked at the top of the window.
Layout
Set layout to docked (the default) or full-screen. Use full-screen in compact windows and docked in larger ones, and switch between them by the window's size.
dockedopens over the bar that opened it, at the same width. It is at least 240dp tall and at most two thirds of the window's height, and it ends 12dp above the bottom of the window where the bar is near that edge.full-screenfills the window, with its bar at the top.
import { Icon, IconButton, List, ListItem, SearchField, SearchView } from '@material-web/react'
import { useRef, useState } from 'react'
import { Input } from 'react-aria-components'
const recent = ['Peanut butter recipes', 'Pet adoption', 'Movie marathon']
export default function FullScreen() {
const [query, setQuery] = useState('')
const [isOpen, setOpen] = useState(false)
const bar = useRef<HTMLDivElement>(null)
return (
<>
<SearchField
ref={bar}
aria-label="Search your messages"
value={query}
onChange={(value) => {
setQuery(value)
setOpen(true)
}}
onClick={() => setOpen(true)}
className="w-full"
>
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
</SearchField>
<SearchView
aria-label="Search your messages"
layout="full-screen"
triggerRef={bar}
isOpen={isOpen}
onOpenChange={setOpen}
>
{({ state }) => (
<>
<SearchField
autoFocus
aria-label="Search your messages"
value={query}
onChange={setQuery}
onSubmit={state.close}
>
<Icon slot="leading" name="search" />
<IconButton slot="leading" aria-label="Back" onPress={state.close}>
<Icon name="arrow_back" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<List
aria-label="Recent searches"
onAction={(key) => {
setQuery(String(key))
state.close()
}}
>
{recent.map((label) => (
<ListItem key={label} id={label}>
{label}
</ListItem>
))}
</List>
</>
)}
</SearchView>
</>
)
}Style
Set variant to choose the style:
contained(the default) keeps the bar in its own container, with the list in a separate container below it. When docked, it opens over a scrim.dividedshows the bar and the list in one container, with a divider between them. When docked, it has no scrim.
import { Icon, IconButton, List, ListItem, SearchField, SearchView } from '@material-web/react'
import { useRef, useState } from 'react'
import { Input } from 'react-aria-components'
const recent = ['Peanut butter recipes', 'Pet adoption', 'Movie marathon']
export default function Divided() {
const [query, setQuery] = useState('')
const [isOpen, setOpen] = useState(false)
const bar = useRef<HTMLDivElement>(null)
return (
<>
<SearchField
ref={bar}
aria-label="Search your messages"
value={query}
onChange={(value) => {
setQuery(value)
setOpen(true)
}}
onClick={() => setOpen(true)}
className="w-full"
>
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
</SearchField>
<SearchView
aria-label="Search your messages"
variant="divided"
triggerRef={bar}
isOpen={isOpen}
onOpenChange={setOpen}
>
{({ state }) => (
<>
<SearchField
autoFocus
aria-label="Search your messages"
value={query}
onChange={setQuery}
onSubmit={state.close}
>
<Icon slot="leading" name="search" />
<IconButton slot="leading" aria-label="Back" onPress={state.close}>
<Icon name="arrow_back" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<List
aria-label="Recent searches"
onAction={(key) => {
setQuery(String(key))
state.close()
}}
>
{recent.map((label) => (
<ListItem key={label} id={label}>
{label}
</ListItem>
))}
</List>
</>
)}
</SearchView>
</>
)
}Keyboard and focus
Set autoFocus on the view's bar. When focused search opens, the focus moves to that bar, with the query in it.
- Tab moves the focus into the list. The arrow keys move through the items, Enter acts on the focused item, and Shift+Tab returns to the bar.
- Typing does not focus an item, so Enter in the bar submits the query as typed. A
Listbehaves this way on its own. For a listbox, setdisableAutoFocusFirston theAutocomplete, as the example does. - Escape clears the bar first, then closes the view. A press outside a docked view closes it too, and so does a back button of yours that calls
state.close(). - When the view closes, the focus returns to the element that opened it.
While the view is open, the page behind it is inert and does not scroll.
Handling input
The bar reports the query as it changes and when it is entered:
| Event | Called |
|---|---|
onChange | With the query, as it is typed or cleared |
onSubmit | With the query, on Enter |
onClear | When Escape or the clear button empties the bar |
Value: none. Submitted: none.
import { Icon, IconButton, SearchField } from '@material-web/react'
import { useState } from 'react'
import { Input } from 'react-aria-components'
export default function Controlled() {
const [value, setValue] = useState('')
const [submitted, setSubmitted] = useState('')
return (
<div className="flex w-full flex-col gap-space-200">
<SearchField
aria-label="Search your messages"
value={value}
onChange={setValue}
onSubmit={setSubmitted}
onClear={() => setSubmitted('')}
>
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
</SearchField>
<p className="m-0 text-body-md text-on-surface-variant">
Value: {value || 'none'}. Submitted: {submitted || 'none'}.
</p>
</div>
)
}Controlled
Search has two states, each controlled on its own component:
- Query: Set the initial query of a
SearchFieldwithdefaultValue, or control it withvalueandonChange. - Open state: Control a
SearchViewwithisOpenandonOpenChange, or setdefaultOpen.
const [query, setQuery] = useState('')
const [isOpen, setOpen] = useState(false)
<SearchField value={query} onChange={setQuery}>
{/* ... */}
</SearchField>
<SearchView isOpen={isOpen} onOpenChange={setOpen}>
{/* ... */}
</SearchView>The page's bar and the view's bar show the same query. Give both the same value, or control the view's Autocomplete with inputValue and onInputChange, as the first example does.
Custom filter
An Autocomplete shows the items that its filter accepts. filter is called with the text of an item and the query, and returns whether the item matches. React Aria's useFilter provides contains, startsWith and endsWith, which compare text in the locale. The first example uses contains, without regard to case.
const { startsWith } = useFilter({ sensitivity: 'base' })
<Autocomplete filter={startsWith}>
{/* ... */}
</Autocomplete>Give each item a textValue when its children are not plain text. Without an Autocomplete, the list is not filtered: show the items you choose, such as recent searches.
Disabled
Set isDisabled to disable the bar. Its leading and trailing buttons are disabled with it, unless one sets isDisabled={false} itself. A SearchField also takes isReadOnly, isRequired and name.
import { Icon, IconButton, SearchField } from '@material-web/react'
import { Input } from 'react-aria-components'
export default function Disabled() {
return (
<SearchField
aria-label="Search your messages"
defaultValue="Weekend hike"
isDisabled
className="w-full"
>
<IconButton slot="leading" aria-label="Menu">
<Icon name="menu" />
</IconButton>
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
<IconButton slot="trailing" aria-label="Voice search">
<Icon name="mic" />
</IconButton>
</SearchField>
)
}Customization
Pass classes in className to restyle one bar. A class of yours replaces the bar's own class for the same property. See Styling for the classes, the states and the theme.
In this example, rounded-shape-lg replaces the bar's round corners, and the voice search button is shown only while the bar is empty.
import { Icon, IconButton, SearchField } from '@material-web/react'
import { Input } from 'react-aria-components'
export default function Custom() {
return (
<SearchField aria-label="Search your messages" className="w-full rounded-shape-lg">
{({ isEmpty }) => (
<>
<Icon slot="leading" name="search" />
<Input placeholder="Search your messages" />
<IconButton aria-label="Clear search">
<Icon name="close" />
</IconButton>
{isEmpty && (
<IconButton slot="trailing" aria-label="Voice search">
<Icon name="mic" />
</IconButton>
)}
</>
)}
</SearchField>
)
}- Width: In a flex row, a bar is 360dp wide: add
w-fullto make it fill the row. Addmax-w-noneto let a bar grow past 720dp. - Input: The
Inputtakes classes of its own, such as a text color. - States: On the bar, style the empty, disabled, invalid, read-only and required states with their variants, such as
disabled:, or withdata-emptyand the like in your own CSS. Hover and focus are reported on the input: use thehas-*form for them, as inhas-hover:bg-surface-container. See Variants. - Functions:
className,styleandchildrencan be functions of the bar's state:isEmpty,isDisabled,isInvalid,isReadOnly,isRequiredandstate. See Children as a function. - Every bar: To restyle all the search bars at once, change the theme.
Limitations
Search app bar
Give the search bar of a search app bar a search icon in its leading slot. Without one, its text jumps as the view opens and closes.
View styling
A SearchView takes no className or style. Its look comes from layout and variant, and from the bar and the list you write in it.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to and focus on a search bar
- View the hinted search text or persistent label
- Input text and complete a search
- Interact with a list of search suggestions and results
- Clear the input text
Interaction and style
- States: Hovering the bar shows a state layer, except over its own buttons, and a press shows a ripple. Keyboard focus in its input shows a focus ring around the bar, and a press does not. In focused search, the bar shows no state layer.
- Touch target: The bar is 56dp tall, and each icon button in it has a 48dp touch target.
- Motion: With reduced motion, focused search opens and closes at once.
- Forced colors: In forced colors, the bar keeps an edge. In focused search, so does the container of the list when
contained, and the whole view whendivided. - Autosuggest: Material advises that screen readers announce suggestions and results as they appear, so that people know there are items to choose.
Note: Nothing is announced as suggestions appear or change.
Focus
- Tab moves the focus through the bar's leading buttons, its input and its trailing buttons, in the order written. So the focus lands first on a leading button, or else on the input, as Material advises.
- The clear button is not a tab stop.
- Pressing the bar outside its controls focuses the input.
- When focused search opens, the focus moves to its bar, which needs
autoFocus, and the page behind it is inert. When it closes, the focus returns to the element that opened it. See Keyboard and focus.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to the next control, or from the bar of focused search into its list |
| Shift+Tab | Move the focus to the previous control, or from the list back to the bar |
| Up or Down arrow | Move the focus between the items of the list |
| Enter | Submit the query in the bar, or act on the focused item |
| Escape | Clear the bar, and in focused search, close the view once the bar is empty |
- Typing in the bar does not focus an item of a
List, so Enter submits the query as typed. The arrow keys move through the list once Tab has moved the focus into it.
Labeling elements
- The input is a search box. Set the bar's
aria-labelto the hinted search text, as Material advises. - The default name of the clear button is Clear search, and an
IconButtonstill requires you to set anaria-label. Give each leading and trailingIconButtonanaria-labeltoo. See Icon button. - Name the list of suggestions and results as any
List. See List. - Focused search is a dialog. Set an
aria-labelon theSearchViewto name it. In aDialogTrigger, a view with noaria-labelis named by the trigger's button.
SearchField
Props of SearchField (54)
classNameClassNameOrFunction<SearchFieldRenderProps>Default'react-aria-SearchField'The CSS className for the element. A function may be provided to compute the class based on component state.
formstringThe
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.patternstringRegex pattern that the value of the input must match to be valid. See MDN.
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
isDisabledbooleanWhether the input is disabled.
autoFocusbooleanWhether the element should receive focus on render.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
onKeyDown(e: KeyboardEvent) => voidHandler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => voidHandler that is called when a key is released.
aria-haspopup'dialog' | 'menu' | 'true' | 'false' | 'grid' | 'listbox' | 'tree' | booleanIndicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
type'text' | 'search' | 'url' | 'tel' | 'email' | 'password' | (string & {})Default'search'The type of input to render. See MDN.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
valuestringThe current value (controlled).
excludeFromTabOrderbooleanWhether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.
onChange(value: string) => voidHandler that is called when the value changes.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
isInvalidbooleanWhether the input value is invalid.
defaultValuestringThe default value (uncontrolled).
isRequiredbooleanWhether user input is required on the input before form submission.
validate(value: string) => true | ValidationError | null | undefinedA function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if
validationBehavior="native". For realtime validation, use theisInvalidprop instead.aria-errormessagestringIdentifies the element that provides an error message for the object.
autoCompletestringDescribes the type of autocomplete functionality the input should provide if any. See MDN.
enterKeyHint'search' | 'enter' | 'done' | 'go' | 'next' | 'previous' | 'send'An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See MDN.
spellCheckstringAn enumerated attribute that defines whether the element may be checked for spelling errors. See MDN.
autoCorrectstringAn attribute that takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. See MDN.
inputMode'search' | 'text' | 'none' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal'Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN.
aria-activedescendantstringIdentifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.
aria-autocomplete'none' | 'list' | 'inline' | 'both'Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made.
onCopyClipboardEventHandler<HTMLInputElement>Handler that is called when the user copies text. See MDN.
onCutClipboardEventHandler<HTMLInputElement>Handler that is called when the user cuts text. See MDN.
onPasteClipboardEventHandler<HTMLInputElement>Handler that is called when the user pastes text. See MDN.
onCompositionEndCompositionEventHandler<HTMLInputElement>Handler that is called when a text composition system completes or cancels the current text composition session. See MDN.
onCompositionStartCompositionEventHandler<HTMLInputElement>Handler that is called when a text composition system starts a new text composition session. See MDN.
onCompositionUpdateCompositionEventHandler<HTMLInputElement>Handler that is called when a new character is received in the current text composition session. See MDN.
onBeforeInputFormEventHandler<HTMLInputElement>Handler that is called when the input value is about to be modified. See MDN.
onInputFormEventHandler<HTMLInputElement>Handler that is called when the input value is modified. See MDN.
onSubmit(value: string) => voidHandler that is called when the SearchField is submitted.
onSelectReactEventHandler<HTMLInputElement>Handler that is called when text in the input is selected. See MDN.
maxLengthnumberThe maximum number of characters supported by the input. See MDN.
minLengthnumberThe minimum number of characters required by the input. See MDN.
onClear() => voidHandler that is called when the clear button is pressed.
validationBehavior'native' | 'aria'Default'native'Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: SearchFieldRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', SearchFieldRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.refRef<HTMLDivElement>
SearchField also takes the 70 attributes and events of the element it renders.
SearchView
layout'docked' | 'full-screen'Default'docked'When focused, search suggestions and results appear in a list below the search bar.
There are two layout options:
- Docked opens a list below the search bar, with a scrim covering main content
- Full-screen expands to fill the screen
When focused, search can switch between showing suggestions or results in a:
- Docked layout: Best for medium and expanded windows
- Full-screen layout: Default for compact breakpoints
Search suggestions or results should swap from full-screen in compact windows to docked in larger breakpoints.
Docked container
- Width: Min: 360dp, max: 720dp
- Height: Min: 240dp, max: 2/3 of screen height
Full-screen container
- Width: Full width
- Height: Full height
variant'contained' | 'divided'Default'contained'Search comes in two styles:
- Contained: Has an expressive look and feel. It uses a filled container to separate a search bar from a list of suggestions or results
- Divided ( baseline): Doesn’t have the latest visual style, motion, or flexibility
The contained style has a persistent, filled container, expressive motion, and rounded shape
The divided (baseline) style uses a divider to separate the search bar from suggestions and results
In the contained style, the search bar container remains the same shape in both the unfocused and focused states. Avoid changing the container behavior.
The container’s margins should be:
- Unfocused: 24dp
- Focused: 12dp
Props of SearchView (6)
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
isOpenbooleanWhether the overlay is open by default (controlled).
defaultOpenbooleanWhether the overlay is open by default (uncontrolled).
onOpenChange(isOpen: boolean) => voidHandler that is called when the overlay's open state changes.
aria-labelstringDefines a string value that labels the current element.
triggerRefRefObject<Element | null>