PeakUI

SearchField

Search input with icon and clear button

Usage

import {Label, SearchField} from "@peakui/react";

Anatomy

import {Description, FieldError, Label, SearchField} from "@peakui/react";

export default () => (
  <SearchField>
    <Label />
    <SearchField.Group>
      <SearchField.SearchIcon />
      <SearchField.Input />
      <SearchField.ClearButton />
    </SearchField.Group>
    <Description />
    <FieldError />
  </SearchField>
);

SearchField is the root: it owns the value, the validation state and the ids that link the parts. Label, Description and FieldError are its children, each a component of its own.

Examples

Variants

variant="primary" (default) carries the field shadow; variant="secondary" is a flat fill for surfaces.

In Surface

Use variant="secondary" inside a Surface.

With Description

Required

isRequired marks the label and makes the field required for form submission.

Disabled State

Full Width

Validation

isInvalid marks the field as invalid and makes FieldError render.

Controlled

onChange receives the value as a string, not an event.

With Validation

Compute isInvalid from the value to validate as the user types.

Custom Icons

Both SearchField.SearchIcon and SearchField.ClearButton accept a custom icon as children.

With Keyboard Shortcut

SearchField.Input forwards ref to the <input>.

Render Function

render replaces the root element; pass the props and the ref through to it.

Form Example

Customization

Tailwind CSS

Global CSS

@layer components {
  .search-field__group {
    --search-field-group-radius: var(--radius-lg);
  }

  .search-field__search-icon {
    color: var(--color-accent);
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The SearchField component uses these CSS classes (view source styles):

Base & Variant Classes

  • .search-field - The field root
  • .search-field--primary - Primary variant (default)
  • .search-field--secondary - Flat fill for surfaces
  • .search-field--full-width - Stretches the root to the container's width

Element Classes

  • .search-field__group - The frame around the icon, the control and the clear button
  • .search-field__group--full-width - Stretches the group to the container's width
  • .search-field__input - The control
  • .search-field__search-icon - The search icon
  • .search-field__clear-button - The clear button

Slots

  • [data-slot="search-field"]
  • [data-slot="search-field-group"]
  • [data-slot="search-field-input"]
  • [data-slot="search-field-search-icon"]
  • [data-slot="search-field-clear-button"]

CSS Variables

VariablePurpose
--search-field-group-bgGroup background color
--search-field-group-bg-hoverGroup background color when hovered
--search-field-group-bg-focusGroup background color when the control is focused
--search-field-group-fgText color
--search-field-group-placeholderPlaceholder color
--search-field-group-borderGroup border color
--search-field-group-border-hoverGroup border color when hovered
--search-field-group-border-focusGroup border color when the control is focused
--search-field-group-radiusGroup corner radius
--search-field-group-shadowGroup box shadow

Interactive States

  • Hover: the group's :hover or [data-hovered]
  • Focus: the group's :focus-within or [data-focus-within]
  • Invalid: the root or group's [data-invalid] or [aria-invalid="true"]; the root also hides a Description while invalid
  • Disabled: the root or group's [data-disabled] or [aria-disabled="true"]
  • Empty: the root's [data-empty="true"] hides the clear button

API Reference

SearchField

PropTypeDefaultDescription
variant'primary' | 'secondary''primary'Surface style, applied to the group. Inside a field it defaults to the outer field's variant
fullWidthbooleanfalseWhether the field and its group take the full width of their container
valuestring-The controlled value
defaultValuestring-The initial value when uncontrolled
onChange(value: string) => void-Handler called with the new value
onClear() => void-Handler called when the clear button is pressed
onSubmit(value: string) => void-Handler called when the user presses Enter
namestring-Name of the field in form submission
isRequiredbooleanfalseWhether a value is required for form submission
isInvalidbooleanfalseWhether the value is invalid
isDisabledbooleanfalseWhether the field is disabled
isReadOnlybooleanfalseWhether the value can be selected but not changed
validate(value: string) => string | string[] | true | null | undefined-Returns an error message, or messages, for an invalid value, and true, null or undefined for a valid one
validationBehavior'native' | 'aria''native'Whether validation blocks form submission (native) or only marks the field (aria)
childrenReact.ReactNode | (values: SearchFieldRenderProps) => React.ReactNode-The parts of the field, or a function of the field's state
classNamestring | (values: SearchFieldRenderProps) => string-Additional classes, or a function of the field's state
renderDOMRenderFunction<'div', SearchFieldRenderProps>-Replaces the rendered root element

Every other React Aria SearchField prop is accepted, including autoFocus, maxLength, autoComplete and the aria-* attributes.

SearchField.Group

PropTypeDefaultDescription
childrenReact.ReactNode | (values: GroupRenderProps) => React.ReactNode-The icon, the control and the clear button
classNamestring | (values: GroupRenderProps) => string-Additional classes, or a function of the group's state

Every other React Aria Group prop is accepted.

SearchField.Input

Renders an <input type="search"> and accepts its attributes, such as placeholder, autoFocus and ref.

PropTypeDefaultDescription
classNamestring | (values: InputRenderProps) => string-Additional classes, or a function of the input's state

SearchField.SearchIcon

Renders IconSearch; a custom icon replaces it as children.

PropTypeDefaultDescription
childrenReact.ReactElement<IconSearch />A custom icon element, which receives the classes and the slot
classNamestring-Additional classes

SearchField.ClearButton

A CloseButton wired to the field: pressing it clears the value and calls onClear. It is hidden while the field is empty, and excluded from the tab order.

PropTypeDefaultDescription
childrenReact.ReactNode | ((values: ButtonRenderProps) => React.ReactNode)CloseIconA custom icon, or a function of the button's state
classNamestring | (values: ButtonRenderProps) => string-Additional classes, or a function of the button's state
aria-labelstring'Clear search'The button's accessible name; a consumer-supplied label overrides the default

Render Props

When children or className of the root is a function, it receives:

PropTypeDescription
isEmptybooleanWhether the field is empty
isDisabledbooleanWhether the field is disabled
isInvalidbooleanWhether the field is invalid
isReadOnlybooleanWhether the field is read only
isRequiredbooleanWhether the field is required

On this page