Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Three steps as when() branches over one data object. Next touches the step's fields and advances only when they pass. Review lists everything with Edit links.
import 'nuclo' const data = { name: '', email: '', street: '', city: '', zip: '' } type Key = keyof typeof data const labels: Record<Key, string> = { name: 'Full name', email: 'Email', street: 'Street', city: 'City', zip: 'ZIP', } const touched = new Set<Key>() let step = 0 let done = false const errors = (): Record<Key, string | null> => ({ name: data.name.trim() ? null : 'Required', email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email) ? null : 'Enter a valid email', street: data.street.trim() ? null : 'Required', city: data.city.trim() ? null : 'Required', zip: /^\d{5}(-\d{4})?$/.test(data.zip) ? null : 'Five digits, like 12345', }) // Each step owns some fields. It is valid when they all are. const steps: { title: string; fields: Key[] }[] = [ { title: 'Account', fields: ['name', 'email'] }, { title: 'Address', fields: ['street', 'city', 'zip'] }, { title: 'Review', fields: [] }, ] const stepValid = (i: number) => steps[i].fields.every((k) => errors()[k] === null) function next() { // Touch the step's fields so every error shows, then advance if clean. steps[step].fields.forEach((k) => touched.add(k)) if (stepValid(step)) step++ update() } function Field(key: Key, type = 'text') { const show = () => touched.has(key) && errors()[key] const ok = () => touched.has(key) && !errors()[key] return div( label(labels[key]), input({ type, value: () => data[key], style: () => ({ borderColor: show() ? '#ef4444' : ok() ? '#22c55e' : '' }), 'aria-invalid': () => String(!!show()), onInput: (e) => { data[key] = e.currentTarget.value; update() }, onBlur: () => { touched.add(key); update() }, }), p({ role: 'alert' }, () => show() || ''), ) } // A bar per step, filled up to the current one const Progress = () => div( { style: { display: 'flex', gap: '6px' } }, ...steps.map((_, i) => div({ style: () => ({ flex: '1', height: '4px', borderRadius: '2px', backgroundColor: i <= step ? '#ff3f00' : '#cccccc', }), }), ), ) // A step is a when() branch, so moving on builds the next step fresh // and keeps the data in the plain object above. export const Wizard = () => div( Progress(), strong(() => steps[step].title, ' ', small(() => `step ${step + 1} of ${steps.length}`)), when(() => step === 0 && !done, Field('name'), Field('email', 'email'), ) .when(() => step === 1 && !done, Field('street'), Field('city'), Field('zip'), ) .when(() => step === 2 && !done, dl(...(Object.keys(labels) as Key[]).flatMap((k) => [ dt(labels[k]), dd(() => data[k]), button('Edit', { type: 'button', onClick: () => { step = steps.findIndex((s) => s.fields.includes(k)) update() }, }), ])), ) .else(p('Submitted. ', code(() => JSON.stringify(data)))), when(() => !done, div( button('Back', { disabled: () => step === 0, onClick: () => { step--; update() }, }), when(() => step < 2, button('Next', { onClick: next })) .else(button('Submit', { onClick: () => { done = true; update() } })), ), ), )