'use client'; import {ActionButton, Content, Heading, IllustratedMessage, Link} from '@react-spectrum/s2'; import {CopyButton} from './CopyButton'; import Edit from '@react-spectrum/s2/icons/Edit'; import { FieldInputContext, Header, Input, InputRenderProps, Key, ListBox, ListBoxItem, ListBoxSection, OverlayTriggerStateContext, TextField } from 'react-aria-components'; import {focusRing, style} from '@react-spectrum/s2/style' with {type: 'macro'}; import {InfoMessage} from './colorSearchData'; import NoSearchResults from '@react-spectrum/s2/illustrations/linear/NoSearchResults'; import React, {useMemo, useRef, useState} from 'react'; const listBoxStyle = style({ width: 'full', display: 'flex', flexDirection: 'row', gap: 12, flexWrap: 'wrap', alignItems: 'start' }); const sectionStyle = style({ display: 'flex', flexDirection: 'column', gap: 4 }); const headerStyle = style({ font: 'heading-xs', marginBottom: 4, display: 'flex', alignItems: 'center', gap: 4, paddingX: 8 }); const typographySections = [ { id: 'heading', name: 'Heading', description: 'Use for headings in content pages', items: [ 'heading-2xs', 'heading-xs', 'heading-sm', 'heading', 'heading-lg', 'heading-xl', 'heading-2xl', 'heading-3xl' ] }, { id: 'body', name: 'Body', description: 'Use for the content of pages that are primarily text', items: ['body-2xs', 'body-xs', 'body-sm', 'body', 'body-lg', 'body-xl', 'body-2xl', 'body-3xl'] }, { id: 'title', name: 'Title', description: 'Use for titles within UI components such as cards or panels', items: ['title-xs', 'title-sm', 'title', 'title-lg', 'title-xl', 'title-2xl', 'title-3xl'] }, { id: 'ui', name: 'UI', description: 'Use within interactive UI components', items: ['ui-xs', 'ui-sm', 'ui', 'ui-lg', 'ui-xl', 'ui-2xl', 'ui-3xl'] }, { id: 'code', name: 'Code', description: 'Use for source code', items: ['code-sm', 'code', 'code-lg', 'code-xl'] }, { id: 'detail', name: 'Detail', description: 'Use for less important metadata', items: ['detail-sm', 'detail', 'detail-lg', 'detail-xl'] } ]; const itemStyle = style({ ...focusRing(), display: 'flex', alignItems: 'center', paddingX: 12, paddingY: 4, backgroundColor: { default: 'gray-50', isHovered: 'gray-100', isFocused: 'gray-100', isSelected: 'accent' }, color: { default: 'body', isSelected: 'white' }, borderRadius: 'lg', transition: 'default', cursor: 'default' }); const fontStyles = { // UI 'ui-xs': style({font: 'ui-xs', color: 'inherit'}), 'ui-sm': style({font: 'ui-sm', color: 'inherit'}), ui: style({font: 'ui', color: 'inherit'}), 'ui-lg': style({font: 'ui-lg', color: 'inherit'}), 'ui-xl': style({font: 'ui-xl', color: 'inherit'}), 'ui-2xl': style({font: 'ui-2xl', color: 'inherit'}), 'ui-3xl': style({font: 'ui-3xl', color: 'inherit'}), // Body 'body-2xs': style({font: 'body-2xs', color: 'inherit'}), 'body-xs': style({font: 'body-xs', color: 'inherit'}), 'body-sm': style({font: 'body-sm', color: 'inherit'}), body: style({font: 'body', color: 'inherit'}), 'body-lg': style({font: 'body-lg', color: 'inherit'}), 'body-xl': style({font: 'body-xl', color: 'inherit'}), 'body-2xl': style({font: 'body-2xl', color: 'inherit'}), 'body-3xl': style({font: 'body-3xl', color: 'inherit'}), // Heading 'heading-2xs': style({font: 'heading-2xs', color: 'inherit'}), 'heading-xs': style({font: 'heading-xs', color: 'inherit'}), 'heading-sm': style({font: 'heading-sm', color: 'inherit'}), heading: style({font: 'heading', color: 'inherit'}), 'heading-lg': style({font: 'heading-lg', color: 'inherit'}), 'heading-xl': style({font: 'heading-xl', color: 'inherit'}), 'heading-2xl': style({font: 'heading-2xl', color: 'inherit'}), 'heading-3xl': style({font: 'heading-3xl', color: 'inherit'}), // Title 'title-xs': style({font: 'title-xs', color: 'inherit'}), 'title-sm': style({font: 'title-sm', color: 'inherit'}), title: style({font: 'title', color: 'inherit'}), 'title-lg': style({font: 'title-lg', color: 'inherit'}), 'title-xl': style({font: 'title-xl', color: 'inherit'}), 'title-2xl': style({font: 'title-2xl', color: 'inherit'}), 'title-3xl': style({font: 'title-3xl', color: 'inherit'}), // Detail 'detail-sm': style({font: 'detail-sm', color: 'inherit'}), detail: style({font: 'detail', color: 'inherit'}), 'detail-lg': style({font: 'detail-lg', color: 'inherit'}), 'detail-xl': style({font: 'detail-xl', color: 'inherit'}), // Code 'code-sm': style({font: 'code-sm', color: 'inherit'}), code: style({font: 'code', color: 'inherit'}), 'code-lg': style({font: 'code-lg', color: 'inherit'}), 'code-xl': style({font: 'code-xl', color: 'inherit'}) } as const; // We hard-code these since the code is dynamic and we can't run our highlighter in the browser. const syntaxStyles = { tag: style({color: 'red-1000'}), attribute: style({color: 'indigo-1000'}), string: style({color: 'green-1000'}) }; type FontStyleKey = keyof typeof fontStyles; const defaultPreviewInputStyle = style({ ...focusRing(), backgroundColor: 'transparent', borderWidth: 2, borderStyle: 'solid', borderColor: { default: 'transparent', isFocused: 'gray-900' }, borderRadius: 'lg', textAlign: 'center', width: 'full', transition: 'default' }); const editButtonStyle = style({ position: 'absolute', insetStart: 'full', marginStart: 8, visibility: { default: 'visible', isHidden: 'hidden' } }); interface TypographySearchViewProps { searchValue?: string; } export function TypographySearchView({searchValue = ''}: TypographySearchViewProps) { const [selectedFont, setSelectedFont] = useState('heading'); const [previewText, setPreviewText] = useState('Sample Text'); const [isPreviewFocused, setIsPreviewFocused] = useState(false); const inputRef = useRef(null); const codeSnippet = `
${previewText}
`; const fontStyleClass = selectedFont in fontStyles ? fontStyles[selectedFont as FontStyleKey] : undefined; const previewInputStyle = (renderProps: InputRenderProps) => `${defaultPreviewInputStyle(renderProps)} ${fontStyleClass || ''}`; const sections = useMemo(() => { const searchLower = searchValue.toLowerCase(); return typographySections .map(section => ({ id: section.id, name: section.name, description: section.description, items: section.items .filter(name => name.toLowerCase().includes(searchLower)) .map(name => ({ id: `${section.id}-${name}`, name })) })) .filter(section => section.items.length > 0); }, [searchValue]); const handleSelectionChange = (keys: Set) => { const selectedKey = Array.from(keys)[0]; if (selectedKey) { // Extract font name from the key (format: "section-fontname") const parts = String(selectedKey).split('-'); // Handle cases like "ui-2xl" or "heading-xl" const fontName = parts.slice(1).join('-'); setSelectedFont(fontName); } }; // Find the currently selected key for the ListBox // oxlint-disable-next-line react/react-compiler const selectedKeys = useMemo(() => { for (const section of sections) { const item = section.items.find(item => item.name === selectedFont); if (item) { return new Set([item.id]); } } return new Set(); }, [selectedFont, sections]); return ( <> Select a typography style and customize the sample text to preview its rendered output and code snippet. See styling for more information.
) => void} className={listBoxStyle} items={sections} renderEmptyState={() => ( No results Try a different search term. )}> {section => (
{section.name}
{section.items.map(item => ( {item.name} ))}
)}
setIsPreviewFocused(true)} onBlur={() => { setIsPreviewFocused(false); if (previewText.trim() === '') { setPreviewText('Sample Text'); } }} className={style({maxWidth: 'full', display: 'flex'})}> { const input = inputRef.current; if (input) { input.focus(); input.select(); } }}>
< div{' '} className={'{'} style {'('} {'{'} font:{' '} '{selectedFont}' {'}'} {')'} {'}'} > {previewText} </ div >
); }