import { useEffect, useId, useRef, useState, type ReactNode } from 'react'
export function Label({ children }: { children: ReactNode }) {
return {children}
}
/** An icon drawn with lines on a 16 by 16 grid, in the color of the text around it. */
export function Icon({ children, size = 16 }: { children: ReactNode; size?: number }) {
return (
)
}
/** A part of the panel that folds away under its title. Folded, it says in a line what it holds. */
export function Section({
id,
title,
summary,
open,
onToggle,
actions,
children,
}: {
id: string
title: string
/** Shown beside the title while the section is folded. */
summary?: string
open: boolean
onToggle: (open: boolean) => void
/** Buttons at the right of the title, while the section is open. */
actions?: ReactNode
children: ReactNode
}) {
return (
{open && actions && {actions}}
{open && (
{children}
)}
)
}
type ButtonVariant = 'plain' | 'quiet' | 'primary'
export function Button({
children,
onClick,
active,
title,
disabled,
variant = 'plain',
onHold,
}: {
children: ReactNode
onClick?: () => void
active?: boolean
title?: string
disabled?: boolean
variant?: ButtonVariant
/** For a button that acts while it is held down: told true on press and false on release. */
onHold?: (held: boolean) => void
}) {
return (
)
}
/** How a button is painted: with a frame, quiet until pointed at, or as the one to press; an active one stands out. */
function buttonLook(variant: ButtonVariant, active?: boolean) {
if (variant === 'primary') return 'border border-cl-accent bg-cl-accent font-semibold text-cl-on-accent hover:bg-cl-accent/85'
// A quiet button keeps an unseen frame, so that turning active does not move its neighbours.
if (variant === 'quiet' && !active) return 'border border-transparent text-cl-muted hover:text-cl-text'
return `border border-cl-border ${active ? 'bg-cl-accent/20 text-cl-accent-text' : 'bg-cl-raised hover:border-cl-muted'}`
}
/** A square button that shows only an icon; `label` is what it is called and its tooltip. */
export function IconButton({
label,
onClick,
disabled,
active,
children,
}: {
label: string
onClick?: () => void
disabled?: boolean
active?: boolean
children: ReactNode
}) {
return (
)
}
export function Segmented({
value,
options,
onChange,
compact,
}: {
value: T
options: { value: T; label: string }[]
onChange: (value: T) => void
/** Low enough to stand in a one-line row. */
compact?: boolean
}) {
return (
{options.map((option) => (
))}
)
}
export function Toggle({
checked,
onChange,
label,
id,
}: {
checked: boolean
onChange: (value: boolean) => void
label: string
id?: string
}) {
return (
)
}
/** A list that can draw each entry in its own font. Closes on a second click, Escape or a click outside. */
export function Select({
label,
value,
options,
onChange,
fontOf,
ariaLabel,
}: {
/** Shown above the list. Without it nothing is shown, and `ariaLabel` names the list instead. */
label?: string
ariaLabel?: string
value: string
options: string[]
onChange: (value: string) => void
fontOf?: (option: string) => string | undefined
}) {
const [open, setOpen] = useState(false)
const root = useRef(null)
useEffect(() => {
if (!open) return
const close = (event: MouseEvent) => {
if (!root.current?.contains(event.target as Node)) setOpen(false)
}
const key = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.stopPropagation()
setOpen(false)
}
}
document.addEventListener('mousedown', close)
document.addEventListener('keydown', key, true)
return () => {
document.removeEventListener('mousedown', close)
document.removeEventListener('keydown', key, true)
}
}, [open])
return (
)
}
/** A slider with its number, in one line. The value is reported while dragging; `onCommit` fires when it is let go.
* The label is short and may be cut off: `hint` says it in full, on hover and to a screen reader. */
export function Slider({
label,
hint,
value,
min,
max,
step,
onChange,
onCommit,
}: {
label: string
hint?: string
value: number
min: number
max: number
step: number
onChange: (value: number) => void
onCommit: () => void
}) {
const rangeId = useId()
const hintId = useId()
const clamp = (raw: number) => Math.min(max, Math.max(min, raw))
return (
)
}
const HEX_COLOR = /^#[0-9a-f]{6}$/i
/** A color that is either left to the script ("auto") or picked: a swatch that opens the system's
* picker, the switch between the two, and the picked color written out. */
export function ColorField({
label,
hint,
value,
onDraft,
onCommit,
}: {
label: string
hint?: string
/** 'auto' or '#rrggbb'. */
value: string
/** The color under the pointer while the picker is open: to show, not to draw with yet. */
onDraft: (value: string) => void
/** The color the picker was closed on, or 'auto'. */
onCommit: (value: string) => void
}) {
const input = useRef(null)
const picked = HEX_COLOR.test(value)
// What the picker opens on while the color is left to the script: the color picked last.
const last = useRef('#ffffff')
if (picked) last.current = value.toLowerCase()
const commit = useRef(onCommit)
commit.current = onCommit
useEffect(() => {
const element = input.current
if (!element) return
// React's onChange hears every move inside the picker. The browser's own "change" comes
// once, when the picker is closed: that is the moment to draw.
const done = () => commit.current(element.value)
element.addEventListener('change', done)
return () => element.removeEventListener('change', done)
}, [])
return (
{label}
{picked && {value.toLowerCase()}}
onDraft(event.target.value)}
className="cl-swatch h-[22px] w-[22px] cursor-pointer rounded border border-cl-border bg-transparent p-0"
/>
{!picked && }
{
if (next === 'auto') {
onCommit('auto')
return
}
// From "auto" the color the picker opens on is taken at once: a browser says
// nothing when the picker is closed on the color it opened with.
if (!picked) onCommit(last.current)
input.current?.click()
}}
/>