-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (188 loc) · 8.09 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="style.css" />
<link rel="icon" type="image/svg" href="capsule-logo.svg" />
<link rel="stylesheet" data-name="vs/editor/editor.main" href="https://packup.deno.dev/monaco-editor/min/vs/editor/editor.main.css" />
<title>Capsule | Event-driven DOM programming in a new style</title>
<meta name="description" content="📦 Zero-config web application packager for Deno 🦕">
</head>
<main class="max-w-screen-lg px-2 m-auto">
<a href="https://github.com/capsidjs/capsule">
<h1 class="text-2xl font-semibold mt-20 flex items-center gap-2">
<img class="w-7 h-7" src="capsule-logo.svg" />
Capsule examples
</h1>
</a>
<p class="text-gray-700 mt-4">
This page shows the examples of
<a href="https://github.com/capsidjs/capsule" class="bg-blue-50 text-blue-800 underline font-medium rounded border px-1 py-0.5">capsule</a>
frontend library.</p>
<section class="example mt-10">
<h2 class="text-lg font-medium">Mirroring example</h2>
<p class="text-gray-700 mt-1">The example of mirroring the input value to another dom.</p>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const { on } = component("mirroring");
on.input = ({ query }) => {
query(".dest").textContent = query(".src").value;
};</pre>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<template>
<div class="mirroring">
<input class="src border" />
<p class="dest"></p>
</div>
</template>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<hr class="my-10 -mx-2 lg:mx-2" />
<section class="example">
<h2 class="text-lg font-medium">Mount hook example</h2>
<p>When you assign a function to "on.__mount__", it's called when the component is mounted.</p>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const { on } = component("hello");
on.__mount__ = ({ el }) => {
el.textContent = `Hello, I'm ${el.textContent}! 👋`;
};</pre>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<template>
<div class="hello">John Doe</div>
</template>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<hr class="my-10 -mx-2 lg:mx-2" />
<section class="example">
<h2 class="text-lg font-medium">Pubsub example</h2>
<p>
<a href="https://github.com/capsidjs/capsule" class="bg-blue-50 text-blue-800 underline font-medium rounded border px-1 py-0.5">capsule</a>
supports pubsub pattern with "pub" and "sub" methods.
</p>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const EVENT = "my-event";
{
const { on } = component("pub-element");
on.click = ({ pub }) => {
pub(EVENT, { hello: "clicked!" });
};
}
{
const { on, sub } = component("sub-element");
sub(EVENT);
on[EVENT] = ({ e, el }) => {
el.textContent += " " + e.detail.hello;
};
}</pre>
<template>
<button class="pub-element">publisher (click here)</button>
<div class="sub-element">subscriber</div>
</template>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<hr class="my-10 -mx-2 lg:mx-2" />
<section class="example">
<h2 class="text-lg font-medium">Event delegation example</h2>
<p>
Capsule supports
<a href="https://www.geeksforgeeks.org/event-delegation-in-javascript/" class="bg-purple-50 text-purple-800 underline font-medium rounded border px-1 py-0.5">Event delegation</a>
pattern.
</p>
<template>
<div class="delegation-example">
<button class="btn border rounded p-2 m-2">btn</button>
<p>
The event handler is bound to wrapper div (.delegation-example), but the handler is
only triggered when the event targeted to '.btn' element.
</p>
<hr class="my-3" />
<div class="result"></div>
</div>
</template>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const { on } = component("delegation-example");
on(".btn").click = ({ query }) => {
query(".result").textContent += " .btn clicked!";
};
</pre>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<hr class="my-10 -mx-2 lg:mx-2" />
<section class="example">
<h2 class="text-lg font-medium">Outside event example</h2>
<p>
When you're creating floating UI patterns such as tooltips or modal dialogs, you often need to handle the events "outside" of the target dom.
Capsule supports this pattern with "on.outside[eventName]".
</p>
<template>
<div class="outside-example">click outside of this area.</div>
</template>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const { on } = component("outside-example");
on.outside.click = ({ el }) => {
el.textContent += " outside clicked!";
}</pre>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<hr class="my-10 -mx-2 lg:mx-2" />
<section class="example">
<h2 class="text-lg font-medium">Prevent default example</h2>
<p>If you need to preventDefault or stopPropagation, you can access it via '.e' prop.</p>
<template>
<a href="https://google.com" class="prevent-example underline">Link</a>
</template>
<h3 class="mt-10">JS</h3>
<pre class="js-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2">const { on } = component("prevent-example");
on.click = ({ e }) => {
e.preventDefault();
alert("A link is clicked, but the page doesn't move to the target url because the default behavior is prevented ;)");
};</pre>
<h3 class="mt-10">HTML</h3>
<pre class="html-source overflow-x-scroll lg:rounded-lg shadow-lg bg-gray-800 text-gray-100 p-5 mt-5 -mx-2"></pre>
<h3 class="mt-10">Result</h3>
<div class="result border rounded-lg p-5 mt-5"></div>
</section>
<p class="mt-10">
More details about capsule library is available in
<a class="underline" href="https://github.com/capsidjs/capsule">the github repository</a>.
</p>
</main>
<footer class="mt-10 py-10 flex items-center justify-center gap-2">
<img class="w-7 h-7" src="capsule-logo.svg" />
<a class="font-medium" href="https://github.com/capsidjs/capsule">Capsule</span>
</footer>
<script>
var require = { paths: { vs: 'https://packup.deno.dev/monaco-editor/min/vs' } };
</script>
<script src="https://packup.deno.dev/monaco-editor/min/vs/loader.js"></script>
<script>
require(['vs/editor/editor.main'], () => {
monaco.editor.setTheme("vs-dark");
});
</script>
<script type="module">
import { component } from "./dist.js";
component("example").on.__mount__ = ({ el, query }) => {
const result = query(".result");
result.appendChild(query("template").content.cloneNode(true));
query(".html-source").textContent = result.innerHTML.trim();
const js = query(".js-source").textContent;
try {
eval(js)
} catch {
// pass
}
monaco.editor.colorizeElement(query(".html-source"), { mimeType: "text/html", theme: "vs-dark" });
monaco.editor.colorizeElement(query(".js-source"), { mimeType: "text/javascript", theme: "vs-dark" });
};
</script>