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.
A component forwards its children with a spread: builders, text, attribute objects and modifiers. Conditional parts go in when(), so the component around them is never rebuilt.
Children, text, an attribute object and a modifier, all passed through.
This paragraph is inside a when(): closing removes it, opening builds it again. The card around it stays.
import 'nuclo' let open = true let saved = 0 let mounts = 0 // Counts every Card built, refreshing only the "stats" scope. const mounted = () => on('mount', () => { mounts++ queueMicrotask(() => update('stats')) }) const row = { style: { display: 'flex', flexWrap: 'wrap', gap: '6px' } } // Children are whatever a tag builder accepts: builders, text, attribute // objects and modifiers. A component forwards them with a spread, so a // caller can add attributes or hooks without the component knowing. function Card(title: string | (() => string), ...children: NodeModLike<'div'>[]) { return div(mounted(), div(h3(title), small('Card')), ...children) } // A modifier is a function with a parameter: it gets the element once. const tooltip = (text: string) => (el: ExpandedElement<'div'>) => { el.title = text } export const App = () => div( div(scope('stats'), span('mounts ', b(() => String(mounts)))), Card( 'Settings', p('Children, text, an attribute object and a modifier, all passed through.'), { 'data-section': 'settings' }, tooltip('Hover: a modifier set this title'), div(row, button(() => `Saved ${saved}×`, { onClick: () => { saved++; update() }, })), ), Card( () => (open ? 'Details (open)' : 'Details (closed)'), // dynamic title // Conditional children go in when(): the card is never rebuilt. when(() => open, p('This paragraph is inside a when(): closing removes it, opening builds it again. The card around it stays.')), div(row, button(() => (open ? 'Close' : 'Open'), { onClick: () => { open = !open; update() }, })), ), )