Repository Public
1"use client";2/** Small controls shared by the toolbar and the panels of the diagram editor. */3import { useRef, useState, type ReactNode } from "react";45export const GLYPHS = {6 select: "M5 3l14 8-6 2-2 6L5 3Z",7 pan: "M12 3v18M3 12h18M9 6l3-3 3 3M9 18l3 3 3-3M6 9l-3 3 3 3M18 9l3 3-3 3",8 connect: "M4 16h5v5H4zM15 3h5v5h-5zM9 16l6-8",9 multi: "M3 3h9v9H3zM12 12h9v9h-9zM16.5 3v6M13.5 6h6",10 grid: "M4 4h16v16H4zM4 12h16M12 4v16",11 plus: "M12 5v14M5 12h14",12 minus: "M5 12h14",13 duplicate: "M8 8h12v12H8zM4 16V4h12",14 trash: "M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13",15 undo: "M9 14L4 9l5-5M4 9h10a6 6 0 0 1 0 12h-3",16 redo: "M15 14l5-5-5-5M20 9H10a6 6 0 0 0 0 12h3",17 fit: "M4 9V5h4M20 9V5h-4M4 15v4h4M20 15v4h-4M9 9h6v6H9Z",18 lock: "M6 11h12v9H6zM8 11V8a4 4 0 0 1 8 0v3",19 unlock: "M6 11h12v9H6zM8 11V8a4 4 0 0 1 7.6-1.7",20 auto: "M4 12a8 8 0 1 0 2.6-5.9M4 4v5h5",21 layers: "M12 3l9 5-9 5-9-5 9-5ZM3 13l9 5 9-5",22 sliders: "M4 6h9M17 6h3M4 12h3M11 12h9M4 18h11M19 18h1M15 4v4M9 10v4M17 16v4",23 steps: "M4 6h9M4 12h9M4 18h6M16 9l5 3.5-5 3.5Z",24 keyboard: "M3 6h18v12H3zM7 10h.01M11 10h.01M15 10h.01M8 14h8",25 up: "M6 15l6-6 6 6",26 down: "M6 9l6 6 6-6",27 pencil: "M4 20l4-1L19 8l-3-3L5 16l-1 4Z",28 close: "M6 6l12 12M18 6L6 18",29 group: "M3 3h4v4H3zM17 3h4v4h-4zM3 17h4v4H3zM17 17h4v4h-4zM7 5h10M7 19h10M5 7v10M19 7v10",30} as const;31export type GlyphName = keyof typeof GLYPHS;3233export const Glyph = ({ name, size = 20 }: { name: GlyphName; size?: number }) => (34 <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" focusable="false"><path d={GLYPHS[name]} /></svg>35);3637const PRESSED = { background: "var(--ui-accent-soft)", color: "var(--ui-accent)" } as const;3839/** Square icon button: 44 px on touch layouts, 36 px on desktop unless `large`. The label is its accessible name. */40export function IconButton({ glyph, label, onClick, pressed, disabled, large, children }: {41 glyph: GlyphName; label: string; onClick: () => void; pressed?: boolean; disabled?: boolean; large?: boolean; children?: ReactNode;42}) {43 return (44 <button type="button" className={`btn btn-ghost shrink-0 px-0 ${large ? "h-11 w-11" : "h-11 w-11 md:h-9 md:min-h-9 md:w-9"}`} style={pressed ? PRESSED : undefined}45 aria-label={label} title={label} aria-pressed={pressed} disabled={disabled} onClick={onClick}>46 <Glyph name={glyph} />{children}47 </button>48 );49}5051/** Text button used inside the panels. */52export function TextButton({ onClick, disabled, pressed, danger, children, label }: { onClick: () => void; disabled?: boolean; pressed?: boolean; danger?: boolean; children: ReactNode; label?: string }) {53 return (54 <button type="button" className={`btn btn-sm min-h-11 md:min-h-9 ${danger ? "btn-danger" : ""}`} style={pressed ? PRESSED : undefined} aria-pressed={pressed} aria-label={label} disabled={disabled} onClick={onClick}>{children}</button>55 );56}5758export const FieldLabel = ({ label, children, hint }: { label: string; children: ReactNode; hint?: string }) => (59 <label className="block min-w-0 text-sm">60 <span className="mb-1 block text-muted">{label}</span>61 {children}62 {hint ? <span className="mt-1 block text-xs text-faint">{hint}</span> : null}63 </label>64);6566interface CommitProps {67 label: string; value: string; onCommit: (value: string) => void; maxLength: number;68 placeholder?: string; hint?: string; hideLabel?: boolean; autoFocus?: boolean; inputMode?: "text" | "decimal"; onDone?: () => void;69}7071/** Local draft; `value` wins again whenever it changes from outside (undo, another panel). */72function useDraft(value: string) {73 const [draft, setDraft] = useState(value);74 const [seen, setSeen] = useState(value);75 if (seen !== value) { setSeen(value); setDraft(value); }76 return [draft, setDraft] as const;77}7879/** Text input that commits on blur or Enter; Escape restores the committed value. Length capped at the schema maximum. */80export function CommitInput({ label, value, onCommit, maxLength, placeholder, hint, hideLabel, autoFocus, inputMode, onDone }: CommitProps) {81 const [draft, setDraft] = useDraft(value);82 const cancelled = useRef(false);83 const input = (84 <input className="field" type="text" inputMode={inputMode} value={draft} maxLength={maxLength} placeholder={placeholder} autoFocus={autoFocus} aria-label={hideLabel ? label : undefined}85 onChange={(e) => setDraft(e.target.value)}86 onBlur={() => { if (cancelled.current) { cancelled.current = false; setDraft(value); } else if (draft !== value) onCommit(draft); onDone?.(); }}87 onKeyDown={(e) => {88 if (e.key === "Enter") { e.preventDefault(); e.currentTarget.blur(); }89 else if (e.key === "Escape") { e.stopPropagation(); cancelled.current = true; e.currentTarget.blur(); }90 }} />91 );92 return hideLabel ? input : <FieldLabel label={label} hint={hint}>{input}</FieldLabel>;93}9495/** Multi-line variant: commits on blur or ⌘/Ctrl + Enter. */96export function CommitTextarea({ label, value, onCommit, maxLength, placeholder, hint, rows = 3 }: CommitProps & { rows?: number }) {97 const [draft, setDraft] = useDraft(value);98 const cancelled = useRef(false);99 return (100 <FieldLabel label={label} hint={hint}>101 <textarea className="field" rows={rows} value={draft} maxLength={maxLength} placeholder={placeholder}102 onChange={(e) => setDraft(e.target.value)}103 onBlur={() => { if (cancelled.current) { cancelled.current = false; setDraft(value); } else if (draft !== value) onCommit(draft); }}104 onKeyDown={(e) => {105 if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); e.currentTarget.blur(); }106 else if (e.key === "Escape") { e.stopPropagation(); cancelled.current = true; e.currentTarget.blur(); }107 }} />108 </FieldLabel>109 );110}111112export function SelectField<V extends string>({ label, value, options, onChange, hideLabel, disabled }: {113 label: string; value: V; options: readonly { value: V; label: string }[]; onChange: (value: V) => void; hideLabel?: boolean; disabled?: boolean;114}) {115 const select = (116 <select className="field" value={value} disabled={disabled} aria-label={hideLabel ? label : undefined}117 onChange={(e) => { const hit = options.find((o) => o.value === e.target.value); if (hit) onChange(hit.value); }}>118 {options.map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}119 </select>120 );121 return hideLabel ? select : <FieldLabel label={label}>{select}</FieldLabel>;122}123124export const Section = ({ title, children, aside }: { title: string; children: ReactNode; aside?: ReactNode }) => (125 <section className="border-b border-line px-3 py-3 last:border-b-0">126 <div className="mb-2 flex items-center justify-between gap-2"><h3 className="eyebrow">{title}</h3>{aside}</div>127 <div className="flex flex-col gap-2.5">{children}</div>128 </section>129);130