Back
React
Motion
Tailwind
Search Bar
A minimal, opinionated search bar component. Built with React, Motion, and Tailwind CSS.
Overview
5 suggestions available.
My main goal with this component was to create a search bar that is both easy to use, and satisfying in terms of animations and UX.
Currently this is a copy-paste component rather than an installable package.
Installation
- Install the dependencies
NPM
npm install motion tailwind tailwindmerge clsx
Bun
bun add motion tailwind tailwindmerge clsx
- Add the util file for tailwind merge @/utils/cn.ts
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
- Copy the source code into your project @/components/searchbar.tsx
"use client";
import * as React from "react";
import { useState, useRef, useEffect, useCallback } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { cn } from "@/lib/utils";
import { useTheme } from "next-themes";
// -------------------- Types --------------------
export interface Option {
id: number;
label: string;
}
export interface SearchBarProps {
dropdownOptions?: Option[];
maxSuggestions?: number;
placeholder?: string;
onSelect?: (selectedOption: Option) => void;
onChange?: (inputValue: string) => void;
disabled?: boolean;
minimizable?: boolean;
showClearButton?: boolean;
clearButtonStyleClass?: string;
clearOnSelect?: boolean;
noResultsMessage?: string;
filterDebounceTime?: number;
renderItem?: (item: Option, isSelected: boolean) => React.ReactNode;
highlightMatches?: boolean;
highlightMatchesStyles?: string;
customLoader?: React.ReactNode;
width?: string;
height?: string;
darkMode?: boolean;
}
// ... rest of component
Usage
Basic
import { SearchBar } from "@/components/ui/searchbar";
<SearchBar
dropdownOptions={[
{ id: 4, label: "Apple" },
{ id: 5, label: "Banana" },
{ id: 6, label: "Orange" },
{ id: 7, label: "Pineapple" },
]}
/>
Props
dropdownOptions{ id: number, label: string }[]— The list of items to search throughplaceholderstring— Input placeholder text (default:"Search...")maxSuggestionsnumber— Max number of suggestions to show (default:5)highlightMatchesboolean— Highlight matching characters in results (default:false)highlightMatchesStylesstring— Custom class for highlighted match textminimizableboolean— Allow the search bar to be collapsed (default:false)showClearButtonboolean— Show a clear button when input has a valueclearOnSelectboolean— Clear the input after selecting a suggestionnoResultsMessagestring— Message shown when no results matchfilterDebounceTimenumber— Debounce delay for filtering in ms (default:100)disabledboolean— Disable the inputdarkModeboolean— Override dark mode detectionwidthstring— Width of the search bar (default:"400px")heightstring— Height of the search bar (default:"48px")renderItem(item, isSelected) => ReactNode— Custom renderer for dropdown itemsonSelect(item) => void— Callback fired when an item is selectedonChange(value) => void— Callback fired on input changecustomLoaderReactNode— Custom loading element shown in the dropdown