import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; import { DesignPanelInputProvider, useTrackDesignInput, } from "../../contexts/DesignPanelInputContext"; import { FIELD, LABEL } from "./propertyPanelHelpers"; import { CommitField } from "./propertyPanelCommitField"; export { CommitField } from "./propertyPanelCommitField"; /* ------------------------------------------------------------------ */ /* MetricField */ /* ------------------------------------------------------------------ */ export function MetricField({ label, value, disabled, liveCommit, scrub, suffix, tooltip, onCommit, }: { label: string; value: string; disabled?: boolean; liveCommit?: boolean; scrub?: boolean; suffix?: string; tooltip?: string; onCommit: (nextValue: string) => void | Promise; }) { const track = useTrackDesignInput(); const scrubRef = useRef<{ startX: number; startValue: number; pointerId: number } | null>(null); const commit = useCallback( (nextValue: string) => { if (nextValue !== value) track("metric", label); return onCommit(nextValue); }, [label, onCommit, track, value], ); const handleScrubPointerDown = useCallback( (e: React.PointerEvent) => { if (disabled || !scrub) return; const parsed = parseFloat(value); if (!Number.isFinite(parsed)) return; (e.target as HTMLElement).setPointerCapture(e.pointerId); scrubRef.current = { startX: e.clientX, startValue: parsed, pointerId: e.pointerId }; }, [disabled, scrub, value], ); const handleScrubPointerMove = useCallback( (e: React.PointerEvent) => { const state = scrubRef.current; if (!state) return; const delta = e.clientX - state.startX; commit(String(Math.round(state.startValue + delta))); }, [commit], ); const handleScrubPointerUp = useCallback(() => { scrubRef.current = null; }, []); const scrubProps = scrub && !disabled ? ({ className: "shrink-0 text-[11px] font-medium text-neutral-500 cursor-ew-resize select-none", onPointerDown: handleScrubPointerDown, onPointerMove: handleScrubPointerMove, onPointerUp: handleScrubPointerUp, onPointerCancel: handleScrubPointerUp, onLostPointerCapture: handleScrubPointerUp, } as const) : ({ className: "shrink-0 text-[11px] font-medium text-neutral-500" } as const); return (
{label} {suffix && {suffix}}
); } /* ------------------------------------------------------------------ */ /* Simple field components */ /* ------------------------------------------------------------------ */ export function DetailField({ label, value, disabled, onCommit, }: { label: string; value: string; disabled?: boolean; onCommit: (nextValue: string) => void; }) { const track = useTrackDesignInput(); const commit = (nextValue: string) => { if (nextValue !== value) track("text", label); onCommit(nextValue); }; return ( ); } export function SliderControl({ trackName, value, min, max, step, displayValue, formatDisplayValue, disabled, onCommit, }: { trackName: string; value: number; min: number; max: number; step: number; displayValue: string; formatDisplayValue?: (nextValue: number) => string; disabled?: boolean; onCommit: (nextValue: number) => void; }) { const track = useTrackDesignInput(); const [draft, setDraft] = useState(value); const commitTimerRef = useRef | null>(null); const interactionChangedRef = useRef(false); const valueRef = useRef(value); valueRef.current = value; useEffect(() => { setDraft(value); }, [value]); useEffect( () => () => { if (commitTimerRef.current) clearTimeout(commitTimerRef.current); }, [], ); const commitDraft = (nextDraft: number) => { if (commitTimerRef.current) clearTimeout(commitTimerRef.current); if (interactionChangedRef.current) { interactionChangedRef.current = false; track("slider", trackName); } if (nextDraft !== valueRef.current) onCommit(nextDraft); }; const scheduleCommit = (nextDraft: number) => { if (commitTimerRef.current) clearTimeout(commitTimerRef.current); commitTimerRef.current = setTimeout(() => { if (nextDraft !== valueRef.current) onCommit(nextDraft); }, 40); }; return (
{ const n = Number(e.target.value); setDraft(n); interactionChangedRef.current = true; scheduleCommit(n); }} onMouseUp={() => commitDraft(draft)} onTouchEnd={() => commitDraft(draft)} onBlur={() => commitDraft(draft)} // h-6 is the 24x24 WCAG 2.2 (2.5.8) target: the visible track stays 2px // and the thumb 10px, only the pointer box grows. className="h-6 min-w-0 w-full cursor-pointer appearance-none bg-transparent disabled:cursor-not-allowed disabled:opacity-50 [&::-webkit-slider-runnable-track]:h-[2px] [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-panel-border [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-[10px] [&::-webkit-slider-thumb]:h-[10px] [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-white [&::-webkit-slider-thumb]:-mt-1 [&::-webkit-slider-thumb]:shadow-[0_0_0_2px_#0C0C0E,0_1px_3px_rgba(0,0,0,0.5)] [&::-webkit-slider-thumb]:cursor-grab [&::-webkit-slider-thumb:active]:cursor-grabbing" />
{formatDisplayValue?.(draft) ?? displayValue}
); } export function SegmentedControl({ trackName, options, value, disabled, onChange, }: { trackName: string; options: Array<{ label: string; value: string }>; value: string; disabled?: boolean; onChange: (nextValue: string) => void; }) { const track = useTrackDesignInput(); return (
{options.map((option) => ( ))}
); } export function SelectField({ label, value, disabled, options, onChange, }: { label: string; value: string; disabled?: boolean; options: string[]; onChange: (nextValue: string) => void; }) { const track = useTrackDesignInput(); const renderedOptions = value && !options.includes(value) ? [value, ...options] : options; return ( ); } export function Section({ title, icon: _icon, children, accessory, defaultCollapsed = false, }: { title: string; icon: ReactNode; children: ReactNode; accessory?: ReactNode; defaultCollapsed?: boolean; }) { const [collapsed, setCollapsed] = useState(defaultCollapsed); const collapseIcon = ( ); const section = slugifyPanelSectionTitle(title); return (
{accessory &&
{accessory}
}
{!collapsed &&
{children}
}
); } // Stable hook for e2e/automation to locate a section without depending on the // display copy (h3 textContent matching breaks on wording tweaks or, if this // panel is ever localized, on translation). function slugifyPanelSectionTitle(title: string): string { return title .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/(^-|-$)/g, ""); }