예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
컴포넌트는 spread로 자식을 그대로 넘겨 줍니다. 빌더, 텍스트, 속성 객체, modifier 모두 가능합니다. 조건부 내용은 when() 안에 두기 때문에 이들을 감싼 컴포넌트는 다시 만들어지지 않습니다.
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() }, })), ), )