feat(ui): @xenia/ui design tokens, Tailwind preset and base components
Platform-v2 step 2.3 (APPS_PLATFORM D-15 §9): - tokens.css: --x- prefixed colours (light/dark/system + 5 venue accent themes), spacing, radius, type scale, shadows, motion, z-index; variables only - components.css: .x-* classes only, no element selectors (new rule CS-16) - Button, IconButton, Dialog, Sheet, Toast, PinPad, InfoTip, EmptyState, Spinner, Field/Input/Textarea/Select, Switch (accessible: focus trap and restore, ARIA wiring, 44px targets) - tailwind-preset.js (adds names only); manager uses it + styles.css, the waiter imports tokens.css; existing screens unchanged - dev-only /ui-gallery in the manager; e2e suite tests/e2e/ui.mjs; unit tests for PIN rules and WCAG AA contrast of every token pair, both in run-all Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { cx } from '../internal/cx.js'
|
||||
import { Spinner } from './Spinner.jsx'
|
||||
import { nextPin, pinAction } from './pin.js'
|
||||
|
||||
const KEYS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', null, '0', 'Backspace']
|
||||
|
||||
/**
|
||||
* Numeric PIN entry: dots + a 3×4 keypad, also driven by the physical keyboard.
|
||||
*
|
||||
* <PinPad length={4} onComplete={async pin => { await unlock(pin) }} />
|
||||
*
|
||||
* onComplete(pin) runs when `length` digits are in. If it returns a promise the pad
|
||||
* shows a spinner until it settles. If it throws / rejects (or returns false), the pad
|
||||
* shakes, shows `error` text if given, and clears for another try. On success it clears too.
|
||||
*
|
||||
* error: text under the dots (e.g. "Wrong PIN"); a change of it also shakes the pad.
|
||||
* captureKeyboard (default true): digits/Backspace typed anywhere go to the pad,
|
||||
* except while the user is typing in a field.
|
||||
* Labels for translation: backspaceLabel, progressLabel(n, length).
|
||||
*/
|
||||
export function PinPad({
|
||||
length = 4, onComplete, onChange, disabled = false, error, title,
|
||||
captureKeyboard = true, backspaceLabel = 'Delete',
|
||||
progressLabel = (n, total) => `${n} of ${total} digits entered`,
|
||||
className,
|
||||
}) {
|
||||
const [value, setValue] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [shake, setShake] = useState(0)
|
||||
const latest = useRef({ value, busy, disabled })
|
||||
useLayoutEffect(() => { latest.current = { value, busy, disabled } })
|
||||
|
||||
const fail = useCallback(() => {
|
||||
setShake(n => n + 1)
|
||||
setValue('')
|
||||
onChange?.('')
|
||||
}, [onChange])
|
||||
|
||||
const submit = useCallback(async pin => {
|
||||
let ok = true
|
||||
try {
|
||||
const result = onComplete?.(pin)
|
||||
if (result && typeof result.then === 'function') {
|
||||
setBusy(true)
|
||||
ok = (await result) !== false
|
||||
} else {
|
||||
ok = result !== false
|
||||
}
|
||||
} catch {
|
||||
ok = false
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
if (ok) { setValue(''); onChange?.('') } else fail()
|
||||
}, [onComplete, onChange, fail])
|
||||
|
||||
const press = useCallback(key => {
|
||||
const { value: current, busy: isBusy, disabled: isDisabled } = latest.current
|
||||
const action = pinAction(key)
|
||||
if (!action || isBusy || isDisabled) return
|
||||
const next = nextPin(current, action, length)
|
||||
if (next === current) return
|
||||
setValue(next)
|
||||
onChange?.(next)
|
||||
if (next.length === length) submit(next)
|
||||
}, [length, onChange, submit])
|
||||
|
||||
useEffect(() => {
|
||||
if (!captureKeyboard) return
|
||||
const onKey = e => {
|
||||
if (e.ctrlKey || e.metaKey || e.altKey) return
|
||||
const t = e.target
|
||||
if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return
|
||||
if (pinAction(e.key)) { e.preventDefault(); press(e.key) }
|
||||
}
|
||||
document.addEventListener('keydown', onKey)
|
||||
return () => document.removeEventListener('keydown', onKey)
|
||||
}, [captureKeyboard, press])
|
||||
|
||||
return (
|
||||
<div className={cx('x-pinpad', disabled && 'x-pinpad--disabled', className)}>
|
||||
{title && <div className="x-pinpad__title">{title}</div>}
|
||||
{/* Re-keyed on every failed attempt and on every new error text, which replays the shake */}
|
||||
<div key={`${shake}:${error || ''}`} className={cx('x-pinpad__dots', (shake > 0 || error) && 'x-pinpad__dots--shake')} aria-hidden="true">
|
||||
{Array.from({ length }, (_, i) => (
|
||||
<span key={i} className={cx('x-pinpad__dot', i < value.length && 'x-pinpad__dot--filled', error && 'x-pinpad__dot--error')} />
|
||||
))}
|
||||
</div>
|
||||
<div className="x-visually-hidden" aria-live="polite">{progressLabel(value.length, length)}</div>
|
||||
<div className="x-pinpad__status">
|
||||
{busy ? <Spinner size="sm" /> : error ? <span className="x-pinpad__error" role="alert">{error}</span> : null}
|
||||
</div>
|
||||
<div className="x-pinpad__keys">
|
||||
{KEYS.map(k => k === null
|
||||
? <span key="blank" aria-hidden="true" />
|
||||
: (
|
||||
<button
|
||||
key={k}
|
||||
type="button"
|
||||
className={cx('x-pinpad__key', k === 'Backspace' && 'x-pinpad__key--backspace')}
|
||||
onClick={() => press(k)}
|
||||
disabled={disabled || busy}
|
||||
aria-label={k === 'Backspace' ? backspaceLabel : undefined}
|
||||
>
|
||||
{k === 'Backspace'
|
||||
? (
|
||||
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
||||
<path d="M21 5H9l-6 7 6 7h12a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1z" /><path d="M17 9l-6 6M11 9l6 6" />
|
||||
</svg>
|
||||
)
|
||||
: k}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user