Skip to content

Commit 27fc0e7

Browse files
committed
feat: Initializing repository
0 parents  commit 27fc0e7

File tree

9 files changed

+273
-0
lines changed

9 files changed

+273
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/*
8+
!.yarn/releases
9+
!.yarn/plugins
10+
.pnp.*
11+
12+
# testing
13+
/coverage
14+
15+
# production
16+
/dist
17+
/build
18+
/.yalc
19+
20+
# misc
21+
.DS_Store
22+
.env.local
23+
.env.development.local
24+
.env.test.local
25+
.env.production.local
26+
27+
npm-debug.log*
28+
yarn-debug.log*
29+
yarn-error.log*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 The Preact Authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# `preact-root-fragment`: partial root rendering for Preact
2+
3+
This is a standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10.
4+
5+
It provides a way to render or hydrate a Preact tree using a subset of the children within the parent element passed to render():
6+
7+
```html
8+
<body>
9+
<div id="root">
10+
⬅ we pass this to render() as the parent DOM element...
11+
12+
<script src="/etc.js"></script>
13+
14+
<div class="app">
15+
⬅ ... but we want to use this tree, not the script
16+
<!-- ... -->
17+
</div>
18+
</div>
19+
</body>
20+
```
21+
22+
### Why do I need this?
23+
24+
This is particularly useful for [partial hydration](https://jasonformat.com/islands-architecture/), which often requires rendering multiple distinct Preact trees into the same parent DOM element. Imagine the scenario below - which elements would we pass to `hydrate(jsx, parent)` such that each widget's `<section>` would get hydrated without clobbering the others?
25+
26+
```html
27+
<div id="sidebar">
28+
<section id="widgetA"><h1>Widget A</h1></section>
29+
<section id="widgetB"><h1>Widget B</h1></section>
30+
<section id="widgetC"><h1>Widget C</h1></section>
31+
</div>
32+
```
33+
34+
Preact 10 provided a somewhat obscure third argument for `render` and `hydrate` called `replaceNode`, which could be used for the above case:
35+
36+
```js
37+
render(<A />, sidebar, widgetA); // render into <div id="sidebar">, but only look at <section id="widgetA">
38+
render(<B />, sidebar, widgetB); // same, but only look at widgetB
39+
render(<C />, sidebar, widgetC); // same, but only look at widgetC
40+
```
41+
42+
While the `replaceNode` argument proved useful for handling scenarios like the above, it was limited to a single DOM element and could not accommodate Preact trees with multiple root elements. It also didn't handle updates well when multiple trees were mounted into the same parent DOM element, which turns out to be a key usage scenario.
43+
44+
Going forward, we're providing this functionality as a standalone library called `preact-root-fragment`.
45+
46+
### How it works
47+
48+
`preact-root-fragment` provides a `createRootFragment` function:
49+
50+
```ts
51+
createRootFragment(parent: ContainerNode, children: ContainerNode | ContainerNode[]);
52+
```
53+
54+
Calling this function with a parent DOM element and one or more child elements returns a "Persistent Fragment". A persistent fragment is a fake DOM element, which pretends to contain the provided children while keeping them in their existing real parent element. It can be passed to `render()` or `hydrate()` instead of the `parent` argument.
55+
56+
Using the previous example, we can change the deprecated `replaceNode` usage out for `createRootFragment`:
57+
58+
```js
59+
import { createRootFragment } from 'preact-root-fragment';
60+
61+
render(<A />, createRootFragment(sidebar, widgetA));
62+
render(<B />, createRootFragment(sidebar, widgetB));
63+
render(<C />, createRootFragment(sidebar, widgetC));
64+
```
65+
66+
Since we're creating separate "Persistent Fragment" parents to pass to each `render()` call, Preact will treat each as an independent Virtual DOM tree.
67+
68+
### Multiple Root Elements
69+
70+
Unlike the `replaceNode` parameter from Preact 10, `createRootFragment` can accept an Array of children that will be used as the root elements when rendering. This is particularly useful when rendering a Virtual DOM tree that produces multiple root elements, such as a Fragment or an Array:
71+
72+
```js
73+
import { createRootFragment } from 'preact-root-fragment';
74+
import { render } from 'preact';
75+
76+
function App() {
77+
return (
78+
<>
79+
<h1>Example</h1>
80+
<p>Hello world!</p>
81+
</>
82+
);
83+
}
84+
85+
// Use only the last two child elements within <body>:
86+
const children = [].slice.call(document.body.children, -2);
87+
88+
render(<App />, createRootFragment(document.body, children));
89+
```
90+
91+
### Preact Version Support
92+
93+
This library works with Preact 10 and 11.
94+
95+
### Changelog
96+
97+
#### 0.2.0 (2022-03-04)
98+
99+
- fix bug where nodes were appended instead of replaced (thanks @danielweck)
100+
- fix `.__k` assignment (thanks @danielweck)
101+
- fix Preact 10.6 debug error due to missing `nodeType` (thanks @danielweck)

jsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"noEmit": true,
7+
"allowJs": true,
8+
"checkJs": true,
9+
"skipLibCheck": false
10+
}
11+
}

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "preact-root-fragment",
3+
"version": "0.2.0",
4+
"type": "module",
5+
"exports": {
6+
".": "./src/index.js",
7+
"./package.json": "./package.json"
8+
},
9+
"license": "MIT",
10+
"description": "A standalone Preact 10+ implementation of the deprecated replaceNode parameter from Preact 10",
11+
"author": "The Preact Authors (https://preactjs.com)",
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/preactjs/preact-root-fragment.git"
15+
},
16+
"files": [
17+
"src",
18+
"!src/internal.d.ts",
19+
"LICENSE",
20+
"package.json",
21+
"README.md"
22+
],
23+
"peerDependencies": {
24+
"preact": "10.x || 11.x"
25+
},
26+
"devDependencies": {
27+
"preact": "^10.26.9"
28+
}
29+
}

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ContainerNode } from 'preact';
2+
3+
export function createRootFragment(
4+
parent: ContainerNode,
5+
replaceNode: ContainerNode | ContainerNode[],
6+
): ContainerNode;

src/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* A Preact 11+ implementation of the `replaceNode` parameter from Preact 10.
3+
*
4+
* This creates a "Persistent Fragment" (a fake DOM element) containing one or more
5+
* DOM nodes, which can then be passed as the `parent` argument to Preact's `render()` method.
6+
*
7+
* @param {import('preact').ContainerNode} parent
8+
* @param {import('preact').ContainerNode | import('preact').ContainerNode[]} replaceNode
9+
* @returns {import('preact').ContainerNode}
10+
*/
11+
export function createRootFragment(parent, replaceNode) {
12+
replaceNode = [].concat(replaceNode);
13+
var s = replaceNode[replaceNode.length - 1].nextSibling;
14+
15+
/**
16+
* @param {import('preact').ContainerNode} c
17+
* @param {import('preact').ContainerNode | null} [r]
18+
* @returns {import('preact').ContainerNode}
19+
*/
20+
function insert(c, r) {
21+
return parent.insertBefore(c, r || s);
22+
}
23+
24+
return (parent.__k = {
25+
nodeType: 1,
26+
parentNode: parent,
27+
firstChild: replaceNode[0],
28+
childNodes: replaceNode,
29+
insertBefore: insert,
30+
appendChild: insert,
31+
removeChild: function (c) {
32+
return parent.removeChild(c);
33+
},
34+
contains: function (c) {
35+
return parent.contains(c);
36+
},
37+
});
38+
}

0 commit comments

Comments
 (0)