Ejemplos
Ejemplos prácticos. Demos en vivo.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Un componente reenvía sus hijos con un spread: builders, texto, objetos de atributos y modificadores. Las partes condicionales viven en when(), así el componente que las rodea nunca se reconstruye.
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() }, })), ), )