SearchDialog
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Button, SearchDialog, SearchGroup, SearchResult } from "@cherry-design-system/react";
const pages = [
{ label: "Button", href: "/components/button" },
{ label: "Modal", href: "/components/modal" },
];
export function DocsSearch() {
const router = useRouter();
const [isOpen, setIsOpen] = useState(false);
const [query, setQuery] = useState("");
const results = pages.filter((page) => page.label.toLowerCase().includes(query.toLowerCase()));
return (
<>
<Button variant="outline" onClick={() => setIsOpen(true)}>Search</Button>
<SearchDialog
open={isOpen}
onClose={() => setIsOpen(false)}
query={query}
onQueryChange={setQuery}
placeholder="Search components"
empty="No results"
status={`${results.length} of ${pages.length}`}
>
{results.length > 0 ? (
<SearchGroup label="Components">
{results.map((page) => (
<SearchResult
key={page.href}
onSelect={() => {
setIsOpen(false);
router.push(page.href);
}}
>
{page.label}
</SearchResult>
))}
</SearchGroup>
) : null}
</SearchDialog>
</>
);
}SearchDialog is a command palette: a search input (role="combobox") over a listbox of results, inside a native modal <dialog>. You own the query and the filtering; the dialog handles the keyboard, the highlight and the layout. Compose results with SearchGroup and SearchResult. For picking a value inside a form use Combobox.
empty shows only when children is empty, so render nothing (not an empty SearchGroup) when there are no matches. Selecting a result does not close the dialog: set open to false yourself in onSelect.
Examples
Icons and descriptions
import { SearchResult } from "@cherry-design-system/react";
import { Box } from "lucide-react";
<SearchResult icon={<Box />} description="Actions" onSelect={() => go("/components/button")}>
Button
</SearchResult>;Preview panel
preview adds a side panel, for example a preview of the highlighted result. Track the highlight with onHighlight on each result.
"use client";
import { useState } from "react";
import { SearchDialog, SearchResult } from "@cherry-design-system/react";
const items = [
{ id: "button", label: "Button", summary: "Triggers an action." },
{ id: "modal", label: "Modal", summary: "Blocks the page until the user responds." },
];
export function PreviewSearch({ open, onClose }: { open: boolean; onClose: () => void }) {
const [query, setQuery] = useState("");
const [highlighted, setHighlighted] = useState(items[0]);
return (
<SearchDialog
open={open}
onClose={onClose}
query={query}
onQueryChange={setQuery}
preview={<p>{highlighted.summary}</p>}
>
{items.map((item) => (
<SearchResult key={item.id} onHighlight={() => setHighlighted(item)} onSelect={onClose}>
{item.label}
</SearchResult>
))}
</SearchDialog>
);
}Translated labels
label names the dialog and the input, placeholder fills the input, and navigateLabel / selectLabel translate the footer hints. hints={false} hides the hints footer.
<SearchDialog
open={open}
onClose={onClose}
query={query}
onQueryChange={setQuery}
label="Rechercher"
placeholder="Rechercher"
navigateLabel="naviguer"
selectLabel="ouvrir"
/>Properties
SearchDialog
Controlled open state.
Called on Escape or a backdrop click. Set open to false here.
The search text (controlled). Filter the results from it.
Called with the new text as the user types.
SearchGroups and SearchResults matching the query.
Shown instead of the results when children is empty.
Side panel, such as a preview of the highlighted result.
End of the footer, announced politely (for example "31 of 31").
Input placeholder. Defaults to "Search".
Accessible name of the dialog and the input. Defaults to "Search".
Footer hint next to the arrow keys. Defaults to "navigate".
Footer hint next to the Enter key. Defaults to "open".
Show the keyboard hints footer. Defaults to true.
SearchDialog renders a <dialog>, forwards its ref to it and passes other <dialog> attributes through. It is a Client Component.
SearchGroup
Small all-caps heading over the group; names it for assistive tech.
SearchGroup renders a <div>, forwards its ref and passes other <div> attributes through.
SearchResult
Icon before the label.
Second line under the label.
Runs on click, or on Enter while it is the highlighted result.
Runs when it becomes the highlighted result, by keyboard or pointer.
Shown but never highlighted or selected.
SearchResult renders a <div role="option">, forwards its ref and passes other <div> attributes through.
Styling
| Hook | Values |
|---|---|
| Class | cherry-search-dialog |
data-layout | list, preview (set when preview is passed) |
| States | [open], ::backdrop |
| Parts | cherry-search-dialog__header, cherry-search-dialog__icon, cherry-search-dialog__input, cherry-search-dialog__content, cherry-search-dialog__results, cherry-search-dialog__group, cherry-search-dialog__group-label, cherry-search-dialog__empty, cherry-search-dialog__preview, cherry-search-dialog__footer, cherry-search-dialog__hints, cherry-search-dialog__hint, cherry-search-dialog__status |
| Result class | cherry-search-result |
| Result states | data-active, aria-selected, aria-disabled, :hover |
| Result parts | cherry-search-result__icon, cherry-search-result__text, cherry-search-result__label, cherry-search-result__description, cherry-search-result__arrow |
The dialog reads --cherry-bg-elevated, --cherry-radius-2xl and --cherry-elevation-xl, with --cherry-bg-overlay for the backdrop. The highlighted result uses --cherry-bg-muted and --cherry-accent-default.
Accessibility
SearchDialog is a modal dialog around a combobox. Up and Down move the active result through aria-activedescendant while focus stays in the input, Enter opens the active result and Escape closes the dialog. Pass status so the result count is announced. Each result is role="option" inside the listbox.