Search Bar Suggestions
ui-components/search-bar-suggestionsFreeBlock
Search bar featuring autosuggestions (Nuqs).
Install it
npx shadcn@latest add https://ui.raulcarini.dev/r/search-bar-suggestions.jsonSource
1"use client";23import * as React from "react";4import { Button } from "@/components/ui/button";5import {6 Command,7 CommandGroup,8 CommandInput,9 CommandItem,10 CommandList,11} from "@/components/ui/command";1213import { useQueryState, parseAsString } from "nuqs";14import { Loader2Icon, XIcon } from "lucide-react";1516import { useDebounce } from "@/hooks/use-debounce";17import { cn } from "@/lib/utils";1819// Code at https://github.com/R4ULtv/ui/tree/master/server/search-frameworks.tsx20import { searchFrameworks, Framework } from "@/server/search-frameworks";2122export default function SearchBarSuggestions() {23 const [query, setQuery] = useQueryState("framework", parseAsString);24 const [inputValue, setInputValue] = React.useState(query || "");25 const debouncedInput = useDebounce(inputValue, 200);2627 const [suggestions, setSuggestions] = React.useState<Framework[]>([]);28 const [isLoading, setIsLoading] = React.useState(false);2930 const clearQuery = React.useCallback(() => {31 setInputValue("");32 setQuery(null);33 setSuggestions([]);34 }, [setQuery]);3536 const handleInputChange = React.useCallback((value: string) => {37 setInputValue(value);38 }, []);3940 const handleSelect = React.useCallback(41 (value: string) => {42 setQuery(value);43 setInputValue(value);44 },45 [setQuery],46 );4748 React.useEffect(() => {49 if (!debouncedInput || debouncedInput.length < 2) {50 setSuggestions([]);51 setIsLoading(false);52 return;53 }54 const getSuggestions = async () => {55 if (debouncedInput && debouncedInput.length > 0) {56 setIsLoading(true);57 const data = await searchFrameworks(debouncedInput);58 setSuggestions(data);59 setIsLoading(false);60 } else {61 setSuggestions([]);62 setIsLoading(false);63 }64 };65 getSuggestions();66 }, [debouncedInput]);6768 return (69 <Command70 shouldFilter={false}71 className="w-full max-w-[322px] bg-input/30 border p-0 rounded-md!"72 >73 <div74 className={cn(75 "relative [&_div[data-slot='command-input-wrapper']]:h-10 [&_[data-slot=input-group]]:!border-0 [&_[data-slot=input-group]]:!shadow-none [&_[data-slot=input-group]]:!bg-transparent",76 suggestions.length > 077 ? "[&_div[data-slot='command-input-wrapper']]:border-b"78 : "[&_div[data-slot='command-input-wrapper']]:border-0",79 )}80 >81 <CommandInput82 className="peer pe-9"83// … truncated
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 |
