a11y-elements 0.1.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/LICENSE.txt +22 -0
- package/README.md +141 -0
- package/dist/a11y.css +1019 -0
- package/dist/accessibility-components/anchor/anchor.element.d.ts +28 -0
- package/dist/accessibility-components/anchor/anchor.element.js +87 -0
- package/dist/accessibility-components/anchor/anchor.element.js.map +1 -0
- package/dist/accessibility-components/anchor/define.d.ts +7 -0
- package/dist/accessibility-components/anchor/define.js +6 -0
- package/dist/accessibility-components/anchor/define.js.map +1 -0
- package/dist/accessibility-components/avatar/avatar.element.d.ts +21 -0
- package/dist/accessibility-components/avatar/avatar.element.js +73 -0
- package/dist/accessibility-components/avatar/avatar.element.js.map +1 -0
- package/dist/accessibility-components/avatar/define.d.ts +7 -0
- package/dist/accessibility-components/avatar/define.js +6 -0
- package/dist/accessibility-components/avatar/define.js.map +1 -0
- package/dist/accessibility-components/checkbox/checkbox.element.d.ts +36 -0
- package/dist/accessibility-components/checkbox/checkbox.element.js +79 -0
- package/dist/accessibility-components/checkbox/checkbox.element.js.map +1 -0
- package/dist/accessibility-components/checkbox/define.d.ts +7 -0
- package/dist/accessibility-components/checkbox/define.js +6 -0
- package/dist/accessibility-components/checkbox/define.js.map +1 -0
- package/dist/accessibility-components/focusable/define.d.ts +7 -0
- package/dist/accessibility-components/focusable/define.js +6 -0
- package/dist/accessibility-components/focusable/define.js.map +1 -0
- package/dist/accessibility-components/focusable/focusable.element.d.ts +30 -0
- package/dist/accessibility-components/focusable/focusable.element.js +52 -0
- package/dist/accessibility-components/focusable/focusable.element.js.map +1 -0
- package/dist/accessibility-components/picture/define.d.ts +7 -0
- package/dist/accessibility-components/picture/define.js +6 -0
- package/dist/accessibility-components/picture/define.js.map +1 -0
- package/dist/accessibility-components/picture/picture.element.d.ts +23 -0
- package/dist/accessibility-components/picture/picture.element.js +43 -0
- package/dist/accessibility-components/picture/picture.element.js.map +1 -0
- package/dist/accessibility-components/progress/define.d.ts +7 -0
- package/dist/accessibility-components/progress/define.js +6 -0
- package/dist/accessibility-components/progress/define.js.map +1 -0
- package/dist/accessibility-components/progress/progress.element.d.ts +20 -0
- package/dist/accessibility-components/progress/progress.element.js +44 -0
- package/dist/accessibility-components/progress/progress.element.js.map +1 -0
- package/dist/accessibility-components/radio-group/define.d.ts +7 -0
- package/dist/accessibility-components/radio-group/define.js +6 -0
- package/dist/accessibility-components/radio-group/define.js.map +1 -0
- package/dist/accessibility-components/radio-group/radio-group.element.d.ts +27 -0
- package/dist/accessibility-components/radio-group/radio-group.element.js +82 -0
- package/dist/accessibility-components/radio-group/radio-group.element.js.map +1 -0
- package/dist/accessibility-components/select/define.d.ts +7 -0
- package/dist/accessibility-components/select/define.js +6 -0
- package/dist/accessibility-components/select/define.js.map +1 -0
- package/dist/accessibility-components/select/select.element.d.ts +25 -0
- package/dist/accessibility-components/select/select.element.js +67 -0
- package/dist/accessibility-components/select/select.element.js.map +1 -0
- package/dist/accessibility-components/skeleton/define.d.ts +7 -0
- package/dist/accessibility-components/skeleton/define.js +6 -0
- package/dist/accessibility-components/skeleton/define.js.map +1 -0
- package/dist/accessibility-components/skeleton/skeleton.element.d.ts +16 -0
- package/dist/accessibility-components/skeleton/skeleton.element.js +40 -0
- package/dist/accessibility-components/skeleton/skeleton.element.js.map +1 -0
- package/dist/accessibility-components/spinner/define.d.ts +7 -0
- package/dist/accessibility-components/spinner/define.js +6 -0
- package/dist/accessibility-components/spinner/define.js.map +1 -0
- package/dist/accessibility-components/spinner/spinner.element.d.ts +21 -0
- package/dist/accessibility-components/spinner/spinner.element.js +60 -0
- package/dist/accessibility-components/spinner/spinner.element.js.map +1 -0
- package/dist/accessibility-components/switch/define.d.ts +7 -0
- package/dist/accessibility-components/switch/define.js +6 -0
- package/dist/accessibility-components/switch/define.js.map +1 -0
- package/dist/accessibility-components/switch/switch.element.d.ts +22 -0
- package/dist/accessibility-components/switch/switch.element.js +65 -0
- package/dist/accessibility-components/switch/switch.element.js.map +1 -0
- package/dist/accessibility-components/visually-hidden/define.d.ts +7 -0
- package/dist/accessibility-components/visually-hidden/define.js +6 -0
- package/dist/accessibility-components/visually-hidden/define.js.map +1 -0
- package/dist/accessibility-components/visually-hidden/visually-hidden.element.d.ts +16 -0
- package/dist/accessibility-components/visually-hidden/visually-hidden.element.js +24 -0
- package/dist/accessibility-components/visually-hidden/visually-hidden.element.js.map +1 -0
- package/dist/browser/accessibility-components/anchor/define.js +192 -0
- package/dist/browser/accessibility-components/anchor/define.js.map +1 -0
- package/dist/browser/accessibility-components/avatar/define.js +159 -0
- package/dist/browser/accessibility-components/avatar/define.js.map +1 -0
- package/dist/browser/accessibility-components/checkbox/define.js +186 -0
- package/dist/browser/accessibility-components/checkbox/define.js.map +1 -0
- package/dist/browser/accessibility-components/focusable/define.js +113 -0
- package/dist/browser/accessibility-components/focusable/define.js.map +1 -0
- package/dist/browser/accessibility-components/picture/define.js +156 -0
- package/dist/browser/accessibility-components/picture/define.js.map +1 -0
- package/dist/browser/accessibility-components/progress/define.js +135 -0
- package/dist/browser/accessibility-components/progress/define.js.map +1 -0
- package/dist/browser/accessibility-components/radio-group/define.js +204 -0
- package/dist/browser/accessibility-components/radio-group/define.js.map +1 -0
- package/dist/browser/accessibility-components/select/define.js +184 -0
- package/dist/browser/accessibility-components/select/define.js.map +1 -0
- package/dist/browser/accessibility-components/skeleton/define.js +136 -0
- package/dist/browser/accessibility-components/skeleton/define.js.map +1 -0
- package/dist/browser/accessibility-components/spinner/define.js +156 -0
- package/dist/browser/accessibility-components/spinner/define.js.map +1 -0
- package/dist/browser/accessibility-components/switch/define.js +192 -0
- package/dist/browser/accessibility-components/switch/define.js.map +1 -0
- package/dist/browser/accessibility-components/visually-hidden/define.js +97 -0
- package/dist/browser/accessibility-components/visually-hidden/define.js.map +1 -0
- package/dist/browser/overlays/blocking-loader/define.js +351 -0
- package/dist/browser/overlays/blocking-loader/define.js.map +1 -0
- package/dist/browser/overlays/context-menu/define.js +774 -0
- package/dist/browser/overlays/context-menu/define.js.map +1 -0
- package/dist/browser/overlays/drawer/define.js +529 -0
- package/dist/browser/overlays/drawer/define.js.map +1 -0
- package/dist/browser/overlays/dropdown/define.js +770 -0
- package/dist/browser/overlays/dropdown/define.js.map +1 -0
- package/dist/browser/overlays/emergency-dialog/define.js +525 -0
- package/dist/browser/overlays/emergency-dialog/define.js.map +1 -0
- package/dist/browser/overlays/modal/define.js +523 -0
- package/dist/browser/overlays/modal/define.js.map +1 -0
- package/dist/browser/overlays/notification-banner/define.js +289 -0
- package/dist/browser/overlays/notification-banner/define.js.map +1 -0
- package/dist/browser/overlays/popover/define.js +625 -0
- package/dist/browser/overlays/popover/define.js.map +1 -0
- package/dist/browser/overlays/snackbar/define.js +268 -0
- package/dist/browser/overlays/snackbar/define.js.map +1 -0
- package/dist/browser/overlays/tooltip/define.js +707 -0
- package/dist/browser/overlays/tooltip/define.js.map +1 -0
- package/dist/core/a11y-element.d.ts +53 -0
- package/dist/core/a11y-element.js +104 -0
- package/dist/core/a11y-element.js.map +1 -0
- package/dist/core/a11y-form-element.d.ts +23 -0
- package/dist/core/a11y-form-element.js +30 -0
- package/dist/core/a11y-form-element.js.map +1 -0
- package/dist/core/a11y-overlay-element.d.ts +100 -0
- package/dist/core/a11y-overlay-element.js +160 -0
- package/dist/core/a11y-overlay-element.js.map +1 -0
- package/dist/core/a11y-wrapper-element.d.ts +48 -0
- package/dist/core/a11y-wrapper-element.js +70 -0
- package/dist/core/a11y-wrapper-element.js.map +1 -0
- package/dist/core/dom-sync.d.ts +15 -0
- package/dist/core/dom-sync.js +30 -0
- package/dist/core/dom-sync.js.map +1 -0
- package/dist/core/focus-trap.d.ts +47 -0
- package/dist/core/focus-trap.js +138 -0
- package/dist/core/focus-trap.js.map +1 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +10 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/overlay-registry.d.ts +36 -0
- package/dist/core/overlay-registry.js +60 -0
- package/dist/core/overlay-registry.js.map +1 -0
- package/dist/core/template.d.ts +32 -0
- package/dist/core/template.js +48 -0
- package/dist/core/template.js.map +1 -0
- package/dist/core/transition.d.ts +16 -0
- package/dist/core/transition.js +53 -0
- package/dist/core/transition.js.map +1 -0
- package/dist/overlays/blocking-loader/blocking-loader.element.d.ts +29 -0
- package/dist/overlays/blocking-loader/blocking-loader.element.js +77 -0
- package/dist/overlays/blocking-loader/blocking-loader.element.js.map +1 -0
- package/dist/overlays/blocking-loader/define.d.ts +9 -0
- package/dist/overlays/blocking-loader/define.js +10 -0
- package/dist/overlays/blocking-loader/define.js.map +1 -0
- package/dist/overlays/context-menu/context-menu.element.d.ts +31 -0
- package/dist/overlays/context-menu/context-menu.element.js +86 -0
- package/dist/overlays/context-menu/context-menu.element.js.map +1 -0
- package/dist/overlays/context-menu/define.d.ts +8 -0
- package/dist/overlays/context-menu/define.js +8 -0
- package/dist/overlays/context-menu/define.js.map +1 -0
- package/dist/overlays/core/a11y-anchored-overlay-element.d.ts +68 -0
- package/dist/overlays/core/a11y-anchored-overlay-element.js +276 -0
- package/dist/overlays/core/a11y-anchored-overlay-element.js.map +1 -0
- package/dist/overlays/core/a11y-menu-overlay-element.d.ts +46 -0
- package/dist/overlays/core/a11y-menu-overlay-element.js +152 -0
- package/dist/overlays/core/a11y-menu-overlay-element.js.map +1 -0
- package/dist/overlays/core/a11y-modal-overlay-element.d.ts +44 -0
- package/dist/overlays/core/a11y-modal-overlay-element.js +163 -0
- package/dist/overlays/core/a11y-modal-overlay-element.js.map +1 -0
- package/dist/overlays/core/a11y-passive-overlay-element.d.ts +38 -0
- package/dist/overlays/core/a11y-passive-overlay-element.js +108 -0
- package/dist/overlays/core/a11y-passive-overlay-element.js.map +1 -0
- package/dist/overlays/drawer/define.d.ts +8 -0
- package/dist/overlays/drawer/define.js +8 -0
- package/dist/overlays/drawer/define.js.map +1 -0
- package/dist/overlays/drawer/drawer.element.d.ts +19 -0
- package/dist/overlays/drawer/drawer.element.js +37 -0
- package/dist/overlays/drawer/drawer.element.js.map +1 -0
- package/dist/overlays/dropdown/define.d.ts +8 -0
- package/dist/overlays/dropdown/define.js +8 -0
- package/dist/overlays/dropdown/define.js.map +1 -0
- package/dist/overlays/dropdown/dropdown.element.d.ts +30 -0
- package/dist/overlays/dropdown/dropdown.element.js +82 -0
- package/dist/overlays/dropdown/dropdown.element.js.map +1 -0
- package/dist/overlays/emergency-dialog/define.d.ts +8 -0
- package/dist/overlays/emergency-dialog/define.js +8 -0
- package/dist/overlays/emergency-dialog/define.js.map +1 -0
- package/dist/overlays/emergency-dialog/emergency-dialog.element.d.ts +23 -0
- package/dist/overlays/emergency-dialog/emergency-dialog.element.js +38 -0
- package/dist/overlays/emergency-dialog/emergency-dialog.element.js.map +1 -0
- package/dist/overlays/modal/define.d.ts +8 -0
- package/dist/overlays/modal/define.js +8 -0
- package/dist/overlays/modal/define.js.map +1 -0
- package/dist/overlays/modal/modal.element.d.ts +26 -0
- package/dist/overlays/modal/modal.element.js +42 -0
- package/dist/overlays/modal/modal.element.js.map +1 -0
- package/dist/overlays/notification-banner/define.d.ts +14 -0
- package/dist/overlays/notification-banner/define.js +26 -0
- package/dist/overlays/notification-banner/define.js.map +1 -0
- package/dist/overlays/notification-banner/notification-banner.element.d.ts +39 -0
- package/dist/overlays/notification-banner/notification-banner.element.js +88 -0
- package/dist/overlays/notification-banner/notification-banner.element.js.map +1 -0
- package/dist/overlays/popover/define.d.ts +8 -0
- package/dist/overlays/popover/define.js +8 -0
- package/dist/overlays/popover/define.js.map +1 -0
- package/dist/overlays/popover/popover.element.d.ts +20 -0
- package/dist/overlays/popover/popover.element.js +41 -0
- package/dist/overlays/popover/popover.element.js.map +1 -0
- package/dist/overlays/snackbar/define.d.ts +14 -0
- package/dist/overlays/snackbar/define.js +26 -0
- package/dist/overlays/snackbar/define.js.map +1 -0
- package/dist/overlays/snackbar/snackbar.element.d.ts +30 -0
- package/dist/overlays/snackbar/snackbar.element.js +61 -0
- package/dist/overlays/snackbar/snackbar.element.js.map +1 -0
- package/dist/overlays/tooltip/define.d.ts +8 -0
- package/dist/overlays/tooltip/define.js +8 -0
- package/dist/overlays/tooltip/define.js.map +1 -0
- package/dist/overlays/tooltip/tooltip.element.d.ts +41 -0
- package/dist/overlays/tooltip/tooltip.element.js +133 -0
- package/dist/overlays/tooltip/tooltip.element.js.map +1 -0
- package/package.json +93 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// src/core/a11y-element.ts
|
|
2
|
+
var A11yElement = class extends HTMLElement {
|
|
3
|
+
_connected = false;
|
|
4
|
+
_listeners = [];
|
|
5
|
+
_updating = false;
|
|
6
|
+
connectedCallback() {
|
|
7
|
+
this._connected = true;
|
|
8
|
+
this.update();
|
|
9
|
+
}
|
|
10
|
+
disconnectedCallback() {
|
|
11
|
+
this._connected = false;
|
|
12
|
+
this._cleanupListeners();
|
|
13
|
+
this.onDisconnect?.();
|
|
14
|
+
}
|
|
15
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
16
|
+
if (oldValue === newValue || !this._connected) return;
|
|
17
|
+
this.update();
|
|
18
|
+
}
|
|
19
|
+
update() {
|
|
20
|
+
if (this._updating) return;
|
|
21
|
+
this._updating = true;
|
|
22
|
+
try {
|
|
23
|
+
this._cleanupListeners();
|
|
24
|
+
this.onBeforeRender?.();
|
|
25
|
+
this.innerHTML = String(this.render());
|
|
26
|
+
this.onAfterRender?.();
|
|
27
|
+
} finally {
|
|
28
|
+
this._updating = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Adds a listener and tracks it for automatic removal on the next render
|
|
33
|
+
* and on disconnect — the direct replacement for the old framework's
|
|
34
|
+
* `[el, type, handler]` cleanup-array convention.
|
|
35
|
+
*/
|
|
36
|
+
listen(target, type, handler, options) {
|
|
37
|
+
target.addEventListener(type, handler, options);
|
|
38
|
+
this._listeners.push([target, type, handler, options]);
|
|
39
|
+
}
|
|
40
|
+
_cleanupListeners() {
|
|
41
|
+
for (const [target, type, handler, options] of this._listeners) {
|
|
42
|
+
target.removeEventListener(type, handler, options);
|
|
43
|
+
}
|
|
44
|
+
this._listeners = [];
|
|
45
|
+
}
|
|
46
|
+
/** Dispatches a bubbling CustomEvent from this element. */
|
|
47
|
+
emit(type, detail) {
|
|
48
|
+
return this.dispatchEvent(new CustomEvent(type, { bubbles: true, detail }));
|
|
49
|
+
}
|
|
50
|
+
// --- small attribute-reflection helpers, shared across components ---
|
|
51
|
+
stringAttr(name, fallback = "") {
|
|
52
|
+
return this.getAttribute(name) ?? fallback;
|
|
53
|
+
}
|
|
54
|
+
optionalStringAttr(name) {
|
|
55
|
+
return this.getAttribute(name) ?? void 0;
|
|
56
|
+
}
|
|
57
|
+
boolAttr(name) {
|
|
58
|
+
return this.hasAttribute(name);
|
|
59
|
+
}
|
|
60
|
+
numberAttr(name, fallback) {
|
|
61
|
+
const value = this.getAttribute(name);
|
|
62
|
+
if (value == null) return fallback;
|
|
63
|
+
const parsed = Number(value);
|
|
64
|
+
return Number.isNaN(parsed) ? fallback : parsed;
|
|
65
|
+
}
|
|
66
|
+
setBoolAttr(name, value) {
|
|
67
|
+
if (value) this.setAttribute(name, "");
|
|
68
|
+
else this.removeAttribute(name);
|
|
69
|
+
}
|
|
70
|
+
/** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */
|
|
71
|
+
setCssVar(name, value) {
|
|
72
|
+
if (value) this.style.setProperty(name, value);
|
|
73
|
+
else this.style.removeProperty(name);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/core/a11y-wrapper-element.ts
|
|
78
|
+
var A11yWrapperElement = class extends A11yElement {
|
|
79
|
+
_observer = null;
|
|
80
|
+
/** Nodes already given their listeners since the last connect — see `wireOnce()`. */
|
|
81
|
+
_wiredNodes = /* @__PURE__ */ new WeakSet();
|
|
82
|
+
connectedCallback() {
|
|
83
|
+
this._connected = true;
|
|
84
|
+
this._sync();
|
|
85
|
+
this._observer = new MutationObserver(() => this._sync());
|
|
86
|
+
this._observer.observe(this, this.observerInit());
|
|
87
|
+
}
|
|
88
|
+
disconnectedCallback() {
|
|
89
|
+
this._observer?.disconnect();
|
|
90
|
+
this._observer = null;
|
|
91
|
+
super.disconnectedCallback();
|
|
92
|
+
this._wiredNodes = /* @__PURE__ */ new WeakSet();
|
|
93
|
+
}
|
|
94
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
95
|
+
if (oldValue === newValue || !this._connected) return;
|
|
96
|
+
this._sync();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Runs `wire` the first time `node` is seen since this element was last
|
|
100
|
+
* connected. `_sync()` runs on every mutation, so listener setup must be
|
|
101
|
+
* idempotent — but those listeners (added via `listen()`) are all removed
|
|
102
|
+
* on disconnect, so the "already wired" memory must reset with them, or a
|
|
103
|
+
* reparented element (disconnect + connect) would silently stop firing.
|
|
104
|
+
*/
|
|
105
|
+
wireOnce(node, wire) {
|
|
106
|
+
if (this._wiredNodes.has(node)) return;
|
|
107
|
+
this._wiredNodes.add(node);
|
|
108
|
+
wire();
|
|
109
|
+
}
|
|
110
|
+
/** What to watch for changes worth re-syncing on. Override for a narrower/wider scope. */
|
|
111
|
+
observerInit() {
|
|
112
|
+
return { childList: true, subtree: true, attributes: true };
|
|
113
|
+
}
|
|
114
|
+
/** Unused — content is the author's own children, never replaced. */
|
|
115
|
+
render() {
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/core/dom-sync.ts
|
|
121
|
+
function syncText(el, text) {
|
|
122
|
+
if (el.textContent !== text) el.textContent = text;
|
|
123
|
+
}
|
|
124
|
+
function syncClass(el, name, present) {
|
|
125
|
+
if (el.classList.contains(name) !== present) el.classList.toggle(name, present);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/accessibility-components/checkbox/checkbox.element.ts
|
|
129
|
+
var CheckboxElement = class extends A11yWrapperElement {
|
|
130
|
+
static get observedAttributes() {
|
|
131
|
+
return ["label", "indeterminate"];
|
|
132
|
+
}
|
|
133
|
+
/** Called with the input's current `checked` state on every native `change` event. */
|
|
134
|
+
onChange;
|
|
135
|
+
getValue() {
|
|
136
|
+
return this._input()?.checked ?? false;
|
|
137
|
+
}
|
|
138
|
+
setValue(checked) {
|
|
139
|
+
const input = this._input();
|
|
140
|
+
if (input) input.checked = checked;
|
|
141
|
+
}
|
|
142
|
+
_input() {
|
|
143
|
+
return this.querySelector('input[type="checkbox"]');
|
|
144
|
+
}
|
|
145
|
+
_sync() {
|
|
146
|
+
const input = this._input();
|
|
147
|
+
if (!input) return;
|
|
148
|
+
const wrapper = this._ensureWrapper(input);
|
|
149
|
+
syncClass(input, "a11y-checkbox__input", true);
|
|
150
|
+
input.indeterminate = this.boolAttr("indeterminate");
|
|
151
|
+
this._syncLabel(wrapper);
|
|
152
|
+
this.wireOnce(input, () => this.listen(input, "change", () => this.onChange?.(input.checked)));
|
|
153
|
+
}
|
|
154
|
+
_ensureWrapper(input) {
|
|
155
|
+
const existing = input.closest("label.a11y-checkbox");
|
|
156
|
+
if (existing && existing.parentElement === this) return existing;
|
|
157
|
+
const wrapper = document.createElement("label");
|
|
158
|
+
wrapper.className = "a11y-checkbox";
|
|
159
|
+
input.replaceWith(wrapper);
|
|
160
|
+
wrapper.appendChild(input);
|
|
161
|
+
return wrapper;
|
|
162
|
+
}
|
|
163
|
+
_syncLabel(wrapper) {
|
|
164
|
+
const text = this.optionalStringAttr("label");
|
|
165
|
+
let span = wrapper.querySelector(".a11y-checkbox__label");
|
|
166
|
+
if (!text) {
|
|
167
|
+
span?.remove();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!span) {
|
|
171
|
+
span = document.createElement("span");
|
|
172
|
+
span.className = "a11y-checkbox__label";
|
|
173
|
+
wrapper.appendChild(span);
|
|
174
|
+
}
|
|
175
|
+
syncText(span, text);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/accessibility-components/checkbox/define.ts
|
|
180
|
+
if (!customElements.get("a11y-checkbox")) {
|
|
181
|
+
customElements.define("a11y-checkbox", CheckboxElement);
|
|
182
|
+
}
|
|
183
|
+
export {
|
|
184
|
+
CheckboxElement
|
|
185
|
+
};
|
|
186
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/a11y-element.ts","../../../../src/core/a11y-wrapper-element.ts","../../../../src/core/dom-sync.ts","../../../../src/accessibility-components/checkbox/checkbox.element.ts","../../../../src/accessibility-components/checkbox/define.ts"],"sourcesContent":["import type { Raw } from './template.js';\n\ntype Listener = [target: EventTarget, type: string, handler: EventListener, options?: AddEventListenerOptions | boolean];\n\n/**\n * Base class for every element in this library. Light DOM only — elements\n * render real child markup styled by the consumer's own CSS, not a shadow\n * root.\n *\n * Lifecycle mirrors the old `onBeforeRender -> innerHTML = template ->\n * onAfterRender` pipeline this library used to run through a proprietary\n * framework, just mapped onto native custom-element callbacks:\n *\n * connectedCallback -> first update()\n * attributeChangedCallback -> update() for any observed attribute change\n * disconnectedCallback -> remove all tracked listeners, then onDisconnect()\n *\n * Subclasses list attributes to watch via `static observedAttributes` and\n * implement `render()` to return the markup for `this.innerHTML`. Attribute/\n * property setters that need to reflect into the DOM should just mutate the\n * attribute — `attributeChangedCallback` re-renders synchronously while\n * connected, so state and DOM never drift out of sync across a render.\n */\nexport abstract class A11yElement extends HTMLElement {\n protected _connected = false;\n private _listeners: Listener[] = [];\n private _updating = false;\n\n connectedCallback(): void {\n this._connected = true;\n this.update();\n }\n\n disconnectedCallback(): void {\n this._connected = false;\n this._cleanupListeners();\n this.onDisconnect?.();\n }\n\n attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null): void {\n if (oldValue === newValue || !this._connected) return;\n this.update();\n }\n\n protected update(): void {\n // Re-entrancy guard: a listener/hook that itself mutates an observed\n // attribute during render must not trigger a nested render.\n if (this._updating) return;\n this._updating = true;\n try {\n this._cleanupListeners();\n this.onBeforeRender?.();\n this.innerHTML = String(this.render());\n this.onAfterRender?.();\n } finally {\n this._updating = false;\n }\n }\n\n /** Returns the markup to set as `this.innerHTML` on every render — a plain string, or an `html\\`...\\`` result. */\n protected abstract render(): string | Raw;\n\n /** Runs before `render()`. Typical use: set root-level classes/ARIA/CSS custom properties. */\n protected onBeforeRender?(): void;\n\n /** Runs after `render()` replaces `innerHTML`. Typical use: query into fresh markup, wire listeners, set DOM-only properties. */\n protected onAfterRender?(): void;\n\n /** Runs once, after listener cleanup, when the element leaves the DOM. */\n protected onDisconnect?(): void;\n\n /**\n * Adds a listener and tracks it for automatic removal on the next render\n * and on disconnect — the direct replacement for the old framework's\n * `[el, type, handler]` cleanup-array convention.\n */\n protected listen(\n target: EventTarget,\n type: string,\n handler: EventListener,\n options?: AddEventListenerOptions | boolean,\n ): void {\n target.addEventListener(type, handler, options);\n this._listeners.push([target, type, handler, options]);\n }\n\n private _cleanupListeners(): void {\n for (const [target, type, handler, options] of this._listeners) {\n target.removeEventListener(type, handler, options);\n }\n this._listeners = [];\n }\n\n /** Dispatches a bubbling CustomEvent from this element. */\n protected emit<T = unknown>(type: string, detail?: T): boolean {\n return this.dispatchEvent(new CustomEvent<T>(type, { bubbles: true, detail: detail as T }));\n }\n\n // --- small attribute-reflection helpers, shared across components ---\n\n protected stringAttr(name: string, fallback = ''): string {\n return this.getAttribute(name) ?? fallback;\n }\n\n protected optionalStringAttr(name: string): string | undefined {\n return this.getAttribute(name) ?? undefined;\n }\n\n protected boolAttr(name: string): boolean {\n return this.hasAttribute(name);\n }\n\n protected numberAttr(name: string, fallback: number): number {\n const value = this.getAttribute(name);\n if (value == null) return fallback;\n const parsed = Number(value);\n return Number.isNaN(parsed) ? fallback : parsed;\n }\n\n protected setBoolAttr(name: string, value: boolean): void {\n if (value) this.setAttribute(name, '');\n else this.removeAttribute(name);\n }\n\n /** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */\n protected setCssVar(name: string, value: string | null | undefined): void {\n if (value) this.style.setProperty(name, value);\n else this.style.removeProperty(name);\n }\n}\n","import { A11yElement } from './a11y-element.js';\n\n/**\n * Base for elements that enhance real, consumer-authored light-DOM children\n * instead of generating their own markup from a template string — e.g. a\n * `<a11y-picture>` wrapping a real `<picture>`/`<img>`/`<figcaption>`, or\n * `<a11y-select>` wrapping a real `<select>`/`<option>`s.\n *\n * Unlike `A11yElement`, this never sets `innerHTML` from a `render()`\n * string — that would destroy the author's children — so it opts out of the\n * normal render pipeline and instead re-syncs (`_sync()`) from two sources:\n *\n * - **This element's own observed attributes** (e.g. `label`, `legend`),\n * listed via `static observedAttributes` same as any other element —\n * these re-sync *synchronously*, matching every other element in this\n * library, so `el.setAttribute('label', 'x')` takes effect immediately.\n * - **Everything else** (children added/removed, an attribute changing on\n * one of *those* children) — light DOM has no `slotchange`, so a\n * `MutationObserver` is the equivalent, necessarily asynchronous (a real\n * microtask/task delay after the mutation).\n *\n * `_sync()` runs inside that `MutationObserver` callback, so every DOM write\n * it makes must be guarded (see `core/dom-sync.ts`) — `setAttribute`/\n * `classList.add`/`textContent =` all queue a mutation record even when the\n * value is unchanged, and an unguarded write would retrigger the observer\n * forever.\n */\nexport abstract class A11yWrapperElement extends A11yElement {\n private _observer: MutationObserver | null = null;\n /** Nodes already given their listeners since the last connect — see `wireOnce()`. */\n private _wiredNodes = new WeakSet<Node>();\n\n override connectedCallback(): void {\n this._connected = true;\n this._sync();\n this._observer = new MutationObserver(() => this._sync());\n this._observer.observe(this, this.observerInit());\n }\n\n override disconnectedCallback(): void {\n this._observer?.disconnect();\n this._observer = null;\n super.disconnectedCallback(); // removes every `listen()` listener…\n this._wiredNodes = new WeakSet(); // …so the next connect must wire them again\n }\n\n override attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null): void {\n if (oldValue === newValue || !this._connected) return;\n this._sync();\n }\n\n /**\n * Runs `wire` the first time `node` is seen since this element was last\n * connected. `_sync()` runs on every mutation, so listener setup must be\n * idempotent — but those listeners (added via `listen()`) are all removed\n * on disconnect, so the \"already wired\" memory must reset with them, or a\n * reparented element (disconnect + connect) would silently stop firing.\n */\n protected wireOnce(node: Node, wire: () => void): void {\n if (this._wiredNodes.has(node)) return;\n this._wiredNodes.add(node);\n wire();\n }\n\n /** What to watch for changes worth re-syncing on. Override for a narrower/wider scope. */\n protected observerInit(): MutationObserverInit {\n return { childList: true, subtree: true, attributes: true };\n }\n\n /** Re-derive this element's own attributes/classes from its current children. */\n protected abstract _sync(): void;\n\n /** Unused — content is the author's own children, never replaced. */\n protected override render(): string {\n return '';\n }\n}\n","/**\n * Guarded DOM writes for use inside `A11yWrapperElement._sync()` methods (or\n * anywhere else re-syncing against a live MutationObserver).\n *\n * `setAttribute`/`removeAttribute`/`classList.add`/`classList.remove`/\n * `textContent =` all queue a mutation record unconditionally — even when\n * the value being written is identical to what's already there. Call one of\n * these unguarded inside a MutationObserver callback that's itself watching\n * the element being written to, and every \"no-op\" write retriggers the\n * observer, forever. These helpers check first, so a `_sync()` call that\n * changes nothing produces zero mutations and the observer loop terminates.\n */\n\nexport function syncText(el: Element, text: string): void {\n if (el.textContent !== text) el.textContent = text;\n}\n\nexport function syncAttr(el: Element, name: string, value: string | null | undefined): void {\n if (value == null) {\n if (el.hasAttribute(name)) el.removeAttribute(name);\n } else if (el.getAttribute(name) !== value) {\n el.setAttribute(name, value);\n }\n}\n\nexport function syncClass(el: Element, name: string, present: boolean): void {\n if (el.classList.contains(name) !== present) el.classList.toggle(name, present);\n}\n","import { A11yWrapperElement } from '../../core/a11y-wrapper-element.js';\nimport { syncClass, syncText } from '../../core/dom-sync.js';\n\n/**\n * Enhances a real `<input type=\"checkbox\">` authored by the consumer with a\n * wrapping `<label>` (for native click-the-label-to-toggle + implicit\n * accessible naming — a custom element can't itself *be* a `<label>`) and a\n * visible label span.\n *\n * ```html\n * <a11y-checkbox label=\"Accept terms\">\n * <input type=\"checkbox\" name=\"terms\" required>\n * </a11y-checkbox>\n * ```\n *\n * The input is never regenerated — id/name/value/checked/disabled/required\n * are plain native HTML attributes the consumer already controls directly,\n * so there's nothing to keep in sync and no risk of an unrelated re-render\n * wiping out a user's live checked state. `indeterminate` (no HTML attribute\n * equivalent) and `onChange` are the two things this element adds.\n *\n * Every DOM write in `_sync()` goes through the guarded `dom-sync` helpers:\n * `setAttribute`/`classList.add`/`textContent =` all queue a mutation record\n * even when the value is unchanged, and `_sync()` itself runs inside a\n * `MutationObserver` callback watching this subtree — an unguarded write\n * would retrigger the observer forever.\n */\nexport class CheckboxElement extends A11yWrapperElement {\n static get observedAttributes(): string[] {\n return ['label', 'indeterminate'];\n }\n\n\n /** Called with the input's current `checked` state on every native `change` event. */\n onChange?: (checked: boolean) => void;\n\n getValue(): boolean {\n return this._input()?.checked ?? false;\n }\n\n setValue(checked: boolean): void {\n const input = this._input();\n if (input) input.checked = checked;\n }\n\n private _input(): HTMLInputElement | null {\n return this.querySelector('input[type=\"checkbox\"]');\n }\n\n protected override _sync(): void {\n const input = this._input();\n if (!input) return;\n\n const wrapper = this._ensureWrapper(input);\n syncClass(input, 'a11y-checkbox__input', true);\n input.indeterminate = this.boolAttr('indeterminate'); // IDL-only property, never reflected — safe unguarded\n this._syncLabel(wrapper);\n\n this.wireOnce(input, () => this.listen(input, 'change', () => this.onChange?.(input.checked)));\n }\n\n private _ensureWrapper(input: HTMLInputElement): HTMLLabelElement {\n const existing = input.closest<HTMLLabelElement>('label.a11y-checkbox');\n if (existing && existing.parentElement === this) return existing;\n\n const wrapper = document.createElement('label');\n wrapper.className = 'a11y-checkbox';\n input.replaceWith(wrapper);\n wrapper.appendChild(input);\n return wrapper;\n }\n\n private _syncLabel(wrapper: HTMLLabelElement): void {\n const text = this.optionalStringAttr('label');\n let span = wrapper.querySelector<HTMLElement>('.a11y-checkbox__label');\n if (!text) {\n span?.remove();\n return;\n }\n if (!span) {\n span = document.createElement('span');\n span.className = 'a11y-checkbox__label';\n wrapper.appendChild(span);\n }\n syncText(span, text);\n }\n}\n","import { CheckboxElement } from './checkbox.element.js';\n\nif (!customElements.get('a11y-checkbox')) {\n customElements.define('a11y-checkbox', CheckboxElement);\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'a11y-checkbox': CheckboxElement;\n }\n}\n\nexport { CheckboxElement };\n"],"mappings":";AAuBO,IAAe,cAAf,cAAmC,YAAY;AAAA,EAC1C,aAAa;AAAA,EACf,aAAyB,CAAC;AAAA,EAC1B,YAAY;AAAA,EAEpB,oBAA0B;AACxB,SAAK,aAAa;AAClB,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,uBAA6B;AAC3B,SAAK,aAAa;AAClB,SAAK,kBAAkB;AACvB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,yBAAyB,OAAe,UAAyB,UAA+B;AAC9F,QAAI,aAAa,YAAY,CAAC,KAAK,WAAY;AAC/C,SAAK,OAAO;AAAA,EACd;AAAA,EAEU,SAAe;AAGvB,QAAI,KAAK,UAAW;AACpB,SAAK,YAAY;AACjB,QAAI;AACF,WAAK,kBAAkB;AACvB,WAAK,iBAAiB;AACtB,WAAK,YAAY,OAAO,KAAK,OAAO,CAAC;AACrC,WAAK,gBAAgB;AAAA,IACvB,UAAE;AACA,WAAK,YAAY;AAAA,IACnB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBU,OACR,QACA,MACA,SACA,SACM;AACN,WAAO,iBAAiB,MAAM,SAAS,OAAO;AAC9C,SAAK,WAAW,KAAK,CAAC,QAAQ,MAAM,SAAS,OAAO,CAAC;AAAA,EACvD;AAAA,EAEQ,oBAA0B;AAChC,eAAW,CAAC,QAAQ,MAAM,SAAS,OAAO,KAAK,KAAK,YAAY;AAC9D,aAAO,oBAAoB,MAAM,SAAS,OAAO;AAAA,IACnD;AACA,SAAK,aAAa,CAAC;AAAA,EACrB;AAAA;AAAA,EAGU,KAAkB,MAAc,QAAqB;AAC7D,WAAO,KAAK,cAAc,IAAI,YA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eAAe,OAA2C;AAChE,UAAM,WAAW,MAAM,QAA0B,qBAAqB;AACtE,QAAI,YAAY,SAAS,kBAAkB,KAAM,QAAO;AAExD,UAAM,UAAU,SAAS,cAAc,OAAO;AAC9C,YAAQ,YAAY;AACpB,UAAM,YAAY,OAAO;AACzB,YAAQ,YAAY,KAAK;AACzB,WAAO;AAAA,EACT;AAAA,EAEQ,WAAW,SAAiC;AAClD,UAAM,OAAO,KAAK,mBAAmB,OAAO;AAC5C,QAAI,OAAO,QAAQ,cAA2B,uBAAuB;AACrE,QAAI,CAAC,MAAM;AACT,YAAM,OAAO;AACb;AAAA,IACF;AACA,QAAI,CAAC,MAAM;AACT,aAAO,SAAS,cAAc,MAAM;AACpC,WAAK,YAAY;AACjB,cAAQ,YAAY,IAAI;AAAA,IAC1B;AACA,aAAS,MAAM,IAAI;AAAA,EACrB;AACF;;;ACpFA,IAAI,CAAC,eAAe,IAAI,eAAe,GAAG;AACxC,iBAAe,OAAO,iBAAiB,eAAe;AACxD;","names":[]}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// src/core/a11y-element.ts
|
|
2
|
+
var A11yElement = class extends HTMLElement {
|
|
3
|
+
_connected = false;
|
|
4
|
+
_listeners = [];
|
|
5
|
+
_updating = false;
|
|
6
|
+
connectedCallback() {
|
|
7
|
+
this._connected = true;
|
|
8
|
+
this.update();
|
|
9
|
+
}
|
|
10
|
+
disconnectedCallback() {
|
|
11
|
+
this._connected = false;
|
|
12
|
+
this._cleanupListeners();
|
|
13
|
+
this.onDisconnect?.();
|
|
14
|
+
}
|
|
15
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
16
|
+
if (oldValue === newValue || !this._connected) return;
|
|
17
|
+
this.update();
|
|
18
|
+
}
|
|
19
|
+
update() {
|
|
20
|
+
if (this._updating) return;
|
|
21
|
+
this._updating = true;
|
|
22
|
+
try {
|
|
23
|
+
this._cleanupListeners();
|
|
24
|
+
this.onBeforeRender?.();
|
|
25
|
+
this.innerHTML = String(this.render());
|
|
26
|
+
this.onAfterRender?.();
|
|
27
|
+
} finally {
|
|
28
|
+
this._updating = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Adds a listener and tracks it for automatic removal on the next render
|
|
33
|
+
* and on disconnect — the direct replacement for the old framework's
|
|
34
|
+
* `[el, type, handler]` cleanup-array convention.
|
|
35
|
+
*/
|
|
36
|
+
listen(target, type, handler, options) {
|
|
37
|
+
target.addEventListener(type, handler, options);
|
|
38
|
+
this._listeners.push([target, type, handler, options]);
|
|
39
|
+
}
|
|
40
|
+
_cleanupListeners() {
|
|
41
|
+
for (const [target, type, handler, options] of this._listeners) {
|
|
42
|
+
target.removeEventListener(type, handler, options);
|
|
43
|
+
}
|
|
44
|
+
this._listeners = [];
|
|
45
|
+
}
|
|
46
|
+
/** Dispatches a bubbling CustomEvent from this element. */
|
|
47
|
+
emit(type, detail) {
|
|
48
|
+
return this.dispatchEvent(new CustomEvent(type, { bubbles: true, detail }));
|
|
49
|
+
}
|
|
50
|
+
// --- small attribute-reflection helpers, shared across components ---
|
|
51
|
+
stringAttr(name, fallback = "") {
|
|
52
|
+
return this.getAttribute(name) ?? fallback;
|
|
53
|
+
}
|
|
54
|
+
optionalStringAttr(name) {
|
|
55
|
+
return this.getAttribute(name) ?? void 0;
|
|
56
|
+
}
|
|
57
|
+
boolAttr(name) {
|
|
58
|
+
return this.hasAttribute(name);
|
|
59
|
+
}
|
|
60
|
+
numberAttr(name, fallback) {
|
|
61
|
+
const value = this.getAttribute(name);
|
|
62
|
+
if (value == null) return fallback;
|
|
63
|
+
const parsed = Number(value);
|
|
64
|
+
return Number.isNaN(parsed) ? fallback : parsed;
|
|
65
|
+
}
|
|
66
|
+
setBoolAttr(name, value) {
|
|
67
|
+
if (value) this.setAttribute(name, "");
|
|
68
|
+
else this.removeAttribute(name);
|
|
69
|
+
}
|
|
70
|
+
/** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */
|
|
71
|
+
setCssVar(name, value) {
|
|
72
|
+
if (value) this.style.setProperty(name, value);
|
|
73
|
+
else this.style.removeProperty(name);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/accessibility-components/focusable/focusable.element.ts
|
|
78
|
+
var FocusableElement = class extends A11yElement {
|
|
79
|
+
static get observedAttributes() {
|
|
80
|
+
return ["aria-label"];
|
|
81
|
+
}
|
|
82
|
+
_warnedEmptyLabel = false;
|
|
83
|
+
onBeforeRender() {
|
|
84
|
+
this.setAttribute("role", "button");
|
|
85
|
+
this.setAttribute("tabindex", "0");
|
|
86
|
+
if (!this._warnedEmptyLabel && !this.getAttribute("aria-label") && !this.textContent?.trim()) {
|
|
87
|
+
this._warnedEmptyLabel = true;
|
|
88
|
+
console.warn("<a11y-focusable> has no accessible name \u2014 add an aria-label or visible text content.");
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Content is whatever the consumer already put inside the tag — this is a
|
|
92
|
+
// same-content round-trip, not a generated template (see VisuallyHiddenElement).
|
|
93
|
+
render() {
|
|
94
|
+
return this.innerHTML;
|
|
95
|
+
}
|
|
96
|
+
onAfterRender() {
|
|
97
|
+
this.listen(this, "keydown", (e) => {
|
|
98
|
+
const ke = e;
|
|
99
|
+
if (ke.key !== "Enter" && ke.key !== " ") return;
|
|
100
|
+
ke.preventDefault();
|
|
101
|
+
this.click();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// src/accessibility-components/focusable/define.ts
|
|
107
|
+
if (!customElements.get("a11y-focusable")) {
|
|
108
|
+
customElements.define("a11y-focusable", FocusableElement);
|
|
109
|
+
}
|
|
110
|
+
export {
|
|
111
|
+
FocusableElement
|
|
112
|
+
};
|
|
113
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/a11y-element.ts","../../../../src/accessibility-components/focusable/focusable.element.ts","../../../../src/accessibility-components/focusable/define.ts"],"sourcesContent":["import type { Raw } from './template.js';\n\ntype Listener = [target: EventTarget, type: string, handler: EventListener, options?: AddEventListenerOptions | boolean];\n\n/**\n * Base class for every element in this library. Light DOM only — elements\n * render real child markup styled by the consumer's own CSS, not a shadow\n * root.\n *\n * Lifecycle mirrors the old `onBeforeRender -> innerHTML = template ->\n * onAfterRender` pipeline this library used to run through a proprietary\n * framework, just mapped onto native custom-element callbacks:\n *\n * connectedCallback -> first update()\n * attributeChangedCallback -> update() for any observed attribute change\n * disconnectedCallback -> remove all tracked listeners, then onDisconnect()\n *\n * Subclasses list attributes to watch via `static observedAttributes` and\n * implement `render()` to return the markup for `this.innerHTML`. Attribute/\n * property setters that need to reflect into the DOM should just mutate the\n * attribute — `attributeChangedCallback` re-renders synchronously while\n * connected, so state and DOM never drift out of sync across a render.\n */\nexport abstract class A11yElement extends HTMLElement {\n protected _connected = false;\n private _listeners: Listener[] = [];\n private _updating = false;\n\n connectedCallback(): void {\n this._connected = true;\n this.update();\n }\n\n disconnectedCallback(): void {\n this._connected = false;\n this._cleanupListeners();\n this.onDisconnect?.();\n }\n\n attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null): void {\n if (oldValue === newValue || !this._connected) return;\n this.update();\n }\n\n protected update(): void {\n // Re-entrancy guard: a listener/hook that itself mutates an observed\n // attribute during render must not trigger a nested render.\n if (this._updating) return;\n this._updating = true;\n try {\n this._cleanupListeners();\n this.onBeforeRender?.();\n this.innerHTML = String(this.render());\n this.onAfterRender?.();\n } finally {\n this._updating = false;\n }\n }\n\n /** Returns the markup to set as `this.innerHTML` on every render — a plain string, or an `html\\`...\\`` result. */\n protected abstract render(): string | Raw;\n\n /** Runs before `render()`. Typical use: set root-level classes/ARIA/CSS custom properties. */\n protected onBeforeRender?(): void;\n\n /** Runs after `render()` replaces `innerHTML`. Typical use: query into fresh markup, wire listeners, set DOM-only properties. */\n protected onAfterRender?(): void;\n\n /** Runs once, after listener cleanup, when the element leaves the DOM. */\n protected onDisconnect?(): void;\n\n /**\n * Adds a listener and tracks it for automatic removal on the next render\n * and on disconnect — the direct replacement for the old framework's\n * `[el, type, handler]` cleanup-array convention.\n */\n protected listen(\n target: EventTarget,\n type: string,\n handler: EventListener,\n options?: AddEventListenerOptions | boolean,\n ): void {\n target.addEventListener(type, handler, options);\n this._listeners.push([target, type, handler, options]);\n }\n\n private _cleanupListeners(): void {\n for (const [target, type, handler, options] of this._listeners) {\n target.removeEventListener(type, handler, options);\n }\n this._listeners = [];\n }\n\n /** Dispatches a bubbling CustomEvent from this element. */\n protected emit<T = unknown>(type: string, detail?: T): boolean {\n return this.dispatchEvent(new CustomEvent<T>(type, { bubbles: true, detail: detail as T }));\n }\n\n // --- small attribute-reflection helpers, shared across components ---\n\n protected stringAttr(name: string, fallback = ''): string {\n return this.getAttribute(name) ?? fallback;\n }\n\n protected optionalStringAttr(name: string): string | undefined {\n return this.getAttribute(name) ?? undefined;\n }\n\n protected boolAttr(name: string): boolean {\n return this.hasAttribute(name);\n }\n\n protected numberAttr(name: string, fallback: number): number {\n const value = this.getAttribute(name);\n if (value == null) return fallback;\n const parsed = Number(value);\n return Number.isNaN(parsed) ? fallback : parsed;\n }\n\n protected setBoolAttr(name: string, value: boolean): void {\n if (value) this.setAttribute(name, '');\n else this.removeAttribute(name);\n }\n\n /** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */\n protected setCssVar(name: string, value: string | null | undefined): void {\n if (value) this.style.setProperty(name, value);\n else this.style.removeProperty(name);\n }\n}\n","import { A11yElement } from '../../core/a11y-element.js';\n\n/**\n * Makes an otherwise-inert element (e.g. a styled `<div>`/`<span>` that\n * can't be a real `<button>` for some layout reason) behave like one:\n * `role=\"button\"`, keyboard-focusable, and Enter/Space-activatable.\n *\n * Prefer a real `<button>` whenever you can — it gets all of this natively,\n * for free, with none of the edge cases custom interactive elements have to\n * re-implement by hand. This is the escape hatch for when you genuinely\n * can't.\n *\n * ```html\n * <a11y-focusable aria-label=\"Expand menu\">\n * <svg aria-hidden=\"true\">…</svg> Menu\n * </a11y-focusable>\n * ```\n *\n * Enter/Space triggers a real synthetic `.click()` rather than a bespoke\n * \"activate\" event/callback — so consumers use the exact same `click`\n * listener (or even an inline `onclick=\"…\"` HTML attribute) regardless of\n * whether the element was activated by mouse or keyboard, matching how a\n * real `<button>` behaves.\n */\nexport class FocusableElement extends A11yElement {\n static get observedAttributes(): string[] {\n return ['aria-label'];\n }\n\n private _warnedEmptyLabel = false;\n\n protected override onBeforeRender(): void {\n this.setAttribute('role', 'button');\n this.setAttribute('tabindex', '0');\n\n if (!this._warnedEmptyLabel && !this.getAttribute('aria-label') && !this.textContent?.trim()) {\n this._warnedEmptyLabel = true;\n console.warn('<a11y-focusable> has no accessible name — add an aria-label or visible text content.');\n }\n }\n\n // Content is whatever the consumer already put inside the tag — this is a\n // same-content round-trip, not a generated template (see VisuallyHiddenElement).\n protected override render(): string {\n return this.innerHTML;\n }\n\n protected override onAfterRender(): void {\n this.listen(this, 'keydown', (e) => {\n const ke = e as KeyboardEvent;\n if (ke.key !== 'Enter' && ke.key !== ' ') return;\n ke.preventDefault();\n this.click();\n });\n }\n}\n","import { FocusableElement } from './focusable.element.js';\n\nif (!customElements.get('a11y-focusable')) {\n customElements.define('a11y-focusable', FocusableElement);\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'a11y-focusable': FocusableElement;\n }\n}\n\nexport { FocusableElement };\n"],"mappings":";AAuBO,IAAe,cAAf,cAAmC,YAAY;AAAA,EAC1C,aAAa;AAAA,EACf,aAAyB,CAAC;AAAA,EAC1B,YAAY;AAAA,EAEpB,oBAA0B;AACxB,SAAK,aAAa;AAClB,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,uBAA6B;AAC3B,SAAK,aAAa;AAClB,SAAK,kBAAkB;AACvB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,yBAAyB,OAAe,UAAyB,UAA+B;AAC9F,QAAI,aAAa,YAAY,CAAC,KAAK,WAAY;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AM,EAAE;AAAA,QAChC,MAAK,gBAAgB,IAAI;AAAA,EAChC;AAAA;AAAA,EAGU,UAAU,MAAc,OAAwC;AACxE,QAAI,MAAO,MAAK,MAAM,YAAY,MAAM,KAAK;AAAA,QACxC,MAAK,MAAM,eAAe,IAAI;AAAA,EACrC;AACF;;;ACzGO,IAAM,mBAAN,cAA+B,YAAY;AAAA,EAChD,WAAW,qBAA+B;AACxC,WAAO,CAAC,YAAY;AAAA,EACtB;AAAA,EAEQ,oBAAoB;AAAA,EAET,iBAAuB;AACxC,SAAK,aAAa,QAAQ,QAAQ;AAClC,SAAK,aAAa,YAAY,GAAG;AAEjC,QAAI,CAAC,KAAK,qBAAqB,CAAC,KAAK,aAAa,YAAY,KAAK,CAAC,KAAK,aAAa,KAAK,GAAG;AAC5F,WAAK,oBAAoB;AACzB,cAAQ,KAAK,2FAAsF;AAAA,IACrG;AAAA,EACF;AAAA;AAAA;AAAA,EAImB,SAAiB;AAClC,WAAO,KAAK;AAAA,EACd;AAAA,EAEmB,gBAAsB;AACvC,SAAK,OAAO,MAAM,WAAW,CAAC,MAAM;AAClC,YAAM,KAAK;AACX,UAAI,GAAG,QAAQ,WAAW,GAAG,QAAQ,IAAK;AAC1C,SAAG,eAAe;AAClB,WAAK,MAAM;AAAA,IACb,CAAC;AAAA,EACH;AACF;;;ACrDA,IAAI,CAAC,eAAe,IAAI,gBAAgB,GAAG;AACzC,iBAAe,OAAO,kBAAkB,gBAAgB;AAC1D;","names":[]}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// src/core/a11y-element.ts
|
|
2
|
+
var A11yElement = class extends HTMLElement {
|
|
3
|
+
_connected = false;
|
|
4
|
+
_listeners = [];
|
|
5
|
+
_updating = false;
|
|
6
|
+
connectedCallback() {
|
|
7
|
+
this._connected = true;
|
|
8
|
+
this.update();
|
|
9
|
+
}
|
|
10
|
+
disconnectedCallback() {
|
|
11
|
+
this._connected = false;
|
|
12
|
+
this._cleanupListeners();
|
|
13
|
+
this.onDisconnect?.();
|
|
14
|
+
}
|
|
15
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
16
|
+
if (oldValue === newValue || !this._connected) return;
|
|
17
|
+
this.update();
|
|
18
|
+
}
|
|
19
|
+
update() {
|
|
20
|
+
if (this._updating) return;
|
|
21
|
+
this._updating = true;
|
|
22
|
+
try {
|
|
23
|
+
this._cleanupListeners();
|
|
24
|
+
this.onBeforeRender?.();
|
|
25
|
+
this.innerHTML = String(this.render());
|
|
26
|
+
this.onAfterRender?.();
|
|
27
|
+
} finally {
|
|
28
|
+
this._updating = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Adds a listener and tracks it for automatic removal on the next render
|
|
33
|
+
* and on disconnect — the direct replacement for the old framework's
|
|
34
|
+
* `[el, type, handler]` cleanup-array convention.
|
|
35
|
+
*/
|
|
36
|
+
listen(target, type, handler, options) {
|
|
37
|
+
target.addEventListener(type, handler, options);
|
|
38
|
+
this._listeners.push([target, type, handler, options]);
|
|
39
|
+
}
|
|
40
|
+
_cleanupListeners() {
|
|
41
|
+
for (const [target, type, handler, options] of this._listeners) {
|
|
42
|
+
target.removeEventListener(type, handler, options);
|
|
43
|
+
}
|
|
44
|
+
this._listeners = [];
|
|
45
|
+
}
|
|
46
|
+
/** Dispatches a bubbling CustomEvent from this element. */
|
|
47
|
+
emit(type, detail) {
|
|
48
|
+
return this.dispatchEvent(new CustomEvent(type, { bubbles: true, detail }));
|
|
49
|
+
}
|
|
50
|
+
// --- small attribute-reflection helpers, shared across components ---
|
|
51
|
+
stringAttr(name, fallback = "") {
|
|
52
|
+
return this.getAttribute(name) ?? fallback;
|
|
53
|
+
}
|
|
54
|
+
optionalStringAttr(name) {
|
|
55
|
+
return this.getAttribute(name) ?? void 0;
|
|
56
|
+
}
|
|
57
|
+
boolAttr(name) {
|
|
58
|
+
return this.hasAttribute(name);
|
|
59
|
+
}
|
|
60
|
+
numberAttr(name, fallback) {
|
|
61
|
+
const value = this.getAttribute(name);
|
|
62
|
+
if (value == null) return fallback;
|
|
63
|
+
const parsed = Number(value);
|
|
64
|
+
return Number.isNaN(parsed) ? fallback : parsed;
|
|
65
|
+
}
|
|
66
|
+
setBoolAttr(name, value) {
|
|
67
|
+
if (value) this.setAttribute(name, "");
|
|
68
|
+
else this.removeAttribute(name);
|
|
69
|
+
}
|
|
70
|
+
/** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */
|
|
71
|
+
setCssVar(name, value) {
|
|
72
|
+
if (value) this.style.setProperty(name, value);
|
|
73
|
+
else this.style.removeProperty(name);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/core/a11y-wrapper-element.ts
|
|
78
|
+
var A11yWrapperElement = class extends A11yElement {
|
|
79
|
+
_observer = null;
|
|
80
|
+
/** Nodes already given their listeners since the last connect — see `wireOnce()`. */
|
|
81
|
+
_wiredNodes = /* @__PURE__ */ new WeakSet();
|
|
82
|
+
connectedCallback() {
|
|
83
|
+
this._connected = true;
|
|
84
|
+
this._sync();
|
|
85
|
+
this._observer = new MutationObserver(() => this._sync());
|
|
86
|
+
this._observer.observe(this, this.observerInit());
|
|
87
|
+
}
|
|
88
|
+
disconnectedCallback() {
|
|
89
|
+
this._observer?.disconnect();
|
|
90
|
+
this._observer = null;
|
|
91
|
+
super.disconnectedCallback();
|
|
92
|
+
this._wiredNodes = /* @__PURE__ */ new WeakSet();
|
|
93
|
+
}
|
|
94
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
95
|
+
if (oldValue === newValue || !this._connected) return;
|
|
96
|
+
this._sync();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Runs `wire` the first time `node` is seen since this element was last
|
|
100
|
+
* connected. `_sync()` runs on every mutation, so listener setup must be
|
|
101
|
+
* idempotent — but those listeners (added via `listen()`) are all removed
|
|
102
|
+
* on disconnect, so the "already wired" memory must reset with them, or a
|
|
103
|
+
* reparented element (disconnect + connect) would silently stop firing.
|
|
104
|
+
*/
|
|
105
|
+
wireOnce(node, wire) {
|
|
106
|
+
if (this._wiredNodes.has(node)) return;
|
|
107
|
+
this._wiredNodes.add(node);
|
|
108
|
+
wire();
|
|
109
|
+
}
|
|
110
|
+
/** What to watch for changes worth re-syncing on. Override for a narrower/wider scope. */
|
|
111
|
+
observerInit() {
|
|
112
|
+
return { childList: true, subtree: true, attributes: true };
|
|
113
|
+
}
|
|
114
|
+
/** Unused — content is the author's own children, never replaced. */
|
|
115
|
+
render() {
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/core/dom-sync.ts
|
|
121
|
+
function syncAttr(el, name, value) {
|
|
122
|
+
if (value == null) {
|
|
123
|
+
if (el.hasAttribute(name)) el.removeAttribute(name);
|
|
124
|
+
} else if (el.getAttribute(name) !== value) {
|
|
125
|
+
el.setAttribute(name, value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function syncClass(el, name, present) {
|
|
129
|
+
if (el.classList.contains(name) !== present) el.classList.toggle(name, present);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/accessibility-components/picture/picture.element.ts
|
|
133
|
+
var PictureElement = class extends A11yWrapperElement {
|
|
134
|
+
observerInit() {
|
|
135
|
+
return { childList: true, subtree: true, attributes: true, attributeFilter: ["alt"] };
|
|
136
|
+
}
|
|
137
|
+
_sync() {
|
|
138
|
+
const img = this.querySelector("img");
|
|
139
|
+
if (!img) return;
|
|
140
|
+
syncClass(this, "a11y-picture", true);
|
|
141
|
+
syncAttr(this, "role", "figure");
|
|
142
|
+
const caption = this.querySelector("figcaption");
|
|
143
|
+
const hasCaption = !!caption?.textContent?.trim();
|
|
144
|
+
const isDecorative = img.getAttribute("alt") === "" && !hasCaption;
|
|
145
|
+
syncAttr(this, "aria-hidden", isDecorative ? "true" : null);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/accessibility-components/picture/define.ts
|
|
150
|
+
if (!customElements.get("a11y-picture")) {
|
|
151
|
+
customElements.define("a11y-picture", PictureElement);
|
|
152
|
+
}
|
|
153
|
+
export {
|
|
154
|
+
PictureElement
|
|
155
|
+
};
|
|
156
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/a11y-element.ts","../../../../src/core/a11y-wrapper-element.ts","../../../../src/core/dom-sync.ts","../../../../src/accessibility-components/picture/picture.element.ts","../../../../src/accessibility-components/picture/define.ts"],"sourcesContent":["import type { Raw } from './template.js';\n\ntype Listener = [target: EventTarget, type: string, handler: EventListener, options?: AddEventListenerOptions | boolean];\n\n/**\n * Base class for every element in this library. Light DOM only — elements\n * render real child markup styled by the consumer's own CSS, not a shadow\n * root.\n *\n * Lifecycle mirrors the old `onBeforeRender -> innerHTML = template ->\n * onAfterRender` pipeline this library used to run through a proprietary\n * framework, just mapped onto native custom-element callbacks:\n *\n * connectedCallback -> first update()\n * attributeChangedCallback -> update() for any observed attribute change\n * disconnectedCallback -> remove all tracked listeners, then onDisconnect()\n *\n * Subclasses list attributes to watch via `static observedAttributes` and\n * implement `render()` to return the markup for `this.innerHTML`. Attribute/\n * property setters that need to reflect into the DOM should just mutate the\n * attribute — `attributeChangedCallback` re-renders synchronously while\n * connected, so state and DOM never drift out of sync across a render.\n */\nexport abstract class A11yElement extends HTMLElement {\n protected _connected = false;\n private _listeners: Listener[] = [];\n private _updating = false;\n\n connectedCallback(): void {\n this._connected = true;\n this.update();\n }\n\n disconnectedCallback(): void {\n this._connected = false;\n this._cleanupListeners();\n this.onDisconnect?.();\n }\n\n attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null): void {\n if (oldValue === newValue || !this._connected) return;\n this.update();\n }\n\n protected update(): void {\n // Re-entrancy guard: a listener/hook that itself mutates an observed\n // attribute during render must not trigger a nested render.\n if (this._updating) return;\n this._updating = true;\n try {\n this._cleanupListeners();\n this.onBeforeRender?.();\n this.innerHTML = String(this.render());\n this.onAfterRender?.();\n } finally {\n this._updating = false;\n }\n }\n\n /** Returns the markup to set as `this.innerHTML` on every render — a plain string, or an `html\\`...\\`` result. */\n protected abstract render(): string | Raw;\n\n /** Runs before `render()`. Typical use: set root-level classes/ARIA/CSS custom properties. */\n protected onBeforeRender?(): void;\n\n /** Runs after `render()` replaces `innerHTML`. Typical use: query into fresh markup, wire listeners, set DOM-only properties. */\n protected onAfterRender?(): void;\n\n /** Runs once, after listener cleanup, when the element leaves the DOM. */\n protected onDisconnect?(): void;\n\n /**\n * Adds a listener and tracks it for automatic removal on the next render\n * and on disconnect — the direct replacement for the old framework's\n * `[el, type, handler]` cleanup-array convention.\n */\n protected listen(\n target: EventTarget,\n type: string,\n handler: EventListener,\n options?: AddEventListenerOptions | boolean,\n ): void {\n target.addEventListener(type, handler, options);\n this._listeners.push([target, type, handler, options]);\n }\n\n private _cleanupListeners(): void {\n for (const [target, type, handler, options] of this._listeners) {\n target.removeEventListener(type, handler, options);\n }\n this._listeners = [];\n }\n\n /** Dispatches a bubbling CustomEvent from this element. */\n protected emit<T = unknown>(type: string, detail?: T): boolean {\n return this.dispatchEvent(new CustomEvent<T>(type, { bubbles: true, detail: detail as T }));\n }\n\n // --- small attribute-reflection helpers, shared across components ---\n\n protected stringAttr(name: string, fallback = ''): string {\n return this.getAttribute(name) ?? fallback;\n }\n\n protected optionalStringAttr(name: string): string | undefined {\n return this.getAttribute(name) ?? undefined;\n }\n\n protected boolAttr(name: string): boolean {\n return this.hasAttribute(name);\n }\n\n protected numberAttr(name: string, fallback: number): number {\n const value = this.getAttribute(name);\n if (value == null) return fallback;\n const parsed = Number(value);\n return Number.isNaN(parsed) ? fallback : parsed;\n }\n\n protected setBoolAttr(name: string, value: boolean): void {\n if (value) this.setAttribute(name, '');\n else this.removeAttribute(name);\n }\n\n /** Sets or removes a CSS custom property on this element depending on truthiness of `value`. */\n protected setCssVar(name: string, value: string | null | undefined): void {\n if (value) this.style.setProperty(name, value);\n else this.style.removeProperty(name);\n }\n}\n","import { A11yElement } from './a11y-element.js';\n\n/**\n * Base for elements that enhance real, consumer-authored light-DOM children\n * instead of generating their own markup from a template string — e.g. a\n * `<a11y-picture>` wrapping a real `<picture>`/`<img>`/`<figcaption>`, or\n * `<a11y-select>` wrapping a real `<select>`/`<option>`s.\n *\n * Unlike `A11yElement`, this never sets `innerHTML` from a `render()`\n * string — that would destroy the author's children — so it opts out of the\n * normal render pipeline and instead re-syncs (`_sync()`) from two sources:\n *\n * - **This element's own observed attributes** (e.g. `label`, `legend`),\n * listed via `static observedAttributes` same as any other element —\n * these re-sync *synchronously*, matching every other element in this\n * library, so `el.setAttribute('label', 'x')` takes effect immediately.\n * - **Everything else** (children added/removed, an attribute changing on\n * one of *those* children) — light DOM has no `slotchange`, so a\n * `MutationObserver` is the equivalent, necessarily asynchronous (a real\n * microtask/task delay after the mutation).\n *\n * `_sync()` runs inside that `MutationObserver` callback, so every DOM write\n * it makes must be guarded (see `core/dom-sync.ts`) — `setAttribute`/\n * `classList.add`/`textContent =` all queue a mutation record even when the\n * value is unchanged, and an unguarded write would retrigger the observer\n * forever.\n */\nexport abstract class A11yWrapperElement extends A11yElement {\n private _observer: MutationObserver | null = null;\n /** Nodes already given their listeners since the last connect — see `wireOnce()`. */\n private _wiredNodes = new WeakSet<Node>();\n\n override connectedCallback(): void {\n this._connected = true;\n this._sync();\n this._observer = new MutationObserver(() => this._sync());\n this._observer.observe(this, this.observerInit());\n }\n\n override disconnectedCallback(): void {\n this._observer?.disconnect();\n this._observer = null;\n super.disconnectedCallback(); // removes every `listen()` listener…\n this._wiredNodes = new WeakSet(); // …so the next connect must wire them again\n }\n\n override attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null): void {\n if (oldValue === newValue || !this._connected) return;\n this._sync();\n }\n\n /**\n * Runs `wire` the first time `node` is seen since this element was last\n * connected. `_sync()` runs on every mutation, so listener setup must be\n * idempotent — but those listeners (added via `listen()`) are all removed\n * on disconnect, so the \"already wired\" memory must reset with them, or a\n * reparented element (disconnect + connect) would silently stop firing.\n */\n protected wireOnce(node: Node, wire: () => void): void {\n if (this._wiredNodes.has(node)) return;\n this._wiredNodes.add(node);\n wire();\n }\n\n /** What to watch for changes worth re-syncing on. Override for a narrower/wider scope. */\n protected observerInit(): MutationObserverInit {\n return { childList: true, subtree: true, attributes: true };\n }\n\n /** Re-derive this element's own attributes/classes from its current children. */\n protected abstract _sync(): void;\n\n /** Unused — content is the author's own children, never replaced. */\n protected override render(): string {\n return '';\n }\n}\n","/**\n * Guarded DOM writes for use inside `A11yWrapperElement._sync()` methods (or\n * anywhere else re-syncing against a live MutationObserver).\n *\n * `setAttribute`/`removeAttribute`/`classList.add`/`classList.remove`/\n * `textContent =` all queue a mutation record unconditionally — even when\n * the value being written is identical to what's already there. Call one of\n * these unguarded inside a MutationObserver callback that's itself watching\n * the element being written to, and every \"no-op\" write retriggers the\n * observer, forever. These helpers check first, so a `_sync()` call that\n * changes nothing produces zero mutations and the observer loop terminates.\n */\n\nexport function syncText(el: Element, text: string): void {\n if (el.textContent !== text) el.textContent = text;\n}\n\nexport function syncAttr(el: Element, name: string, value: string | null | undefined): void {\n if (value == null) {\n if (el.hasAttribute(name)) el.removeAttribute(name);\n } else if (el.getAttribute(name) !== value) {\n el.setAttribute(name, value);\n }\n}\n\nexport function syncClass(el: Element, name: string, present: boolean): void {\n if (el.classList.contains(name) !== present) el.classList.toggle(name, present);\n}\n","import { A11yWrapperElement } from '../../core/a11y-wrapper-element.js';\nimport { syncAttr, syncClass } from '../../core/dom-sync.js';\n\n/**\n * An accessible figure wrapper around a real `<picture>`/`<img>` (and an\n * optional `<figcaption>`) authored directly by the consumer — native HTML\n * already handles responsive source selection via real `<source>` children,\n * so this element's only job is accessibility: it toggles `aria-hidden`\n * automatically when the image is decorative (an empty `alt` and no\n * caption), the same rule the old framework-driven version used.\n *\n * ```html\n * <a11y-picture>\n * <picture>\n * <source srcset=\"hero.webp\" type=\"image/webp\">\n * <img src=\"hero.jpg\" alt=\"Sunset over the Rockies\" loading=\"lazy\" decoding=\"async\">\n * </picture>\n * <figcaption>Sunset over the Rockies</figcaption>\n * </a11y-picture>\n * ```\n */\nexport class PictureElement extends A11yWrapperElement {\n protected override observerInit(): MutationObserverInit {\n return { childList: true, subtree: true, attributes: true, attributeFilter: ['alt'] };\n }\n\n protected override _sync(): void {\n const img = this.querySelector('img');\n if (!img) return;\n\n // Guarded writes: this runs inside a MutationObserver callback watching\n // this subtree, and setAttribute/classList.add queue a mutation record\n // even when the value is unchanged — unguarded, that retriggers the\n // observer forever. The attributeFilter above already keeps this element's\n // own writes (role/class/aria-hidden) out of scope, but staying guarded\n // here too means that isn't load-bearing for correctness.\n syncClass(this, 'a11y-picture', true);\n syncAttr(this, 'role', 'figure');\n\n const caption = this.querySelector('figcaption');\n const hasCaption = !!caption?.textContent?.trim();\n const isDecorative = img.getAttribute('alt') === '' && !hasCaption;\n\n syncAttr(this, 'aria-hidden', isDecorative ? 'true' : null);\n }\n}\n","import { PictureElement } from './picture.element.js';\n\nif (!customElements.get('a11y-picture')) {\n customElements.define('a11y-picture', PictureElement);\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'a11y-picture': PictureElement;\n }\n}\n\nexport { PictureElement };\n"],"mappings":";AAuBO,IAAe,cAAf,cAAmC,YAAY;AAAA,EAC1C,aAAa;AAAA,EACf,aAAyB,CAAC;AAAA,EAC1B,YAAY;AAAA,EAEpB,oBAA0B;AACxB,SAAK,aAAa;AAClB,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,uBAA6B;AAC3B,SAAK,aAAa;AAClB,SAAK,kBAAkB;AACvB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,yBAAyB,OAAe,UAAyB,UAA+B;AAC9F,QAAI,aAAa,YAAY,CAAC,KAAK,WAAY;AAC/C,SAAK,OAAO;AAAA,EACd;AAAA,EAEU,SAAe;AAGvB,QAAI,KAAK,UAAW;AACpB,SAAK,YAAY;AACjB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