SearchField

A search field allows a user to enter and clear a search query.

installyarn add react-aria-components
version1.3.3
usageimport {SearchField} from 'react-aria-components'

Example#


import {SearchField, Label, Input, Button} from 'react-aria-components';

<SearchField>
  <Label>Search</Label>
  <Input />
  <Button></Button>
</SearchField>
import {
  Button,
  Input,
  Label,
  SearchField
} from 'react-aria-components';

<SearchField>
  <Label>Search</Label>
  <Input />
  <Button></Button>
</SearchField>
import {
  Button,
  Input,
  Label,
  SearchField
} from 'react-aria-components';

<SearchField>
  <Label>Search</Label>
  <Input />
  <Button></Button>
</SearchField>
Show CSS
@import "@react-aria/example-theme";

.react-aria-SearchField {
  display: grid;
  grid-template-areas: "label label"
                       "input button"
                       "help  help";
  grid-template-columns: 1fr auto;
  align-items: center;
  width: fit-content;
  color: var(--text-color);

  .react-aria-Input {
    grid-area: input;
    padding: 0.286rem 1.714rem 0.286rem 0.286rem;
    margin: 0;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--field-background);
    font-size: 1.143rem;
    color: var(--field-text-color);

    &::-webkit-search-cancel-button,
    &::-webkit-search-decoration {
      -webkit-appearance: none;
    }

    &[data-focused] {
      outline: 2px solid var(--focus-ring-color);
      outline-offset: -1px;
    }
  }

  .react-aria-Button {
    grid-area: button;
    width: 1.143rem;
    height: 1.143rem;
    border-radius: 1.143rem;
    margin-left: -1.429rem;
    font-size: 0.857rem;
    line-height: 0.857rem;
    vertical-align: middle;
    text-align: center;
    background: var(--gray-500);
    color: var(--gray-50);
    border: none;
    padding: 0;

    &[data-pressed] {
      background: var(--gray-600);
    }
  }

  &[data-empty] button {
    display: none;
  }
}
@import "@react-aria/example-theme";

.react-aria-SearchField {
  display: grid;
  grid-template-areas: "label label"
                       "input button"
                       "help  help";
  grid-template-columns: 1fr auto;
  align-items: center;
  width: fit-content;
  color: var(--text-color);

  .react-aria-Input {
    grid-area: input;
    padding: 0.286rem 1.714rem 0.286rem 0.286rem;
    margin: 0;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--field-background);
    font-size: 1.143rem;
    color: var(--field-text-color);

    &::-webkit-search-cancel-button,
    &::-webkit-search-decoration {
      -webkit-appearance: none;
    }

    &[data-focused] {
      outline: 2px solid var(--focus-ring-color);
      outline-offset: -1px;
    }
  }

  .react-aria-Button {
    grid-area: button;
    width: 1.143rem;
    height: 1.143rem;
    border-radius: 1.143rem;
    margin-left: -1.429rem;
    font-size: 0.857rem;
    line-height: 0.857rem;
    vertical-align: middle;
    text-align: center;
    background: var(--gray-500);
    color: var(--gray-50);
    border: none;
    padding: 0;

    &[data-pressed] {
      background: var(--gray-600);
    }
  }

  &[data-empty] button {
    display: none;
  }
}
@import "@react-aria/example-theme";

.react-aria-SearchField {
  display: grid;
  grid-template-areas: "label label"
                       "input button"
                       "help  help";
  grid-template-columns: 1fr auto;
  align-items: center;
  width: fit-content;
  color: var(--text-color);

  .react-aria-Input {
    grid-area: input;
    padding: 0.286rem 1.714rem 0.286rem 0.286rem;
    margin: 0;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--field-background);
    font-size: 1.143rem;
    color: var(--field-text-color);

    &::-webkit-search-cancel-button,
    &::-webkit-search-decoration {
      -webkit-appearance: none;
    }

    &[data-focused] {
      outline: 2px solid var(--focus-ring-color);
      outline-offset: -1px;
    }
  }

  .react-aria-Button {
    grid-area: button;
    width: 1.143rem;
    height: 1.143rem;
    border-radius: 1.143rem;
    margin-left: -1.429rem;
    font-size: 0.857rem;
    line-height: 0.857rem;
    vertical-align: middle;
    text-align: center;
    background: var(--gray-500);
    color: var(--gray-50);
    border: none;
    padding: 0;

    &[data-pressed] {
      background: var(--gray-600);
    }
  }

  &[data-empty] button {
    display: none;
  }
}

Features#


Search fields can be built with <input type="search">, but these can be hard to style consistently cross browser. SearchField helps achieve accessible search fields that can be styled as needed.

  • Clearable – A custom clear button can be shown to allow the input to be easily reset.
  • Accessible – Uses a native <input type="search"> element, with support for the Enter and Escape keys to submit and clear the field, respectively. Label, description, and error message elements are automatically associated with the field.
  • Validation – Support for native HTML constraint validation with customizable UI, custom validation functions, realtime validation, and server-side validation errors.

Anatomy#


ValueLabelInputLabelClear button

Search fields consist of an input element, a label, and an optional clear button. SearchField automatically manages the labeling and relationships between the elements, and handles keyboard events. Users can press the Escape key to clear the search field, or the Enter key to trigger the onSubmit event.

SearchField also supports optional description and error message elements, which can be used to provide more context about the field, and any validation messages. These are linked with the input via the aria-describedby attribute.

import {Button, FieldError, Input, Label, SearchField, Text} from 'react-aria-components';

<SearchField>
  <Label />
  <Input />
  <Button />
  <Text slot="description" />
  <FieldError />
</SearchField>
import {
  Button,
  FieldError,
  Input,
  Label,
  SearchField,
  Text
} from 'react-aria-components';

<SearchField>
  <Label />
  <Input />
  <Button />
  <Text slot="description" />
  <FieldError />
</SearchField>
import {
  Button,
  FieldError,
  Input,
  Label,
  SearchField,
  Text
} from 'react-aria-components';

<SearchField>
  <Label />
  <Input />
  <Button />
  <Text slot="description" />
  <FieldError />
</SearchField>

If there is no visual label, an aria-label or aria-labelledby prop must be passed instead to identify the element to screen readers.

Concepts#

SearchField makes use of the following concepts:

Forms
Validating and submitting form data, and integrating with form libraries.

Composed components#

A SearchField uses the following components, which may also be used standalone or reused in other components.

Label
A label provides context for an input element.
Input
An input allows a user to enter a plain text value with a keyboard.
Button
A button allows a user to perform an action.

Starter kits#


To help kick-start your project, we offer starter kits that include example implementations of all React Aria components with various styling solutions. All components are fully styled, including support for dark mode, high contrast mode, and all UI states. Each starter comes with a pre-configured Storybook that you can experiment with, or use as a starting point for your own component library.

Vanilla CSS
Download ZIP
Preview
Tailwind CSS
Download ZIP
Preview

Reusable wrappers#


If you will use a SearchField in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.

This example wraps SearchField and all of its children together into a single component which accepts a label prop, which is passed to the right place. It also shows how to use the description slot to render help text, and FieldError component to render validation errors.

import type {SearchFieldProps, ValidationResult} from 'react-aria-components';
import {FieldError, Text} from 'react-aria-components';

interface MySearchFieldProps extends SearchFieldProps {
  label?: string;
  description?: string;
  errorMessage?: string | ((validation: ValidationResult) => string);
}

function MySearchField(
  { label, description, errorMessage, ...props }: MySearchFieldProps
) {
  return (
    <SearchField {...props}>
      <Label>{label}</Label>
      <Input />
      <Button></Button>
      {description && <Text slot="description">{description}</Text>}
      <FieldError>{errorMessage}</FieldError>
    </SearchField>
  );
}

<MySearchField label="Search" />
import type {
  SearchFieldProps,
  ValidationResult
} from 'react-aria-components';
import {FieldError, Text} from 'react-aria-components';

interface MySearchFieldProps extends SearchFieldProps {
  label?: string;
  description?: string;
  errorMessage?:
    | string
    | ((validation: ValidationResult) => string);
}

function MySearchField(
  { label, description, errorMessage, ...props }:
    MySearchFieldProps
) {
  return (
    <SearchField {...props}>
      <Label>{label}</Label>
      <Input />
      <Button></Button>
      {description && (
        <Text slot="description">{description}</Text>
      )}
      <FieldError>{errorMessage}</FieldError>
    </SearchField>
  );
}

<MySearchField label="Search" />
import type {
  SearchFieldProps,
  ValidationResult
} from 'react-aria-components';
import {
  FieldError,
  Text
} from 'react-aria-components';

interface MySearchFieldProps
  extends
    SearchFieldProps {
  label?: string;
  description?: string;
  errorMessage?:
    | string
    | ((
      validation:
        ValidationResult
    ) => string);
}

function MySearchField(
  {
    label,
    description,
    errorMessage,
    ...props
  }: MySearchFieldProps
) {
  return (
    <SearchField
      {...props}
    >
      <Label>
        {label}
      </Label>
      <Input />
      <Button></Button>
      {description && (
        <Text slot="description">
          {description}
        </Text>
      )}
      <FieldError>
        {errorMessage}
      </FieldError>
    </SearchField>
  );
}

<MySearchField label="Search" />

Value#


Default value#

A SearchField's value is empty by default, but an initial, uncontrolled, value can be provided using the defaultValue prop.

<MySearchField
  label="Search"
  defaultValue="Puppies" />
<MySearchField
  label="Search"
  defaultValue="Puppies" />
<MySearchField
  label="Search"
  defaultValue="Puppies"
/>

Controlled value#

The value prop can be used to make the value controlled. The onChange event is fired when the user edits the text, and receives the new value.

function Example() {
  let [text, setText] = React.useState('');

  return (
    <>
      <MySearchField label="Search" onChange={setText} />
      <p>Mirrored text: {text}</p>
    </>
  );
}
function Example() {
  let [text, setText] = React.useState('');

  return (
    <>
      <MySearchField label="Search" onChange={setText} />
      <p>Mirrored text: {text}</p>
    </>
  );
}
function Example() {
  let [text, setText] =
    React.useState('');

  return (
    <>
      <MySearchField
        label="Search"
        onChange={setText}
      />
      <p>
        Mirrored text:
        {' '}
        {text}
      </p>
    </>
  );
}

HTML forms#

SearchField supports the name prop for integration with HTML forms. In addition, attributes such as type, pattern, inputMode, and others are passed through to the underlying <input> element.

<MySearchField label="Email" name="email" type="email" />
<MySearchField label="Email" name="email" type="email" />
<MySearchField
  label="Email"
  name="email"
  type="email"
/>

Events#


The most commonly used handlers for events in SearchField are the:

  • onChange prop which is triggered whenever the value is edited by the user.
  • onSubmit prop which is triggered whenever the value is submitted by the user (e.g. by pressing Enter).
  • onClear prop which is triggered whenever the value is cleared by the user (e.g. by pressing clear button or Escape key).

The example below uses onChange, onSubmit, and onClear to update two separate elements with the text entered into the SearchField.

function Example() {
  let [currentText, setCurrentText] = React.useState('');
  let [submittedText, setSubmittedText] = React.useState('');

  return (
    <div>
      <MySearchField
        onClear={() => setCurrentText('')}
        onChange={setCurrentText}
        onSubmit={setSubmittedText}
        label="Your text"
        value={currentText}
      />
      <p>Mirrored text: {currentText}</p>
      <p>Submitted text: {submittedText}</p>
    </div>
  );
}
function Example() {
  let [currentText, setCurrentText] = React.useState('');
  let [submittedText, setSubmittedText] = React.useState(
    ''
  );

  return (
    <div>
      <MySearchField
        onClear={() => setCurrentText('')}
        onChange={setCurrentText}
        onSubmit={setSubmittedText}
        label="Your text"
        value={currentText}
      />
      <p>Mirrored text: {currentText}</p>
      <p>Submitted text: {submittedText}</p>
    </div>
  );
}
function Example() {
  let [
    currentText,
    setCurrentText
  ] = React.useState('');
  let [
    submittedText,
    setSubmittedText
  ] = React.useState('');

  return (
    <div>
      <MySearchField
        onClear={() =>
          setCurrentText(
            ''
          )}
        onChange={setCurrentText}
        onSubmit={setSubmittedText}
        label="Your text"
        value={currentText}
      />
      <p>
        Mirrored text:
        {' '}
        {currentText}
      </p>
      <p>
        Submitted text:
        {' '}
        {submittedText}
      </p>
    </div>
  );
}

Validation#


SearchField supports HTML constraint validation props such as isRequired, minLength, and pattern, as well as custom validation functions, realtime validation, and server-side validation. It can also be integrated with other form libraries. See the Forms guide to learn more.

To display validation errors, add a <FieldError> element as a child of the SearchField. This allows you to render error messages from all of the above sources with consistent custom styles.

import {Form, FieldError} from 'react-aria-components';

<Form>
  <SearchField name="search" isRequired>    <Label>Search</Label>
    <Input />
    <Button></Button>
    <FieldError />  </SearchField>
  <Button type="submit">Submit</Button>
</Form>
import {Form, FieldError} from 'react-aria-components';

<Form>
  <SearchField name="search" isRequired>    <Label>Search</Label>
    <Input />
    <Button></Button>
    <FieldError />  </SearchField>
  <Button type="submit">Submit</Button>
</Form>
import {
  FieldError,
  Form
} from 'react-aria-components';

<Form>
  <SearchField
    name="search"
    isRequired
  >    <Label>
      Search
    </Label>
    <Input />
    <Button></Button>
    <FieldError />  </SearchField>
  <Button type="submit">
    Submit
  </Button>
</Form>
Show CSS
.react-aria-SearchField {
  .react-aria-Input{
    &[data-invalid] {
      border-color: var(--invalid-color);
    }
  }

  .react-aria-FieldError {
    grid-area: help;
    font-size: 12px;
    color: var(--invalid-color);
  }
}
.react-aria-SearchField {
  .react-aria-Input{
    &[data-invalid] {
      border-color: var(--invalid-color);
    }
  }

  .react-aria-FieldError {
    grid-area: help;
    font-size: 12px;
    color: var(--invalid-color);
  }
}
.react-aria-SearchField {
  .react-aria-Input{
    &[data-invalid] {
      border-color: var(--invalid-color);
    }
  }

  .react-aria-FieldError {
    grid-area: help;
    font-size: 12px;
    color: var(--invalid-color);
  }
}

By default, FieldError displays default validation messages provided by the browser. See Customizing error messages in the Forms guide to learn how to provide your own custom errors.

Description#

The description slot can be used to associate additional help text with a search field.

<SearchField>
  <Label>Email</Label>
  <Input />
  <Button></Button>
  <Text slot="description">
    Enter an email for us to contact you about your order.
  </Text></SearchField>
<SearchField>
  <Label>Email</Label>
  <Input />
  <Button></Button>
  <Text slot="description">
    Enter an email for us to contact you about your order.
  </Text></SearchField>
<SearchField>
  <Label>Email</Label>
  <Input />
  <Button></Button>
  <Text slot="description">
    Enter an email for
    us to contact you
    about your order.
  </Text></SearchField>
Show CSS
.react-aria-SearchField {
  [slot=description] {
    grid-area: help;
    font-size: 12px;
  }
}
.react-aria-SearchField {
  [slot=description] {
    grid-area: help;
    font-size: 12px;
  }
}
.react-aria-SearchField {
  [slot=description] {
    grid-area: help;
    font-size: 12px;
  }
}

Disabled#


A SearchField can be disabled using the isDisabled prop.

<MySearchField label="Email" isDisabled />
<MySearchField label="Email" isDisabled />
<MySearchField
  label="Email"
  isDisabled
/>
Show CSS
.react-aria-SearchField {
  .react-aria-Input {
    &[data-disabled] {
      border-color: var(--border-color-disabled);
      color: var(--text-color-disabled);
    }
  }
}
.react-aria-SearchField {
  .react-aria-Input {
    &[data-disabled] {
      border-color: var(--border-color-disabled);
      color: var(--text-color-disabled);
    }
  }
}
.react-aria-SearchField {
  .react-aria-Input {
    &[data-disabled] {
      border-color: var(--border-color-disabled);
      color: var(--text-color-disabled);
    }
  }
}

Read only#

The isReadOnly boolean prop makes the SearchField's text content immutable. Unlike isDisabled, the SearchField remains focusable and the contents can still be copied. See the MDN docs for more information.

<MySearchField label="Email" defaultValue="abc@adobe.com" isReadOnly />
<MySearchField
  label="Email"
  defaultValue="abc@adobe.com"
  isReadOnly
/>
<MySearchField
  label="Email"
  defaultValue="abc@adobe.com"
  isReadOnly
/>

Props#


SearchField#

NameTypeDefaultDescription
isDisabledbooleanWhether the input is disabled.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
isRequiredbooleanWhether user input is required on the input before form submission.
isInvalidbooleanWhether the input value is invalid.
validate( (value: string )) => ValidationErrortruenullundefined

A 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 the isInvalid prop instead.

autoFocusbooleanWhether the element should receive focus on render.
valuestringThe current value (controlled).
defaultValuestringThe default value (uncontrolled).
autoCompletestringDescribes the type of autocomplete functionality the input should provide if any. See MDN.
maxLengthnumberThe maximum number of characters supported by the input. See MDN.
minLengthnumberThe minimum number of characters required by the input. See MDN.
patternstringRegex pattern that the value of the input must match to be valid. See MDN.
type'text''search''url''tel''email''password'string{}The type of input to render. See MDN.
inputMode'none''text''tel''url''email''numeric''decimal''search'Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
validationBehavior'native''aria''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.

childrenReactNode( (values: SearchFieldRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: SearchFieldRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: SearchFieldRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onSubmit( (value: string )) => voidHandler that is called when the SearchField is submitted.
onClear() => voidHandler that is called when the clear button is pressed.
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.
onChange( (value: T )) => voidHandler that is called when the value changes.
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.
onCompositionStartCompositionEventHandler<HTMLInputElement>Handler that is called when a text composition system starts a new text composition session. See MDN.
onCompositionEndCompositionEventHandler<HTMLInputElement>Handler that is called when a text composition system completes or cancels the current 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.
onSelectReactEventHandler<HTMLInputElement>Handler that is called when text in the input is selected. 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.
Layout
NameTypeDescription
slotstringnull

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.
excludeFromTabOrderboolean

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

aria-activedescendantstringIdentifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.
aria-autocomplete'none''inline''list''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.

aria-haspopupboolean'false''true''menu''listbox''tree''grid''dialog'Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
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.
aria-errormessagestringIdentifies the element that provides an error message for the object.

Label#

A <Label> accepts all HTML attributes.

Input#

An <Input> accepts all HTML attributes.

Button#

A <Button> accepts its contents as children. Other props such as onPress and isDisabled will be set by the SearchField.

Show props
NameTypeDefaultDescription
formstring

The <form> element to associate the button with. The value of this attribute must be the id of a <form> in the same document.

formActionstring

The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner.

formEncTypestringIndicates how to encode the form data that is submitted.
formMethodstringIndicates the HTTP method used to submit the form.
formNoValidatebooleanIndicates that the form is not to be validated when it is submitted.
formTargetstringOverrides the target attribute of the button's form owner.
namestringSubmitted as a pair with the button's value as part of the form data.
valuestringThe value associated with the button's name when it's submitted with the form data.
isDisabledbooleanWhether the button is disabled.
autoFocusbooleanWhether the element should receive focus on render.
type'button''submit''reset''button'The behavior of the button when used in an HTML form.
preventFocusOnPressboolean

Whether to prevent focus from moving to the button when pressing it.

Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control.

childrenReactNode( (values: ButtonRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: ButtonRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: ButtonRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onPress( (e: PressEvent )) => voidHandler that is called when the press is released over the target.
onPressStart( (e: PressEvent )) => voidHandler that is called when a press interaction starts.
onPressEnd( (e: PressEvent )) => void

Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target.

onPressChange( (isPressed: boolean )) => voidHandler that is called when the press state changes.
onPressUp( (e: PressEvent )) => void

Handler that is called when a press is released over the target, regardless of whether it started on the target or not.

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.
onHoverStart( (e: HoverEvent )) => voidHandler that is called when a hover interaction starts.
onHoverEnd( (e: HoverEvent )) => voidHandler that is called when a hover interaction ends.
onHoverChange( (isHovering: boolean )) => voidHandler that is called when the hover state changes.
Layout
NameTypeDescription
slotstringnull

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.
excludeFromTabOrderboolean

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

aria-expandedboolean'true''false'Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-haspopupboolean'menu''listbox''tree''grid''dialog''true''false'Indicates 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.
aria-pressedboolean'true''false''mixed'Indicates the current "pressed" state of toggle buttons.
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.

Text#

<Text> accepts all HTML attributes.

FieldError#

A <FieldError> displays validation errors.

Show props
NameTypeDescription
childrenReactNode( (values: FieldErrorRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: FieldErrorRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: FieldErrorRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.

Styling#


React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName naming convention.

.react-aria-SearchField {
  /* ... */
}
.react-aria-SearchField {
  /* ... */
}
.react-aria-SearchField {
  /* ... */
}

A custom className can also be specified on any component. This overrides the default className provided by React Aria with your own.

<SearchField className="my-searchfield">
  {/* ... */}
</SearchField>
<SearchField className="my-searchfield">
  {/* ... */}
</SearchField>
<SearchField className="my-searchfield">
  {/* ... */}
</SearchField>

In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using data attributes, which you can target in CSS selectors. For example:

input[data-hovered] {
  /* ... */
}

input[data-disabled] {
  /* ... */
}
input[data-hovered] {
  /* ... */
}

input[data-disabled] {
  /* ... */
}
input[data-hovered] {
  /* ... */
}

input[data-disabled] {
  /* ... */
}

The className and style props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.

<Button
  className={({ isPressed }) => isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Button
  className={({ isPressed }) =>
    isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Button
  className={(
    { isPressed }
  ) =>
    isPressed
      ? 'bg-gray-700'
      : 'bg-gray-600'}
/>

Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could render the clear button only when the input is non-empty.

<SearchField>
  {({state}) => (
    <>
      <Label>Search</Label>
      <Input />
      {state.value !== '' && <Button></Button>}
    </>
  )}
</SearchField>
<SearchField>
  {({state}) => (
    <>
      <Label>Search</Label>
      <Input />
      {state.value !== '' && <Button></Button>}
    </>
  )}
</SearchField>
<SearchField>
  {({ state }) => (
    <>
      <Label>
        Search
      </Label>
      <Input />
      {state.value !==
          '' && (
        <Button></Button>
      )}
    </>
  )}
</SearchField>

The states, selectors, and render props for each component used in a SearchField are documented below.

SearchField#

A SearchField can be targeted with the .react-aria-SearchField CSS selector, or by overriding with a custom className. It supports the following states:

NameCSS SelectorDescription
isEmpty[data-empty]Whether the search field is empty.
isDisabled[data-disabled]Whether the search field is disabled.
isInvalid[data-invalid]Whether the search field is invalid.
stateState of the search field.

Label#

A Label can be targeted with the .react-aria-Label CSS selector, or by overriding with a custom className.

Input#

An Input can be targeted with the .react-aria-Input CSS selector, or by overriding with a custom className. It supports the following states:

NameCSS SelectorDescription
isHovered[data-hovered]Whether the input is currently hovered with a mouse.
isFocused[data-focused]Whether the input is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the input is keyboard focused.
isDisabled[data-disabled]Whether the input is disabled.
isInvalid[data-invalid]Whether the input is invalid.

Button#

A Button can be targeted with the .react-aria-Button CSS selector, or by overriding with a custom className. It supports the following states:

NameCSS SelectorDescription
isHovered[data-hovered]Whether the button is currently hovered with a mouse.
isPressed[data-pressed]Whether the button is currently in a pressed state.
isFocused[data-focused]Whether the button is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the button is keyboard focused.
isDisabled[data-disabled]Whether the button is disabled.

Text#

The help text elements within a SearchField can be targeted with the [slot=description] and [slot=errorMessage] CSS selectors, or by adding a custom className.

FieldError#

A FieldError can be targeted with the .react-aria-FieldError CSS selector, or by overriding with a custom className. It supports the following render props:

NameDescription
isInvalidWhether the input value is invalid.
validationErrorsThe current error messages for the input if it is invalid, otherwise an empty array.
validationDetailsThe native validation details for the input.

Advanced customization#


Composition#

If you need to customize one of the components within a SearchField, such as Label or Input, in many cases you can create a wrapper component. This lets you customize the props passed to the component.

function MyInput(props) {
  return <Input {...props} className="my-input" />
}
function MyInput(props) {
  return <Input {...props} className="my-input" />
}
function MyInput(props) {
  return (
    <Input
      {...props}
      className="my-input"
    />
  );
}

Contexts#

All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).

ComponentContextPropsRef
SearchFieldSearchFieldContextSearchFieldPropsHTMLDivElement

This example shows a FieldGroup component that renders a group of search fields with a title. The entire group can be marked as disabled via the isDisabled prop, which is passed to all child search fields via the SearchFieldContext provider.

import {SearchFieldContext} from 'react-aria-components';

interface FieldGroupProps {
  title?: string,
  children?: React.ReactNode,
  isDisabled?: boolean
}

function FieldGroup({title, children, isDisabled}: FieldGroupProps) {
  return (
    <fieldset>
      <legend>{title}</legend>
      <SearchFieldContext.Provider value={{isDisabled}}>        {children}
      </SearchFieldContext.Provider>
    </fieldset>
  );
}

<FieldGroup title="Filters" isDisabled>
  <MySearchField label="Name" defaultValue="Devon" />
  <MySearchField label="Email" defaultValue="devon@example.com" />
</FieldGroup>
import {SearchFieldContext} from 'react-aria-components';

interface FieldGroupProps {
  title?: string;
  children?: React.ReactNode;
  isDisabled?: boolean;
}

function FieldGroup(
  { title, children, isDisabled }: FieldGroupProps
) {
  return (
    <fieldset>
      <legend>{title}</legend>
      <SearchFieldContext.Provider value={{ isDisabled }}>        {children}
      </SearchFieldContext.Provider>
    </fieldset>
  );
}

<FieldGroup title="Filters" isDisabled>
  <MySearchField label="Name" defaultValue="Devon" />
  <MySearchField
    label="Email"
    defaultValue="devon@example.com"
  />
</FieldGroup>
import {SearchFieldContext} from 'react-aria-components';

interface FieldGroupProps {
  title?: string;
  children?:
    React.ReactNode;
  isDisabled?: boolean;
}

function FieldGroup(
  {
    title,
    children,
    isDisabled
  }: FieldGroupProps
) {
  return (
    <fieldset>
      <legend>
        {title}
      </legend>
      <SearchFieldContext.Provider
        value={{
          isDisabled
        }}
      >        {children}
      </SearchFieldContext.Provider>
    </fieldset>
  );
}

<FieldGroup
  title="Filters"
  isDisabled
>
  <MySearchField
    label="Name"
    defaultValue="Devon"
  />
  <MySearchField
    label="Email"
    defaultValue="devon@example.com"
  />
</FieldGroup>
Show CSS
fieldset {
  padding: 1.5em;
  width: fit-content;
}
fieldset {
  padding: 1.5em;
  width: fit-content;
}
fieldset {
  padding: 1.5em;
  width: fit-content;
}

Custom children#

SearchField passes props to its child components, such as the label and input, via their associated contexts. These contexts are exported so you can also consume them in your own custom components. This enables you to reuse existing components from your app or component library together with React Aria Components.

ComponentContextPropsRef
LabelLabelContextLabelPropsHTMLLabelElement
InputInputContextInputPropsHTMLInputElement
ButtonButtonContextButtonPropsHTMLButtonElement
TextTextContextTextPropsHTMLElement

This example consumes from LabelContext in an existing styled label component to make it compatible with React Aria Components. The useContextProps hook merges the local props and ref with the ones provided via context by SearchField.

import type {LabelProps} from 'react-aria-components';
import {LabelContext, useContextProps} from 'react-aria-components';

const MyCustomLabel = React.forwardRef(
  (props: LabelProps, ref: React.ForwardedRef<HTMLLabelElement>) => {
    // Merge the local props and ref with the ones provided via context.
    [props, ref] = useContextProps(props, ref, LabelContext);
    // ... your existing Label component
    return <label {...props} ref={ref} />;
  }
);
import type {LabelProps} from 'react-aria-components';
import {
  LabelContext,
  useContextProps
} from 'react-aria-components';

const MyCustomLabel = React.forwardRef(
  (
    props: LabelProps,
    ref: React.ForwardedRef<HTMLLabelElement>
  ) => {
    // Merge the local props and ref with the ones provided via context.
    [props, ref] = useContextProps(
      props,
      ref,
      LabelContext
    );
    // ... your existing Label component
    return <label {...props} ref={ref} />;
  }
);
import type {LabelProps} from 'react-aria-components';
import {
  LabelContext,
  useContextProps
} from 'react-aria-components';

const MyCustomLabel =
  React.forwardRef(
    (
      props: LabelProps,
      ref:
        React.ForwardedRef<
          HTMLLabelElement
        >
    ) => {
      // Merge the local props and ref with the ones provided via context.
      [props, ref] =
        useContextProps(
          props,
          ref,
          LabelContext
        );
      // ... your existing Label component
      return (
        <label
          {...props}
          ref={ref}
        />
      );
    }
  );

Now you can use MyCustomLabel within a SearchField, in place of the builtin React Aria Components Label.

<SearchField>
  <MyCustomLabel>Name</MyCustomLabel>  <Input />
</SearchField>
<SearchField>
  <MyCustomLabel>Name</MyCustomLabel>  <Input />
</SearchField>
<SearchField>
  <MyCustomLabel>
    Name
  </MyCustomLabel>  <Input />
</SearchField>

Hooks#

If you need to customize things even further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useSearchField for more details.