'use client'; import { useEffect, useRef, useState, type ReactNode } from 'react'; import { CheckIcon, CopyIcon } from './icons'; /** * THE code-example shell — the "View code" card. One presentational * component for every sample display on both sites: * * - live demo + code → preview surface, control bar, collapsible editor * - code only → the dark editor card alone (no dead preview box) * - demo only → the preview surface alone (mode="demo") * * The chrome invariant: the last open region can't collapse, so the card is * never empty; the code toggle keeps its place on the left in every state. * Sites keep resolution (which files, which demo) and pass the results in. */ export type ExampleFile = { filename: string; code: string; language?: string; githubUrl?: string; highlightedCode?: string; }; export type ExampleMode = 'default' | 'open' | 'demo' | 'code'; /** * How tall the live preview is: the demo's own height (`content`), or a frame of its own that the * demo fills (`height: 100%`), for a demo with no height of its own, such as the ready-made viewer: * `viewer` suits the docs column, `tall` a whole app. A frame is never taller than most of the * screen, so the page around it still scrolls on a phone. */ export type ExampleFrame = 'content' | 'viewer' | 'tall'; const FRAME_HEIGHT: Record = { content: '', viewer: 'h-[min(560px,75svh)]', tall: 'h-[min(760px,85svh)]', }; type Tone = 'light' | 'dark'; const ICON_BTN: Record = { light: 'text-[var(--dk-muted)] hover:bg-[var(--dk-accent-surface)] hover:text-[var(--dk-heading)]', dark: 'text-[#8FA5D9] hover:bg-white/[0.06] hover:text-white', }; function ChevronDownIcon({ size = 12, className = '' }: { size?: number; className?: string }) { return ( ); } function CodeIcon({ size = 14 }: { size?: number }) { return ( ); } function EyeIcon({ size = 14, off = false }: { size?: number; off?: boolean }) { return ( {off ? ( <> ) : ( <> )} ); } function GitHubIcon({ size = 14 }: { size?: number }) { return ( ); } function FileGlyph() { return ( ); } /** Expand/collapse via the grid `0fr → 1fr` trick: animates height:auto with * no measurement. Content stays in the DOM (collapsed demos keep running). */ function Collapsible({ open, children }: { open: boolean; children: ReactNode }) { return (
{children}
); } /** The dotted light surface the demo sits on, in its frame. */ function PreviewSurface({ frame, children }: { frame: ExampleFrame; children: ReactNode }) { return (
{frame === 'content' ? children :
{children}
}
); } function CopyButton({ code, tone }: { code: string; tone: Tone }) { const [copied, setCopied] = useState(false); const copy = () => { void navigator.clipboard.writeText(code).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1600); }); }; const done = tone === 'light' ? 'text-emerald-600' : 'text-[#A5E3B6]'; return ( ); } function GitHubAction({ files, tone }: { files: ExampleFile[]; tone: Tone }) { const linked = files.filter((file) => file.githubUrl); const [open, setOpen] = useState(false); const menuRef = useRef(null); useEffect(() => { if (!open) return; const close = (event: MouseEvent) => { if (!menuRef.current?.contains(event.target as Node)) setOpen(false); }; document.addEventListener('mousedown', close); return () => document.removeEventListener('mousedown', close); }, [open]); if (linked.length === 0) return null; if (linked.length === 1) { return ( ); } const menu = tone === 'light' ? 'border border-[var(--dk-border)] bg-white shadow-[0_16px_36px_-12px_rgba(7,32,76,0.22)]' : 'border border-[#21305F] bg-[#0E1A40] shadow-[0_16px_36px_-12px_rgba(4,10,30,0.8)]'; const head = tone === 'light' ? 'text-[#8FA0C4]' : 'text-[#5E72A8]'; const item = tone === 'light' ? 'text-[var(--dk-muted)] hover:bg-[#F3F7FE] hover:text-[var(--dk-heading)]' : 'text-[#8FA5D9] hover:bg-white/[0.06] hover:text-white'; return (
{open ? (
View on GitHub
{linked.map((file) => ( setOpen(false)} > {file.filename} ))}
) : null}
); } /** The dark file bar — tabs for multi-file, a filename for single — sitting * directly on the dark code, plus an optional right-side action slot. */ function FileBar({ files, activeFile, onSelect, right, }: { files: ExampleFile[]; activeFile: number; onSelect: (index: number) => void; right?: ReactNode; }) { return (
{files.length > 1 ? (
{files.map((f, i) => ( ))}
) : (
{files[0].filename}
)} {right ?
{right}
: null}
); } /** The code, scrolling: at most 520px tall, or, with `fill`, the rest of its column. */ function CodePane({ file, fill = false }: { file: ExampleFile; fill?: boolean }) { return (
      
    
); } function escapeHtml(value: string) { return value.replace(/&/g, '&').replace(//g, '>'); } export function CodeExampleCard({ files, demo, mode = 'default', frame = 'content', }: { files: ExampleFile[]; /** The live preview; receives whether the preview region is open. */ demo?: (active: boolean) => ReactNode; mode?: ExampleMode; /** How tall the live preview is (a framed demo fills its frame's height). */ frame?: ExampleFrame; }) { const [activeFile, setActiveFile] = useState(0); const hasDemo = Boolean(demo); const [previewOpen, setPreviewOpen] = useState(hasDemo && mode !== 'code'); const [codeOpen, setCodeOpen] = useState(!hasDemo || mode === 'open' || mode === 'code'); if (files.length === 0 && !hasDemo) return null; const active = Math.min(activeFile, Math.max(files.length - 1, 0)); const file = files[active]; const totalLines = files.reduce((sum, f) => sum + f.code.trim().split('\n').length, 0); // No live demo → the card is just a code block: the dark file bar carries // the actions, and it reads exactly like a standalone code fence. if (!hasDemo) { return (
} />
); } // Demo-only showcase: no chrome at all — a light card, just the demo. if (mode === 'demo' || files.length === 0) { return (
{demo?.(true)}
); } const barBorder = previewOpen ? 'border-t border-[var(--dk-border)]' : ''; const canCollapseCode = previewOpen; // else code is the only content left return (
{demo?.(previewOpen)}
{!codeOpen ? ( ) : canCollapseCode ? ( ) : ( Code )}
{codeOpen ? ( ) : null} {codeOpen ? : null}
); } /** * An example's code alone, filling its box: the dark file bar, then the code, which scrolls in * the rest. For code beside a running example, such as on an example's own page; give it a `key` * per version, so a new one starts at its first file. */ export function CodeFiles({ files }: { files: ExampleFile[] }) { const [activeFile, setActiveFile] = useState(0); if (files.length === 0) return null; const active = Math.min(activeFile, files.length - 1); const file = files[active]; return (
} />
); }