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 squarified layout you drill into. Switching the metric slides every cell to its new box, and clicking a group swaps in its children.
import 'nuclo' type Node = { name: string; kb: number; gz: number; hue: number; parent?: Node; children?: Node[] } type Rect = { x: number; y: number; w: number; h: number } type Metric = 'kb' | 'gz' | 'count' const W = 320 const H = 200 const colors = ['#ff3f00', '#2563eb', '#ff8a00', '#6b7280'] const fade = keyframes({ from: { opacity: '0' }, to: { opacity: '1' } }) const slide = 'cubic-bezier(0.22, 1, 0.36, 1)' const leaf = (name: string, kb: number, gz: number): Node => ({ name, kb, gz, hue: 0 }) const group = (name: string, ...children: Node[]): Node => ({ name, kb: 0, gz: 0, hue: 0, children }) const root = group('bundle', group('core', leaf('element', 14, 5.1), leaf('list', 9, 3.2), leaf('when', 4, 1.6), leaf('update', 6, 2.4), leaf('style', 11, 3.9), leaf('svg', 8, 2.1), leaf('ssr', 12, 4.4)), group('router', leaf('match', 5, 1.9), leaf('layers', 4, 1.5), leaf('loaders', 6, 2.2), leaf('link', 2, 0.9), leaf('history', 3, 1.3)), group('docs', group('pages', leaf('home', 18, 5), leaf('api', 24, 7.5), leaf('examples', 30, 9.4)), group('demos', leaf('charts', 22, 7.1), leaf('forms', 16, 5.2), leaf('apps', 34, 10.2)), ), group('tools', leaf('cli', 10, 3.7), leaf('vite', 7, 2.8), leaf('lint', 5, 1.8)), ) // parents and colors: one hue per top-level child root.children!.forEach((top, i) => { const paint = (n: Node, parent: Node) => { n.parent = parent n.hue = i n.children?.forEach((c) => paint(c, n)) } paint(top, root) }) let metric: Metric = 'kb' let focus = root // the node we zoomed into let hover: Node | null = null let shown: Node[] = [] // the cells on screen let pos = new Map<Node, Rect>() // the box of every cell const sum = (list: number[]) => list.reduce((a, b) => a + b, 0) const weight = (n: Node): number => n.children ? sum(n.children.map(weight)) : metric === 'count' ? 1 : n[metric] const trail = (n: Node): Node[] => (n.parent ? [...trail(n.parent), n] : [n]) const format = (n: Node) => metric === 'count' ? weight(n) + (weight(n) === 1 ? ' module' : ' modules') : weight(n).toFixed(1) + ' KB' // Squarified layout: keep adding to the row while the worst // aspect ratio of the row gets better, then start a new row. function tile(nodes: Node[], box: Rect) { const total = sum(nodes.map(weight)) let rest = nodes .map((node) => ({ node, area: (weight(node) / total) * box.w * box.h })) .sort((a, b) => b.area - a.area) let { x, y, w, h } = box const worst = (row: number[], side: number) => { const s = sum(row) return Math.max(...row.map((a) => Math.max((side * side * a) / (s * s), (s * s) / (side * side * a)))) } while (rest.length) { const side = Math.min(w, h) let n = 1 while (n < rest.length && worst(rest.slice(0, n + 1).map((r) => r.area), side) <= worst(rest.slice(0, n).map((r) => r.area), side)) n++ const row = rest.slice(0, n) const s = sum(row.map((r) => r.area)) if (w >= h) { // a column on the left let cy = y for (const r of row) { pos.set(r.node, { x, y: cy, w: s / h, h: r.area / (s / h) }); cy += r.area / (s / h) } x += s / h; w -= s / h } else { // a row on top let cx = x for (const r of row) { pos.set(r.node, { x: cx, y, w: r.area / (s / w), h: s / w }); cx += r.area / (s / w) } y += s / w; h -= s / w } rest = rest.slice(n) } } // Two levels: the children of the focus, and inside each its own. function relayout() { pos = new Map() tile(focus.children!, { x: 0, y: 0, w: W, h: H }) for (const n of focus.children!) { const r = pos.get(n)! if (n.children) tile(n.children, { x: r.x + 3, y: r.y + 17, w: Math.max(1, r.w - 6), h: Math.max(1, r.h - 20) }) } } function go(node: Node) { focus = node hover = null shown = node.children!.flatMap((n) => (n.children ? [n, ...n.children] : [n])) relayout() update() } go(root) const box = (n: Node) => pos.get(n) ?? { x: 0, y: 0, w: 0, h: 0 } // a name is only shown when the cell is big enough for it const fits = (n: Node, w: number, h: number) => (box(n).w > w && box(n).h > h ? 1 : 0) const caption = () => hover ? trail(hover).slice(1).map((n) => n.name).join(' / ') + ' · ' + format(hover) + ' · ' + Math.round((weight(hover) / weight(focus)) * 100) + '%' : format(focus) + ' · ' + (focus.children!.some((n) => n.children) ? 'click a group to zoom in' : focus.children!.length + ' modules') const row = { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: '6px' } // list() matches nodes by identity. Switching the metric keeps // every cell, so the CSS transition on width, height and // transform slides it to its new place. Drilling into a node // swaps the cells, and a fade animation brings the new ones in. export const Treemap = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px', width: '100%', maxWidth: '380px' } }, svgSvg( { viewBox: '0 0 320 200', role: 'img', 'aria-label': 'Treemap', style: { width: '100%', overflow: 'visible' }, onPointerLeave: () => { hover = null; update() }, }, list(() => shown, (n) => gSvg( { style: () => ({ animation: fade + ' 0.45s ease both', transition: 'transform 0.45s ' + slide, transform: 'translate(' + box(n).x + 'px,' + box(n).y + 'px)', cursor: n.children ? 'pointer' : 'default', }), onClick: () => { if (n.children) go(n) }, onPointerEnter: () => { hover = n; update() }, }, rectSvg({ x: 0.5, y: 0.5, rx: 3, fill: colors[n.hue], stroke: '#fff', style: { transition: 'width 0.45s ' + slide + ', height 0.45s ' + slide + ', fill-opacity 0.2s' }, 'fill-opacity': () => n.children ? (n.parent === focus ? 0.18 : hover === n ? 0.8 : 0.55) : (hover === n ? 1 : 0.82), width: () => Math.max(0, box(n).w - 1), height: () => Math.max(0, box(n).h - 1), }), textSvg( { x: 6, y: n.children ? 12 : 14, 'font-size': n.children ? 11 : 10, 'font-weight': n.children ? 600 : 400, style: { pointerEvents: 'none', transition: 'opacity 0.2s', fill: n.children ? 'currentColor' : '#fff' }, opacity: () => fits(n, n.name.length * 5.6 + 12, 16), }, n.name, ), n.children ? null : textSvg( { x: 6, y: 26, 'font-size': 10, 'fill-opacity': 0.8, style: { pointerEvents: 'none', transition: 'opacity 0.2s', fill: '#fff' }, opacity: () => fits(n, 54, 34), }, () => format(n), ), ), ), ), // breadcrumbs: click one to zoom back out div( { style: { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: '4px', fontSize: '0.8125rem' } }, list(() => trail(focus), (n) => button(n.name, { 'aria-current': () => String(n === focus), style: () => ({ height: 'auto', padding: '2px 8px', borderRadius: '10px', border: 'none', fontWeight: n === focus ? '700' : '400' }), onClick: () => { if (n !== focus) go(n) }, }), ), ), div( { style: { fontFamily: 'ui-monospace, monospace', fontSize: '12px', textAlign: 'center', minHeight: '36px', opacity: '0.7' } }, caption, ), div( { style: row }, ...([['kb', 'Size'], ['gz', 'Gzip'], ['count', 'Modules']] as const).map(([m, name]) => button(name, { 'aria-pressed': () => String(metric === m), style: () => ({ outline: metric === m ? '2px solid currentColor' : 'none', outlineOffset: '2px' }), onClick: () => { metric = m; relayout(); update() }, }), ), ), )