-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
183 lines (165 loc) · 7.23 KB
/
demo.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
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="description" content="Demo of sig-nal. Sig-nal is a tiny web component for server-side rendering with signals."/>
<meta name="theme-color" content="#ffffff">
<link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="manifest.json">
<link rel="icon" href="favicon.ico"/>
<title>Demo | sig-nal</title>
<link rel="preload" href="./accordion.css" as="style">
<style>
body, div {
display: flex;
flex-direction: column;
align-items: center;
font-family: sans-serif;
}
body {
background-repeat: no-repeat;
background-image: radial-gradient( circle 2000vh at 9% 80.3%, rgba(55,60,245,1) 0%, rgba(234,161,15,0.90) 100.2% );
color: #fff;
}
details {
cursor: pointer;
padding: 1rem 3rem;
}
</style>
<script>
const files = ['index', 'plugins/class-map'];
const isProduction = location.search.includes('prod');
for(let file of files) {
import(`./${isProduction ? 'dist' : 'src'}/${file}${isProduction ? '.min.js' : '.js'}`);
}
</script>
<!-- inlined script src="dist/plugins/sty-le.js" -->
<script>(()=>{let e=new Map;class t extends HTMLElement{async connectedCallback(){let{id:t}=this,s=e.get(t);if(!s){s=new CSSStyleSheet;let i=this.getAttribute("src"),n=new Promise((async e=>{let t=i?await(await fetch(i)).text():this.previousElementSibling.textContent;s.replaceSync(t),e(s)}));e.set(t,n)}s&&this.getRootNode().adoptedStyleSheets.push(await s),this.remove()}}customElements.define("sty-le",t)})();</script>
<!-- inlined script src="dist/plugins/part-ial.js" -->
<script>(()=>{class e extends HTMLElement{#e;get src(){return this.#e||(this.#e=this.getAttribute("src"))}set src(e){this.#e=e,this.connectedCallback()}async connectedCallback(){return this.src&&await(await fetch(this.src,JSON.parse(this.getAttribute("options")||"{}"))).body.pipeThrough(new TextDecoderStream).pipeTo(((e,t)=>{let r=document.implementation.createHTMLDocument();r.write("<article>");let i=r.body.firstElementChild;return e.appendChild(i),new WritableStream({write(e){r.write(e)},close(){r.close(),function e(t){t.querySelectorAll("template[shadowrootmode]").forEach((t=>{let r=t.parentNode.attachShadow({mode:"open"});r.appendChild(t.content),t.remove(),e(r)}))}(i),t&&e[t](i.firstElementChild)},abort(e){r.close(),console.error(`part-ial aborted: ${e}`)}})})(this,this.getAttribute("mode")))}}customElements.define("part-ial",e)})();</script>
</head>
<body>
<div id="counter1">
<template shadowrootmode="open">
<sty-le id="counter-styles" src="./counter.css"></sty-le>
<h1 class="heading heading-1" .classMap="counter">
<sig-nal ref="h1"></sig-nal>
<a href="https://github.com/cloudspeech/sig-nal" target="_blank" !title="counter" .textContent="title"><sig-nal ref="title"></sig-nal></a>
</h1>
<div class="container">
<button @click="counter">
<sig-nal ref="inc">+</sig-nal>
</button>
<span .textContent="counter">
<sig-nal new="counter" type="number">0</sig-nal>
</span>
<button @click="counter">
<sig-nal ref="dec">-</sig-nal>
</button>
</div>
</template>
</div>
<div class="counter2">
<template shadowrootmode="open">
<sty-le id="counter-styles"></sty-le>
<h1 !title="counter">
<sig-nal ref="title">Counters</sig-nal>
</h1>
<div class="container">
<button @click="counter">
<sig-nal ref="inc">+</sig-nal>
</button>
<span .textContent="counter">
<sig-nal new="counter" type="number">0</sig-nal>
</span>
<button @click="counter">
<sig-nal ref="dec">-</sig-nal>
</button>
</div>
</template>
</div>
<div class="clock">
<template shadowrootmode="open">
<style>.weekday:after{content:','}.day:after{content:'.'}.hour:after,.minute:after{content:':';margin-right: -2px}</style>
<h1>Date and Time</h1>
<table><tr>
<td class="weekday" .textContent="weekday"><sig-nal new="weekday" id="weekday">Donnerstag</sig-nal></td>
<td class="day" .textContent="day"><sig-nal new="day" id="day">9</sig-nal></td>
<td class="month" .textContent="month"><sig-nal new="month" id="month">Mai</sig-nal></td>
<td class="year" .textContent="year"><sig-nal new="year" id="year">2024</sig-nal></td>
<td class="hour" .textContent="hour"><sig-nal new="hour" id="hour">15</sig-nal></td>
<td class="minute" .textContent="minute"><sig-nal new="minute" id="minute">05</sig-nal></td>
<td .textContent="second"><sig-nal new="second" id="second">05</sig-nal></td>
<td .textContent="timeZoneName"><sig-nal new="timeZoneName" id="timeZoneName">MESZ</sig-nal></td>
</tr></table>
</template>
</div>
<details><summary>Info</summary>
<p>Demo (c) 2024 by Markus Walther. Witness how both counter/FAQ instances are independent of each other, thanks to shadowRoot scoping!
In Chrome DevTools, enable Rendering → Paint flashing to see fine-grained DOM updates!</p>
</details>
<div class="more-stuff">
<template shadowrootmode="open">
<style>button{box-shadow:1px 2px 3px rgba(0,0,0,.3);padding:.5rem;cursor:pointer;margin-bottom:150rem}acc-ordion{display:block;width:100%}acc-ordion p{color:#464de4}</style>
<button @click="partial">
<sig-nal ref="more">More</sig-nal>
</button>
<part-ial .src="partial"><sig-nal new="partial"></sig-nal></part-ial>
</template>
</div>
<script type="module">
const { hydrate, render, rerender, usable } = await customElements.whenDefined("sig-nal");
await usable(['.classMap']);
hydrate(["div#counter1", "div.counter2"], {
title: {
"!title": rerender,
".textContent": render(document.title),
},
counter: { ".textContent": rerender },
inc: { "@click": ({ signal }) => signal.value++ },
dec: { "@click": ({ signal }) => signal.value-- },
h1: {
".classMap": {
odd: ({ signal }) => signal.when((value) => value % 2),
even: ({
signals: {
counter: { signal },
},
}) => signal.when((value) => !(value % 2)),
},
},
});
let clockSignals = {},
last = [];
const dateParts = () =>
Intl.DateTimeFormat("de-CH", { dateStyle: "full", timeStyle: "long" })
.formatToParts(Date.now())
.filter(
({ type, value }, i) =>
type !== "literal" && value !== last[i] && (last[i] = value),
);
const generate = (parts) => {
const renderSpec = {};
parts.forEach(
({ type, value }) => (renderSpec[type] = { ".textContent": rerender }),
);
return renderSpec;
};
hydrate(["div.clock"], generate(dateParts()), clockSignals);
last = [];
const updateDateParts = () =>
dateParts().forEach(
({ type, value }) => (clockSignals[type].value = value),
);
updateDateParts();
setInterval(updateDateParts, 1000);
hydrate([".more-stuff"], {
more: { "@click": ({domNode, signal}) => ( (domNode.disabled = true) && (signal.value = "./accordion.html") ) },
partial: { ".src": rerender }
});
</script>
</body>
</html>