Skip to content

Commit f7df756

Browse files
authored
Post/013 vnode (#28)
* docs: Post 13 - Shades VNode refactor * rephrase * Related content fine-tune * added husky, fixed lint error
1 parent 050285f commit f7df756

File tree

9 files changed

+332
-145
lines changed

9 files changed

+332
-145
lines changed

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarn lint-staged

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
"preview": "astro preview",
1414
"lint": "eslint .",
1515
"lint:fix": "eslint --fix .",
16-
"format": "prettier --write ."
16+
"format": "prettier --write .",
17+
"prepare": "husky"
18+
},
19+
"lint-staged": {
20+
"*.{js,ts,astro}": "eslint --fix",
21+
"*": "prettier --write --ignore-unknown"
1722
},
1823
"engines": {
1924
"node": ">=22.0.0"
@@ -33,6 +38,8 @@
3338
"@types/node": "^25.3.3",
3439
"eslint": "^10.0.2",
3540
"eslint-plugin-astro": "^1.6.0",
41+
"husky": "^9.1.7",
42+
"lint-staged": "^16.3.2",
3643
"prettier": "^3.8.1",
3744
"prettier-plugin-astro": "^0.14.1",
3845
"typescript": "^5.9.3",

src/components/ReadNext.astro

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
---
22
import PostCard from './PostCard.astro';
3-
import ReadNextCard from './ReadNextCard.astro';
43
import type { CollectionEntry } from 'astro:content';
54
65
interface Props {
76
currentSlug: string;
8-
tags: string[];
97
relatedPosts: CollectionEntry<'posts'>[];
108
prevPost?: CollectionEntry<'posts'> | null;
119
nextPost?: CollectionEntry<'posts'> | null;
1210
}
1311
14-
const { currentSlug, tags, relatedPosts, prevPost, nextPost } = Astro.props;
15-
const showRelatedPosts = relatedPosts.length > 1;
16-
const hasContent = showRelatedPosts || prevPost || nextPost;
12+
const { currentSlug, relatedPosts, prevPost, nextPost } = Astro.props;
13+
14+
const MAX_CARDS = 3;
15+
const seen = new Set<string>([currentSlug]);
16+
const cards: CollectionEntry<'posts'>[] = [];
17+
18+
for (const post of relatedPosts) {
19+
if (cards.length >= MAX_CARDS) break;
20+
if (!seen.has(post.id)) {
21+
seen.add(post.id);
22+
cards.push(post);
23+
}
24+
}
25+
26+
for (const post of [prevPost, nextPost]) {
27+
if (cards.length >= MAX_CARDS) break;
28+
if (post && !seen.has(post.id)) {
29+
seen.add(post.id);
30+
cards.push(post);
31+
}
32+
}
1733
---
1834

1935
{
20-
hasContent && (
36+
cards.length > 0 && (
2137
<aside class="read-next">
2238
<div class="read-next-inner">
2339
<h3 class="read-next-heading">Read Next</h3>
2440
<div class="read-next-feed">
25-
{showRelatedPosts && (
26-
<ReadNextCard currentSlug={currentSlug} tags={tags} relatedPosts={relatedPosts} />
27-
)}
28-
{prevPost && <PostCard post={prevPost} />}
29-
{nextPost && <PostCard post={nextPost} />}
41+
{cards.map(post => (
42+
<PostCard post={post} />
43+
))}
3044
</div>
3145
</div>
3246
</aside>

src/components/ReadNextCard.astro

Lines changed: 0 additions & 124 deletions
This file was deleted.
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 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.
534 KB
Loading

src/layouts/PostLayout.astro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ const primaryTag = tags?.[0];
8989

9090
<ReadNext
9191
currentSlug={post.id}
92-
tags={tags || []}
9392
relatedPosts={relatedPosts}
9493
prevPost={prevPost}
9594
nextPost={nextPost}

src/pages/posts/[...slug].astro

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ export async function getStaticPaths() {
1313
return sorted.map((post, index) => {
1414
const prev = index > 0 ? sorted[index - 1] : null;
1515
const next = index < sorted.length - 1 ? sorted[index + 1] : null;
16-
const primaryTag = post.data.tags?.[0];
17-
const relatedPosts = primaryTag
18-
? sorted.filter(p => p.data.tags?.includes(primaryTag) && p.id !== post.id)
19-
: [];
16+
const currentTags = post.data.tags ?? [];
17+
const relatedPosts =
18+
currentTags.length > 0
19+
? sorted
20+
.filter(p => p.id !== post.id && p.data.tags?.some(t => currentTags.includes(t)))
21+
.map(p => ({
22+
post: p,
23+
overlap: (p.data.tags ?? []).filter(t => currentTags.includes(t)).length,
24+
}))
25+
.sort(
26+
(a, b) =>
27+
b.overlap - a.overlap || b.post.data.date.valueOf() - a.post.data.date.valueOf(),
28+
)
29+
.map(r => r.post)
30+
: [];
2031
const authorData = post.data.author
2132
.map(authorId => authors.find(a => a.data.id === authorId)?.data)
2233
.filter(Boolean);

0 commit comments

Comments
 (0)