Search Bar

v1.0.0
Input

A powerful search component with keyboard shortcuts, suggestions, and multiple variants

Dependencies

framer-motion
lucide-react
@/components/ui/input
@/components/ui/button
@/components/ui/badge
@/lib/utils
Installation
Add this component to your project

CLI Installation

npx shadcn@latest add https://ui.authmate.xyz/registry/search-bar.json

Manual Installation

// 1. Install dependencies
npm install framer-motion lucide-react

// 2. Make sure you have these shadcn components installed:
npx shadcn@latest add input button badge

// 3. Copy the component code to components/ui/search-bar.tsx

// 4. Add global keyboard event listener to your root layout (optional for global shortcuts):
// In your app/layout.tsx or _app.tsx:
useEffect(() => {
  const handleKeyDown = (e: KeyboardEvent) => {
    if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
      e.preventDefault()
      // Focus your search bar
      document.querySelector('[data-search-bar]')?.focus()
    }
  }
  
  document.addEventListener('keydown', handleKeyDown)
  return () => document.removeEventListener('keydown', handleKeyDown)
}, [])

// 5. Usage example:
import { SearchBar } from "@/components/ui/search-bar"
import { useState } from "react"

export default function MySearchComponent() {
  const [searchValue, setSearchValue] = useState("")
  
  const suggestions = [
    {
      id: "1",
      title: "Dashboard",
      description: "Go to dashboard",
      category: "Navigation"
    },
    {
      id: "2", 
      title: "Settings",
      description: "Open settings",
      category: "Configuration"
    }
  ]

  return (
    <div className="w-full max-w-md mx-auto">
      <SearchBar
        placeholder="Search or press Cmd+K..."
        value={searchValue}
        onChange={setSearchValue}
        suggestions={suggestions}
        onSearch={(query) => {
          console.log("Searching for:", query)
          // Perform your search logic here
        }}
        onSelect={(suggestion) => {
          console.log("Selected:", suggestion)
          // Handle suggestion selection
        }}
        shortcutKey="k" // Cmd+K or Ctrl+K to focus
        variant="default"
        showSuggestions={true}
      />
    </div>
  )
}
Props
Component properties and their types
placeholder
string
default: Search...

Placeholder text for the search input

value
string
default: undefined

Controlled value of the search input

onChange
(value: string) => void
default: undefined

Callback fired when the input value changes

onSearch
(value: string) => void
default: undefined

Callback fired when search is submitted

onSelect
(suggestion: SearchSuggestion) => void
default: undefined

Callback fired when a suggestion is selected

suggestions
SearchSuggestion[]
default: []

Array of search suggestions to display

showSuggestions
boolean
default: true

Whether to show the suggestions dropdown

showShortcuts
boolean
default: true

Whether to enable keyboard shortcuts

shortcutKey
string
default: k

Key to use for the focus shortcut (Cmd/Ctrl + key)

variant
default | command | floating
default: default

Visual variant of the search bar

size
sm | md | lg
default: md

Size of the search bar

disabled
boolean
default: false

Whether the search bar is disabled

loading
boolean
default: false

Whether to show loading state

maxSuggestions
number
default: 8

Maximum number of suggestions to display

Examples
4 different usage examples

Basic Usage

import { SearchBar } from "@/components/ui/search-bar"
import { useState } from "react"

export function BasicSearch() {
  const [value, setValue] = useState("")
  
  return (
    <SearchBar
      placeholder="Search..."
      value={value}
      onChange={setValue}
      onSearch={(query) => console.log("Search:", query)}
    />
  )
}

Registry

View the component registry file

View Registry
Live Preview
Interactive
4 examples available