Skip to content

Overlays

SearchDialog

tsx
"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

tsx
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.

tsx
"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.

tsx
<SearchDialog
  open={open}
  onClose={onClose}
  query={query}
  onQueryChange={setQuery}
  label="Rechercher"
  placeholder="Rechercher"
  navigateLabel="naviguer"
  selectLabel="ouvrir"
/>

Properties

SearchDialog

openbooleanrequired

Controlled open state.

onClose() => voidrequired

Called on Escape or a backdrop click. Set open to false here.

querystringrequired

The search text (controlled). Filter the results from it.

onQueryChange(query: string) => voidrequired

Called with the new text as the user types.

childrenReactNode

SearchGroups and SearchResults matching the query.

emptyReactNode

Shown instead of the results when children is empty.

previewReactNode

Side panel, such as a preview of the highlighted result.

statusReactNode

End of the footer, announced politely (for example "31 of 31").

placeholderstring

Input placeholder. Defaults to "Search".

labelstring

Accessible name of the dialog and the input. Defaults to "Search".

navigateLabelstring

Footer hint next to the arrow keys. Defaults to "navigate".

selectLabelstring

Footer hint next to the Enter key. Defaults to "open".

hintsboolean

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

labelReactNoderequired

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

iconReactNode

Icon before the label.

descriptionReactNode

Second line under the label.

onSelect() => void

Runs on click, or on Enter while it is the highlighted result.

onHighlight() => void

Runs when it becomes the highlighted result, by keyboard or pointer.

disabledboolean

Shown but never highlighted or selected.

SearchResult renders a <div role="option">, forwards its ref and passes other <div> attributes through.

Styling

HookValues
Classcherry-search-dialog
data-layoutlist, preview (set when preview is passed)
States[open], ::backdrop
Partscherry-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 classcherry-search-result
Result statesdata-active, aria-selected, aria-disabled, :hover
Result partscherry-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.