Search Input

Search field with a search icon and a button that clears the value.

React

Ark UI has no search input yet, so the parts come from @isbatak/ark-search-input, an Ark UI style package for React, Solid, Vue and Svelte. It keeps the value in plain component state, with no state machine. The input shares its sizes and variants with the input recipe. Only React has the styled component so far.

  • Renders a native <input type="search"> and hides the browser's own clear button
  • The clear trigger only shows when there is a value, and puts focus back on the input after clearing
  • Escape clears the value while the input has focus

Usage

import { Icon } from "@isbatak/react-ui/icon"
import { SearchInput } from "@isbatak/react-ui/search-input"

export function SearchInputDemo() {
  return (
    <SearchInput.Root maxW="xs">
      <SearchInput.Label>Search</SearchInput.Label>
      <SearchInput.Control>
        <SearchInput.Icon>
          <Icon name="search" />
        </SearchInput.Icon>
        <SearchInput.Input placeholder="Search components" />
        <SearchInput.ClearTrigger>
          <Icon name="close" />
        </SearchInput.ClearTrigger>
      </SearchInput.Control>
    </SearchInput.Root>
  )
}
<SearchInput.Root>
  <SearchInput.Label />
  <SearchInput.Control>
    <SearchInput.Icon />
    <SearchInput.Input />
    <SearchInput.ClearTrigger />
  </SearchInput.Control>
</SearchInput.Root>

SearchInput.Icon is hidden from assistive technology, and clicking it focuses the input. The clear trigger looks like a ghost icon button. The icons here are search and close from the active sprite, so they follow your own icons.

Examples

Sizes

Set size on the root to xs, sm, md or lg. The default is md.

Variants

Set variant on the root to outline, subtle or flushed. The default is outline.

Default value

Set defaultValue to start with a query.

Controlled

Pass value and onValueChange to own the query, for example to filter a list as people type.

Query: empty

Root provider

Create the state with useSearchInput and pass it to SearchInput.RootProvider to control the input from outside. The returned API has value, empty, setValue, clearValue and focus.

Without a label

Leave out SearchInput.Label when the field explains itself, and give the input an aria-label instead.

Without an icon

Leave out SearchInput.Icon and the input drops the space it reserves at its start.

Disabled

Set disabled on the root to disable the input and the clear trigger. readOnly keeps the value selectable and hides the clear trigger.

Invalid

Set invalid to give the input the error border. Add the error text below it, here with the field recipe's errorText. required, name and form go on the root too.

Type at least 2 characters.

Translations

The clear trigger's label is "Clear search". Pass translations.clearTrigger to change it.

Recipe

VariantValuesDefault
variantoutline, subtle, flushedoutline
sizexs, sm, md, lgmd
VariableDescription
--input-heightThe input height, set by size. The icon area and the space for the clear trigger
--focus-colorThe focus ring color, colors.colorPalette.focusRing
--error-colorThe border and focus ring color when invalid, colors.border.error