Search Bar
ui-components/search-barFreeBlock
Search Bar managing URL queries client-side (Nuqs).
Install it
npx shadcn@latest add https://ui.raulcarini.dev/r/search-bar.jsonSource
1"use client";23import * as React from "react";45import { Input } from "@/components/ui/input";6import { Button } from "@/components/ui/button";7import { useDebounce } from "@/hooks/use-debounce";89import { useQueryState, parseAsString } from "nuqs";10import { SearchIcon, XIcon } from "lucide-react";1112export default function SearchBar() {13 const [query, setQuery] = useQueryState("search", parseAsString);14 const [inputValue, setInputValue] = React.useState(query || "");15 const debouncedInput = useDebounce(inputValue, 300);1617 React.useEffect(() => {18 if (debouncedInput.trim() === "") {19 if (query !== null) setQuery(null);20 } else if (debouncedInput !== query) {21 setQuery(debouncedInput);22 }23 }, [debouncedInput, query, setQuery]);2425 const clearQuery = React.useCallback(() => {26 setInputValue("");27 setQuery(null);28 }, [setQuery]);2930 const handleInputChange = React.useCallback(31 (event: React.ChangeEvent<HTMLInputElement>) => {32 setInputValue(event.target.value);33 },34 [],35 );3637 return (38 <form39 className="relative w-full max-w-xs"40 role="search"41 aria-label="Site search"42 >43 <Input44 className="peer ps-9 pe-9 h-10 [&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none"45 placeholder="Search..."46 type="search"47 value={inputValue}48 onChange={handleInputChange}49 autoComplete="off"50 aria-label="Search input"51 />52 <div className="text-muted-foreground/80 pointer-events-none absolute inset-y-0 start-0 flex items-center justify-center ps-3 peer-disabled:opacity-50">53 <SearchIcon aria-hidden="true" className="size-4" />54 </div>55 {inputValue && (56 <Button57 type="button"58 variant="ghost"59 size="icon"60 onClick={clearQuery}61 aria-label="Clear search"62 className="text-muted-foreground hover:text-foreground hover:bg-transparent dark:hover:bg-transparent absolute inset-y-0 end-0 h-full w-10 transition-colors"63 >64 <XIcon aria-hidden="true" className="animate-in fade-in zoom-in-95" />65 </Button>66 )}67 </form>68 );69}
What it pulls in
npm packages
Other registry items
Files it writes
More from UI Components
All 25 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| GitHub Contributionsgithub-contributions | UI Components | Blocks | Free | no deps | |
| GitHub Contributions Advancedgithub-contributions-advanced | UI Components | Blocks | Free | no deps | |
| GitHub Contributions Advanced Fetchergithub-contributions-advanced-fetcher | UI Components | Blocks | Free | no deps | |
| GitHub Contributions Fetchergithub-contributions-fetcher | UI Components | Blocks | Free | no deps | |
| GitHub Forksgithub-forks | UI Components | Blocks | Free | 1 dep | |
| GitHub Issuesgithub-issues | UI Components | Blocks | Free | 1 dep | |
| GitHub Pull Requestsgithub-pr | UI Components | Blocks | Free | 1 dep | |
| GitHub Starsgithub-stars | UI Components | Blocks | Free | 1 dep |
