-
-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathrender.ts
More file actions
53 lines (47 loc) · 1.46 KB
/
render.ts
File metadata and controls
53 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type * as d from '../declarations';
import { renderVdom } from './vdom/vdom-render';
/**
* A WeakMap to persist HostRef objects across multiple render() calls to the
* same container. This enables VNode diffing on re-renders — without it, each
* call creates a fresh HostRef with no previous VNode, causing renderVdom to
* replace the entire DOM subtree instead of patching only what changed.
*/
const hostRefCache = new WeakMap<Element, d.HostRef>();
/**
* Method to render a virtual DOM tree to a container element.
*
* Supports efficient re-renders: calling `render()` again on the same container
* will diff the new VNode tree against the previous one and only update what changed,
* preserving existing DOM elements and their state.
*
* @example
* ```tsx
* import { render } from '@stencil/core';
*
* const vnode = (
* <div>
* <h1>Hello, world!</h1>
* </div>
* );
* render(vnode, document.body);
* ```
*
* @param vnode - The virtual DOM tree to render
* @param container - The container element to render the virtual DOM tree to
*/
export function render(vnode: d.VNode, container: Element) {
let ref = hostRefCache.get(container);
if (!ref) {
const cmpMeta: d.ComponentRuntimeMeta = {
$flags$: 0,
$tagName$: container.tagName,
};
ref = {
$flags$: 0,
$cmpMeta$: cmpMeta,
$hostElement$: container as d.HostElement,
};
hostRefCache.set(container, ref);
}
renderVdom(ref, vnode);
}