tapcn
Components

Search Bar

A mobile-friendly search input with icon, clear button, and optional cancel action.

Search Bar

A mobile-friendly search input with a leading search icon, inline clear button, and an optional cancel action. Built on React Native's TextInput.

Installation

npx @tapcn/cli add search-bar

Or using shadcn directly:

npx shadcn@latest add https://tapcn.vercel.app/r/search-bar.json

Usage

import { useState } from 'react';
import { SearchBar } from '~/components/ui/search-bar';

export function Example() {
  const [query, setQuery] = useState('');

  return (
    <SearchBar
      value={query}
      onChangeText={setQuery}
      placeholder="Search..."
    />
  );
}

With cancel button

import { useState } from 'react';
import { SearchBar } from '~/components/ui/search-bar';

export function Example() {
  const [query, setQuery] = useState('');

  return (
    <SearchBar
      value={query}
      onChangeText={setQuery}
      showCancel
      onCancel={() => setQuery('')}
      autoFocus
    />
  );
}

Props

PropTypeDefaultDescription
valuestring--The current search text.
onChangeText(text: string) => void--Callback when the text changes.
placeholderstring"Search"Placeholder text for the input.
onCancel() => void--Callback when the cancel button is pressed.
showCancelbooleanfalseWhether to show the cancel button.
autoFocusbooleanfalseWhether to auto-focus the input on mount.
classNamestring--Additional NativeWind classes for the outer container.
refRef<TextInput>--Ref forwarded to the underlying TextInput.

Features

  • Search icon: A leading magnifying glass icon inside the input field.
  • Clear button: An X button appears when the input has text, clearing the value on press.
  • Cancel action: An optional "Cancel" text button to the right of the input.

Platform Behavior

  • Web: Uses outline-none for the input and web-specific placeholder styling.
  • Native: Uses native placeholder color styling.

Dependencies

  • icon -- for the search and clear icons
  • lucide-react-native -- Search and X icons

On this page