import { Box } from '@luke-ui/react/box'; import { Button } from '@luke-ui/react/button'; import { Icon } from '@luke-ui/react/icon'; import { LoadingSkeleton } from '@luke-ui/react/loading-skeleton'; import { LoadingSpinner } from '@luke-ui/react/loading-spinner'; import { Text } from '@luke-ui/react/text'; import { vars } from '@luke-ui/react/theme'; import type { ComponentProps, ComponentType, JSX, ReactNode } from 'react'; import { Suspense, use } from 'react'; import type { HighlightedSource } from '../lib/highlighted-source.js'; import { StoryWrapper } from '../lib/story-wrapper.js'; import { DocsLink } from './docs-link.js'; import { ExampleCodePreview } from './example-code-preview.js'; import { ExamplePreview } from './example-preview.js'; /** Concentric with the frame's `borderRadius="control"` after the 1px border. */ const INNER_RADIUS = `max(0px, calc(${vars.radius.control} - 1px))`; const frameEndRadiusStyle = { borderEndEndRadius: INNER_RADIUS, borderEndStartRadius: INNER_RADIUS, } as const; type ExampleBlockProps = { src: string; title: string; layout?: ComponentProps['layout']; }; export function ExampleBlock(props: ExampleBlockProps): JSX.Element { return ( }> ); } export function ExampleLoadingState({ layout, title, }: Pick) { const loadingLabel = `Loading ${title} example`; const isFullBleed = layout === 'full-bleed'; return ( } ariaLabel={loadingLabel} title={title}> ); } function ExampleContent({ layout, src, title }: ExampleBlockProps): JSX.Element { const slashIndex = src.indexOf('/'); const component = src.slice(0, slashIndex); const name = src.slice(slashIndex + 1); const result = use(loadExample(component, name)); if (!result.ok) { return ( Failed to load example {component}/{name}: {result.error.message} ); } const [PreviewComponent, highlightedSource] = result.data; return ( ) : null } title={title} > {/* Remount when the source changes so expand state resets without an effect. */} ); } type ExampleFrameProps = { actions?: ReactNode; ariaLabel?: string; children: ReactNode; title: string; }; function ExampleFrame({ actions, ariaLabel, children, title }: ExampleFrameProps) { return ( {title} {actions != null ? ( {actions} ) : null} {children} ); } function OpenInPlayground({ hash }: { hash: string }) { return ( } target="_blank" to="/playground" > Open in playground ); } function ExampleLoadingActions() { return ( ); } const _modules = import.meta.glob('../examples/*/*.tsx', { eager: false, import: 'default', }); const _highlightedSources = import.meta.glob('../examples/*/*.tsx', { eager: false, import: 'default', query: '?highlight', }); type ExampleLoader = [() => Promise, () => Promise]; type ExampleTuple = [ComponentType, HighlightedSource]; type ExampleResult = | { ok: true; data: ExampleTuple; } | { ok: false; error: Error; }; const exampleCache = new Map>(); function findExample(component: string, name: string): ExampleLoader | null { const key = `../examples/${component}/${name}.tsx`; const loadModule = _modules[key]; const loadHighlightedSource = _highlightedSources[key]; if (!loadModule || !loadHighlightedSource) return null; return [loadModule, loadHighlightedSource]; } function loadExample(component: string, name: string): Promise { const key = `${component}/${name}`; const cached = exampleCache.get(key); if (cached) return cached; const match = findExample(component, name); if (!match) { const promise = Promise.resolve({ error: new Error(`Example not found: ${key}`), ok: false, } satisfies ExampleResult); exampleCache.set(key, promise); return promise; } const [loadModule, loadHighlightedSource] = match; const promise = Promise.all([loadModule(), loadHighlightedSource()]) .then(([loadedComponent, loadedSource]): ExampleResult => ({ data: [loadedComponent, loadedSource], ok: true, })) .catch((err): ExampleResult => ({ error: err instanceof Error ? err : new Error(String(err)), ok: false, })); exampleCache.set(key, promise); return promise; }