Skip to main content

Search Dialog

The header search for a site with a search index: a field-like trigger that grows into a modal search dialog and shrinks back. It's built on @fairgarden/docs: it calls the engine's useSearch over your sitemap and shows its results. Where a site has no index, use the Search trigger kind instead.

import { SearchDialog } from '@fairgarden/design/overlays/search-dialog'

Install @fairgarden/docs to use it. It's an optional peer dependency, and only Search Dialog, Code Block and Demo import it.

The trigger looks like a field: a magnifier, the label in --role-muted and, with keyboardShortcut, a ⌘K or Ctrl K key cap from 1024 px. The cap's box is in the server HTML, sized for "Ctrl K", and the label reserves 64 px, so neither the platform's label arriving after hydration nor the web font arriving changes the trigger's width, even where it sizes to its content, as in the Navigation Bar. Pressing it runs the expanding box's morph into the dialog. Below 768 px the dialog is the full-screen sheet, square and frameless. From 768 px it is the centered panel in the Dialog's wide width. Both have a "Close search" button at the end of the input row; from 1024 px, with keyboardShortcut, an esc key cap that closes takes its place. The page stays visible, inert and scroll-locked, with no tint.

SearchDialog takes the sitemap loader useSearch takes and builds the index when it mounts, so it's ready by the time someone opens the dialog. Each change to the input runs the engine's search, with at most five results per group. The results show under sticky group labels, with one icon per result type, matches marked by weight, and a stats row. A row is a link to the engine's URL for it (buildResultUrl). Choosing one closes the dialog and calls onNavigate, so a framework router can take over. Without onNavigate the link navigates natively.

'use client'
import { useRouter } from 'next/navigation'
import { SearchDialog } from '@fairgarden/design/overlays/search-dialog'

export function HeaderSearch() {
  const router = useRouter()
  return (
    <SearchDialog
      sitemap={() => import('./sitemap')}
      onNavigate={(href) => router.push(href)}
      keyboardShortcut
    />
  )
}

Searching these docs

Searching these docs

This one searches these docs. Press the trigger. Focus lands in the input. Arrow keys move the highlight, which starts on the first result, and Enter goes to that page. Escape, the "Close search" button or a press outside the panel closes the dialog, and focus returns to the trigger. Type "button" to see groups and the stats row, or a word with no match to see the empty status. This demo leaves keyboardShortcut off, because a page has one ⌘K listener, and here it belongs to the header's search.

SearchDialogBasic.tsx
'use client'

import * as React from 'react'
import { useRouter } from 'next/navigation'
import { SearchDialog } from '@fairgarden/design/overlays/search-dialog'

/**
 * The search dialog over this site's own sitemap: `useSearch` builds the
 * index on mount, and a chosen result navigates with the Next.js router.
 * No `keyboardShortcut`: the page's header search owns ⌘K / Ctrl K.
 */
export function SearchDialogBasic() {
  const router = useRouter()
  return (
    <SearchDialog
      label="Search the docs"
      sitemap={() => import('@/app/sitemap')}
      onNavigate={(href) => router.push(href)}
    />
  )
}

Results and timing

Before typing, the list shows the engine's default results, the site's first ten pages. Results for a new query replace the list as soon as they arrive. An empty result waits emptyDelay (400 ms) first, so "No results" never flashes while someone is still typing. After the dialog closes, the list goes back to the default results. The stats row reads "Found 12 in 0.4ms" and keeps its height while hidden. The empty message and the stats row are live regions, so screen readers announce them. Outside production builds, each row also shows its score (showScores).

searchOptions passes options to useSearch over the defaults (maxDefaultResults: 10, tolerance: 1, limit: 20, enableStemming: true). Its searchBy replaces the options each query runs with, { groupBy: { properties: ['group'], maxResult: 5 } }.

Opening takes 333 ms and closing 200 ms. Only the frame moves, and nothing fades. The input's focus ring appears in place as the box lands, never before, so it can't run ahead of the frame. A second open or close within 100 ms is ignored. Under reduced motion, in forced colors or print, in a browser without View Transitions, with morph={false}, or where --fgd-outline-morph: none applies to the trigger (the shared switch), the dialog opens and closes at once.

In print, neither the trigger nor the dialog shows.

API Reference

SearchDialog

The search dialog over a @fairgarden/docs sitemap: the trigger, and the dialog in a portal. It owns the engine (useSearch), open state, the shortcut listener, the delayed results copy and the transitions.

PropTypeDescription
label
string | undefined

Trigger text, input placeholder and the dialog’s accessible name. Default "Search".

defaultOpen
boolean | undefined

Default false.

open
boolean | undefined

Controlled open. SearchDialog calls onOpenChange inside the morph’s flushSync, so set open synchronously in it.

onOpenChange
((open: boolean) => void) | undefined
emptyDelay
number | undefined

Milliseconds before empty results replace the list. Default 400.

keyboardShortcut
boolean | undefined

⌘K / Ctrl K: a keydown listener on window in the capture phase, preventDefault + stopPropagation, guarded while open, opening or closing. Also shows the platform-aware hint and, from --lg-n-above, the “esc” button in place of the “Close search” button. The hint renders nothing on the server, then “⌘K” on Apple platforms or “Ctrl K” elsewhere. One per page. Default false.

morph
boolean | undefined

The expanding box’s morph between the trigger and the dialog [D194]. Default true; false opens and closes the dialog at once. It also honours the shared switch, --fgd-outline-morph: none on the trigger or an ancestor, and a 0 duration token (§9.14, §9.17) [D204].

onNavigate
| ((href: string, result: SearchResult) => void)
| undefined

A plain activation of a row (click, or Enter on the highlighted row) closes the dialog, preventDefault()s the row’s link and calls this with the engine’s buildResultUrl(result); a Next.js site passes (href) => router.push(href). Without it, the link navigates natively. A modifier click always follows the link natively and leaves the dialog open.

onOpenChangeComplete
((open: boolean) => void) | undefined

After the morph (or fallback) finishes. On close, the results are already reset to the engine’s defaultResults.

primary
| 'olive'
| 'sage'
| 'slate'
| 'sand'
| 'gray'
| 'mauve'
| 'brown'
| 'bronze'
| 'gold'
| 'red'
| 'ruby'
| 'crimson'
| 'tomato'
| 'pink'
| 'plum'
| 'indigo'
| 'iris'
| 'violet'
| 'purple'
| null
| undefined

Primary Radix scale: frame, labels, highlighted row, focus ring. Never defaulted; omitted, the trigger inherits the scope and the popup takes the white overlay preset’s default [D133, D139].

searchOptions
SearchDialogSearchOptions | undefined

useSearch options, merged over the source’s defaults: maxDefaultResults: 10, tolerance: 1, limit: 20, enableStemming: true. Keep flattenPage and generateSlug stable: useSearch rebuilds its index when they change.

secondary
| 'olive'
| 'sage'
| 'slate'
| 'sand'
| 'gray'
| 'mauve'
| 'brown'
| 'bronze'
| 'gold'
| 'red'
| 'ruby'
| 'crimson'
| 'tomato'
| 'pink'
| 'plum'
| 'indigo'
| 'iris'
| 'violet'
| 'purple'
| 'amber'
| 'blue'
| 'cyan'
| 'grass'
| 'green'
| 'jade'
| 'lime'
| 'mint'
| 'orange'
| 'sky'
| 'teal'
| 'yellow'
| null
| undefined

Secondary Radix scale. Never defaulted.

showScores
boolean | undefined

The score badge on each row. Default: outside production builds, as in the source.

sitemap
() => Promise<{ sitemap?: Sitemap }>

The sitemap loader useSearch indexes, e.g. () => import('./sitemap'). Read once, on mount, when the index builds; an inline arrow is fine.

className
string | undefined

On the trigger.

Additional types

searchDialog

On both roots: the trigger (the page scope) and the popup (the overlay scope). Axes: primary, secondary → scales module classes, never defaulted [D133].

type searchDialog = searchDialog
SearchDialogProps

Props for SearchDialog.

type SearchDialogProps = {
  /**
   * The sitemap loader `useSearch` indexes, e.g. `() => import('./sitemap')`.
   * Read once, on mount, when the index builds; an inline arrow is fine.
   */
  sitemap: () => Promise<{ sitemap?: Sitemap }>;
  /**
   * `useSearch` options, merged over the source's defaults:
   * `maxDefaultResults: 10`, `tolerance: 1`, `limit: 20`,
   * `enableStemming: true`. Keep `flattenPage` and `generateSlug` stable:
   * `useSearch` rebuilds its index when they change.
   */
  searchOptions?: SearchDialogSearchOptions;
  /**
   * A plain activation of a row (click, or Enter on the highlighted row)
   * closes the dialog, `preventDefault()`s the row's link and calls this
   * with the engine's `buildResultUrl(result)`; a Next.js site passes
   * `(href) => router.push(href)`. Without it, the link navigates natively.
   * A modifier click always follows the link natively and leaves the dialog
   * open.
   */
  onNavigate?: (href: string, result: SearchResult) => void;
  /**
   * Controlled open. SearchDialog calls `onOpenChange` inside the morph's
   * `flushSync`, so set `open` synchronously in it.
   */
  open?: boolean;
  /** Default `false`. */
  defaultOpen?: boolean;
  onOpenChange?: (open: boolean) => void;
  /** After the morph (or fallback) finishes. On close, the results are already reset to the engine's `defaultResults`. */
  onOpenChangeComplete?: (open: boolean) => void;
  /**
   * ⌘K / Ctrl K: a `keydown` listener on `window` in the capture phase,
   * `preventDefault` + `stopPropagation`, guarded while open, opening or
   * closing. Also shows the platform-aware hint and, from `--lg-n-above`,
   * the "esc" button in place of the "Close search" button. The hint renders
   * nothing on the server, then "⌘K" on Apple platforms or "Ctrl K"
   * elsewhere. One per page. Default `false`.
   */
  keyboardShortcut?: boolean;
  /** Milliseconds before empty results replace the list. Default `400`. */
  emptyDelay?: number;
  /** Trigger text, input placeholder and the dialog's accessible name. Default `"Search"`. */
  label?: string;
  /** The score badge on each row. Default: outside production builds, as in the source. */
  showScores?: boolean;
  /**
   * Primary Radix scale: frame, labels, highlighted row, focus ring. Never
   * defaulted; omitted, the trigger inherits the scope and the popup takes
   * the `white` overlay preset's default [D133, D139].
   */
  primary?:
    | 'olive'
    | 'sage'
    | 'slate'
    | 'sand'
    | 'gray'
    | 'mauve'
    | 'brown'
    | 'bronze'
    | 'gold'
    | 'red'
    | 'ruby'
    | 'crimson'
    | 'tomato'
    | 'pink'
    | 'plum'
    | 'indigo'
    | 'iris'
    | 'violet'
    | 'purple'
    | null;
  /** Secondary Radix scale. Never defaulted. */
  secondary?:
    | 'olive'
    | 'sage'
    | 'slate'
    | 'sand'
    | 'gray'
    | 'mauve'
    | 'brown'
    | 'bronze'
    | 'gold'
    | 'red'
    | 'ruby'
    | 'crimson'
    | 'tomato'
    | 'pink'
    | 'plum'
    | 'indigo'
    | 'iris'
    | 'violet'
    | 'purple'
    | 'amber'
    | 'blue'
    | 'cyan'
    | 'grass'
    | 'green'
    | 'jade'
    | 'lime'
    | 'mint'
    | 'orange'
    | 'sky'
    | 'teal'
    | 'yellow'
    | null;
  /** On the trigger. */
  className?: string;
  /**
   * The expanding box's morph between the trigger and the dialog [D194].
   * Default `true`; `false` opens and closes the dialog at once. It also
   * honours the shared switch, `--fgd-outline-morph: none` on the trigger
   * or an ancestor, and a 0 duration token (§9.14, §9.17) [D204].
   */
  morph?: boolean;
}
SearchDialogSearchBy

The engine’s per-query options: the second argument of useSearch().search.

type SearchDialogSearchBy = NonNullable<SearchBy<__type> | undefined>
SearchDialogSearchOptions

useSearch’s options (all but sitemap), plus the per-query options.

type SearchDialogSearchOptions = {
  /**
   * Passed with every query, `search(value, searchBy)`. Replaces the
   * default, `{ groupBy: { properties: ['group'], maxResult: 5 } }`.
   */
  searchBy?: SearchBy<__type>;
}
searchDialogTypeIcons

Row icons by the engine’s result type: page description, part widgets, export deployed_code, section format_h2, subsection format_h3.

type searchDialogTypeIcons = {
  page: 'description';
  part: 'widgets';
  export: 'deployed_code';
  section: 'format_h2';
  subsection: 'format_h3';
}

Specification: DESIGN-SYSTEM.md §9.10 (search) [D194], with the expanding box and the Dialog's sheet (§10.14).