-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.html
150 lines (128 loc) · 4.46 KB
/
dom.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!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;
}
.danger {
border: 2px solid #ff0000 !important;
}
</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 with Nibble DOM -->
<!-- bind:value|text|html|property, class:, this, if -->
<div class="main border" class:danger="isDanger">
<p>
<a href="index.html"> Home </a>
</p>
<div>
<h1>NibbleJS DOM</h1>
</div>
<div>
<input type="number" bind:value="count">
</div>
<p>
Value: <span bind:text="count"></span> <br>
Computed: <span bind:text="computedValue"></span>
</p>
<p>
<button this="button">Reset</button>
</p>
<p>
<fieldset>
<legend>Fruits</legend>
<div>
<ul bind:html="fruits" class="list-group"></ul>
</div>
<div>
<input type="checkbox" id="apple" name="fruit[]" value="apple" bind:group="selectedFruits" />
<label for="apple">Apple</label>
</div>
<div>
<input type="checkbox" id="banana" name="fruit[]" value="banana" bind:group="selectedFruits" />
<label for="banana">Banana</label>
</div>
<div>
<input type="checkbox" id="mango" name="fruit[]" value="mango" bind:group="selectedFruits" />
<label for="mango">Mango</label>
</div>
</fieldset>
</p>
<p>
<fieldset>
<legend>Agree</legend>
<div if="agreement" class:danger="isDanger">
<h3>Conditional Agree</h3>
</div>
<div class="form-check">
<input type="radio" id="agree-no" name="agree" value="no" bind:group="agree" />
<label for="agree-no">No</label>
</div>
<div>
<input type="radio" id="agree-yes" name="agree" value="yes" bind:group="agree"
/>
<label for="agree-yes">Yes</label>
</div>
</fieldset>
</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 Nibble DOM api has only following directives
// with dom, signal must use var
var count = signal(0);
var computedValue = compute(() => count.value * 10);
var selectedFruits = signal(["mango"]);
var fruits = compute(() =>
selectedFruits.value.map((f) => `<li class="list-group-item">${f}</li>`).join(""),
);
var agree = signal("yes");
var agreement = compute(() =>
agree.value == "yes" ? true : false,
);
var isDanger = compute(() => count.value > 30 ? true : false);
var showItem = compute(() => count.value > 30 ? true : false);
document.addEventListener('DOMContentLoaded', app);
function app() {
console.log('loaded')
button.addEventListener('click', function () {
count.value = 0;
})
// make sure it runs after DOMContentLoaded
watch(count, (oldValue, newValue) => {
console.log(`count changed from ${oldValue} to ${newValue}`)
if (newValue > 50) {
alert("Above 50 not allowed");
count.value = 50;
}
})
}
</script>
</body>
</html>