import { useMemo, useState } from 'react';
import { Meta, StoryObj } from '@storybook/react-vite';
import { GlobalSearch, GlobalSearchGroup } from './GlobalSearch';
import {
globalSearchStates,
GlobalSearchState,
resultGroups,
suggestionGroups,
} from './GlobalSearch.states';
import './global-search-story.css';
// Live example: filters the sample results by what is typed. The screen owns
// this in real use; the component only renders the groups it is given.
const InteractiveSearch = (args: React.ComponentProps<typeof GlobalSearch>) => {
const [query, setQuery] = useState('');
const groups = useMemo<GlobalSearchGroup[]>(() => {
const typed = query.trim().toLowerCase();
if (!typed) return suggestionGroups;
return resultGroups
.map((group) => ({
...group,
options: group.options.filter(
(option) =>
group.id === 'assistant' ||
option.label.toLowerCase().includes(typed) ||
option.description?.toLowerCase().includes(typed),
),
}))
.filter((group) => group.options.length > 0);
}, [query]);
const hasMatches = groups.some((group) => group.id !== 'assistant');
return (
<div
className={`global-search-story global-search-story--open${args.variant === 'dialog' ? ' global-search-story--nav' : ''}`}
>
<GlobalSearch
{...args}
groups={groups}
noResultsText={
query.trim() && !hasMatches
? `We couldn’t find results for “${query.trim()}”. Check the spelling or try another search.`
: undefined
}
onQueryChange={setQuery}
query={query}
statusText={`${groups.flatMap((group) => group.options).length} results available`}
/>
</div>
);
};
const meta: Meta<typeof GlobalSearch> = {
tags: ['new-pattern', 'in-progress'],
title: 'Component Proposals/Global search',
component: GlobalSearch,
parameters: {
docs: {
description: {
component:
'**New pattern:** not in PDS v2 today. One input that searches across many kinds of things (sites, environments, workspaces, business accounts) and an assistant. The results open in groups; while open, the field and the results are one expanding box. The screen passes the groups and decides what choosing a row does.',
},
},
},
argTypes: {
className: { table: { category: 'Base attributes' } },
defaultActiveId: { table: { category: 'Component configuration' } },
defaultQuery: { control: 'text', table: { category: 'Content' } },
errorText: { control: 'text', table: { category: 'Content' } },
groups: { table: { category: 'Content' } },
hasBackdrop: {
control: 'boolean',
table: { category: 'Component configuration' },
},
id: { table: { category: 'Base attributes' } },
isLoading: {
control: 'boolean',
table: { category: 'Component configuration' },
},
isOpen: {
control: 'boolean',
table: { category: 'Component configuration' },
},
label: { control: 'text', table: { category: 'Content' } },
loadingText: { control: 'text', table: { category: 'Content' } },
noResultsText: { control: 'text', table: { category: 'Content' } },
onOpenChange: { table: { category: 'Functions' } },
onOptionSelect: { table: { category: 'Functions' } },
onQueryChange: { table: { category: 'Functions' } },
onQuerySubmit: { table: { category: 'Functions' } },
placeholder: { control: 'text', table: { category: 'Content' } },
query: { control: 'text', table: { category: 'Content' } },
shortcut: {
control: 'text',
table: { category: 'Component configuration' },
},
statusText: { control: 'text', table: { category: 'Content' } },
submitLabel: { control: 'text', table: { category: 'Content' } },
translationStrings: { table: { category: 'Translation strings' } },
variant: {
control: 'inline-radio',
options: ['inline', 'dialog'],
table: { category: 'Component configuration' },
},
},
args: {
id: 'global-search',
label: 'Ask anything or search',
loadingText: 'Searching…',
placeholder: 'Ask anything or search across your account…',
submitLabel: 'Ask the assistant',
},
};
export default meta;
type Story = StoryObj<typeof GlobalSearch>;
const stateStory = (title: string): Story => {
const state = globalSearchStates.find(
(item: GlobalSearchState) => item.title === title,
);
if (!state) throw new Error(`Unknown GlobalSearch state: ${title}`);
const classes = [
'global-search-story',
state.narrow ? 'global-search-story--narrow' : '',
state.props.isOpen ? 'global-search-story--open' : '',
]
.filter(Boolean)
.join(' ');
return {
args: state.props,
parameters: { docs: { description: { story: state.description } } },
render: (args) => (
<div className={classes}>
<GlobalSearch {...args} />
</div>
),
};
};
/** Try it: click the field, type “canopy”, then use the arrow keys, Enter and Esc. */
export const Default: Story = {
render: (args) => <InteractiveSearch {...args} />,
};
export const AtRest: Story = stateStory('Default');
export const Suggestions: Story = stateStory('Suggestions');
export const Results: Story = stateStory('Results');
export const ActiveRow: Story = stateStory('Active row');
export const Loading: Story = stateStory('Loading');
export const LoadingWithResults: Story = stateStory('Loading with results');
export const NoResults: Story = stateStory('No results');
export const SearchError: Story = stateStory('Error');
export const Narrow: Story = stateStory('Narrow');
// The dialog variant: a compact trigger for a navigation bar that opens a dialog over itself.
const dialogArgs = {
label: 'Search',
placeholder: 'Search',
shortcut: '/',
variant: 'dialog' as const,
};
/** Try it: click the trigger or press `/`, then use the arrow keys, Enter and Esc. */
export const DialogTrigger: Story = {
args: dialogArgs,
parameters: {
docs: {
description: {
story:
'Variant for a navigation bar: a compact trigger with the shortcut key. It opens a dialog over the trigger (top edge level with the trigger, centered on it) with its own field, the same results and a footer of key hints. No send button.',
},
},
},
render: (args) => <InteractiveSearch {...args} />,
};
export const DialogOpen: Story = {
args: { ...dialogArgs, groups: suggestionGroups, isOpen: true },
parameters: {
docs: {
description: {
story:
'The dialog open with nothing typed: suggestions per kind and the assistant, above the key hints.',
},
story: { height: '34rem', inline: false },
},
layout: 'fullscreen',
},
render: (args) => (
<div className='global-search-story global-search-story--nav'>
<GlobalSearch {...args} />
</div>
),
};
export const DialogResults: Story = {
args: {
...dialogArgs,
defaultActiveId: 'site-main',
defaultQuery: 'canopy',
groups: resultGroups,
isOpen: true,
},
parameters: {
docs: {
description: {
story:
'The dialog with a query: rows carry tags and a second line, and the match is highlighted.',
},
story: { height: '34rem', inline: false },
},
layout: 'fullscreen',
},
render: (args) => (
<div className='global-search-story global-search-story--nav'>
<GlobalSearch {...args} />
</div>
),
};
Global search
A net-new component, GlobalSearch: one input that shows results in groups.
Net-new componentOpen in Storybook