← All proposals

Global search

A net-new component, GlobalSearch: one input that shows results in groups.

Net-new componentOpen in Storybook

proposals/GlobalSearch/GlobalSearch.mdx59 lines · View on GitHub
import { Canvas, Controls, Meta, Title } from '@storybook/addon-docs/blocks';

import * as GlobalSearchStories from './GlobalSearch.stories';

<Meta of={GlobalSearchStories} name='Usage' />

<Title />

## Purpose

One input that searches across many kinds of things and shows the results in groups. While it is open, the field and the results are one expanding box. It also offers an assistant, so the same field can start a chat.

## Usage

Use it as the entry point for finding things across an account: sites, environments, workspaces and business accounts. For a single list of one kind of thing, use `SiteSearch` or `Combobox` instead. For writing a prompt, use `Composer`.

- The screen owns the data. Pass `groups` (each with a `heading` and `options`) and keep them in step with `query`. The component filters nothing.
- Focus opens the list. With nothing typed, pass suggestions (recent searches, then one group per kind). Once someone types, pass results: rows show a second line and tags, and the match is highlighted.
- Choosing a row calls `onOptionSelect`. The screen decides what happens (navigate, refill the field, start a chat). Enter with no row active calls `onQuerySubmit`.
- Show the state of the search with `isLoading` and `errorText`. While loading with results, keep passing the current groups so the list never flashes empty between keystrokes.
- When nothing matched, pass `noResultsText` with a next step. The screen decides when, because the assistant row can keep the list non-empty.
- Opening eases in: the page dims and the results fade and slide in (about 200 to 300ms), and the open box is lifted with a shadow. Closing is instant, and none of the motion plays for people who prefer reduced motion.
- Set `hasBackdrop` to false when the field is not the main thing on the page, or to show several open states at once.
- In a narrow container the tags go and only the active row keeps its action label.
- For a navigation bar use `variant='dialog'`. A compact trigger with the `shortcut` key beside it opens a dialog over itself, with its own field, the same results and a footer of key hints. It has no send button, so `submitLabel` is not needed. The dialog always dims the page, and opening eases in. Use the default `inline` for a field that sits on the page.

<Canvas of={GlobalSearchStories.DialogOpen} />

## Keyboard

- Arrow keys move the active row. The focus stays in the input.
- Enter chooses the active row.
- Esc closes the list. Tab closes it and moves on.
- Clicking outside closes it. In the dialog, a click on the dimmed page closes it, and focus returns to the trigger.
- In the dialog, the `shortcut` key (for example `/`) opens it from anywhere on the page. It does not fire while someone types in a field, or while another PDS overlay is open.

## Accessibility

The input is a `combobox` and the list a `listbox` with groups and options, using `aria-activedescendant`. `statusText` is announced through a live region (the result count). Provide `translationStrings` for the Esc hint and the list label. In the dialog variant the trigger is a button with `aria-haspopup='dialog'` and `aria-expanded`. The dialog has `role='dialog'`, `aria-modal='true'` and a name (`translationStrings.dialogLabel`). Focus moves into its field on open and back to the trigger on close. The key hints in the footer are hidden from assistive technology, since the keys are standard. The rest of the page is not made inert; Tab closes the dialog instead of leaving it. Not yet tested with a screen reader.

## Implementation

See the table below for the available props. The other stories show every state.

- `defaultActiveId` — Id of the option that starts active. Use it to show the keyboard state.
- `defaultQuery` — Initial query when the component is uncontrolled.
- `isOpen` — Control whether the results are open. Leave unset to let the component open on focus and close on Esc, Tab and outside clicks.
- `loadingText` — Text shown next to the spinner while the first results load.
- `onOpenChange` — Called when the results open or close.
- `onQueryChange` — Called when the text changes.
- `shortcut` — Key that opens the dialog from anywhere on the page, for example `/`. Shown in the trigger and bound with `useKeyboardShortcut`. Only used with `variant='dialog'`.
- `submitLabel` — Accessible name of the send button (a translatable string). The send button shows with `variant='inline'` only.
- `variant` — `inline` (default): the field stays where it is and its results open under it as one box. `dialog`: a compact trigger opens a dialog over itself, with its own field.

<hr />

<Canvas of={GlobalSearchStories.Default} />
<Controls of={GlobalSearchStories.Default} />