Forms

Search Field

Search input with a leading icon, a clear button and Escape-to-clear; optional shortcut hint.

react-aria-componentslucide-react

Preview

Type to search.

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a search-field.tsx file and paste the following code into it.

    src/components/ui/search-field.tsx
    "use client";
    
    import { Search, X } from "lucide-react";
    import type { ReactNode } from "react";
    import { Button as AriaButton, Input, SearchField as AriaSearchField, type SearchFieldProps as AriaSearchFieldProps } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Label, fieldBase } from "./field";
    
    export interface SearchFieldProps extends Omit<AriaSearchFieldProps, "className" | "children"> {
      label?: string;
      placeholder?: string;
      /** Right-side hint shown while empty, e.g. <Kbd>⌘K</Kbd>. */
      shortcut?: ReactNode;
      size?: "sm" | "md";
      className?: string;
    }
    
    /** Search input with a leading icon, a clear button when filled and Escape-to-clear. */
    export function SearchField({ label, placeholder = "Search…", shortcut, size = "md", className, ...props }: SearchFieldProps) {
      return (
        <AriaSearchField {...props} className={cx("group flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <div className={cx(fieldBase, "flex items-center gap-2 px-3 group-data-focus-within:border-accent group-data-focus-within:shadow-focus", size === "md" ? "h-10" : "h-9")}>
            <Search aria-hidden className="size-4 shrink-0 text-fg-muted" />
            <Input placeholder={placeholder} className="h-full min-w-0 flex-1 bg-transparent outline-none placeholder:text-fg-muted [&::-webkit-search-cancel-button]:hidden" />
            <span className="group-data-empty:hidden">
              <AriaButton aria-label="Clear search" className="inline-flex size-5 items-center justify-center rounded-full bg-muted text-fg-muted outline-none data-hovered:bg-active data-hovered:text-fg data-focus-visible:shadow-focus">
                <X className="size-3" strokeWidth={2.4} />
              </AriaButton>
            </span>
            {shortcut && <span className="hidden group-data-empty:inline-flex">{shortcut}</span>}
          </div>
        </AriaSearchField>
      );
    }
  3. Create a field.tsx file and paste the following code into it.

    src/components/ui/field.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { FieldError, Label as AriaLabel, Text } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */
    export const fieldBase =
      "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " +
      "placeholder:text-fg-muted data-hovered:border-fg-disabled " +
      "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " +
      "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " +
      "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled";
    
    export function Label({ className, children }: { className?: string; children: ReactNode }) {
      return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>;
    }
    
    export function Description({ children }: { children: ReactNode }) {
      return (
        <Text slot="description" className="text-sm text-fg-muted">
          {children}
        </Text>
      );
    }
    
    export function ErrorMessage({ children }: { children?: ReactNode }) {
      return <FieldError className="text-sm text-error-text">{children}</FieldError>;
    }
  4. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.