import { Badge, Button, DropdownMenuItem, DropdownMenuSeparator, SplitButton, Swap, Tabs, TabsContent, TabsList, TabsTrigger, cn, toast } from "@twoux/ui"; import { Check, ChevronLeft, ChevronRight, Copy, ExternalLink, FileText } from "lucide-react"; import { type ReactNode, useEffect, useState } from "react"; import { Link, useLocation } from "react-router-dom"; import { codeToHtml } from "shiki"; import { type Bullet, type Doc, type Example, PAGE_ORDER, docById, docPath } from "../content"; import { examplePrompt } from "./prompt"; /* ---------- inline text: `backticks` become code chips that never break mid-token ---------- */ export function Inline({ text }: { text: string }) { return ( <> {text.split(/(`[^`]+`)/g).map((part, i) => part.startsWith("`") && part.endsWith("`") ? ( // biome-ignore lint/suspicious/noArrayIndexKey: static split {part.slice(1, -1)} ) : ( part ), )} ); } /* ---------- code ---------- */ export function CodeBlock({ code, lang = "tsx", className, copyable = true }: { code: string; lang?: string; className?: string; copyable?: boolean }) { const [html, setHtml] = useState(""); const [copied, setCopied] = useState(false); useEffect(() => { let live = true; codeToHtml(code.trim(), { lang, themes: { light: "github-light", dark: "github-dark" }, defaultColor: false }).then((h) => live && setHtml(h)); return () => { live = false; }; }, [code, lang]); return (
{copyable && } {html ? ( // biome-ignore lint/security/noDangerouslySetInnerHtml: shiki output from our own source files
) : (
{code.trim()}
)}
); } /* ---------- live example: preview first, then the brief for a coding agent, then the raw source ---------- */ type ExampleTab = "preview" | "prompt" | "code"; export function ExampleBlock({ example, id, doc }: { example: Example; id?: string; doc?: Doc }) { const [tab, setTab] = useState("preview"); const Comp = example.Component; return (

{example.title} {id && }

{example.description &&

{example.description}

} setTab(v as ExampleTab)} className="mt-4 gap-0 overflow-hidden rounded-sm border border-line bg-surface">
Preview {doc && Prompt} Code {example.file}
{doc && ( {tab === "prompt" && } )}
); } /** The brief for a coding agent: what to build, which component, the rules that bind it, the data to replace and the code. */ function PromptPanel({ doc, example }: { doc: Doc; example: Example }) { const text = examplePrompt(doc, example); const [copied, setCopied] = useState(false); const copy = () => (navigator.clipboard?.writeText(text) ?? Promise.reject()).then( () => { setCopied(true); setTimeout(() => setCopied(false), 2000); toast.success("Prompt copied", { description: "Paste it into Claude Code, Cursor or Codex." }); }, () => toast.error("Couldn't copy the prompt", { description: "Allow clipboard access for this site, then try again." }), ); const ask = encodeURIComponent(text); return (

A brief for your coding agent: the job, the rules that bind it, the data to replace and the code.

Open in Claude(opens in a new tab) Open in ChatGPT(opens in a new tab) } > {copied ? : } Copy prompt
); } /* ---------- cited guidance ---------- */ export function refNumbers(doc: Doc) { return Object.fromEntries(doc.references.map((r, i) => [r.id, i + 1])); } export function Cites({ doc, ids }: { doc: Doc; ids: string[] }) { const n = refNumbers(doc); const nums = ids.filter((s) => n[s]); if (!nums.length) return null; return ( [ {nums.map((s, i) => ( {n[s]} {i < nums.length - 1 ? "," : ""} ))} ] ); } export function Bullets({ doc, items, tone }: { doc: Doc; items?: Bullet[]; tone?: "do" | "dont" }) { if (!items?.length) return null; return (
    {items.map((b) => (
  • ))}
); } /** Section heading with a shareable # link (appears on hover and focus). */ export function H2({ id, children, className }: { id: string; children: ReactNode; className?: string }) { return (

{children}

); } export function AnchorLink({ id, label }: { id: string; label: string }) { const [copied, setCopied] = useState(false); return ( { const url = `${location.origin}${location.pathname}${location.search}#${id}`; navigator.clipboard?.writeText(url).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1200); }); }} className="text-md font-normal text-ink-3 no-underline opacity-0 group-hover:opacity-100 hover:text-primary-ink focus-visible:opacity-100" > {copied ? "Copied" : "#"} ); } const KIND: Record = { standard: { label: "Standard", tone: "success" }, "design-system": { label: "Design system", tone: "default" }, law: { label: "UX law", tone: "warning" }, research: { label: "Research", tone: "warning" }, article: { label: "Article", tone: "secondary" }, "reference-app": { label: "Reference app", tone: "secondary" }, }; export function References({ doc }: { doc: Doc }) { return (
    {doc.references.map((r, i) => (
  1. {i + 1} {r.label} {KIND[r.kind]?.label ?? r.kind} {new URL(r.url).hostname.replace(/^www\./, "")}
  2. ))}
); } export function Related({ ids }: { ids?: string[] }) { const items = (ids ?? []).map((r) => docById[r]).filter(Boolean); if (!items.length) return null; return (
{items.map((d) => ( {d.name} ))}
); } /* ---------- on-this-page: links are real #anchors, the current section is highlighted as you scroll ---------- */ export function OnThisPage({ items }: { items: { id: string; label: string }[] }) { const [active, setActive] = useState(items[0]?.id); const key = items.map((i) => i.id).join("|"); useEffect(() => { const root = document.querySelector("main"); const els = items.map((i) => document.getElementById(i.id)).filter(Boolean) as HTMLElement[]; if (!root || !els.length) return; const onScroll = () => { const top = root.getBoundingClientRect().top + 80; let current = els[0].id; for (const el of els) if (el.getBoundingClientRect().top <= top) current = el.id; if (root.scrollTop + root.clientHeight >= root.scrollHeight - 4) current = els[els.length - 1].id; setActive(current); }; onScroll(); const later = [setTimeout(onScroll, 150), setTimeout(onScroll, 600)]; root.addEventListener("scroll", onScroll, { passive: true }); window.addEventListener("hashchange", onScroll); return () => { later.forEach(clearTimeout); root.removeEventListener("scroll", onScroll); window.removeEventListener("hashchange", onScroll); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [key]); if (!items.length) return
; return ( ); } /* ---------- previous / next: the neighbours of this page in sidebar order ---------- */ function Neighbour({ page, dir, from }: { page: (typeof PAGE_ORDER)[number]; dir: "prev" | "next"; from: string }) { const Icon = dir === "prev" ? ChevronLeft : ChevronRight; return ( {/* Indented by the chevron (14px) plus its gap (4px), so the label lines up with the title. */} {dir === "prev" ? "Previous" : "Next"} ); } export function PrevNext() { const { pathname } = useLocation(); const i = PAGE_ORDER.findIndex((p) => p.to === pathname); if (i < 0) return null; const prev = PAGE_ORDER[i - 1]; const next = PAGE_ORDER[i + 1]; const from = PAGE_ORDER[i].section; return ( ); } /** The foot of every docs page: who makes TwoUX, its licence, and where to find and reach it. */ export function SiteFooter() { const link = "hover:text-ink hover:underline hover:underline-offset-4"; return ( ); } /* ---------- shared building blocks for every docs page ---------- */ /** The number of a step in an ordered list: outlined, so the list reads as a sequence without competing with the text. */ export function NumberMarker({ n, solid = false }: { n: number; /** Filled, for markers placed over content such as an anatomy preview. */ solid?: boolean }) { return {n}; } /** A card that goes somewhere: icon, title and one line. External files open in a new tab. */ export function LinkCard({ icon: Icon, title, text, href }: { icon: typeof FileText; title: string; text: string; href: string }) { const external = /\.(md|txt)$/.test(href); const cls = "block h-full rounded-md border border-line bg-surface p-5 transition duration-(--duration-base) hover:border-line-strong hover:shadow-raised focus-visible:outline-offset-2"; const body = ( <>