-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.html
104 lines (86 loc) · 2.77 KB
/
signal.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NibbleJS</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.main {
padding: 1rem;
border: 1px solid #FFBF00;
border-radius: 1rem;
}
</style>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/picocss/2.0.6/pico.amber.min.css' integrity='sha512-kvYLueAc7RD0XOfxhjiaUKbXBmh5JTZdyJo/12oY/zpT0l6o82H2Ap5f27CrAa16VgvXj02Rsb0Prwtq7oYOSw==' crossorigin='anonymous'/>
</head>
<body>
<!-- Signals Only without Nibble DOM -->
<div class="main border">
<p>
<a href="index.html"> Home </a>
</p>
<div>
<h1>NibbleJS Signal</h1>
</div>
<div>
<input type="number" id="number">
</div>
<p>
Value: <span id="value"></span> <br>
Computed: <span id="computed"></span>
</p>
<p>
<button id="button">Reset</button>
</p>
<p>
<a href="https://github.com/nabeelalihashmi/nibble">GitHub Repo</a>
</p>
<p>
<small>
Created by Nabeel Ali - <a href="https://linkedin.com/in/nabeelalihashmi">linkedin.com/in/nabeelalihashmi</a>
</small>
</p>
</div>
<!-- Nibble DOM -->
<script src="./js/v1/signal.js"></script>
<script src="./js/v1/dom.js"></script>
<script>
// Complete signal api has only following members
// signal, compute, effect, watch
let count = signal(0);
let computedValue = compute(() => count.value * 10);
let input = document.querySelector("#number");
let valueDiv = document.querySelector("#value");
let computedDiv = document.querySelector("#computed");
let resetButton = document.querySelector("#button");
input.addEventListener('input', function (event) {
count.value = event.target.value
})
resetButton.addEventListener('click', function () {
count.value = 0;
})
effect(() => {
valueDiv.innerText = count.value;
})
effect(() => {
input.value = count.value;
})
effect(() => {
computedDiv.innerText = computedValue.value;
})
watch(count, (newValue, oldValue) => {
console.log(`count changed from ${oldValue} to ${newValue}`)
if (newValue > 50) {
alert("Above 50 not allowed");
count.value = 50;
}
})
</script>
</body>
</html>