← All proposals

Global search

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

Net-new componentOpen in Storybook

proposals/GlobalSearch/GlobalSearch.states.ts249 lines · View on GitHub
import type { GlobalSearchGroup, GlobalSearchProps } from './GlobalSearch';

/**
 * Sample data and named states for GlobalSearch. The preview page renders each one; the stories
 * can reuse them as args. Sample copy, not content of the component.
 */

const assistant: GlobalSearchGroup = {
	heading: 'Assistant',
	id: 'assistant',
	options: [
		{
			actionLabel: 'Start a new chat',
			iconName: 'zappy',
			id: 'assistant-chat',
			label: 'Chat with Zappy',
			showActionAlways: true,
		},
	],
};

export const suggestionGroups: GlobalSearchGroup[] = [
	{
		heading: 'Recent searches',
		id: 'recent',
		options: [
			{
				actionLabel: 'Search',
				iconName: 'clock',
				id: 'recent-canopy',
				label: 'canopy',
			},
			{
				actionLabel: 'Search',
				iconName: 'clock',
				id: 'recent-willowmere',
				label: 'willowmere agency',
			},
			{
				actionLabel: 'Search',
				iconName: 'clock',
				id: 'recent-header',
				label: 'fix-header',
			},
		],
	},
	{
		heading: 'Sites',
		id: 'sites',
		options: ['Alumni Giving', 'Canopy Main Site', 'Bramblewood Main Site'].map(
			(label) => ({
				actionLabel: 'Jump to',
				iconName: 'laptopCode' as const,
				id: `site-${label}`,
				label,
				showActionAlways: true,
			}),
		),
	},
	{
		heading: 'Workspaces',
		id: 'workspaces',
		options: [
			['Alumni Relations', 'Bramblewood University'],
			['Canopy Coffee Roasters', 'Verdant Group'],
			['Greenline Grocery', 'Verdant Group'],
		].map(([label, inlineText]) => ({
			actionLabel: 'Jump to',
			iconName: 'folders' as const,
			id: `workspace-${label}`,
			inlineText,
			label,
			showActionAlways: true,
		})),
	},
	{
		heading: 'Business accounts',
		id: 'accounts',
		options: [
			['Bramblewood University', '35 workspaces'],
			['Verdant Group', '4 workspaces'],
			['Willowmere Agency', '5 workspaces'],
		].map(([label, inlineText]) => ({
			actionLabel: 'Jump to',
			iconName: 'buildings' as const,
			id: `account-${label}`,
			inlineText,
			label,
			showActionAlways: true,
		})),
	},
	assistant,
];

export const resultGroups: GlobalSearchGroup[] = [
	{
		heading: 'Sites',
		id: 'sites',
		options: [
			{
				actionLabel: 'Jump to',
				description: 'canopycoffee.com · Canopy Coffee Roasters',
				iconName: 'laptopCode',
				id: 'site-main',
				label: 'Canopy Main Site',
				tags: ['Framework: WordPress 7.0', 'Region: North America', '+1'],
			},
			{
				actionLabel: 'Jump to',
				description: 'wholesale.canopycoffee.com · Canopy Coffee Roasters',
				iconName: 'laptopCode',
				id: 'site-wholesale',
				label: 'Canopy Wholesale Portal',
				tags: ['Framework: WordPress 7.0', 'Region: North America', '+2'],
			},
			{
				actionLabel: 'Jump to',
				description: 'rewards.canopycoffee.com · Canopy Coffee Roasters',
				iconName: 'laptopCode',
				id: 'site-loyalty',
				label: 'Canopy Loyalty App',
				tags: ['Framework: Drupal 10', 'Region: North America', '+1'],
			},
		],
	},
	{
		heading: 'Environments',
		id: 'environments',
		options: [
			{
				actionLabel: 'Jump to',
				description: 'checkout-v2-canopy-wholesale-portal.pantheonsite.io',
				iconName: 'cloud',
				id: 'env-checkout',
				inlineText: 'Canopy Wholesale Portal',
				label: 'checkout-v2',
			},
		],
	},
	assistant,
];

export interface GlobalSearchState {
	description: string;
	/** Width the state is shown at; narrow states show the container-query behavior. */
	narrow?: boolean;
	props: Partial<GlobalSearchProps>;
	title: string;
}

const base: Partial<GlobalSearchProps> = {
	hasBackdrop: false,
	label: 'Ask anything or search',
	loadingText: 'Searching…',
	placeholder: 'Ask anything or search across your account…',
	submitLabel: 'Ask the assistant',
};

export const globalSearchStates: GlobalSearchState[] = [
	{
		description: 'At rest: an input with a send button.',
		props: { ...base },
		title: 'Default',
	},
	{
		description:
			'Focused with nothing typed: recent searches, then suggestions per kind, then the assistant. Suggestions are one line each.',
		props: { ...base, groups: suggestionGroups, isOpen: true },
		title: 'Suggestions',
	},
	{
		description:
			'Typed: rows grow to show tags and a second line; the match is highlighted; only the active row shows its action.',
		props: {
			...base,
			defaultQuery: 'canopy',
			groups: resultGroups,
			isOpen: true,
		},
		title: 'Results',
	},
	{
		description:
			'A row is active (arrow keys or hover): grey fill and its action label.',
		props: {
			...base,
			defaultActiveId: 'site-wholesale',
			defaultQuery: 'canopy',
			groups: resultGroups,
			isOpen: true,
		},
		title: 'Active row',
	},
	{
		description:
			'First results are loading: a spinner and text. Nothing else to show yet.',
		props: { ...base, defaultQuery: 'canopy', isLoading: true, isOpen: true },
		title: 'Loading',
	},
	{
		description:
			'The next results are loading: the current list stays until they arrive, so it never flashes empty between keystrokes.',
		props: {
			...base,
			defaultQuery: 'canopy w',
			groups: resultGroups,
			isLoading: true,
			isOpen: true,
		},
		title: 'Loading with results',
	},
	{
		description:
			'Nothing matched: a message with a next step; the assistant is still offered.',
		props: {
			...base,
			defaultQuery: 'zzzz',
			groups: [assistant],
			isOpen: true,
			noResultsText:
				'We couldn’t find results for “zzzz”. Check the spelling or try another search.',
		},
		title: 'No results',
	},
	{
		description: 'The search failed: a message in place of the results.',
		props: {
			...base,
			defaultQuery: 'canopy',
			errorText: 'Search isn’t available right now. Try again in a moment.',
			isOpen: true,
		},
		title: 'Error',
	},
	{
		description:
			'In a narrow container the tags go and only the active row keeps its action.',
		narrow: true,
		props: {
			...base,
			defaultActiveId: 'site-main',
			defaultQuery: 'canopy',
			groups: resultGroups,
			isOpen: true,
		},
		title: 'Narrow',
	},
];