Components
Search Field
A text field for searching, with a search icon and a button that clears it. Built on react-aria-components.
Introduction
Search Field is a text input made for search: a magnifier icon in front, a clear button behind, and Enter to submit. It does not filter anything by itself. It gives you the text, and you decide what to do with it: filter a list on every keystroke, or run a query when Enter is pressed.
- The clear button appears only when there is text. Escape clears the field too.
onChangefires on every keystroke,onSubmiton Enter, andonClearwhen the field is cleared.- A label, help text, and an error message that shows when the field is invalid.
- Works in a form: give it a
nameand the text is submitted with it.
Need suggestions while typing? Use Autocomplete instead.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Search Field uses Button for its clear button, so it needs everything Button needs.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxAdd Button first (see its Manual installation), then follow the steps below.
Manual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/search-field-styles.tsShow code
import { tv } from "tailwind-variants";
// The frame around the icon, the input and the clear button. It is a plain
// element, not the input itself, so everything sits inside one border and the
// focus ring wraps all of it.
export const searchFieldStyles = tv({
base: [
"flex w-full items-center gap-2 rounded-md text-fg transition-colors",
"focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-bg",
"group-data-invalid/search-field:border-danger",
"group-data-disabled/search-field:pointer-events-none group-data-disabled/search-field:opacity-50",
"[&_svg]:shrink-0",
],
variants: {
variant: {
outline: "border bg-transparent",
secondary: "bg-secondary text-secondary-fg",
ghost: "bg-transparent focus-within:bg-muted hover:bg-muted",
},
size: {
sm: "h-8 pr-1 pl-2.5 text-sm [&>svg]:size-4",
md: "h-9 pr-1 pl-3 text-sm [&>svg]:size-4",
lg: "h-11 pr-1.5 pl-3.5 text-base [&>svg]:size-5",
},
},
defaultVariants: { variant: "outline", size: "md" },
});2. Add the Search Field file
src/components/ui/search-field/search-field.tsxShow code
"use client";
import type { ReactNode } from "react";
import {
SearchField as AriaSearchField,
type SearchFieldProps as AriaSearchFieldProps,
FieldError,
Input,
Label,
Text,
type ValidationResult,
} from "react-aria-components";
import { SearchIcon, XIcon } from "@/components/icons";
import { Button } from "@/components/ui/button/button";
import { cn } from "@/lib/cn";
import { searchFieldStyles } from "@/types/search-field-styles";
type FieldSize = "sm" | "md" | "lg";
export interface SearchFieldProps
extends Omit<AriaSearchFieldProps, "children" | "className"> {
label?: ReactNode;
/** Help text under the field. */
description?: ReactNode;
/** Shown when the field is invalid. A function receives the validation result. */
errorMessage?: string | ((validation: ValidationResult) => string);
/** Look of the field. */
variant?: "outline" | "secondary" | "ghost";
/** Size of the field. */
size?: FieldSize;
placeholder?: string;
/** Replaces the magnifier icon. */
icon?: ReactNode;
/** Show the button that clears the text. Defaults to `true`. */
isClearable?: boolean;
className?: string;
}
const clearSize = { sm: "xs", md: "xs", lg: "sm" } as const;
/**
* A text field for searching. It has a search icon and a button that clears
* the text (also bound to Escape). Enter submits it (`onSubmit`). Built on
* react-aria's SearchField, so keyboard support, screen reader support, form
* submission (`name`) and validation come from there.
*
* It does not filter anything by itself: read the text with `onChange` (or
* `onSubmit`) and filter your own data.
*
* @example
* <SearchField label="Search" placeholder="Search components" onSubmit={search} />
*/
export function SearchField({
label,
description,
errorMessage,
variant,
size = "md",
placeholder,
icon,
isClearable = true,
className,
...props
}: SearchFieldProps) {
return (
<AriaSearchField
{...props}
className={cn(
"group/search-field flex w-full flex-col gap-1.5",
className,
)}
>
{label ? (
<Label className="text-sm font-medium group-data-required/search-field:after:ml-0.5 group-data-required/search-field:after:text-danger group-data-required/search-field:after:content-['*']">
{label}
</Label>
) : null}
<div className={searchFieldStyles({ variant, size })}>
{icon ?? <SearchIcon className="text-muted-fg" />}
<Input
placeholder={placeholder}
className="h-full min-w-0 flex-1 bg-transparent outline-none placeholder:text-muted-fg [&::-webkit-search-cancel-button]:hidden [&::-webkit-search-decoration]:hidden"
/>
{isClearable ? (
<Button
isIcon
variant="ghost"
size={clearSize[size]}
aria-label="Clear search"
// Nothing to clear while the field is empty.
className="group-data-empty/search-field:hidden"
>
<XIcon />
</Button>
) : null}
</div>
{description ? (
<Text slot="description" className="text-sm text-muted-fg">
{description}
</Text>
) : null}
<FieldError className="text-sm text-danger">{errorMessage}</FieldError>
</AriaSearchField>
);
}It imports SearchIcon and XIcon from @/components/icons. Add those icons, or swap them for your own.
3. Use it
import { SearchField } from "@/components/ui/search-field/search-field";Anatomy
A search field is a framed input with an icon and a button.
<SearchField
label="Search"
description="Searches every page"
placeholder="Search components"
/>| Part | Rendered as | Notes |
|---|---|---|
| Label | label | Names the field. Clicking it focuses the input. |
| Icon | svg | A magnifier by default. Replace it with the icon prop. |
| Input | input type=search | Where the user types. |
| Clear button | Button | Empties the input. Shown only when there is text. Escape does the same. |
| Description / error | text under the field | Help text, replaced by the error message when the field is invalid. |
Without a label, give the field an aria-label so a screen reader can name it.
Examples
Live previews with code you can copy.
Usage
Label
Every field needs a name. Use label, or aria-label when the label is written elsewhere.
Description
Help text under the field.
Custom icon
Swap the magnifier for another icon.
Uncontrolled
Let the component keep its own text. Start with defaultValue, and give it a name to read the value from a form.
Controlled
Keep the text in your own state with value and onChange, so you can read it, set it or reset it from outside.
value: “button”
Submit
onSubmit runs when Enter is pressed, which suits searches that hit a server. onClear runs when the field is cleared.
Nothing submitted yet.
Filtering a list
Filter as the user types by reading the value on every change.
- Autocomplete
- Button
- Button Group
- File Trigger
- Search Field
- Select
- Toggle
- Toggle Group
Sizes
Variants
Disabled
Validation
Set isInvalid with an errorMessage. The field gets a red border and the message replaces the description.
Component API
Search Field takes the props below, plus everything the react-aria SearchField accepts.
| Prop | Type | Default |
|---|---|---|
| label | ReactNode | - |
| description | ReactNode | - |
| errorMessage | string | (validation) => string | - |
| placeholder | string | - |
| value | string | - |
| defaultValue | string | "" |
| onChange | (value: string) => void | - |
| onSubmit | (value: string) => void | - |
| onClear | () => void | - |
| icon | ReactNode | magnifier |
| isClearable | boolean | true |
| name | string | - |
| variant | "outline" | "secondary" | "ghost" | "outline" |
| size | "sm" | "md" | "lg" | "md" |
| autoFocus | boolean | false |
| isDisabled | boolean | false |
| isReadOnly | boolean | false |
| isInvalid | boolean | false |
| isRequired | boolean | false |
| aria-label | string | - (when there is no label) |