예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
CSS transition으로 X자로 변하는 메뉴 아이콘, 그리고 클릭하면 SMIL 애니메이션으로 통통 튀어 오르는 하트입니다.
import 'nuclo' let open = false let liked = false const line = (y: number, transform: () => string) => lineSvg({ x1: 4, y1: y, x2: 20, y2: y, style: () => ({ transition: 'transform 0.3s', transformOrigin: '12px 12px', transform: transform(), }), }) // Menu → cross: each line's CSS transform is a function of `open`. const MenuIcon = () => svgSvg( { viewBox: '0 0 24 24', width: '64', height: '64', fill: 'none', stroke: '#222', 'stroke-width': 2, 'stroke-linecap': 'round', style: { cursor: 'pointer' }, onClick: () => { open = !open; update() }, }, line(6, () => (open ? 'translateY(6px) rotate(45deg)' : '')), line(12, () => (open ? 'scaleX(0)' : '')), line(18, () => (open ? 'translateY(-6px) rotate(-45deg)' : '')), ) // Heart: the fill is a function of `liked`. A SMIL scale that // begins on click pops it, with no state of its own. const HeartIcon = () => svgSvg( { viewBox: '0 0 24 24', width: '64', height: '64', stroke: '#ff3f00', 'stroke-width': 2, 'stroke-linejoin': 'round', style: { cursor: 'pointer' }, onClick: () => { liked = !liked; update() }, }, gSvg( { transform: 'translate(12 12)' }, animateTransformSvg({ attributeName: 'transform', type: 'scale', additive: 'sum', values: '1;1.3;1', dur: '0.35s', begin: 'click', }), pathSvg({ transform: 'translate(-12 -12)', d: 'M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z', style: { transition: 'fill 0.2s' }, fill: () => (liked ? '#ff3f00' : 'none'), }), ), ) // Both icons side by side, and a text that reads the same two variables. export const IconToggles = () => div( div( { style: { display: 'flex', gap: '28px', justifyContent: 'center' } }, MenuIcon(), HeartIcon(), ), div( { style: { textAlign: 'center', fontSize: '12px', color: '#666' } }, () => `menu ${open ? 'open' : 'closed'} · ${liked ? 'liked' : 'not liked'}`, ), )