generated from mirisuzanne/web-component-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
312 lines (258 loc) · 9.33 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Demo of ground-control Web Component" />
<title>Ground-Control Web Component</title>
<script type="module" src="index.js"></script>
<link rel="stylesheet" href="demo.css">
<link rel="stylesheet" href="switch-control.css">
</head>
<body>
<main data-layout="narrow">
<h1>Ground Controls</h1>
<p>
Use the <code><input-control></code>
and <code><toggle-control></code>
web components
to control attributes and properties
from UI input/select or toggle buttons.
Or extend the <code><ground-control></code> base class
to create other control types
that need to broadcast a value
to surrounding elements.
</p>
<ul>
<li>
<a href="https://github.com/mirisuzanne/ground-control">Source on github</a>.
</li>
<li>
<a href="https://www.npmjs.com/package/@terriblemia/ground-control">Package on NPM</a>
</li>
</ul>
<hr>
<h2>Input-Control: Range</h2>
<input-control data-prop="--hue" data-session="hue" data-event="input">
<label for="hue">Hue</label>
<input id="hue" type="range" min="0" max="360" value="200">
<output for="hue"></output>
</input-control>
<details>
<dl>
<dt><code>data-prop: --hue</code></dt>
<dd>Control the <code>--hue</code> CSS custom property</dd>
<dt><code>data-session: hue</code></dt>
<dd>
Save the setting to <code>sessionStorage</code>
as <code>hue</code>
</dd>
<dt><code>data-event: input</code></dt>
<dd>
Listen for the <code>input</code> event,
rather than the default <code>change</code> event
</dd>
</dl>
<p>
Since no <code>data-for</code> is provided,
the property is applied to the <code>:root</code> element.
This example also includes an
<code><output for="hue"></code> element,
which is updated with the active value
of the <code><input id="hue"></code>.
</p>
</details>
<h2>Input-Control: Select</h2>
<input-control data-for="h1, h2, h3" data-prop="font-family" data-local="title-font">
<label for="title-font">Title Font</label>
<select name="title-font" id="title-font">
<option value="var(--serif)">Serif</option>
<option value="var(--sans)" selected>Sans</option>
<option value="var(--slab)">slab</option>
<option value="var(--code)">Monospace</option>
</select>
</input-control>
<details>
<dl>
<dt><code>data-for: h1, h2, h3</code></dt>
<dd>Provide values to three levels of heading elements</dd>
<dt><code>data-prop: font-family</code></dt>
<dd>
Control the <code>font-family</code> CSS property
on those elements
</dd>
<dt><code>data-local: title-font</code></dt>
<dd>
Save the setting to <code>localStorage</code>
as <code>title-font</code>
</dd>
</dl>
</details>
<h2>Toggle-Control: Multi-Button</h2>
<toggle-control id="color-scheme" data-prop="color-scheme" data-local="color-scheme" data-off="light dark">
<strong>Color Scheme</strong>
<button data-value="light dark" aria-pressed="true">auto</button>
<button>light</button>
<button>dark</button>
</toggle-control>
<details>
<dl>
<dt><code>id: color-scheme</code></dt>
<dd>
Provide an <code>id</code> for toggle groups,
in order to reference them
with a reset button or output display
</dd>
<dt><code>data-prop: color-scheme</code></dt>
<dd>Control the <code>color-scheme</code> CSS property</dd>
<dt><code>data-local: color-scheme</code></dt>
<dd>
Save the setting to <code>localStorage</code>
as <code>color-scheme</code>
</dd>
<dt><code>data-off: light dark</code></dt>
<dd>
Provide a value when all toggles are un-pressed.
By using one of the toggle values,
we set that one as the default.
</dd>
</dl>
<p>On the nested <code>button</code>s, we also have…</p>
<dl>
<dt><code>aria-pressed: true</code></dt>
<dd>
Set one of the <code>buttons</code> as default-on
(all toggle controls are exclusive)
</dd>
<dt><code>data-value: light dark</code></dt>
<dd>
Set an explicit value
that is different from the
button text of <code>auto</code>
</dd>
</dl>
</details>
<h2>Toggle-Control: Single Button (switch)</h2>
<toggle-control id="layout" data-for="main" data-attr="data-layout" data-off="narrow">
<strong>Layout</strong>
<button>wide</button>
</toggle-control>
<details>
<dl>
<dt><code>id: layout</code></dt>
<dd>
Provide an <code>id</code> for toggles,
in order to reference them
with a reset button or output display
</dd>
<dt><code>data-for: main</code></dt>
<dd>Control the <code>main</code> element</dd>
<dt><code>data-attr: data-layout</code></dt>
<dd>Set the <code>data-layout</code> attribute</dd>
<dt><code>data-off: narrow</code></dt>
<dd>Set the <code>narrow</code> value when toggled off</dd>
</dl>
</details>
<h2>Switch-Control: Basics</h2>
<p>
A single-button <code>toggle-control</code>
works a bit like a switch,
but the <code>switch-control</code>
component is more specialized to the use-case.
</p>
<switch-control data-attr="data-blackout" data-session="blackout">
<button id="blackout" is-switch>blackout</button>
</switch-control>
<details>
<dl>
<dt><code>id: layout</code></dt>
<dd>
Provide an <code>id</code> for toggles,
in order to reference them
with a reset button or output display
</dd>
<dt><code>data-session: blackout</code></dt>
<dd>
Save the setting to <code>sessionStorage</code>
as <code>blackout</code>
</dd>
<dt><code>data-attr: data-blackout</code></dt>
<dd>Set the <code>data-blackout</code> attribute</dd>
</dl>
</details>
<h2>Switch Control: Extended</h2>
<p>
This CSS toggle switch extends
the switch control base class,
using the <code>onPress()</code> and <code>onUnPress()</code>
methods to turn site styles on and off.
In this case there is no attribute or property
being set on a target element.
</p>
<css-toggle data-session="site-styles">
<button id="site-styles" is-switch aria-pressed="true">site styles</button>
</css-toggle>
<details>
<dt><code>data-session: site-styles</code></dt>
<dd>
Save the setting to <code>sessionStorage</code>
as <code>site-styles</code>
</dd>
<dt><code>aria-pressed: true</code></dt>
<dd>
By default, the site styles are turned on.
</dd>
</details>
<hr>
<h2>Outputs & Resets</h2>
<p>
We can setup <code><output for="id"></code> elements
to display the value of a given control,
or use <code><button data-reset="list of ids"></code>
to reset controls back to their initial value.
</p>
<dl>
<dt>Current Hue</dt>
<dd><output for="hue"></output></dd>
<dt>Current Title Font</dt>
<dd><output for="title-font"></output></dd>
<dt>Current Color Scheme</dt>
<dd><output for="color-scheme"></output></dd>
</dl>
<button data-reset="*">reset all</button>
<h2>ToDo</h2>
<ul>
<li>
Allow <code>id</code> to be set on a solo switch
rather than on the <code><toggle-control></code> wrapper.
</li>
<li>
Support for e.g. dialog/disclosure toggles?
</li>
</ul>
</main>
<script type="module">
import {SwitchControl, GroundControl} from "./index.js";
class CSSToggle extends SwitchControl {
static register(tagName) {
if ('customElements' in window) {
customElements.define(tagName || 'css-toggle', CSSToggle);
}
};
set CSS(enable) {
const embed = document.querySelectorAll('style');
const link = document.querySelectorAll('link[rel=stylesheet]');
const styles = [...embed, ...link];
styles.forEach((sheet) => sheet.disabled = !enable);
};
constructor() {
super();
GroundControl.blockDisplay(this);
};
onPress = () => this.CSS = true;
onUnPress = () => this.CSS = false;
}
CSSToggle.register();
</script>
</body>
</html>