-
Notifications
You must be signed in to change notification settings - Fork 5
/
reactivity.html
60 lines (53 loc) · 1.85 KB
/
reactivity.html
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
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>🧡</title>
<script src="tailwind.js"></script>
</head>
<body>
<div class="flex h-screen items-center justify-center">
<div id="app"></div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
// Import reactivity elements from Vue
const { ref, watch } = Vue;
// Import vdom elemens from our own VDOM engine
import { h, mount, patch } from './vue-vdom.js';
const counter = ref(0);
// Render function
const render = () => {
return h('div', { class: 'text-center' }, [
h(
'h1',
{ class: 'text-3xl font-bold' },
`Count: ${counter.value}`
),
h(
'button',
{
class: 'bg-gray-200 p-2 rounded',
onClick: () => counter.value++,
},
'Click!'
),
]);
};
// Initial DOM tree
let vdom = render();
// Mount the DOM tree
const app = document.getElementById('app');
mount(vdom, app);
// Watch for changes in the counter
watch(counter, () => {
// Patch the DOM tree
const vdomNew = render();
patch(vdom, vdomNew);
vdom = vdomNew;
});
</script>
</body>
</html>