-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydrate.js
36 lines (32 loc) · 995 Bytes
/
hydrate.js
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
import { render } from 'fre'
function createWalker(node) {
return document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT, {
acceptNode: () => NodeFilter.FILTER_ACCEPT,
})
}
function morph(sourceElement, targetElement) {
const sourceWalker = createWalker(sourceElement)
const targetWalker = createWalker(targetElement)
const walk = fn =>
sourceWalker.nextNode() && targetWalker.nextNode() && fn() && walk(fn)
walk(() => {
const { currentNode } = sourceWalker
const targetNode = targetWalker.currentNode
// TODO more things
if (currentNode.tagName === targetNode.tagName) {
if (currentNode === document.activeElement) {
requestAnimationFrame(() => {
targetNode.focus()
})
}
}
return true
})
}
export default function hydrate(vnode, node, config) {
config.hydrate = true
const clone = node.cloneNode(false)
render(vnode, clone, config)
morph(clone, node)
node.parentNode.replaceChild(clone, node)
}