@openelement/ui 1.0.0-alpha.14 → 1.0.0-alpha.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CUSTOMIZATION.md +106 -112
- package/README.md +19 -39
- package/custom-elements.json +369 -0
- package/package.json +3 -2
- package/src/button-machine.js +54 -0
- package/src/component-config.js +7 -0
- package/src/component-recipes.js +2 -24
- package/src/copy-feedback.js +32 -0
- package/src/dialog-machine.js +104 -0
- package/src/dropdown-machine.js +253 -0
- package/src/generated-manifest.json +135 -87
- package/src/index.d.ts +7 -8
- package/src/index.js +7 -8
- package/src/input-machine.js +33 -0
- package/src/open-button.css +119 -108
- package/src/open-button.d.ts +34 -342
- package/src/open-button.generated.js +592 -0
- package/src/open-button.js +115 -1140
- package/src/open-code-block.css +24 -44
- package/src/open-code-block.d.ts +15 -175
- package/src/open-code-block.generated.js +293 -0
- package/src/open-code-block.js +112 -224
- package/src/open-dialog.css +114 -127
- package/src/open-dialog.d.ts +22 -261
- package/src/open-dialog.generated.js +501 -0
- package/src/open-dialog.js +137 -632
- package/src/open-dropdown.css +26 -26
- package/src/open-dropdown.d.ts +15 -218
- package/src/open-dropdown.generated.js +211 -0
- package/src/open-dropdown.js +74 -355
- package/src/open-input.css +69 -74
- package/src/open-input.d.ts +34 -461
- package/src/open-input.generated.js +685 -0
- package/src/open-input.js +161 -1802
- package/src/open-theme-toggle.css +37 -47
- package/src/open-theme-toggle.d.ts +19 -190
- package/src/open-theme-toggle.generated.js +564 -0
- package/src/open-theme-toggle.js +209 -509
- package/src/theme-contract.js +40 -0
- package/src/theme.css +26 -8
- package/src/control-recipe.css +0 -21
- package/src/register.d.ts +0 -4
- package/src/register.js +0 -22
package/src/open-theme-toggle.js
CHANGED
|
@@ -1,525 +1,225 @@
|
|
|
1
1
|
import { readInstanceState, writeInstanceState } from "./instance-state.js";
|
|
2
|
+
import { defineComponentConfig } from "./component-config.js";
|
|
2
3
|
import { log } from "./component-recipes.js";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
4
|
+
import { THEME_STORAGE_KEY, applyDocumentTheme, isSiteTheme, resolveSiteTheme } from "./theme-contract.js";
|
|
5
|
+
import { jsx, jsxs } from "@openelement/element/jsx-runtime";
|
|
6
|
+
import { onConnect, onPropertyChange } from "@openelement/element/lifecycle";
|
|
5
7
|
//#region src/open-theme-toggle.tsx
|
|
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
|
-
"id": "e3",
|
|
52
|
-
"tag": "line",
|
|
53
|
-
"attrs": [
|
|
54
|
-
["x1", "8"],
|
|
55
|
-
["y1", "1"],
|
|
56
|
-
["x2", "8"],
|
|
57
|
-
["y2", "3"]
|
|
58
|
-
],
|
|
59
|
-
"children": []
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
"k": "el",
|
|
63
|
-
"id": "e4",
|
|
64
|
-
"tag": "line",
|
|
65
|
-
"attrs": [
|
|
66
|
-
["x1", "8"],
|
|
67
|
-
["y1", "13"],
|
|
68
|
-
["x2", "8"],
|
|
69
|
-
["y2", "15"]
|
|
70
|
-
],
|
|
71
|
-
"children": []
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
"k": "el",
|
|
75
|
-
"id": "e5",
|
|
76
|
-
"tag": "line",
|
|
77
|
-
"attrs": [
|
|
78
|
-
["x1", "1"],
|
|
79
|
-
["y1", "8"],
|
|
80
|
-
["x2", "3"],
|
|
81
|
-
["y2", "8"]
|
|
82
|
-
],
|
|
83
|
-
"children": []
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
"k": "el",
|
|
87
|
-
"id": "e6",
|
|
88
|
-
"tag": "line",
|
|
89
|
-
"attrs": [
|
|
90
|
-
["x1", "13"],
|
|
91
|
-
["y1", "8"],
|
|
92
|
-
["x2", "15"],
|
|
93
|
-
["y2", "8"]
|
|
94
|
-
],
|
|
95
|
-
"children": []
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
"k": "el",
|
|
99
|
-
"id": "e7",
|
|
100
|
-
"tag": "line",
|
|
101
|
-
"attrs": [
|
|
102
|
-
["x1", "3.05"],
|
|
103
|
-
["y1", "3.05"],
|
|
104
|
-
["x2", "4.46"],
|
|
105
|
-
["y2", "4.46"]
|
|
106
|
-
],
|
|
107
|
-
"children": []
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
"k": "el",
|
|
111
|
-
"id": "e8",
|
|
112
|
-
"tag": "line",
|
|
113
|
-
"attrs": [
|
|
114
|
-
["x1", "11.54"],
|
|
115
|
-
["y1", "11.54"],
|
|
116
|
-
["x2", "12.95"],
|
|
117
|
-
["y2", "12.95"]
|
|
118
|
-
],
|
|
119
|
-
"children": []
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
"k": "el",
|
|
123
|
-
"id": "e9",
|
|
124
|
-
"tag": "line",
|
|
125
|
-
"attrs": [
|
|
126
|
-
["x1", "3.05"],
|
|
127
|
-
["y1", "12.95"],
|
|
128
|
-
["x2", "4.46"],
|
|
129
|
-
["y2", "11.54"]
|
|
130
|
-
],
|
|
131
|
-
"children": []
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
"k": "el",
|
|
135
|
-
"id": "e10",
|
|
136
|
-
"tag": "line",
|
|
137
|
-
"attrs": [
|
|
138
|
-
["x1", "11.54"],
|
|
139
|
-
["y1", "4.46"],
|
|
140
|
-
["x2", "12.95"],
|
|
141
|
-
["y2", "3.05"]
|
|
142
|
-
],
|
|
143
|
-
"children": []
|
|
144
|
-
}
|
|
145
|
-
]
|
|
146
|
-
}, {
|
|
147
|
-
"k": "el",
|
|
148
|
-
"id": "e11",
|
|
149
|
-
"tag": "svg",
|
|
150
|
-
"attrs": [
|
|
151
|
-
["class", "icon-moon"],
|
|
152
|
-
["part", "icon-moon"],
|
|
153
|
-
["viewBox", "0 0 16 16"],
|
|
154
|
-
["fill", "none"],
|
|
155
|
-
["stroke", "currentColor"],
|
|
156
|
-
["stroke-width", "1.2"],
|
|
157
|
-
["stroke-linecap", "round"]
|
|
158
|
-
],
|
|
159
|
-
"children": [{
|
|
160
|
-
"k": "el",
|
|
161
|
-
"id": "e12",
|
|
162
|
-
"tag": "path",
|
|
163
|
-
"attrs": [["d", "M13.5 9.14A5.5 5.5 0 0 1 6.86 2.5 5.5 5.5 0 1 0 13.5 9.14Z"]],
|
|
164
|
-
"children": []
|
|
165
|
-
}]
|
|
166
|
-
}]
|
|
167
|
-
}],
|
|
168
|
-
"parts": [{
|
|
169
|
-
"k": "attr",
|
|
170
|
-
"signal": "theme",
|
|
171
|
-
"name": "data-theme",
|
|
172
|
-
"path": [0],
|
|
173
|
-
"index": 0,
|
|
174
|
-
"location": {
|
|
175
|
-
"id": "p0",
|
|
176
|
-
"kind": "sink",
|
|
177
|
-
"path": [0],
|
|
178
|
-
"node": "e0"
|
|
179
|
-
}
|
|
180
|
-
}, {
|
|
181
|
-
"k": "event",
|
|
182
|
-
"event": "click",
|
|
183
|
-
"handler": "handleToggle",
|
|
184
|
-
"action": {
|
|
185
|
-
"kind": "method",
|
|
186
|
-
"name": "handleToggle"
|
|
187
|
-
},
|
|
188
|
-
"path": [0],
|
|
189
|
-
"index": 1,
|
|
190
|
-
"location": {
|
|
191
|
-
"id": "p1",
|
|
192
|
-
"kind": "sink",
|
|
193
|
-
"path": [0],
|
|
194
|
-
"node": "e0"
|
|
195
|
-
}
|
|
196
|
-
}],
|
|
197
|
-
"regions": [],
|
|
198
|
-
"dependencies": [{
|
|
199
|
-
"signal": "theme",
|
|
200
|
-
"owner": {
|
|
201
|
-
"kind": "part",
|
|
202
|
-
"index": 0
|
|
203
|
-
},
|
|
204
|
-
"location": "p0"
|
|
205
|
-
}],
|
|
206
|
-
"locations": [
|
|
207
|
-
{
|
|
208
|
-
"id": "e0",
|
|
209
|
-
"kind": "element",
|
|
210
|
-
"tag": "button",
|
|
211
|
-
"path": [0]
|
|
212
|
-
},
|
|
213
|
-
{
|
|
214
|
-
"id": "p0",
|
|
215
|
-
"kind": "sink",
|
|
216
|
-
"part": 0,
|
|
217
|
-
"node": "e0",
|
|
218
|
-
"path": [0]
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
"id": "p1",
|
|
222
|
-
"kind": "sink",
|
|
223
|
-
"part": 1,
|
|
224
|
-
"node": "e0",
|
|
225
|
-
"path": [0]
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
"id": "e1",
|
|
229
|
-
"kind": "element",
|
|
230
|
-
"tag": "svg",
|
|
231
|
-
"path": [0, 0]
|
|
232
|
-
},
|
|
233
|
-
{
|
|
234
|
-
"id": "e2",
|
|
235
|
-
"kind": "element",
|
|
236
|
-
"tag": "circle",
|
|
237
|
-
"path": [
|
|
238
|
-
0,
|
|
239
|
-
0,
|
|
240
|
-
0
|
|
241
|
-
]
|
|
242
|
-
},
|
|
243
|
-
{
|
|
244
|
-
"id": "e3",
|
|
245
|
-
"kind": "element",
|
|
246
|
-
"tag": "line",
|
|
247
|
-
"path": [
|
|
248
|
-
0,
|
|
249
|
-
0,
|
|
250
|
-
1
|
|
251
|
-
]
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
"id": "e4",
|
|
255
|
-
"kind": "element",
|
|
256
|
-
"tag": "line",
|
|
257
|
-
"path": [
|
|
258
|
-
0,
|
|
259
|
-
0,
|
|
260
|
-
2
|
|
261
|
-
]
|
|
262
|
-
},
|
|
263
|
-
{
|
|
264
|
-
"id": "e5",
|
|
265
|
-
"kind": "element",
|
|
266
|
-
"tag": "line",
|
|
267
|
-
"path": [
|
|
268
|
-
0,
|
|
269
|
-
0,
|
|
270
|
-
3
|
|
271
|
-
]
|
|
272
|
-
},
|
|
273
|
-
{
|
|
274
|
-
"id": "e6",
|
|
275
|
-
"kind": "element",
|
|
276
|
-
"tag": "line",
|
|
277
|
-
"path": [
|
|
278
|
-
0,
|
|
279
|
-
0,
|
|
280
|
-
4
|
|
281
|
-
]
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
"id": "e7",
|
|
285
|
-
"kind": "element",
|
|
286
|
-
"tag": "line",
|
|
287
|
-
"path": [
|
|
288
|
-
0,
|
|
289
|
-
0,
|
|
290
|
-
5
|
|
291
|
-
]
|
|
292
|
-
},
|
|
293
|
-
{
|
|
294
|
-
"id": "e8",
|
|
295
|
-
"kind": "element",
|
|
296
|
-
"tag": "line",
|
|
297
|
-
"path": [
|
|
298
|
-
0,
|
|
299
|
-
0,
|
|
300
|
-
6
|
|
301
|
-
]
|
|
302
|
-
},
|
|
303
|
-
{
|
|
304
|
-
"id": "e9",
|
|
305
|
-
"kind": "element",
|
|
306
|
-
"tag": "line",
|
|
307
|
-
"path": [
|
|
308
|
-
0,
|
|
309
|
-
0,
|
|
310
|
-
7
|
|
311
|
-
]
|
|
312
|
-
},
|
|
313
|
-
{
|
|
314
|
-
"id": "e10",
|
|
315
|
-
"kind": "element",
|
|
316
|
-
"tag": "line",
|
|
317
|
-
"path": [
|
|
318
|
-
0,
|
|
319
|
-
0,
|
|
320
|
-
8
|
|
321
|
-
]
|
|
322
|
-
},
|
|
323
|
-
{
|
|
324
|
-
"id": "e11",
|
|
325
|
-
"kind": "element",
|
|
326
|
-
"tag": "svg",
|
|
327
|
-
"path": [0, 1]
|
|
328
|
-
},
|
|
329
|
-
{
|
|
330
|
-
"id": "e12",
|
|
331
|
-
"kind": "element",
|
|
332
|
-
"tag": "path",
|
|
333
|
-
"path": [
|
|
334
|
-
0,
|
|
335
|
-
1,
|
|
336
|
-
0
|
|
337
|
-
]
|
|
338
|
-
}
|
|
339
|
-
],
|
|
340
|
-
"metadata": {
|
|
341
|
-
"tag": "open-theme-toggle",
|
|
342
|
-
"className": "OpenThemeToggle",
|
|
343
|
-
"sourceFile": "open-theme-toggle.tsx",
|
|
344
|
-
"properties": [{
|
|
345
|
-
"name": "theme",
|
|
346
|
-
"attribute": "theme",
|
|
347
|
-
"type": "string",
|
|
348
|
-
"converter": "string",
|
|
349
|
-
"reflect": false,
|
|
350
|
-
"default": "dark"
|
|
351
|
-
}],
|
|
352
|
-
"observedAttributes": ["theme"],
|
|
353
|
-
"cem": {
|
|
354
|
-
"tagName": "open-theme-toggle",
|
|
355
|
-
"className": "OpenThemeToggle",
|
|
356
|
-
"declaration": {
|
|
357
|
-
"name": "OpenThemeToggle",
|
|
358
|
-
"module": "open-theme-toggle.tsx"
|
|
359
|
-
},
|
|
360
|
-
"attributes": [{
|
|
361
|
-
"name": "theme",
|
|
362
|
-
"fieldName": "theme",
|
|
363
|
-
"type": "string",
|
|
364
|
-
"reflect": false
|
|
365
|
-
}],
|
|
366
|
-
"members": [{
|
|
367
|
-
"name": "theme",
|
|
368
|
-
"fieldName": "theme",
|
|
369
|
-
"type": "string",
|
|
370
|
-
"attribute": "theme",
|
|
371
|
-
"reflect": false
|
|
372
|
-
}]
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
const __compiledProperties = [{
|
|
377
|
-
"name": "theme",
|
|
378
|
-
"attribute": "theme",
|
|
379
|
-
"type": "string",
|
|
380
|
-
"converter": "string",
|
|
381
|
-
"reflect": false,
|
|
382
|
-
"default": "dark"
|
|
383
|
-
}];
|
|
384
|
-
const __elementMetadata = {
|
|
385
|
-
"tag": "open-theme-toggle",
|
|
386
|
-
"className": "OpenThemeToggle",
|
|
387
|
-
"sourceFile": "open-theme-toggle.tsx",
|
|
388
|
-
"properties": [{
|
|
389
|
-
"name": "theme",
|
|
390
|
-
"attribute": "theme",
|
|
391
|
-
"type": "string",
|
|
392
|
-
"converter": "string",
|
|
393
|
-
"reflect": false,
|
|
394
|
-
"default": "dark"
|
|
395
|
-
}],
|
|
396
|
-
"observedAttributes": ["theme"],
|
|
397
|
-
"cem": {
|
|
398
|
-
"tagName": "open-theme-toggle",
|
|
399
|
-
"className": "OpenThemeToggle",
|
|
400
|
-
"declaration": {
|
|
401
|
-
"name": "OpenThemeToggle",
|
|
402
|
-
"module": "open-theme-toggle.tsx"
|
|
403
|
-
},
|
|
404
|
-
"attributes": [{
|
|
405
|
-
"name": "theme",
|
|
406
|
-
"fieldName": "theme",
|
|
407
|
-
"type": "string",
|
|
408
|
-
"reflect": false
|
|
409
|
-
}],
|
|
410
|
-
"members": [{
|
|
411
|
-
"name": "theme",
|
|
412
|
-
"fieldName": "theme",
|
|
413
|
-
"type": "string",
|
|
414
|
-
"attribute": "theme",
|
|
415
|
-
"reflect": false
|
|
416
|
-
}]
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
const __observedAttributes = ["theme"];
|
|
420
|
-
const __compiledProps = { theme: {
|
|
421
|
-
type: String,
|
|
422
|
-
default: "dark",
|
|
423
|
-
reflect: false,
|
|
424
|
-
attribute: "theme"
|
|
425
|
-
} };
|
|
426
|
-
var OpenThemeToggle = class extends OpenElement {
|
|
427
|
-
static __partProgram = __partProgram;
|
|
428
|
-
static __compiledProperties = __compiledProperties;
|
|
429
|
-
static __elementMetadata = __elementMetadata;
|
|
430
|
-
static props = __compiledProps;
|
|
431
|
-
static observedAttributes = __observedAttributes;
|
|
432
|
-
static delegatesFocus = true;
|
|
433
|
-
static styles = [openThemeToggleStyles];
|
|
434
|
-
theme = "dark";
|
|
435
|
-
connectedCallback() {
|
|
8
|
+
/**
|
|
9
|
+
* @openelement/ui - open-theme-toggle
|
|
10
|
+
*
|
|
11
|
+
* Theme toggle button for dark/light mode: reads and flips the document's
|
|
12
|
+
* `data-theme` (with an initialization priority chain) and persists the
|
|
13
|
+
* choice; icon visibility follows the current theme attribute.
|
|
14
|
+
*
|
|
15
|
+
* Experimental (owner ruling C1, #1468): internal implementation may change
|
|
16
|
+
* or be removed before 1.0; the declared `::part` names follow the
|
|
17
|
+
* CUSTOMIZATION.md contract (`::part` names never shrink).
|
|
18
|
+
*
|
|
19
|
+
* Generated authoring. The `theme` property drives the compiled `data-theme`
|
|
20
|
+
* attribute sink on the toggle button; CSS selectors ([data-theme='light'])
|
|
21
|
+
* own icon visibility. The initialization priority chain and persistence stay
|
|
22
|
+
* imperative; the one-time init guard lives in the shared instance-state
|
|
23
|
+
* module, and the document write is delegated to the theme contract's single
|
|
24
|
+
* owner.
|
|
25
|
+
*
|
|
26
|
+
* @csspart toggle -The button element
|
|
27
|
+
* @csspart icon-sun -The sun SVG icon
|
|
28
|
+
* @csspart icon-moon -The moon SVG icon
|
|
29
|
+
*
|
|
30
|
+
* Usage:
|
|
31
|
+
* ```html
|
|
32
|
+
* <open-theme-toggle theme="light"></open-theme-toggle>
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Platform facts for the generated element: shadow root with delegated focus,
|
|
37
|
+
* the authored component stylesheet, and the declared `theme` property.
|
|
38
|
+
*/
|
|
39
|
+
const openElement = /* @__PURE__ */ defineComponentConfig({
|
|
40
|
+
root: "shadow",
|
|
41
|
+
delegatesFocus: true,
|
|
42
|
+
styles: ["./open-theme-toggle.css"],
|
|
43
|
+
properties: { theme: { default: "dark" } }
|
|
44
|
+
});
|
|
45
|
+
/**
|
|
46
|
+
* Theme toggle button for dark/light mode: reads and flips the document's
|
|
47
|
+
* `data-theme` (with an initialization priority chain) and persists the
|
|
48
|
+
* choice; icon visibility follows the current theme attribute.
|
|
49
|
+
*/
|
|
50
|
+
function OpenThemeToggle(props) {
|
|
51
|
+
/** Saved preference; a blocked storage read means "no preference". */
|
|
52
|
+
const readSavedTheme = () => {
|
|
436
53
|
try {
|
|
437
|
-
|
|
438
|
-
this._requestAnimationFrame(() => this.initTheme());
|
|
54
|
+
return globalThis.localStorage.getItem(THEME_STORAGE_KEY);
|
|
439
55
|
} catch (error) {
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
onDsdHydrated() {
|
|
444
|
-
this._requestAnimationFrame(() => this.initTheme());
|
|
445
|
-
}
|
|
446
|
-
initTheme() {
|
|
447
|
-
if (readInstanceState(this, "initDone", () => false)) return;
|
|
448
|
-
writeInstanceState(this, "initDone", true);
|
|
449
|
-
const themeAttr = this.getAttribute("theme");
|
|
450
|
-
if (themeAttr === "light") this.theme = "light";
|
|
451
|
-
else if (themeAttr === "dark") this.theme = "dark";
|
|
452
|
-
else {
|
|
453
|
-
const docTheme = document.documentElement?.dataset?.theme;
|
|
454
|
-
if (docTheme === "light") this.theme = "light";
|
|
455
|
-
else if (docTheme === "dark") this.theme = "dark";
|
|
456
|
-
else {
|
|
457
|
-
let resolved = false;
|
|
458
|
-
try {
|
|
459
|
-
const saved = localStorage.getItem("open-theme");
|
|
460
|
-
if (saved === "light") {
|
|
461
|
-
this.theme = "light";
|
|
462
|
-
resolved = true;
|
|
463
|
-
} else if (saved === "dark") {
|
|
464
|
-
this.theme = "dark";
|
|
465
|
-
resolved = true;
|
|
466
|
-
}
|
|
467
|
-
} catch (e) {
|
|
468
|
-
log.debug("localStorage read unavailable:", e);
|
|
469
|
-
}
|
|
470
|
-
if (!resolved && globalThis.matchMedia) this.theme = globalThis.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
|
|
471
|
-
}
|
|
56
|
+
log.debug("localStorage read unavailable:", error);
|
|
57
|
+
return null;
|
|
472
58
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const changed = readInstanceState(this, "lastPropagated", () => void 0) !== theme;
|
|
477
|
-
this.theme = theme;
|
|
478
|
-
this.setAttribute("data-theme", theme);
|
|
479
|
-
document.documentElement.setAttribute("data-theme", theme);
|
|
480
|
-
if (document.documentElement.style) document.documentElement.style.colorScheme = theme;
|
|
59
|
+
};
|
|
60
|
+
/** System light preference; missing or throwing matchMedia means no light preference. */
|
|
61
|
+
const prefersLight = () => {
|
|
481
62
|
try {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
} catch (
|
|
485
|
-
log.debug("
|
|
486
|
-
|
|
487
|
-
if (changed) {
|
|
488
|
-
writeInstanceState(this, "lastPropagated", theme);
|
|
489
|
-
this.dispatchThemeChange(theme);
|
|
63
|
+
if (typeof globalThis.matchMedia !== "function") return false;
|
|
64
|
+
return globalThis.matchMedia("(prefers-color-scheme: light)").matches === true;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
log.debug("matchMedia unavailable:", error);
|
|
67
|
+
return false;
|
|
490
68
|
}
|
|
491
|
-
}
|
|
492
|
-
|
|
69
|
+
};
|
|
70
|
+
const dispatchThemeChange = (theme) => {
|
|
493
71
|
try {
|
|
494
|
-
|
|
495
|
-
} catch (
|
|
496
|
-
log.debug("theme
|
|
72
|
+
if (typeof globalThis.CustomEvent !== "undefined") globalThis.dispatchEvent(new globalThis.CustomEvent("open:theme-change", { detail: { theme } }));
|
|
73
|
+
} catch (error) {
|
|
74
|
+
log.debug("theme event dispatch unavailable:", error);
|
|
497
75
|
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Apply one resolved theme. The `theme` attribute is the declarative carrier
|
|
79
|
+
* (its property drives the button sink), the `data-theme` attribute is the
|
|
80
|
+
* host presentation sink, the document write is delegated to the contract's
|
|
81
|
+
* single owner, and the event fires only on a real transition.
|
|
82
|
+
*/
|
|
83
|
+
const applyTheme = (host, theme) => {
|
|
84
|
+
if (readInstanceState(host, "lastPropagated", () => void 0) === theme) return;
|
|
85
|
+
writeInstanceState(host, "lastPropagated", theme);
|
|
86
|
+
host.setAttribute("theme", theme);
|
|
87
|
+
host.setAttribute("data-theme", theme);
|
|
88
|
+
applyDocumentTheme(host.ownerDocument.documentElement, theme);
|
|
505
89
|
try {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
90
|
+
const root = host.getRootNode();
|
|
91
|
+
if (root instanceof globalThis.ShadowRoot && root.host) root.host.setAttribute("data-theme", theme);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
log.debug("root theme propagation unavailable:", error);
|
|
94
|
+
}
|
|
95
|
+
dispatchThemeChange(theme);
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Resolve the theme once per instance: explicit `theme` attribute > the
|
|
99
|
+
* document's live `data-theme` (the owner's value, so multiple instances
|
|
100
|
+
* converge) > the contract's acceptance rule over the saved preference and
|
|
101
|
+
* the system light preference.
|
|
102
|
+
*/
|
|
103
|
+
const initTheme = (host) => {
|
|
104
|
+
if (readInstanceState(host, "initDone", () => false)) return;
|
|
105
|
+
writeInstanceState(host, "initDone", true);
|
|
106
|
+
const themeAttr = host.getAttribute("theme");
|
|
107
|
+
if (isSiteTheme(themeAttr)) {
|
|
108
|
+
applyTheme(host, themeAttr);
|
|
109
|
+
return;
|
|
509
110
|
}
|
|
510
|
-
|
|
511
|
-
|
|
111
|
+
const docTheme = host.ownerDocument.documentElement?.dataset?.theme;
|
|
112
|
+
if (isSiteTheme(docTheme)) {
|
|
113
|
+
applyTheme(host, docTheme);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
applyTheme(host, resolveSiteTheme(readSavedTheme(), prefersLight()));
|
|
117
|
+
};
|
|
118
|
+
/** Only an explicit user toggle persists the choice (#804). */
|
|
119
|
+
const toggle = (host) => {
|
|
120
|
+
const theme = host.getAttribute("theme") === "light" ? "dark" : "light";
|
|
121
|
+
host.setAttribute("theme", theme);
|
|
512
122
|
try {
|
|
513
|
-
|
|
514
|
-
if (old === val) return;
|
|
515
|
-
if (name === "theme" && val) this.applyTheme(val === "light" ? "light" : "dark");
|
|
123
|
+
globalThis.localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
516
124
|
} catch (error) {
|
|
517
|
-
|
|
125
|
+
log.debug("theme persistence unavailable:", error);
|
|
518
126
|
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
}
|
|
523
|
-
|
|
127
|
+
};
|
|
128
|
+
onConnect((env) => {
|
|
129
|
+
globalThis.requestAnimationFrame(() => initTheme(env.host));
|
|
130
|
+
});
|
|
131
|
+
onPropertyChange((event) => {
|
|
132
|
+
if (event.name !== "theme") return;
|
|
133
|
+
applyTheme(event.host, event.value === "light" ? "light" : "dark");
|
|
134
|
+
});
|
|
135
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
136
|
+
type: "button",
|
|
137
|
+
class: "theme-toggle inline-flex size-[38px] cursor-pointer items-center justify-center rounded-full p-0 text-muted-foreground transition-all duration-150 ease-in-out hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-[3px] focus-visible:outline-ring",
|
|
138
|
+
part: "toggle",
|
|
139
|
+
"data-theme": props.theme,
|
|
140
|
+
"aria-label": "Toggle theme",
|
|
141
|
+
onClick: (event) => {
|
|
142
|
+
const target = event.currentTarget;
|
|
143
|
+
toggle(target.getRootNode().host);
|
|
144
|
+
},
|
|
145
|
+
children: [/* @__PURE__ */ jsxs("svg", {
|
|
146
|
+
class: "icon-sun size-4",
|
|
147
|
+
part: "icon-sun",
|
|
148
|
+
viewBox: "0 0 16 16",
|
|
149
|
+
fill: "none",
|
|
150
|
+
stroke: "currentColor",
|
|
151
|
+
"stroke-width": "1.2",
|
|
152
|
+
"stroke-linecap": "round",
|
|
153
|
+
children: [
|
|
154
|
+
/* @__PURE__ */ jsx("circle", {
|
|
155
|
+
cx: "8",
|
|
156
|
+
cy: "8",
|
|
157
|
+
r: "3"
|
|
158
|
+
}),
|
|
159
|
+
/* @__PURE__ */ jsx("line", {
|
|
160
|
+
x1: "8",
|
|
161
|
+
y1: "1",
|
|
162
|
+
x2: "8",
|
|
163
|
+
y2: "3"
|
|
164
|
+
}),
|
|
165
|
+
/* @__PURE__ */ jsx("line", {
|
|
166
|
+
x1: "8",
|
|
167
|
+
y1: "13",
|
|
168
|
+
x2: "8",
|
|
169
|
+
y2: "15"
|
|
170
|
+
}),
|
|
171
|
+
/* @__PURE__ */ jsx("line", {
|
|
172
|
+
x1: "1",
|
|
173
|
+
y1: "8",
|
|
174
|
+
x2: "3",
|
|
175
|
+
y2: "8"
|
|
176
|
+
}),
|
|
177
|
+
/* @__PURE__ */ jsx("line", {
|
|
178
|
+
x1: "13",
|
|
179
|
+
y1: "8",
|
|
180
|
+
x2: "15",
|
|
181
|
+
y2: "8"
|
|
182
|
+
}),
|
|
183
|
+
/* @__PURE__ */ jsx("line", {
|
|
184
|
+
x1: "3.05",
|
|
185
|
+
y1: "3.05",
|
|
186
|
+
x2: "4.46",
|
|
187
|
+
y2: "4.46"
|
|
188
|
+
}),
|
|
189
|
+
/* @__PURE__ */ jsx("line", {
|
|
190
|
+
x1: "11.54",
|
|
191
|
+
y1: "11.54",
|
|
192
|
+
x2: "12.95",
|
|
193
|
+
y2: "12.95"
|
|
194
|
+
}),
|
|
195
|
+
/* @__PURE__ */ jsx("line", {
|
|
196
|
+
x1: "3.05",
|
|
197
|
+
y1: "12.95",
|
|
198
|
+
x2: "4.46",
|
|
199
|
+
y2: "11.54"
|
|
200
|
+
}),
|
|
201
|
+
/* @__PURE__ */ jsx("line", {
|
|
202
|
+
x1: "11.54",
|
|
203
|
+
y1: "4.46",
|
|
204
|
+
x2: "12.95",
|
|
205
|
+
y2: "3.05"
|
|
206
|
+
})
|
|
207
|
+
]
|
|
208
|
+
}), /* @__PURE__ */ jsx("svg", {
|
|
209
|
+
class: "icon-moon size-4",
|
|
210
|
+
part: "icon-moon",
|
|
211
|
+
viewBox: "0 0 16 16",
|
|
212
|
+
fill: "none",
|
|
213
|
+
stroke: "currentColor",
|
|
214
|
+
"stroke-width": "1.2",
|
|
215
|
+
"stroke-linecap": "round",
|
|
216
|
+
children: /* @__PURE__ */ jsx("path", { d: "M13.5 9.14A5.5 5.5 0 0 1 6.86 2.5 5.5 5.5 0 1 0 13.5 9.14Z" })
|
|
217
|
+
})]
|
|
218
|
+
});
|
|
219
|
+
}
|
|
524
220
|
//#endregion
|
|
525
|
-
export { OpenThemeToggle
|
|
221
|
+
export { OpenThemeToggle as default, openElement };
|
|
222
|
+
|
|
223
|
+
export { OpenThemeToggleElement, OpenThemeToggle__ssrHost } from './open-theme-toggle.generated.js';
|
|
224
|
+
|
|
225
|
+
// openElement:class-candidates ["cursor-pointer","duration-150","ease-in-out","focus-visible:outline-2","focus-visible:outline-offset-[3px]","focus-visible:outline-ring","hover:text-foreground","icon-moon","icon-sun","inline-flex","items-center","justify-center","p-0","rounded-full","size-4","size-[38px]","text-muted-foreground","theme-toggle","transition-all"]
|