Skip to Content
docscn

Search dialog

Search your docs from a dialog opened with a keyboard shortcut.

Installation

npx shadcn@latest add @docscn/search-dialog-default

Usage

RootProvider opens the default search dialog with ⌘ K (Ctrl K on Windows and Linux), or from the search triggers in the layouts. It queries /api/search, which you create with Fumadocs Core's search server:

app/api/search/route.ts
import { createFromSource } from "fumadocs-core/search/server";
import { source } from "@/lib/source";

export const { GET } = createFromSource(source);

Results show pages, headings, text and table rows. Use the arrow keys and Enter to open one, and Esc to close the dialog. If a search fails, the dialog says so above the results of the last search that worked.

Matches are highlighted with the CSS Custom Highlight API, in your --primary colour and underlined. Style them with ::highlight(fd-search).

Options

Pass options to the default dialog through RootProvider:

<RootProvider
  search={{
    options: {
      api: "/api/search",
      delayMs: 100,
      tags: [{ name: "Guides", value: "guides" }],
    },
    links: [["Getting started", "/docs/getting-started"]],
  }}
>
OptionTypeDescription
apistringThe search API URL. Defaults to /api/search.
delayMsnumberDebounce delay before searching.
tagsTagItem[]Tag filters shown below the results.
defaultTagstringThe tag selected at first.
allowClearbooleanAllow deselecting the tag.
footerReactNodeExtra content at the bottom of the dialog.
links[name, href][]Links shown before the reader types a query.

Build your own dialog

The search-dialog component exports the parts the default dialog is made of, for other search clients. Fumadocs Core has a search hook for each client, like useAlgoliaSearch() or useOramaCloudSearch(), and experimental_useSearch() for others. They return the dialog's props:

import { useFetchSearch } from "fumadocs-core/search/client";
import {
  SearchDialog,
  SearchDialogClose,
  SearchDialogContent,
  SearchDialogHeader,
  SearchDialogIcon,
  SearchDialogInput,
  SearchDialogList,
  SearchDialogOverlay,
  type SharedProps,
} from "@/components/docs/components/dialog/search";

export default function CustomSearchDialog(props: SharedProps) {
  const search = useFetchSearch({ api: "/api/search" });

  return (
    <SearchDialog {...search} {...props}>
      <SearchDialogOverlay />
      <SearchDialogContent>
        <SearchDialogHeader>
          <SearchDialogIcon />
          <SearchDialogInput />
          <SearchDialogClose />
        </SearchDialogHeader>
        <SearchDialogList />
      </SearchDialogContent>
    </SearchDialog>
  );
}

SearchDialogList shows the results of the last search, or its defaultItems before the reader types. Pass Item or Table to render results your own way.

Pass it to RootProvider as search={{ SearchDialog: CustomSearchDialog }}. The dialog is built on Base UI's Dialog, the primitive under shadcn/ui's dialog, with shadcn/ui kbd.