|
| 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 had a beautifully dumb rendering model: your `render()` function spits out real DOM elements, the framework diffs them against what's already on screen, and patches the differences. Simple, honest, easy to reason about... and wasteful. Every single render cycle spun up a full shadow DOM tree _just to throw it away after comparison_. That's a lot of garbage collection for what often boils down to changing one text node. |
| 14 | + |
| 15 | +v12 rips that out and replaces it with a **VNode-based reconciler**. Now the JSX factory produces lightweight descriptor objects — plain JS, no DOM involved. The reconciler diffs the old VNode tree against the new one and pokes the real DOM only where something actually changed. No throwaway trees. No phantom elements. Just surgical updates. |
| 16 | + |
| 17 | +## Hooks in, lifecycle callbacks out |
| 18 | + |
| 19 | +The old API had three separate lifecycle hooks: `constructed`, `onAttach`, and `onDetach`. Three places to scatter your setup and teardown logic, three sets of timing semantics to keep in your head. In v12, they're all gone — consolidated into one composable primitive: **`useDisposable`**. |
| 20 | + |
| 21 | +```typescript |
| 22 | +// Before — lifecycle spaghetti |
| 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 cleanup live together, right where you use them |
| 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 is also gone. Reaching into the host element and mutating it imperatively was always a bit... rebellious for a declarative framework. Say hello to **`useHostProps`** instead — it lets you declare attributes, styles, CSS custom properties, ARIA attrs, and event handlers without ever touching the DOM yourself: |
| 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 | +And for those moments when you _do_ need a handle on a child element (focusing an input, measuring a bounding rect), there's **`useRef`** — no more `querySelector` treasure hunts through the shadow DOM: |
| 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 (a.k.a. stop re-rendering so much) |
| 70 | + |
| 71 | +`updateComponent()` used to be synchronous. Fire three observable changes in a row? Enjoy your three render passes. In v12, updates go through `queueMicrotask` and get coalesced — hammer as many observables as you want within a synchronous block and the component renders _once_. The new `flushUpdates()` utility lets tests await pending renders properly, so you can finally delete those sketchy `sleepAsync(50)` calls. |
| 72 | + |
| 73 | +## SVG — for real this time |
| 74 | + |
| 75 | +Shades now handles SVG elements as first-class citizens. Elements are created with `createElementNS` under the correct namespace, attributes go through `setAttribute` instead of property assignment (because SVG is picky like that), and there's a full set of typed interfaces covering shapes, gradients, filters, and animations. Your editor's autocomplete will thank you. |
| 76 | + |
| 77 | +## Migration cheat sheet |
| 78 | + |
| 79 | +| Gone | Use this instead | |
| 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 v12.x train keeps rolling — we've already shipped dependency tracking for `useDisposable`, a `css` property for component-level styling with pseudo-selectors, and a brand new routing system. Stay tuned for a dedicated post on the `NestedRouter`. |
| 89 | + |
| 90 | +Want to take it for a spin? `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 all the gory details. |
0 commit comments