Skip to content

Commit 43ccc00

Browse files
committed
docs: Post 13 - Shades VNode refactor
1 parent df09c9e commit 43ccc00

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: 'Shades 12: The VNode Refactor'
3+
author: [gallayl]
4+
tags: ['shades']
5+
date: '2026-03-05T18:00:00.000Z'
6+
draft: false
7+
image: img/013-vnode.jpg
8+
excerpt: Shades v12 replaces the rendering engine with a VNode-based reconciler and drops lifecycle callbacks in favor of hooks — here's the full story.
9+
---
10+
11+
## Why rewrite the renderer
12+
13+
Shades has always had a simple rendering model: your `render()` function returns real DOM elements, the framework diffs them against the current tree, and patches the differences. It works — but it has a cost. Every render cycle creates a full shadow DOM tree just to compare it with the one on screen, even if nothing meaningful changed. That's a lot of allocation pressure and GC work for what often amounts to updating a single text node.
14+
15+
The v12 release replaces this with a **VNode-based reconciler**. Instead of creating real DOM elements during render, the JSX factory produces lightweight descriptor objects. A reconciler diffs the previous VNode tree against the new one and applies surgical DOM updates using tracked element references. No throwaway DOM trees, no redundant element creation.
16+
17+
## New hooks, fewer lifecycle callbacks
18+
19+
The old API surface had three separate lifecycle entry points: `constructed`, `onAttach`, and `onDetach`. Each had its own timing semantics and cleanup patterns. In v12, all three are gone — replaced by a single, composable primitive: **`useDisposable`**.
20+
21+
```typescript
22+
// Before — scattered lifecycle management
23+
Shade({
24+
shadowDomName: 'my-component',
25+
constructed: ({ element }) => {
26+
const listener = () => { /* ... */ }
27+
window.addEventListener('click', listener)
28+
return () => window.removeEventListener('click', listener)
29+
},
30+
render: () => <div>Hello</div>,
31+
})
32+
33+
// After — setup and teardown live together in render
34+
Shade({
35+
shadowDomName: 'my-component',
36+
render: ({ useDisposable }) => {
37+
useDisposable('click-handler', () => {
38+
const listener = () => { /* ... */ }
39+
window.addEventListener('click', listener)
40+
return { [Symbol.dispose]: () => window.removeEventListener('click', listener) }
41+
})
42+
return <div>Hello</div>
43+
},
44+
})
45+
```
46+
47+
The `element` parameter — direct access to the host custom element — is also gone. Imperatively mutating the host was always a bit at odds with a declarative component model. The replacement is **`useHostProps`**, which lets you set attributes, styles (including CSS custom properties), ARIA attributes, and event handlers declaratively:
48+
49+
```typescript
50+
render: ({ useHostProps, props }) => {
51+
useHostProps({
52+
'data-variant': props.variant,
53+
style: { '--color': colors.main },
54+
})
55+
return <button>{props.label}</button>
56+
}
57+
```
58+
59+
There's also a new **`useRef`** hook for capturing child element references — no more querying the shadow DOM manually:
60+
61+
```typescript
62+
render: ({ useRef }) => {
63+
const inputRef = useRef<HTMLInputElement>('input')
64+
return <input ref={inputRef} />
65+
// Later: inputRef.current?.focus()
66+
}
67+
```
68+
69+
## Batched updates
70+
71+
`updateComponent()` used to render synchronously. Call it three times in a row and you'd get three render passes. In v12, updates are scheduled via `queueMicrotask` and coalesced — multiple observable changes within the same synchronous block produce a single render pass. The new `flushUpdates()` utility gives tests a reliable way to wait for pending renders without arbitrary `sleepAsync` calls.
72+
73+
## SVG support
74+
75+
Shades now handles SVG elements natively. Elements are created with `createElementNS` under the correct namespace, and attributes are applied via `setAttribute` instead of property assignment. A full set of typed SVG attribute interfaces covers shapes, gradients, filters, and animations — so you get proper autocompletion in your editor.
76+
77+
## Migration at a glance
78+
79+
| Removed | Replacement |
80+
| ------------------------------- | ---------------------------------------- |
81+
| `constructed` callback | `useDisposable` in `render` |
82+
| `element` in render options | `useHostProps` hook |
83+
| `onAttach` / `onDetach` | `useDisposable` |
84+
| Synchronous `updateComponent()` | Async batched updates + `flushUpdates()` |
85+
86+
## What's next
87+
88+
The subsequent v12.x releases have already landed dependency tracking for `useDisposable`, a `css` property for component-level styling with pseudo-selectors, and a brand new routing system. The framework is moving fast — stay tuned for a dedicated post on the new `NestedRouter`.
89+
90+
If you want to try it out: `npm install @furystack/shades@latest` and check the [changelog](https://github.com/furystack/furystack/blob/develop/packages/shades/CHANGELOG.md#1220---2026-02-22) for the full details.
534 KB
Loading

0 commit comments

Comments
 (0)