feat(signage): venue badges, widgets' own look, widgets placed inside stacks, Save and leave, shared controls (owner review 5)
- Badges: the venue's list (setting signage.badges: names el/en, colours, an icon from a food and drink set) edited under Signage -> "Message & settings"; sent with the venue to screens and previews; several badges per product (MenuList.badges lists, a single one still read) in the shared multiple Dropdown; a board's badge colour / text colour override them (None = each own). - Widgets: colour, background plate, font, bold / italic / underline, text size, QR colours; widgets in a stack's part get every setting, vertical alignment, and a custom box inside their place, picked (stack, then widget) and dragged on the preview (selection by path "2.1.0"). - Menu slides: vertical alignment of the list (board_valign); "Where the list sits" is now "Horizontal alignment". - Message of the day: one component in the settings tab, on the Overview and from its widget. - Leaving a slide with changes: "Save and leave" (ConfirmDialog altLabel). - @xenia/ui: ColourField (with colour.js helpers), FieldRow (framed only while wrapped), Slider, IconPicker, Segmented multiple; native selects on the Overview and signage replaced by Dropdown, the device settings' colour input by ColourField. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -465,6 +465,77 @@
|
||||
box-shadow: var(--x-shadow-popover); outline: none;
|
||||
animation: x-fade-in var(--x-duration-fast) var(--x-ease);
|
||||
}
|
||||
/* ── FieldRow (2026-11-21): fields that wrap, framed while wrapped ─────────────── */
|
||||
.x-field-row {
|
||||
display: grid; gap: var(--x-space-3); align-items: end;
|
||||
grid-template-columns: repeat(auto-fill, minmax(min(var(--x-field-min, 10.5rem), 100%), 1fr));
|
||||
padding: var(--x-space-3); border: var(--x-border-width) solid transparent; border-radius: var(--x-radius-control);
|
||||
transition: background-color var(--x-duration-fast) var(--x-ease), border-color var(--x-duration-fast) var(--x-ease);
|
||||
}
|
||||
.x-field-row--wrapped { border-color: var(--x-line); background: var(--x-surface); }
|
||||
.x-field-row--wrapped.x-field-row--shade { background: var(--x-surface-2); }
|
||||
|
||||
/* ── Slider (2026-11-21): framed like a field, its value beside it ──────────────── */
|
||||
.x-slider {
|
||||
min-height: var(--x-control-md); display: flex; align-items: center; gap: var(--x-space-3); padding: 0 var(--x-space-3);
|
||||
border: var(--x-border-width) solid var(--x-line); border-radius: var(--x-radius-control); background: var(--x-surface);
|
||||
}
|
||||
.x-slider__input { flex: 1; min-width: 0; accent-color: var(--x-accent); }
|
||||
.x-slider__value { width: 3rem; text-align: right; font-size: var(--x-text-label); color: var(--x-ink-2); font-variant-numeric: tabular-nums; }
|
||||
|
||||
/* ── ColourField (2026-11-21): a colour as a field; its panel over the page ──────── */
|
||||
.x-swatch {
|
||||
display: block; flex-shrink: 0; width: 24px; height: 24px; border-radius: 999px; border: var(--x-border-width) solid var(--x-line-strong);
|
||||
background: linear-gradient(var(--x-swatch), var(--x-swatch)), repeating-conic-gradient(#d1d5db 0 25%, #ffffff 0 50%) 0 0 / 8px 8px;
|
||||
}
|
||||
.x-colour {
|
||||
width: 100%; min-width: 0; min-height: var(--x-control-md); display: flex; align-items: center; gap: var(--x-space-2); padding: 0 var(--x-space-3);
|
||||
border: var(--x-border-width) solid var(--x-line); border-radius: var(--x-radius-control); background: var(--x-surface);
|
||||
color: var(--x-ink); font: inherit; font-size: var(--x-text-label); cursor: pointer; text-align: left;
|
||||
transition: border-color var(--x-duration-fast) var(--x-ease);
|
||||
}
|
||||
.x-colour:hover { border-color: var(--x-line-strong); }
|
||||
.x-colour:focus-visible { outline: none; box-shadow: var(--x-focus-ring); }
|
||||
.x-colour__value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--x-ink-2); }
|
||||
.x-colour__value--code { font-family: var(--x-font-mono, ui-monospace, monospace); }
|
||||
.x-colour__panel { display: grid; gap: var(--x-space-3); padding: var(--x-space-3); width: 17.5rem; }
|
||||
.x-colour__auto {
|
||||
min-height: var(--x-control-md); padding: 0 var(--x-space-3); text-align: left; font: inherit; font-size: var(--x-text-label); cursor: pointer;
|
||||
border: var(--x-border-width) solid var(--x-line); border-radius: var(--x-radius-control); background: var(--x-surface); color: var(--x-ink);
|
||||
}
|
||||
.x-colour__auto:hover { background: var(--x-surface-2); }
|
||||
.x-colour__auto--on { border-color: var(--x-accent); background: var(--x-accent-soft); color: var(--x-accent-ink); }
|
||||
.x-colour__presets { display: grid; grid-template-columns: repeat(6, 1fr); gap: var(--x-space-2); justify-items: center; }
|
||||
.x-colour__preset { padding: 0; border: 0; background: none; border-radius: 999px; cursor: pointer; transition: transform var(--x-duration-fast) var(--x-ease); }
|
||||
.x-colour__preset .x-swatch { width: 32px; height: 32px; }
|
||||
.x-colour__preset:hover { transform: scale(1.1); }
|
||||
.x-colour__preset:focus-visible { outline: none; box-shadow: var(--x-focus-ring); }
|
||||
.x-colour__preset--on { box-shadow: 0 0 0 2px var(--x-surface), 0 0 0 4px var(--x-accent); }
|
||||
.x-colour__own { display: grid; gap: var(--x-space-2); border-top: var(--x-border-width) solid var(--x-line); padding-top: var(--x-space-3); }
|
||||
.x-colour__heading { font-size: var(--x-text-overline, 11px); font-weight: var(--x-weight-semibold); text-transform: uppercase; letter-spacing: 0.04em; color: var(--x-ink-3); }
|
||||
.x-colour__row { display: flex; align-items: center; gap: var(--x-space-2); }
|
||||
.x-colour__picker {
|
||||
width: 40px; height: var(--x-control-md); flex-shrink: 0; padding: 2px; cursor: pointer;
|
||||
border: var(--x-border-width) solid var(--x-line); border-radius: var(--x-radius-control); background: var(--x-surface);
|
||||
}
|
||||
.x-colour__code { flex: 1; min-width: 0; font-family: var(--x-font-mono, ui-monospace, monospace); }
|
||||
.x-colour__opacity { display: grid; gap: 4px; font-size: var(--x-text-caption); color: var(--x-ink-2); }
|
||||
.x-colour__opacity-row { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
|
||||
.x-colour__opacity input { accent-color: var(--x-accent); }
|
||||
|
||||
/* ── IconPicker (2026-11-21) ──────────────────────────────────────────────────── */
|
||||
.x-icon-pick__current { width: 24px; height: 24px; display: grid; place-items: center; color: var(--x-ink); flex-shrink: 0; }
|
||||
.x-icon-pick { display: grid; gap: var(--x-space-2); padding: var(--x-space-3); width: 20rem; }
|
||||
.x-icon-pick__grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: var(--x-space-1); }
|
||||
.x-icon-pick__item, .x-icon-pick__none {
|
||||
display: grid; place-items: center; min-height: 36px; border: var(--x-border-width) solid transparent; border-radius: var(--x-radius-control);
|
||||
background: none; color: var(--x-ink-2); cursor: pointer; font: inherit; font-size: var(--x-text-label);
|
||||
}
|
||||
.x-icon-pick__none { border-color: var(--x-line); }
|
||||
.x-icon-pick__item:hover, .x-icon-pick__none:hover { background: var(--x-surface-2); color: var(--x-ink); }
|
||||
.x-icon-pick__item:focus-visible, .x-icon-pick__none:focus-visible { outline: none; box-shadow: var(--x-focus-ring); }
|
||||
.x-icon-pick__item--on { border-color: var(--x-accent); background: var(--x-accent-soft); color: var(--x-accent-ink); }
|
||||
|
||||
/* ── Popover (2026-11-21): a small panel under a button, kept inside the window ── */
|
||||
.x-popover {
|
||||
position: fixed; z-index: var(--x-z-popover); box-sizing: border-box;
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { useCallback, useRef, useState } from 'react'
|
||||
import { cx } from '../internal/cx.js'
|
||||
import { Field, Input } from './Field.jsx'
|
||||
import { Popover } from './Popover.jsx'
|
||||
import { useLabels } from './labels.js'
|
||||
import { COLOUR_PRESETS, joinAlpha, splitAlpha, typedColour } from './colour.js'
|
||||
|
||||
/**
|
||||
* The one colour field of the product (2026-11-21): its swatch and code, as tall as the fields
|
||||
* beside it; a click opens a small panel over the page (Popover) with the ready colours, then the
|
||||
* picker, the opacity and the code typed by hand. Stores #rrggbb, or #rrggbbaa when see-through.
|
||||
* <ColourField label="Text colour" value={c} onChange={setC} />
|
||||
* auto: { label, on, pick } — a first choice that leaves the colour to something else ("Automatic",
|
||||
* "Each badge's own"); `on` shows it chosen (the field then reads its label).
|
||||
* opacity: false hides the opacity (a colour that can't be see-through).
|
||||
*/
|
||||
export function ColourField({ label, hint, value, onChange, presets = COLOUR_PRESETS, auto, opacity = true, className, testId }) {
|
||||
const labels = useLabels()
|
||||
const [open, setOpen] = useState(false)
|
||||
const [typed, setTyped] = useState(null)
|
||||
const btn = useRef(null)
|
||||
const v = (value || '#000000').toLowerCase()
|
||||
const { rgb, alpha } = splitAlpha(v)
|
||||
const close = useCallback(() => { setOpen(false); setTyped(null) }, [])
|
||||
const tid = s => (testId ? `${testId}-${s}` : undefined)
|
||||
return (
|
||||
<>
|
||||
<Field label={label} hint={hint} className={className}>
|
||||
<button ref={btn} type="button" className="x-colour" onClick={() => setOpen(o => !o)} aria-expanded={open} aria-haspopup="dialog" data-testid={tid('open')}>
|
||||
<Swatch colour={v} />
|
||||
<span className={cx('x-colour__value', !auto?.on && 'x-colour__value--code')}>{auto?.on ? auto.label : v}</span>
|
||||
</button>
|
||||
</Field>
|
||||
<Popover open={open} anchor={btn} onClose={close} aria-label={label} className="x-colour__panel" data-testid={tid('panel')}>
|
||||
{auto && (
|
||||
<button type="button" className={cx('x-colour__auto', auto.on && 'x-colour__auto--on')} aria-pressed={!!auto.on}
|
||||
onClick={() => { auto.pick(); close() }} data-testid={tid('auto')}>{auto.label}</button>
|
||||
)}
|
||||
<div className="x-colour__presets">
|
||||
{presets.map(c => {
|
||||
const chosen = !auto?.on && rgb === c.toLowerCase()
|
||||
return (
|
||||
<button key={c} type="button" className={cx('x-colour__preset', chosen && 'x-colour__preset--on')} aria-label={c} aria-pressed={chosen} title={c}
|
||||
onClick={() => { onChange(opacity ? joinAlpha(c.toLowerCase(), alpha) : c.toLowerCase()); close() }} data-testid={tid('preset')}>
|
||||
<Swatch colour={c} />
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
<div className="x-colour__own">
|
||||
<span className="x-colour__heading">{labels.colourOwn}</span>
|
||||
<div className="x-colour__row">
|
||||
<input type="color" className="x-colour__picker" value={rgb} aria-label={label} data-testid={testId}
|
||||
onChange={e => { setTyped(null); onChange(opacity ? joinAlpha(e.target.value, alpha) : e.target.value) }} />
|
||||
<Input value={typed ?? v} maxLength={9} spellCheck={false} aria-label={labels.colourCode} className="x-colour__code" data-testid={tid('code')}
|
||||
onChange={e => { setTyped(e.target.value); const c = typedColour(e.target.value); if (c && (opacity || c.length === 7)) onChange(c) }}
|
||||
onBlur={() => setTyped(null)} />
|
||||
</div>
|
||||
{opacity && (
|
||||
<label className="x-colour__opacity">
|
||||
<span className="x-colour__opacity-row"><span>{labels.opacity}</span><span>{alpha}%</span></span>
|
||||
<input type="range" min={0} max={100} step={5} value={alpha} data-testid={tid('opacity')}
|
||||
onChange={e => { setTyped(null); onChange(joinAlpha(rgb, Number(e.target.value))) }} />
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</Popover>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** A colour's swatch over a checkerboard, so a see-through colour shows as one. */
|
||||
export function Swatch({ colour, className }) {
|
||||
return <span className={cx('x-swatch', className)} aria-hidden="true" style={{ '--x-swatch': colour }} />
|
||||
}
|
||||
@@ -8,15 +8,19 @@ import { useLabels } from './labels.js'
|
||||
* (2026-11-21). tone 'danger' for deleting / throwing work away.
|
||||
* <ConfirmDialog open title="Delete this slide?" confirmLabel="Delete" tone="danger"
|
||||
* onConfirm={remove} onCancel={close}>It goes from the playlist.</ConfirmDialog>
|
||||
* altLabel / onAlt: a third choice, the main one (2026-11-21: unsaved changes → "Save and leave"
|
||||
* beside "Leave without saving"); the confirm button is then the secondary danger one.
|
||||
*/
|
||||
export function ConfirmDialog({ open, title, children, confirmLabel, cancelLabel, tone = 'primary', onConfirm, onCancel, busy = false, testId }) {
|
||||
export function ConfirmDialog({ open, title, children, confirmLabel, cancelLabel, tone = 'primary', onConfirm, onCancel, altLabel, onAlt, busy = false, testId }) {
|
||||
const labels = useLabels()
|
||||
const alt = altLabel && onAlt
|
||||
return (
|
||||
<Dialog open={open} size="sm" title={title} onClose={onCancel}
|
||||
<Dialog open={open} size={alt ? 'md' : 'sm'} title={title} onClose={onCancel}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={onCancel} data-testid={testId ? `${testId}-cancel` : 'confirm-cancel'}>{cancelLabel ?? labels.cancel}</Button>
|
||||
<Button variant={tone === 'danger' ? 'danger' : 'primary'} loading={busy} onClick={onConfirm}
|
||||
<Button variant={alt ? 'secondary' : tone === 'danger' ? 'danger' : 'primary'} tone={alt && tone === 'danger' ? 'danger' : undefined} loading={busy && !alt} onClick={onConfirm}
|
||||
data-testid={testId ? `${testId}-ok` : 'confirm-ok'}>{confirmLabel ?? labels.submit}</Button>
|
||||
{alt && <Button variant="primary" loading={busy} onClick={onAlt} data-testid={testId ? `${testId}-alt` : 'confirm-alt'}>{altLabel}</Button>}
|
||||
</>}>
|
||||
{children && <div className="x-confirm__body">{children}</div>}
|
||||
</Dialog>
|
||||
@@ -27,6 +31,7 @@ export function ConfirmDialog({ open, title, children, confirmLabel, cancelLabel
|
||||
* The same as a hook that answers with a promise:
|
||||
* const [confirm, confirmDialog] = useConfirm()
|
||||
* if (!(await confirm({ title, body, confirmLabel, tone: 'danger' }))) return
|
||||
* With altLabel the promise answers 'alt' for the third choice (truthy: check it first).
|
||||
* …render {confirmDialog} somewhere in the component.
|
||||
*/
|
||||
export function useConfirm() {
|
||||
@@ -35,6 +40,7 @@ export function useConfirm() {
|
||||
const answer = value => { ask?.resolve(value); setAsk(null) }
|
||||
const dialog = ask ? (
|
||||
<ConfirmDialog open title={ask.title} confirmLabel={ask.confirmLabel} cancelLabel={ask.cancelLabel} tone={ask.tone} testId={ask.testId}
|
||||
altLabel={ask.altLabel} onAlt={ask.altLabel ? () => answer('alt') : undefined}
|
||||
onConfirm={() => answer(true)} onCancel={() => answer(false)}>{ask.body}</ConfirmDialog>
|
||||
) : null
|
||||
return [confirm, dialog]
|
||||
|
||||
@@ -45,13 +45,20 @@ export function FilterChip({ pressed = false, tone = 'neutral', count, size = 'm
|
||||
* A few mutually exclusive views (List / Map). options: [{ value, label, icon?, ariaLabel? }]
|
||||
* (ariaLabel: the name of an option shown as an icon only). fill: as wide as its column, the options sharing it.
|
||||
* <Segmented value={view} onChange={setView} options={[…]} aria-label="View" />
|
||||
* multiple (2026-11-21): toggles that go together (bold / italic / underline); value is an array.
|
||||
*/
|
||||
export function Segmented({ options = [], value, onChange, size = 'md', fill = false, className, ...rest }) {
|
||||
export function Segmented({ options = [], value, onChange, size = 'md', fill = false, multiple = false, className, ...rest }) {
|
||||
const on = v => (multiple ? (value || []).includes(v) : v === value)
|
||||
const pick = v => {
|
||||
if (!multiple) return onChange?.(v)
|
||||
const list = value || []
|
||||
return onChange?.(list.includes(v) ? list.filter(x => x !== v) : [...list, v])
|
||||
}
|
||||
return (
|
||||
<div role="group" className={cx('x-segmented', `x-segmented--${size}`, fill && 'x-segmented--fill', className)} {...rest}>
|
||||
{options.map(o => (
|
||||
<button key={o.value} type="button" aria-pressed={o.value === value} className={cx('x-segmented__item', o.value === value && 'x-segmented__item--on')}
|
||||
onClick={() => onChange?.(o.value)} data-testid={o.testId} aria-label={o.ariaLabel} title={o.ariaLabel}>
|
||||
<button key={o.value} type="button" aria-pressed={on(o.value)} className={cx('x-segmented__item', on(o.value) && 'x-segmented__item--on')}
|
||||
onClick={() => pick(o.value)} data-testid={o.testId} aria-label={o.ariaLabel} title={o.ariaLabel}>
|
||||
{o.icon}{o.label}
|
||||
</button>
|
||||
))}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Children, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { cx } from '../internal/cx.js'
|
||||
|
||||
/**
|
||||
* A row of fields that wraps (2026-11-21): each field keeps its least width (`min`) and the ones
|
||||
* that don't fit go to a second line, in columns that line up from row to row. While a row is
|
||||
* wrapped it is framed and (with `shade`, every other row) tinted, so its two lines read as one
|
||||
* set; a row that fits on one line looks like any other row.
|
||||
* <FieldRow><Field …/><Field …/></FieldRow>
|
||||
* <FieldRow shade>…</FieldRow> (alternate rows)
|
||||
*/
|
||||
export function FieldRow({ min = '10.5rem', shade = false, className, style, children, ...rest }) {
|
||||
const ref = useRef(null)
|
||||
const [wrapped, setWrapped] = useState(false)
|
||||
const count = Children.count(children)
|
||||
useLayoutEffect(() => {
|
||||
const el = ref.current
|
||||
if (!el) return undefined
|
||||
const check = () => {
|
||||
const kids = [...el.children].filter(k => k.offsetParent !== null || k.getClientRects().length)
|
||||
setWrapped(kids.length > 1 && kids.some(k => k.offsetTop > kids[0].offsetTop + 2))
|
||||
}
|
||||
check()
|
||||
const ro = new ResizeObserver(check)
|
||||
ro.observe(el)
|
||||
return () => ro.disconnect()
|
||||
}, [count])
|
||||
return (
|
||||
<div ref={ref} className={cx('x-field-row', wrapped && 'x-field-row--wrapped', wrapped && shade && 'x-field-row--shade', className)}
|
||||
style={{ ...style, '--x-field-min': min }} data-wrapped={wrapped ? 'true' : undefined} {...rest}>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import { cx } from '../internal/cx.js'
|
||||
import { Field } from './Field.jsx'
|
||||
import { Popover } from './Popover.jsx'
|
||||
import { useLabels } from './labels.js'
|
||||
|
||||
/**
|
||||
* Pick an icon from a set (2026-11-21): a field showing the icon chosen; a click opens a grid of
|
||||
* the set over the page (Popover), with "No icon" first.
|
||||
* <IconPicker label="Icon" icons={{ flame: Flame, leaf: Leaf }} value={key} onChange={setKey} />
|
||||
* icons: { key: a component taking `size` }. value null = no icon.
|
||||
*/
|
||||
export function IconPicker({ label, icons = {}, value, onChange, className, testId }) {
|
||||
const labels = useLabels()
|
||||
const [open, setOpen] = useState(false)
|
||||
const btn = useRef(null)
|
||||
const Current = value ? icons[value] : null
|
||||
const pick = k => { onChange?.(k); setOpen(false) }
|
||||
const tid = s => (testId ? `${testId}-${s}` : undefined)
|
||||
return (
|
||||
<>
|
||||
<Field label={label} className={className}>
|
||||
<button ref={btn} type="button" className="x-colour" onClick={() => setOpen(o => !o)} aria-expanded={open} aria-haspopup="dialog" data-testid={tid('open')}>
|
||||
<span className="x-icon-pick__current" aria-hidden="true">{Current ? <Current size={18} /> : null}</span>
|
||||
<span className="x-colour__value">{value || labels.noIcon}</span>
|
||||
</button>
|
||||
</Field>
|
||||
<Popover open={open} anchor={btn} onClose={() => setOpen(false)} aria-label={label} className="x-icon-pick" data-testid={tid('panel')}>
|
||||
<button type="button" className={cx('x-icon-pick__none', !value && 'x-icon-pick__item--on')} onClick={() => pick(null)} data-testid={tid('none')}>{labels.noIcon}</button>
|
||||
<div className="x-icon-pick__grid">
|
||||
{Object.entries(icons).map(([k, Icon]) => (
|
||||
<button key={k} type="button" className={cx('x-icon-pick__item', value === k && 'x-icon-pick__item--on')} aria-label={k} aria-pressed={value === k} title={k}
|
||||
onClick={() => pick(k)} data-testid={tid(k)}>
|
||||
<Icon size={20} aria-hidden="true" />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</Popover>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Field } from './Field.jsx'
|
||||
|
||||
/**
|
||||
* A slider with its label and its value, framed and as tall as the fields beside it (2026-11-21).
|
||||
* <Slider label="Text size" value={size} min={50} max={250} step={10} suffix="%" onChange={setSize} />
|
||||
* onChange receives the number.
|
||||
*/
|
||||
export function Slider({ label, value, min = 0, max = 100, step = 1, onChange, suffix = '', className, testId }) {
|
||||
return (
|
||||
<Field label={label} className={className}>
|
||||
<span className="x-slider">
|
||||
<input type="range" min={min} max={max} step={step} value={value} onChange={e => onChange?.(Number(e.target.value))}
|
||||
className="x-slider__input" aria-label={label} data-testid={testId} />
|
||||
<span className="x-slider__value">{value}{suffix}</span>
|
||||
</span>
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// A colour's helpers for ColourField (2026-11-21): pure, unit-tested (tests/colour.test.mjs).
|
||||
|
||||
/** Twelve ready colours, two rows of six (a venue's colours: data, not the theme's). */
|
||||
export const COLOUR_PRESETS = [
|
||||
'#ffffff', '#d1d5db', '#6b7280', '#111827', '#000000', '#fde68a',
|
||||
'#f59e0b', '#ef4444', '#f472b6', '#a78bfa', '#38bdf8', '#22c55e',
|
||||
]
|
||||
|
||||
/** A colour as its #rrggbb and its opacity (0–100 %): #rrggbbaa is see-through. */
|
||||
export function splitAlpha(hex) {
|
||||
const h = String(hex || '#000000').toLowerCase()
|
||||
const rgb = /^#[0-9a-f]{6}/.test(h) ? h.slice(0, 7) : '#000000'
|
||||
const alpha = /^#[0-9a-f]{8}$/.test(h) ? Math.round((parseInt(h.slice(7, 9), 16) / 255) * 100) : 100
|
||||
return { rgb, alpha }
|
||||
}
|
||||
|
||||
/** #rrggbb with an opacity: #rrggbb when it is solid, #rrggbbaa otherwise. */
|
||||
export function joinAlpha(rgb, alpha) {
|
||||
const a = Math.max(0, Math.min(100, Math.round(alpha)))
|
||||
return a >= 100 ? rgb : `${rgb}${Math.round((a / 100) * 255).toString(16).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
/** A colour typed by hand: #rgb, #rrggbb or #rrggbbaa (the # optional) → #rrggbb(aa), or null. */
|
||||
export function typedColour(text) {
|
||||
let h = String(text || '').trim().toLowerCase().replace(/^#/, '')
|
||||
if (/^[0-9a-f]{3}$/.test(h)) h = h.split('').map(x => x + x).join('')
|
||||
return /^[0-9a-f]{6}([0-9a-f]{2})?$/.test(h) ? `#${h}` : null
|
||||
}
|
||||
@@ -21,6 +21,10 @@ export const DEFAULT_LABELS = {
|
||||
all: 'All',
|
||||
noOptions: 'Nothing found',
|
||||
selected: n => `${n} selected`,
|
||||
colourOwn: 'Your own colour',
|
||||
colourCode: 'Colour code',
|
||||
opacity: 'Opacity',
|
||||
noIcon: 'No icon',
|
||||
pinProgress: (n, total) => `${n} of ${total} digits entered`,
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,11 @@ export { Spinner } from './components/Spinner.jsx'
|
||||
export { Field, Input, Textarea, Select } from './components/Field.jsx'
|
||||
export { Dropdown } from './components/Dropdown.jsx'
|
||||
export { Popover } from './components/Popover.jsx'
|
||||
export { ColourField, Swatch } from './components/ColourField.jsx'
|
||||
export { COLOUR_PRESETS, joinAlpha, splitAlpha, typedColour } from './components/colour.js'
|
||||
export { FieldRow } from './components/FieldRow.jsx'
|
||||
export { Slider } from './components/Slider.jsx'
|
||||
export { IconPicker } from './components/IconPicker.jsx'
|
||||
export { Page, PageHeader, Pane, Toolbar, ToolbarSpacer } from './components/Page.jsx'
|
||||
export { SearchInput, FilterChip, Segmented, Tabs } from './components/Controls.jsx'
|
||||
export { Switch } from './components/Switch.jsx'
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { COLOUR_PRESETS, joinAlpha, splitAlpha, typedColour } from '../src/components/colour.js'
|
||||
|
||||
test('a colour with its opacity: #rrggbbaa, typed by hand', () => {
|
||||
assert.equal(COLOUR_PRESETS.length, 12)
|
||||
assert.deepEqual(splitAlpha('#FF0000'), { rgb: '#ff0000', alpha: 100 })
|
||||
assert.deepEqual(splitAlpha('#ff000080'), { rgb: '#ff0000', alpha: 50 })
|
||||
assert.equal(joinAlpha('#ff0000', 100), '#ff0000')
|
||||
assert.equal(joinAlpha('#ff0000', 50), '#ff000080')
|
||||
assert.equal(joinAlpha('#ff0000', 0), '#ff000000')
|
||||
assert.equal(typedColour('abc'), '#aabbcc')
|
||||
assert.equal(typedColour(' #11223344 '), '#11223344')
|
||||
assert.equal(typedColour('#12345'), null)
|
||||
})
|
||||
Reference in New Issue
Block a user