import { Switch } from '@components/common/ui/Switch.js'; import { ChevronDown } from 'lucide-react'; import React, { useEffect, useState } from 'react'; /** * Drawer-style primitives shared by every widget setting form. Same vocabulary * across the codebase: an 11px-label `Field`, a collapsible `Section` card, * a `Segmented` radio-style control, and a `Slider` whose drag commits on * release (controlled-value updates on every frame made dragging feel like * a click — see slideshow drawer history). * * Until this module landed, every drawer (Slideshow / Banner / Menu / * Collection / Columns) inlined its own copies. Behaviour now flows from * here; the per-widget files just compose these. */ // --------------------------------------------------------------------------- // Field — label + (hint) + child input. // --------------------------------------------------------------------------- export function Field({ label, hint, children }: { label?: string; hint?: string; children: React.ReactNode; }) { return (
{label && (
{label}
)}
{children}
{hint &&
{hint}
}
); } // --------------------------------------------------------------------------- // Section — collapsible card with a title row and optional right-slot. // --------------------------------------------------------------------------- export function Section({ title, children, rightSlot, defaultOpen = true }: { title: string; children: React.ReactNode; rightSlot?: React.ReactNode; defaultOpen?: boolean; }) { const [open, setOpen] = useState(defaultOpen); return (
{rightSlot}
{open && (
{children}
)}
); } // --------------------------------------------------------------------------- // Segmented — generic radio-style row control. Each option can carry an // optional leading icon; labels stay short. // --------------------------------------------------------------------------- export function Segmented({ value, options, onChange, size = 'sm' }: { value: T; options: ReadonlyArray<{ value: T; label: string; icon?: React.ReactNode; title?: string; }>; onChange: (v: T) => void; size?: 'sm' | 'md'; }) { const padding = size === 'md' ? 'px-3 py-2 text-sm' : 'px-2 py-1 text-xs'; return (
{options.map((opt) => { const active = value === opt.value; return ( ); })}
); } // --------------------------------------------------------------------------- // Slider — local display state, commit on mouse/touch/key release. The // commit-on-release pattern avoids RHF round-trips during the drag (which // produced the click-only behaviour we hit in the slideshow drawer). // --------------------------------------------------------------------------- export function Slider({ value, min, max, step = 1, onCommit, unit, format }: { value: number; min: number; max: number; step?: number; onCommit: (v: number) => void; unit?: string; format?: (v: number) => string; }) { const [display, setDisplay] = useState(value); useEffect(() => { setDisplay(value); }, [value]); const shown = format ? format(display) : `${display}${unit ?? ''}`; return (
setDisplay(Number(e.target.value))} onMouseUp={() => onCommit(display)} onTouchEnd={() => onCommit(display)} onKeyUp={() => onCommit(display)} className="flex-1 accent-primary" />
{shown}
); } // --------------------------------------------------------------------------- // Toggle — a labelled row with a description and shadcn Switch on the right. // Used heavily for boolean settings; the row pattern keeps the rhythm of the // drawer consistent (matches Slideshow / Banner). // --------------------------------------------------------------------------- export function Toggle({ label, description, checked, onChange, disabled }: { label: string; description?: string; checked: boolean; onChange: (v: boolean) => void; disabled?: boolean; }) { return (
{label}
{description && (
{description}
)}
); } // --------------------------------------------------------------------------- // TextInput — plain text input that matches drawer typography. Exposed as a // helper so the surrounding setting components don't drift on padding/font. // --------------------------------------------------------------------------- export const drawerInputClass = 'w-full rounded-md border border-divider bg-card px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-primary'; export const drawerTextareaClass = 'w-full resize-vertical rounded-md border border-divider bg-card px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-primary';