vireuikit 0.4.0 → 0.6.0

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/dist/web.js ADDED
@@ -0,0 +1,567 @@
1
+ import { CONTROL_IDLE, A11Y_NONE, pressResponse, POINTER_INPUT, createControlVisual, TOUCH_INPUT, CONTROL_SIZES, defaultSize, stepControl, minTarget, hitSlop, settleControlVisual, presentationFor, sameA11y, iconButtonSize, BUTTON_SIZES, visualAtomState, atomMaterial, stepControlVisual, atomBody, resolveA11y } from './chunk-I3BD3XGC.js';
2
+ import { VIREGLASS_CONTROL_MATERIAL } from 'vireglass';
3
+
4
+ // src/web/a11y.ts
5
+ var QUERIES = {
6
+ reduceMotion: "(prefers-reduced-motion: reduce)",
7
+ increaseContrast: "(prefers-contrast: more)",
8
+ reduceTransparency: "(prefers-reduced-transparency: reduce)"
9
+ };
10
+ var KEYS = Object.keys(QUERIES);
11
+ function systemA11y() {
12
+ if (typeof matchMedia === "undefined") return A11Y_NONE;
13
+ return {
14
+ reduceMotion: matchMedia(QUERIES.reduceMotion).matches,
15
+ increaseContrast: matchMedia(QUERIES.increaseContrast).matches,
16
+ reduceTransparency: matchMedia(QUERIES.reduceTransparency).matches
17
+ };
18
+ }
19
+ function watchSystemA11y(onChange) {
20
+ if (typeof matchMedia === "undefined") return () => {
21
+ };
22
+ const lists = KEYS.map((key) => matchMedia(QUERIES[key]));
23
+ for (const list of lists) list.addEventListener("change", onChange);
24
+ return () => {
25
+ for (const list of lists) list.removeEventListener("change", onChange);
26
+ };
27
+ }
28
+ var globalOverride = {};
29
+ var listeners = /* @__PURE__ */ new Set();
30
+ function setA11yOverride(next) {
31
+ globalOverride = { ...globalOverride, ...next };
32
+ for (const listener of listeners) listener();
33
+ }
34
+ function getA11yOverride() {
35
+ return globalOverride;
36
+ }
37
+ function watchA11yOverride(onChange) {
38
+ listeners.add(onChange);
39
+ return () => {
40
+ listeners.delete(onChange);
41
+ };
42
+ }
43
+ function effectiveA11y(own) {
44
+ const merged = { ...globalOverride };
45
+ for (const key of KEYS) if (own[key] !== void 0) merged[key] = own[key];
46
+ return resolveA11y(systemA11y(), merged);
47
+ }
48
+
49
+ // src/web/glass.ts
50
+ async function attachControlGlass(el, params, stillWanted) {
51
+ const { attachGlass } = await import('vireglass/dom');
52
+ if (!stillWanted()) return null;
53
+ const opts = {
54
+ material: params.material,
55
+ geometry: params.geometry,
56
+ accessibility: params.accessibility,
57
+ variant: params.onTouch ? "interactive" : "present",
58
+ // The kit paints its own body: the core's is not lighter than its surround over a light backdrop and
59
+ // near black under reduced transparency with light ink (docs/acceptance.md, "Resting body").
60
+ body: false
61
+ };
62
+ if (params.sample) opts.sample = params.sample;
63
+ const handle = attachGlass(el, opts);
64
+ return {
65
+ setMaterial(material) {
66
+ opts.material = material;
67
+ handle.update();
68
+ },
69
+ setGeometry(geometry) {
70
+ opts.geometry = geometry;
71
+ handle.update();
72
+ },
73
+ setAppear: (t) => handle.setAppear(t),
74
+ setMorph: (morph) => handle.setMorph(morph),
75
+ setSample: (sample) => handle.setSample(sample),
76
+ destroy: () => handle.destroy()
77
+ };
78
+ }
79
+
80
+ // src/web/input.ts
81
+ var COARSE = "(pointer: coarse)";
82
+ function isCoarsePointer() {
83
+ return typeof matchMedia !== "undefined" && matchMedia(COARSE).matches;
84
+ }
85
+ function watchCoarsePointer(onChange) {
86
+ if (typeof matchMedia === "undefined") return () => {
87
+ };
88
+ const list = matchMedia(COARSE);
89
+ list.addEventListener("change", onChange);
90
+ return () => list.removeEventListener("change", onChange);
91
+ }
92
+
93
+ // src/web/styles.ts
94
+ var HOST = ":is(vire-button,vire-icon-button)[data-vk]";
95
+ var BODY = `${HOST}>button`;
96
+ var STYLES = `
97
+ vire-button,vire-icon-button{display:inline-flex;vertical-align:middle}
98
+ ${HOST}{position:relative;--vk-press:0;--vk-hover:0;--vk-body-alpha:0;--vk-body-white:0;--vk-body-lift:0;--vk-grow:0;--vk-defocus:0;--vk-slop-x:0;--vk-slop-y:0}
99
+ ${HOST}::before{content:'';position:absolute;inset:calc(var(--vk-slop-y)*-1px) calc(var(--vk-slop-x)*-1px)}
100
+ ${BODY}{appearance:none;-webkit-appearance:none;box-sizing:border-box;margin:0;border:0;outline:none;
101
+ font:inherit;font-weight:500;line-height:1;white-space:nowrap;color:inherit;
102
+ display:inline-flex;align-items:center;justify-content:center;gap:8px;position:relative;
103
+ height:var(--vk-height);min-width:var(--vk-height);padding:0 var(--vk-pad-x);border-radius:var(--vk-radius);
104
+ -webkit-text-fill-color:color-mix(in srgb,currentColor calc((1 - var(--vk-press)*.55)*100%),transparent);
105
+ text-shadow:0 0 calc(var(--vk-press)*var(--vk-defocus)*1px) currentColor;
106
+ background:transparent;cursor:pointer;touch-action:manipulation;user-select:none;-webkit-tap-highlight-color:transparent;
107
+ transform:scale(calc(1 + var(--vk-press)*var(--vk-grow)))}
108
+ vire-icon-button[data-vk]>button{padding:0;width:var(--vk-height)}
109
+ ${BODY}>svg{width:var(--vk-icon);height:var(--vk-icon);flex:none;
110
+ opacity:calc(1 - var(--vk-press)*.45);filter:blur(calc(var(--vk-press)*var(--vk-defocus)*1px))}
111
+ ${BODY}:disabled{cursor:default}
112
+ ${HOST}[data-disabled]>button{-webkit-text-fill-color:color-mix(in srgb,currentColor 40%,transparent)}
113
+ ${HOST}[data-disabled]>button>svg{opacity:.4}
114
+ ${HOST}[data-focus]>button{outline:2px solid currentColor;outline-offset:2px}
115
+
116
+ ${HOST}[data-surface=glass]>button{color:var(--vireglass-ink-color,inherit);
117
+ background:color-mix(in srgb,color-mix(in srgb,#fff calc((var(--vk-body-white) + var(--vk-body-lift)*2)*100%),var(--vireglass-tint-color,#fff)) calc(var(--vk-body-alpha)*100%),transparent);
118
+ background:oklab(from color-mix(in srgb,#fff calc(var(--vk-body-white)*100%),var(--vireglass-tint-color,#fff)) calc(l + var(--vk-body-lift)) a b / var(--vk-body-alpha))}
119
+ ${HOST}[data-frost][data-surface=glass]>button{color:oklab(from var(--vireglass-tint-color,#fff) calc(.95 - .87*clamp(0,(l + var(--vk-body-lift) - .6)*20,1)) 0 0)}
120
+
121
+ ${HOST}[data-surface=plain]>button{background:color-mix(in srgb,currentColor calc(10% + var(--vk-hover)*6% + var(--vk-press)*14%),transparent)}
122
+
123
+ ${HOST}[data-surface=frosted]>button{
124
+ background:color-mix(in srgb,Canvas calc(88% - var(--vk-hover)*4% - var(--vk-press)*10%),currentColor);
125
+ backdrop-filter:blur(20px);-webkit-backdrop-filter:blur(20px);
126
+ box-shadow:0 1px 3px color-mix(in srgb,currentColor 22%,transparent)}
127
+
128
+ @supports not (color:oklab(from red l a b)){
129
+ ${HOST}[data-frost][data-surface=glass]>button{color:inherit;background:color-mix(in srgb,Canvas 82%,currentColor)}
130
+ }
131
+
132
+ ${HOST}[data-contrast]:is([data-surface=plain],[data-surface=frosted])>button{box-shadow:inset 0 0 0 2px currentColor}
133
+ ${HOST}[data-contrast][data-surface=frosted]>button{background:Canvas}
134
+ ${HOST}[data-contrast][data-surface=glass]>button{background:color-mix(in srgb,color-mix(in srgb,#fff calc((1 - var(--vireglass-ink,0))*100%),#000) 88%,transparent)}
135
+
136
+ @media (forced-colors:active){
137
+ ${BODY}{-webkit-text-fill-color:currentColor;text-shadow:none;color:ButtonText;background:ButtonFace;border:1px solid ButtonText;backdrop-filter:none;-webkit-backdrop-filter:none;transform:none}
138
+ ${BODY}>svg{opacity:1;filter:none}
139
+ ${HOST}[data-disabled]>button{-webkit-text-fill-color:currentColor;color:GrayText;border-color:GrayText}
140
+ ${HOST}[data-focus]>button{outline-color:Highlight}
141
+ }
142
+ `;
143
+ var STYLE_ATTR = "data-vireuikit";
144
+ function ensureStyles(from, nonce) {
145
+ const root = from.getRootNode();
146
+ const host = root instanceof ShadowRoot ? root : from.ownerDocument;
147
+ if (host.querySelector(`style[${STYLE_ATTR}]`)) return;
148
+ const style = from.ownerDocument.createElement("style");
149
+ style.setAttribute(STYLE_ATTR, "");
150
+ if (nonce) style.nonce = nonce;
151
+ style.textContent = STYLES;
152
+ (host instanceof ShadowRoot ? host : host.head).appendChild(style);
153
+ }
154
+
155
+ // src/web/element.ts
156
+ var FORCE_ATTRIBUTE = "data-vire-force-state";
157
+ var FORCED = {
158
+ rest: CONTROL_IDLE,
159
+ hover: { ...CONTROL_IDLE, hovered: true },
160
+ focus: { ...CONTROL_IDLE, focused: true },
161
+ pressed: { ...CONTROL_IDLE, press: { by: "pointer" } },
162
+ disabled: { ...CONTROL_IDLE, disabled: true }
163
+ };
164
+ var MATERIAL_STEPS = 10;
165
+ var SUPPRESS_MS = 100;
166
+ function createControlElementClass(kind, options = {}) {
167
+ const response = pressResponse(kind);
168
+ return class VireControl extends HTMLElement {
169
+ static observedAttributes = ["glass", "size", "disabled", FORCE_ATTRIBUTE];
170
+ #control = null;
171
+ #state = CONTROL_IDLE;
172
+ #caps = POINTER_INPUT;
173
+ #visual = createControlVisual();
174
+ #own = {};
175
+ #mode = null;
176
+ #presentation = null;
177
+ #glass = null;
178
+ #glassToken = 0;
179
+ #glassKey = "";
180
+ #glassBroken = false;
181
+ #lastMaterialKey = "";
182
+ #frame = 0;
183
+ #lastFrame = 0;
184
+ #cleanups = [];
185
+ #suppressClick = false;
186
+ #suppressTimer = 0;
187
+ #slop = { x: 0, y: 0 };
188
+ #morph = null;
189
+ #sample = null;
190
+ get a11y() {
191
+ return this.#own;
192
+ }
193
+ set a11y(next) {
194
+ this.#own = { ...next };
195
+ if (this.#control) this.#refreshMode();
196
+ }
197
+ get sample() {
198
+ return this.#sample;
199
+ }
200
+ set sample(next) {
201
+ this.#sample = next;
202
+ this.#glass?.setSample(next);
203
+ }
204
+ setMorph(morph) {
205
+ this.#morph = morph;
206
+ this.#glass?.setMorph(morph);
207
+ }
208
+ connectedCallback() {
209
+ for (const key of ["a11y", "sample"]) {
210
+ if (!Object.prototype.hasOwnProperty.call(this, key)) continue;
211
+ const value = Reflect.get(this, key);
212
+ Reflect.deleteProperty(this, key);
213
+ Reflect.set(this, key, value);
214
+ }
215
+ ensureStyles(this, options.nonce);
216
+ this.#cleanups.push(watchCoarsePointer(() => this.#onInputChange()));
217
+ this.#cleanups.push(watchSystemA11y(() => this.#refreshMode()));
218
+ this.#cleanups.push(watchA11yOverride(() => this.#refreshMode()));
219
+ this.#awaitControl();
220
+ }
221
+ disconnectedCallback() {
222
+ for (const cleanup of this.#cleanups.splice(0)) cleanup();
223
+ cancelAnimationFrame(this.#frame);
224
+ clearTimeout(this.#suppressTimer);
225
+ this.#frame = 0;
226
+ this.#dropGlass();
227
+ this.#control = null;
228
+ this.#state = CONTROL_IDLE;
229
+ this.#suppressClick = false;
230
+ for (const name of ["data-vk", "data-surface", "data-glass", "data-frost", "data-size"]) this.removeAttribute(name);
231
+ for (const name of Array.from(this.style)) if (name.startsWith("--vk-")) this.style.removeProperty(name);
232
+ this.#presentation = null;
233
+ this.#mode = null;
234
+ }
235
+ attributeChangedCallback(name) {
236
+ if (!this.#control) return;
237
+ if (name === "disabled") {
238
+ const wanted = this.hasAttribute("disabled");
239
+ if (this.#control.disabled !== wanted) this.#control.disabled = wanted;
240
+ this.#syncDisabled();
241
+ } else if (name === FORCE_ATTRIBUTE) {
242
+ this.#applyForced();
243
+ } else {
244
+ this.#refreshMode(true);
245
+ }
246
+ }
247
+ // The parser connects the element before its children exist, so the button is waited for
248
+ // rather than made up.
249
+ #awaitControl() {
250
+ const found = this.querySelector(":scope > button");
251
+ if (found) {
252
+ this.#upgrade(found);
253
+ return;
254
+ }
255
+ const observer = new MutationObserver(() => {
256
+ const button = this.querySelector(":scope > button");
257
+ if (!button) return;
258
+ observer.disconnect();
259
+ this.#upgrade(button);
260
+ });
261
+ observer.observe(this, { childList: true });
262
+ this.#cleanups.push(() => observer.disconnect());
263
+ }
264
+ #upgrade(control) {
265
+ this.#control = control;
266
+ this.setAttribute("data-vk", "");
267
+ this.#caps = isCoarsePointer() ? TOUCH_INPUT : POINTER_INPUT;
268
+ this.#listen(control);
269
+ this.#cleanups.push(this.#observeSize(control));
270
+ this.#syncDisabled();
271
+ if (control.matches(":focus")) {
272
+ this.#dispatch({ type: "focus", visible: control.matches(":focus-visible") });
273
+ }
274
+ this.#mode = null;
275
+ this.#refreshMode();
276
+ }
277
+ #onInputChange() {
278
+ if (!this.#control) return;
279
+ this.#caps = isCoarsePointer() ? TOUCH_INPUT : POINTER_INPUT;
280
+ this.#refreshMode(true);
281
+ }
282
+ #size() {
283
+ const wanted = this.getAttribute("size");
284
+ if (wanted && CONTROL_SIZES.includes(wanted)) return wanted;
285
+ return defaultSize({ touch: isCoarsePointer() });
286
+ }
287
+ #glassMode() {
288
+ const wanted = this.getAttribute("glass");
289
+ if (wanted === "touch") return "touch";
290
+ return wanted === "" || wanted === "present" ? "present" : "off";
291
+ }
292
+ #forced() {
293
+ const wanted = this.getAttribute(FORCE_ATTRIBUTE);
294
+ return wanted !== null && Object.hasOwn(FORCED, wanted) ? wanted : null;
295
+ }
296
+ #dispatch(event) {
297
+ if (this.#forced()) return false;
298
+ const step = stepControl(this.#state, event, this.#caps);
299
+ this.#state = step.state;
300
+ this.#render();
301
+ return step.activated;
302
+ }
303
+ #listen(control) {
304
+ const on = (target, type, handler, capture = false) => {
305
+ target.addEventListener(type, handler, capture);
306
+ this.#cleanups.push(() => target.removeEventListener(type, handler, capture));
307
+ };
308
+ const inside = (e) => {
309
+ const r = this.getBoundingClientRect();
310
+ return e.clientX >= r.left - this.#slop.x && e.clientX <= r.right + this.#slop.x && e.clientY >= r.top - this.#slop.y && e.clientY <= r.bottom + this.#slop.y;
311
+ };
312
+ const holdSuppression = () => {
313
+ this.#suppressClick = true;
314
+ clearTimeout(this.#suppressTimer);
315
+ this.#suppressTimer = window.setTimeout(() => this.#suppressClick = false, SUPPRESS_MS);
316
+ };
317
+ const releaseSuppression = () => {
318
+ this.#suppressClick = false;
319
+ clearTimeout(this.#suppressTimer);
320
+ };
321
+ const bind = (target, own) => {
322
+ const mine = (e) => own || e.target === this;
323
+ on(target, "pointerdown", (e) => {
324
+ if (e.button !== 0 || !mine(e)) return;
325
+ releaseSuppression();
326
+ this.#dispatch({ type: "down", source: "pointer" });
327
+ });
328
+ on(target, "pointermove", (e) => {
329
+ if (!mine(e)) return;
330
+ if (this.#state.press.by === "pointer" && !inside(e)) this.#dispatch({ type: "leave" });
331
+ });
332
+ on(target, "pointerup", (e) => {
333
+ if (!mine(e)) return;
334
+ const wasPressed = this.#state.press.by === "pointer";
335
+ const activated = this.#dispatch({ type: "up", source: "pointer" });
336
+ if (own && !activated && !wasPressed && !inside(e)) holdSuppression();
337
+ });
338
+ on(target, "pointercancel", (e) => {
339
+ if (mine(e)) this.#dispatch({ type: "cancel" });
340
+ });
341
+ on(target, "pointerenter", (e) => {
342
+ if (mine(e) && e.pointerType !== "touch") this.#dispatch({ type: "enter" });
343
+ });
344
+ on(target, "pointerleave", (e) => {
345
+ if (mine(e) && !control.hasPointerCapture?.(e.pointerId)) this.#dispatch({ type: "leave" });
346
+ });
347
+ };
348
+ bind(control, true);
349
+ bind(this, false);
350
+ on(
351
+ this,
352
+ "click",
353
+ (e) => {
354
+ if (e.target !== this) return;
355
+ e.stopImmediatePropagation();
356
+ e.preventDefault();
357
+ control.click();
358
+ },
359
+ true
360
+ );
361
+ on(
362
+ control,
363
+ "click",
364
+ (e) => {
365
+ if (!this.#suppressClick) return;
366
+ releaseSuppression();
367
+ if (e.detail === 0) return;
368
+ e.preventDefault();
369
+ e.stopImmediatePropagation();
370
+ },
371
+ true
372
+ );
373
+ on(control, "keydown", (e) => {
374
+ releaseSuppression();
375
+ if ((e.key === " " || e.key === "Enter") && !e.repeat) this.#dispatch({ type: "down", source: "keyboard" });
376
+ });
377
+ on(control, "keyup", (e) => {
378
+ if (e.key === " " || e.key === "Enter") this.#dispatch({ type: "up", source: "keyboard" });
379
+ });
380
+ on(control, "focus", () => this.#dispatch({ type: "focus", visible: control.matches(":focus-visible") }));
381
+ on(control, "blur", () => this.#dispatch({ type: "blur" }));
382
+ on(control, "focusout", () => this.#dispatch({ type: "blur" }));
383
+ const mutations = new MutationObserver(() => this.#syncDisabled());
384
+ mutations.observe(control, { attributes: true, attributeFilter: ["disabled"] });
385
+ this.#cleanups.push(() => mutations.disconnect());
386
+ }
387
+ #observeSize(control) {
388
+ if (typeof ResizeObserver === "undefined") return () => {
389
+ };
390
+ const observer = new ResizeObserver(() => {
391
+ this.#applySlop();
392
+ this.#glass?.setGeometry(this.#geometry(control));
393
+ });
394
+ observer.observe(control);
395
+ return () => observer.disconnect();
396
+ }
397
+ #geometry(control) {
398
+ const radius = parseFloat(getComputedStyle(control).borderTopLeftRadius) || 0;
399
+ return { width: control.offsetWidth, height: control.offsetHeight, cornerRadius: radius };
400
+ }
401
+ #applySlop() {
402
+ const control = this.#control;
403
+ if (!control) return;
404
+ const floor = minTarget({ touch: isCoarsePointer() });
405
+ this.#slop = { x: hitSlop(control.offsetWidth, floor), y: hitSlop(control.offsetHeight, floor) };
406
+ this.style.setProperty("--vk-slop-x", String(this.#slop.x));
407
+ this.style.setProperty("--vk-slop-y", String(this.#slop.y));
408
+ }
409
+ #syncDisabled() {
410
+ const control = this.#control;
411
+ if (!control) return;
412
+ if (this.hasAttribute("disabled") && !control.disabled) control.disabled = true;
413
+ const disabled = control.disabled;
414
+ if (this.#forced()) return;
415
+ if (disabled !== this.#state.disabled) {
416
+ this.#state = stepControl(this.#state, { type: "disable", disabled }, this.#caps).state;
417
+ this.#render();
418
+ }
419
+ }
420
+ #applyForced() {
421
+ const forced = this.#forced();
422
+ if (forced) {
423
+ this.#state = FORCED[forced];
424
+ settleControlVisual(this.#visual, this.#state);
425
+ if (forced === "disabled" && this.#control && !this.#control.disabled) this.#control.disabled = true;
426
+ } else {
427
+ this.#state = CONTROL_IDLE;
428
+ this.#syncDisabled();
429
+ }
430
+ this.#render(true);
431
+ }
432
+ #refreshMode(force = false, afterFailure = false) {
433
+ if (!afterFailure) this.#glassBroken = false;
434
+ const mode = effectiveA11y(this.#own);
435
+ let next = presentationFor(mode, this.#glassMode() !== "off");
436
+ if (this.#glassBroken && next.glass) next = { ...next, glass: false, surface: next.frost ? "frosted" : "plain" };
437
+ const changed = force || !this.#mode || !sameA11y(this.#mode, mode) || next.surface !== this.#presentation?.surface;
438
+ this.#mode = mode;
439
+ this.#presentation = next;
440
+ this.#applySize();
441
+ this.setAttribute("data-surface", next.surface);
442
+ this.toggleAttribute("data-contrast", next.contrastBorder);
443
+ this.toggleAttribute("data-frost", next.frost);
444
+ this.setAttribute("data-glass", next.glass ? "on" : "off");
445
+ if (changed) this.#reattachGlass(mode);
446
+ if (this.#forced()) this.#applyForced();
447
+ else this.#render(true);
448
+ }
449
+ #applySize() {
450
+ const size = this.#size();
451
+ const spec = kind === "iconButton" ? iconButtonSize(size) : BUTTON_SIZES[size];
452
+ const set = (name, value) => this.style.setProperty(name, value);
453
+ set("--vk-height", `${spec.height}px`);
454
+ set("--vk-pad-x", `${spec.paddingX}px`);
455
+ set("--vk-icon", `${spec.icon}px`);
456
+ set("--vk-radius", `${spec.radius}px`);
457
+ set("--vk-grow", String(response.grow));
458
+ set("--vk-defocus", String(response.defocus));
459
+ this.setAttribute("data-size", size);
460
+ this.#applySlop();
461
+ }
462
+ #dropGlass() {
463
+ this.#glassToken += 1;
464
+ this.#glass?.destroy();
465
+ this.#glass = null;
466
+ this.#glassKey = "";
467
+ this.#lastMaterialKey = "";
468
+ }
469
+ #reattachGlass(mode) {
470
+ const control = this.#control;
471
+ const want = this.#presentation?.glass === true && control !== null;
472
+ const key = want ? `${this.#glassMode()}|${JSON.stringify(mode)}|${this.#size()}` : "";
473
+ if (key === this.#glassKey) return;
474
+ this.#dropGlass();
475
+ this.#glassKey = key;
476
+ if (!want || !control) return;
477
+ const token = this.#glassToken;
478
+ const sample = this.#sample;
479
+ void attachControlGlass(
480
+ control,
481
+ {
482
+ material: VIREGLASS_CONTROL_MATERIAL,
483
+ geometry: this.#geometry(control),
484
+ accessibility: mode,
485
+ onTouch: this.#glassMode() === "touch",
486
+ sample
487
+ },
488
+ () => token === this.#glassToken && this.isConnected
489
+ ).then((session) => {
490
+ if (!session) return;
491
+ this.#glass = session;
492
+ if (this.#morph) session.setMorph(this.#morph);
493
+ if (this.#sample !== sample) session.setSample(this.#sample);
494
+ this.#pushMaterial(true);
495
+ this.dispatchEvent(new CustomEvent("vire-glass-ready"));
496
+ }).catch(() => {
497
+ if (token !== this.#glassToken) return;
498
+ this.#glassKey = "";
499
+ this.#glassBroken = true;
500
+ this.#refreshMode(false, true);
501
+ });
502
+ }
503
+ #pushMaterial(force = false) {
504
+ const glass = this.#glass;
505
+ if (!glass) return;
506
+ const atom = visualAtomState(this.#visual, this.#state);
507
+ const snap = (v) => Math.round(Math.min(Math.max(v, 0), 1) * MATERIAL_STEPS) / MATERIAL_STEPS;
508
+ const quantized = { ...atom, pressed: snap(atom.pressed), hovered: snap(atom.hovered) };
509
+ const key = `${quantized.pressed}|${quantized.hovered}|${quantized.disabled}`;
510
+ if (!force && key === this.#lastMaterialKey) return;
511
+ this.#lastMaterialKey = key;
512
+ const material = atomMaterial(VIREGLASS_CONTROL_MATERIAL, kind, quantized);
513
+ glass.setMaterial(material);
514
+ if (this.#glassMode() === "touch" && this.#forced()) glass.setAppear(quantized.pressed);
515
+ }
516
+ #render(immediate = false) {
517
+ if (immediate && this.#forced()) settleControlVisual(this.#visual, this.#state);
518
+ this.#paint();
519
+ if (this.#frame || immediate && this.#forced()) return;
520
+ this.#lastFrame = 0;
521
+ this.#frame = requestAnimationFrame((t) => this.#tick(t));
522
+ }
523
+ #tick(now) {
524
+ this.#frame = 0;
525
+ if (!this.isConnected) return;
526
+ const dt = this.#lastFrame ? Math.min((now - this.#lastFrame) / 1e3, 0.05) : 1 / 60;
527
+ this.#lastFrame = now;
528
+ const moving = stepControlVisual(this.#visual, dt, this.#state, this.#presentation?.instantSprings ?? false);
529
+ this.#paint();
530
+ if (moving) this.#frame = requestAnimationFrame((t) => this.#tick(t));
531
+ }
532
+ #paint() {
533
+ const v = this.#visual;
534
+ const set = (name, value) => this.style.setProperty(name, value.toFixed(3));
535
+ set("--vk-press", v.pressed.x);
536
+ set("--vk-hover", v.hovered.x);
537
+ const body = atomBody(VIREGLASS_CONTROL_MATERIAL, visualAtomState(v, this.#state), this.#presentation?.frost ?? false);
538
+ set("--vk-body-alpha", body.alpha);
539
+ set("--vk-body-white", body.white);
540
+ set("--vk-body-lift", body.lift);
541
+ this.toggleAttribute("data-pressed", this.#state.press.by !== null);
542
+ this.toggleAttribute("data-hovered", this.#state.hovered);
543
+ this.toggleAttribute("data-focus", this.#state.focused);
544
+ this.toggleAttribute("data-disabled", this.#state.disabled);
545
+ this.#pushMaterial();
546
+ }
547
+ };
548
+ }
549
+
550
+ // src/web/index.ts
551
+ var BUTTON_TAG = "vire-button";
552
+ var ICON_BUTTON_TAG = "vire-icon-button";
553
+ function defineVireElements(options = {}) {
554
+ const registry = options.registry ?? (typeof customElements === "undefined" ? void 0 : customElements);
555
+ if (!registry) return;
556
+ const pairs = [
557
+ [BUTTON_TAG, "button"],
558
+ [ICON_BUTTON_TAG, "iconButton"]
559
+ ];
560
+ for (const [tag, kind] of pairs) {
561
+ if (!registry.get(tag)) registry.define(tag, createControlElementClass(kind, { nonce: options.nonce }));
562
+ }
563
+ }
564
+
565
+ export { BUTTON_TAG, ICON_BUTTON_TAG, STYLES, defineVireElements, getA11yOverride, setA11yOverride, systemA11y };
566
+ //# sourceMappingURL=web.js.map
567
+ //# sourceMappingURL=web.js.map