contour-ds 1.7.2 → 1.9.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/README.md +244 -326
- package/dist/agent-cookbook.json +960 -0
- package/dist/agent-examples.json +65 -0
- package/dist/agent-map-wiring.json +152 -0
- package/dist/agent-rules.json +16 -0
- package/dist/build-manifest.json +344 -126
- package/dist/contour-wc-content.esm.js +1939 -0
- package/dist/contour-wc-content.esm.min.js +1 -0
- package/dist/contour-wc-core.esm.js +3724 -0
- package/dist/contour-wc-core.esm.min.js +1 -0
- package/dist/contour-wc-dashboard.esm.js +6986 -0
- package/dist/contour-wc-dashboard.esm.min.js +1 -0
- package/dist/contour-wc-data.esm.js +6572 -0
- package/dist/contour-wc-data.esm.min.js +1 -0
- package/dist/contour-wc-docs.esm.js +976 -0
- package/dist/contour-wc-docs.esm.min.js +1 -0
- package/dist/contour-wc-feedback.esm.js +1742 -0
- package/dist/contour-wc-feedback.esm.min.js +1 -0
- package/dist/contour-wc-forms.esm.js +2437 -0
- package/dist/contour-wc-forms.esm.min.js +1 -0
- package/dist/contour-wc-interaction.esm.js +1343 -0
- package/dist/contour-wc-interaction.esm.min.js +1 -0
- package/dist/contour-wc-layout.esm.js +845 -0
- package/dist/contour-wc-layout.esm.min.js +1 -0
- package/dist/contour-wc-map.esm.js +2411 -0
- package/dist/contour-wc-map.esm.min.js +1 -0
- package/dist/contour-wc-misc.esm.js +945 -0
- package/dist/contour-wc-misc.esm.min.js +1 -0
- package/dist/contour-wc-navigation.esm.js +2842 -0
- package/dist/contour-wc-navigation.esm.min.js +1 -0
- package/dist/contour-wc-ui.esm.js +6519 -0
- package/dist/contour-wc-ui.esm.min.js +1 -0
- package/dist/contour-wc.esm.js +14435 -5723
- package/dist/contour-wc.esm.min.js +1 -1
- package/dist/contour-wc.js +14436 -5725
- package/dist/contour-wc.min.js +1 -1
- package/dist/contour.a11y.json +402 -0
- package/dist/contour.code-snippets +4534 -0
- package/dist/contour.components.json +4870 -1222
- package/dist/contour.css +6672 -4711
- package/dist/contour.d.ts +753 -1085
- package/dist/contour.esm.js +440 -18
- package/dist/contour.esm.min.js +1 -1
- package/dist/contour.html-data.json +11343 -0
- package/dist/contour.js +440 -18
- package/dist/contour.min.css +1 -1
- package/dist/contour.min.js +1 -1
- package/dist/contour.react.d.ts +422 -0
- package/dist/contour.single.html +17 -0
- package/dist/contour.single.min.css +1 -0
- package/dist/contour.themes.json +56 -12
- package/dist/contour.tokens.json +1318 -1053
- package/dist/contour.vue.d.ts +420 -0
- package/dist/contour.web-types.json +11703 -0
- package/dist/custom-elements.json +36120 -0
- package/dist/docs/actions.md +16 -6
- package/dist/docs/affordances.md +21 -85
- package/dist/docs/apireference.md +9 -5
- package/dist/docs/colour.md +10 -4
- package/dist/docs/componentindex.md +9 -22
- package/dist/docs/data.md +39 -58
- package/dist/docs/docs.md +12 -7
- package/dist/docs/feedback.md +30 -12
- package/dist/docs/field.md +28 -13
- package/dist/docs/forms.md +35 -19
- package/dist/docs/index.json +70 -61
- package/dist/docs/install.md +12 -7
- package/dist/docs/landing.md +7 -4
- package/dist/docs/mapengine.md +30 -8
- package/dist/docs/motion.md +12 -6
- package/dist/docs/navigation.md +31 -7
- package/dist/docs/overlays.md +32 -25
- package/dist/docs/overview.md +3 -2
- package/dist/docs/patterns.md +20 -10
- package/dist/docs/playground.md +11 -4
- package/dist/docs/principles.md +8 -6
- package/dist/docs/recipes.md +117 -13
- package/dist/docs/space.md +10 -6
- package/dist/docs/status.md +13 -14
- package/dist/docs/surfaces.md +17 -22
- package/dist/docs/theming.md +18 -32
- package/dist/docs/type.md +12 -5
- package/dist/docs/webcomponents.md +75 -33
- package/dist/hljs.css +1 -20
- package/dist/map-chrome.mapbox.css +68 -92
- package/dist/map-chrome.maplibre.css +68 -92
- package/dist/registry/items/contour-core.json +2 -2
- package/dist/registry/items/contour-data.json +2 -2
- package/dist/registry/items/contour-docs.json +2 -2
- package/dist/registry/items/contour-landing.json +2 -2
- package/dist/registry/registry.json +8 -8
- package/dist/surfaces/contour-core.min.css +1 -1
- package/dist/surfaces/contour-data.min.css +1 -1
- package/dist/surfaces/contour-docs.min.css +1 -1
- package/dist/surfaces/contour-landing.min.css +1 -1
- package/dist/svg/icons.svg +367 -0
- package/dist/wc/behaviours.mjs +222 -192
- package/dist/wc/content.mjs +486 -142
- package/dist/wc/core.mjs +917 -395
- package/dist/wc/dashboard.mjs +252 -25
- package/dist/wc/data-chart.mjs +1333 -0
- package/dist/wc/data-export.mjs +391 -0
- package/dist/wc/data-gauge.mjs +779 -0
- package/dist/wc/data-geo.mjs +696 -0
- package/dist/wc/data-glyphs.mjs +133 -0
- package/dist/wc/data-legend.mjs +431 -0
- package/dist/wc/data-readout.mjs +227 -0
- package/dist/wc/data-series.mjs +1004 -0
- package/dist/wc/data-table.mjs +751 -0
- package/dist/wc/data.mjs +15 -3707
- package/dist/wc/docs.mjs +113 -19
- package/dist/wc/feedback.mjs +320 -116
- package/dist/wc/forms-controls.mjs +635 -0
- package/dist/wc/forms-field.mjs +547 -0
- package/dist/wc/forms-groups.mjs +347 -0
- package/dist/wc/forms-input.mjs +292 -0
- package/dist/wc/forms.mjs +10 -1122
- package/dist/wc/glyph.mjs +204 -0
- package/dist/wc/index.mjs +16 -7
- package/dist/wc/interaction.mjs +322 -75
- package/dist/wc/layout.mjs +100 -29
- package/dist/wc/map.mjs +888 -285
- package/dist/wc/misc.mjs +137 -26
- package/dist/wc/navigation-menu.mjs +735 -0
- package/dist/wc/navigation-shell.mjs +495 -0
- package/dist/wc/navigation-tree.mjs +995 -0
- package/dist/wc/navigation.mjs +9 -1636
- package/dist/wc/registration.mjs +1 -8
- package/dist/wc/shadow.mjs +285 -113
- package/dist/wc/states.mjs +75 -78
- package/dist/wc/svg.mjs +209 -34
- package/dist/wc/ui-atoms.mjs +608 -0
- package/dist/wc/ui-data.mjs +1404 -0
- package/dist/wc/ui-feedback.mjs +419 -0
- package/dist/wc/ui-inputs.mjs +1570 -0
- package/dist/wc/ui-menus.mjs +307 -0
- package/dist/wc/ui-overlays.mjs +1605 -0
- package/integrations/README.md +25 -5
- package/integrations/ai/CLAUDE.md +6 -3
- package/integrations/ai/COPILOT.md +14 -0
- package/integrations/ai/GEMINI.md +14 -0
- package/integrations/ai/WINDSURF.md +14 -0
- package/integrations/ai/v0-prompt.md +13 -1
- package/integrations/frameworks/README.md +235 -0
- package/integrations/native/README.md +0 -3
- package/integrations/native/style-dictionary.config.json +1 -1
- package/integrations/tailwind/contour-theme.css +71 -13
- package/integrations/validate-markup.mjs +172 -0
- package/package.json +103 -39
- package/dist/wc/ui.mjs +0 -3560
|
@@ -0,0 +1,2437 @@
|
|
|
1
|
+
import { CLASS_ONLY, CONTOUR_EVENTS, ContourBase, MERCATOR_MAX_LAT, MODS, OBSERVED, REGISTERED, applyLabel, bearing, clampLat, contourId, cssLength, define, flagOn, flagOnValue, formatValue, haversine, headingTag, iconMarkup, isRtl, labelText, levelFromAttributes, mercatorResolution, navKey, normaliseLng, onDispose, safeColor, safeHref, sanitizeHtml, setHTML, simple, statusIcon, supportsUntilFound, unwrapBounds, warnBadJson, wcEmitOverlay, withBrownout, MARKER_SHAPES, arcPath, assignMarkers, barRects, compassNeedle, contourPaths, donutSlices, isMissing, marchingSquares, markerStyles, niceTicks, polylinePoints, scaleBarTicks, smoothPath, modalContain, wcReset, wireCombobox, wireCopy, wireMenu, wirePagination, wireRemovable, wireSegmented, wireTabs, wireTree, STATE_FALLBACK, loadRemote, parseArrayAttr, remoteData, remoteInvalidate, remoteRefresh, remoteVisible, renderEmpty, renderStates, stateLabel, watchViewport, wireStates } from "./contour-wc-core.esm.js";
|
|
2
|
+
|
|
3
|
+
// ---- Registration: class-only wrappers run last (define() skips claimed names) ----
|
|
4
|
+
define("contour-box", simple(["c-box"]));
|
|
5
|
+
define("contour-cluster", simple(["c-cluster"]));
|
|
6
|
+
define("contour-container", simple(["c-container"]));
|
|
7
|
+
define("contour-row", simple(["c-row"]));
|
|
8
|
+
define("contour-scrim", simple(["c-scrim"]));
|
|
9
|
+
define("contour-stack", simple(["c-stack"]));
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// R31-19: the text a shadow <label> shows through its <slot>, read from the
|
|
15
|
+
// light-tree assigned nodes (the shadow label's own textContent is empty).
|
|
16
|
+
const assignedText = (label) => {
|
|
17
|
+
const slot = label.querySelector("slot");
|
|
18
|
+
const nodes = slot ? slot.assignedElements({ flatten: true }) : [];
|
|
19
|
+
const text = nodes.map((n) => (n.textContent || "").trim()).join(" ").trim();
|
|
20
|
+
return text || (label.textContent || "").trim();
|
|
21
|
+
};
|
|
22
|
+
// ---- Shadow DOM wrappers (opt-in) ------------------------------------
|
|
23
|
+
let ADOPTED = null;
|
|
24
|
+
let ADOPTED_KEY = "";
|
|
25
|
+
const _hashCss = (str) => {
|
|
26
|
+
let h = 2166136261;
|
|
27
|
+
for (let i = 0; i < str.length; i++) {
|
|
28
|
+
h ^= str.charCodeAt(i);
|
|
29
|
+
h = Math.imul(h, 16777619);
|
|
30
|
+
}
|
|
31
|
+
return (h >>> 0).toString(36);
|
|
32
|
+
};
|
|
33
|
+
export const adoptedSheets = () => {
|
|
34
|
+
let key = document.styleSheets.length + ":";
|
|
35
|
+
for (const sheet of document.styleSheets) {
|
|
36
|
+
let sig = "-1";
|
|
37
|
+
try {
|
|
38
|
+
const rules = sheet.cssRules;
|
|
39
|
+
let text = "";
|
|
40
|
+
for (let i = 0; i < rules.length; i++) text += rules[i].cssText + "\n";
|
|
41
|
+
sig = rules.length + ":" + _hashCss(text);
|
|
42
|
+
} catch (e) { /* cross-origin: unreadable */ }
|
|
43
|
+
key += (sheet.href || "inline") + "~" + sig + "|";
|
|
44
|
+
}
|
|
45
|
+
if (ADOPTED && key === ADOPTED_KEY) return ADOPTED;
|
|
46
|
+
ADOPTED_KEY = key;
|
|
47
|
+
ADOPTED = [];
|
|
48
|
+
// Collect every rule a sheet carries, descending into @imported sheets,
|
|
49
|
+
const collect = (sheet, out) => {
|
|
50
|
+
let rules;
|
|
51
|
+
try { rules = sheet.cssRules; } catch (e) { return; }
|
|
52
|
+
if (!rules) return;
|
|
53
|
+
for (const r of rules) {
|
|
54
|
+
if (r instanceof CSSImportRule) {
|
|
55
|
+
if (r.styleSheet) collect(r.styleSheet, out);
|
|
56
|
+
} else {
|
|
57
|
+
try { out.push(r.cssText); } catch (e) { /* unreadable */ }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
for (const sheet of document.styleSheets) {
|
|
62
|
+
try {
|
|
63
|
+
// Include same-origin linked sheets AND same-origin inline <style> sheets (!sheet.href).
|
|
64
|
+
if (!sheet.href || new URL(sheet.href).origin === location.origin) {
|
|
65
|
+
const collected = [];
|
|
66
|
+
collect(sheet, collected);
|
|
67
|
+
const built = new CSSStyleSheet();
|
|
68
|
+
if (collected.length) built.replaceSync(collected.join("\n"));
|
|
69
|
+
ADOPTED.push(built);
|
|
70
|
+
}
|
|
71
|
+
} catch (e) { /* cross-origin or unreadable */ }
|
|
72
|
+
}
|
|
73
|
+
return ADOPTED;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// One base for every wrapper: adopt the sheets, clone the subclass template,
|
|
77
|
+
const _ShadowParent = (typeof HTMLElement !== "undefined") ? HTMLElement : Object;
|
|
78
|
+
class ContourShadow extends _ShadowParent {
|
|
79
|
+
// R30-33: observe `labels` alongside the modifier attributes so a labels
|
|
80
|
+
// change re-runs _applyLabels(). applyMods() ignores it (it only maps MODS),
|
|
81
|
+
// so adding it here does not create a bogus modifier class.
|
|
82
|
+
static get observedAttributes() { return MODS.concat("labels"); }
|
|
83
|
+
constructor() {
|
|
84
|
+
super();
|
|
85
|
+
// Reuse a Declarative Shadow Root when the platform already attached one
|
|
86
|
+
// (SSR/DSD hydration); otherwise create and populate the shadow root.
|
|
87
|
+
let root = this.shadowRoot;
|
|
88
|
+
if (!root) {
|
|
89
|
+
root = this.attachShadow({ mode: "open" });
|
|
90
|
+
if (this.constructor.contourTemplate) {
|
|
91
|
+
root.appendChild(this.constructor.contourTemplate.content.cloneNode(true));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const sheets = adoptedSheets();
|
|
95
|
+
if (sheets.length) {
|
|
96
|
+
try { root.adoptedStyleSheets = sheets; } catch (e) { /* unsupported or read-only */ }
|
|
97
|
+
}
|
|
98
|
+
this._shadowBase = root.firstElementChild ? root.firstElementChild.className : "";
|
|
99
|
+
}
|
|
100
|
+
// Subclasses with behaviour beyond the template override this.
|
|
101
|
+
enhance(root) {
|
|
102
|
+
if (!root) root = this.shadowRoot;
|
|
103
|
+
if (!root) return;
|
|
104
|
+
// ARIA setup is idempotent (guards on hasAttribute), so it re-runs on
|
|
105
|
+
root.querySelectorAll("details").forEach((d) => {
|
|
106
|
+
const summary = d.querySelector("summary");
|
|
107
|
+
if (summary && !summary.hasAttribute("aria-expanded")) {
|
|
108
|
+
summary.setAttribute("aria-expanded", String(d.open));
|
|
109
|
+
d.addEventListener("toggle", () => {
|
|
110
|
+
summary.setAttribute("aria-expanded", String(d.open));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
// ARIA: ensure live regions have proper roles
|
|
115
|
+
root.querySelectorAll("[data-live]").forEach((el) => {
|
|
116
|
+
if (!el.hasAttribute("role")) el.setAttribute("role", "status");
|
|
117
|
+
if (!el.hasAttribute("aria-live")) el.setAttribute("aria-live", "polite");
|
|
118
|
+
});
|
|
119
|
+
// ARIA: spinners and toasts get live region roles (opt-in: an author-set role wins)
|
|
120
|
+
root.querySelectorAll(".c-spinner").forEach((el) => {
|
|
121
|
+
if (!el.hasAttribute("role")) el.setAttribute("role", "status");
|
|
122
|
+
if (!el.hasAttribute("aria-live")) el.setAttribute("aria-live", "polite");
|
|
123
|
+
});
|
|
124
|
+
// ARIA: toast elements get live region roles
|
|
125
|
+
root.querySelectorAll(".c-toast, .c-toast-host").forEach((el) => {
|
|
126
|
+
if (!el.hasAttribute("role")) el.setAttribute("role", "status");
|
|
127
|
+
if (!el.hasAttribute("aria-live")) el.setAttribute("aria-live", "polite");
|
|
128
|
+
if (!el.hasAttribute("aria-atomic")) el.setAttribute("aria-atomic", "true");
|
|
129
|
+
});
|
|
130
|
+
// ARIA: link tabs to panels via aria-controls
|
|
131
|
+
root.querySelectorAll("[role='tab']").forEach((tab) => {
|
|
132
|
+
const ctrl = tab.getAttribute("aria-controls");
|
|
133
|
+
if (ctrl) {
|
|
134
|
+
const panel = root.querySelector("#" + CSS.escape(ctrl)) || document.getElementById(ctrl);
|
|
135
|
+
if (panel && !panel.hasAttribute("aria-labelledby")) {
|
|
136
|
+
if (!tab.id) tab.id = contourId("tab");
|
|
137
|
+
panel.setAttribute("aria-labelledby", tab.id);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
// ARIA: link inputs to their labels/hints via aria-describedby
|
|
142
|
+
root.querySelectorAll("input, textarea, select").forEach((input) => {
|
|
143
|
+
if (input.hasAttribute("aria-describedby")) return;
|
|
144
|
+
const field = input.closest(".c-field");
|
|
145
|
+
if (field) {
|
|
146
|
+
const hint = field.querySelector(".c-field__hint");
|
|
147
|
+
if (hint) {
|
|
148
|
+
if (!hint.id) hint.id = contourId("hint");
|
|
149
|
+
input.setAttribute("aria-describedby", hint.id);
|
|
150
|
+
}
|
|
151
|
+
const err = field.querySelector(".c-field__error");
|
|
152
|
+
if (err) {
|
|
153
|
+
if (!err.id) err.id = contourId("err");
|
|
154
|
+
const existing = input.getAttribute("aria-describedby") || "";
|
|
155
|
+
input.setAttribute("aria-describedby", (existing + " " + err.id).trim());
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
// ARIA: alert elements are live regions, and the variant sets the volume.
|
|
160
|
+
root.querySelectorAll(".c-alert").forEach((el) => {
|
|
161
|
+
const urgent = el.classList.contains("c-alert--warning") || el.classList.contains("c-alert--danger");
|
|
162
|
+
const role = urgent ? "alert" : "status";
|
|
163
|
+
const live = urgent ? "assertive" : "polite";
|
|
164
|
+
if (!el.hasAttribute("role") || el.dataset.contourAutoRole) {
|
|
165
|
+
el.setAttribute("role", role);
|
|
166
|
+
el.dataset.contourAutoRole = role;
|
|
167
|
+
}
|
|
168
|
+
if (!el.hasAttribute("aria-live") || el.dataset.contourAutoLive) {
|
|
169
|
+
el.setAttribute("aria-live", live);
|
|
170
|
+
el.dataset.contourAutoLive = live;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
// ARIA: required form fields
|
|
174
|
+
root.querySelectorAll("input[required], textarea[required], select[required]").forEach((el) => {
|
|
175
|
+
if (!el.hasAttribute("aria-required")) el.setAttribute("aria-required", "true");
|
|
176
|
+
});
|
|
177
|
+
// Interaction is delegated to the implementation the light-DOM wrappers
|
|
178
|
+
this._wireLabels(root);
|
|
179
|
+
this._wireClose(root);
|
|
180
|
+
}
|
|
181
|
+
// R31-22: the shadow callout/alert/tag/filter-pill close/remove buttons were
|
|
182
|
+
// inert — a control the reader could focus and never use. Wire them to
|
|
183
|
+
// dismiss the host (hide + emit contour:close/dismiss), once per host.
|
|
184
|
+
_wireClose(root) {
|
|
185
|
+
if (!root || this._wiredShadowClose) return;
|
|
186
|
+
const btns = root.querySelectorAll(".c-callout__close, .c-alert__close, .c-tag__remove, .c-filter-pill__remove");
|
|
187
|
+
if (!btns.length) return;
|
|
188
|
+
this._wiredShadowClose = true;
|
|
189
|
+
for (const b of btns) {
|
|
190
|
+
b.addEventListener("click", () => {
|
|
191
|
+
this.hidden = true;
|
|
192
|
+
this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// R31-03: a shadow <label> wrapping a <slot> does not label a slotted input.
|
|
197
|
+
// The input is in the light tree, the label in the shadow tree, and HTML's
|
|
198
|
+
// label/for association does not cross the boundary. Give the slotted
|
|
199
|
+
// control an id and point the shadow label's `for` at it, and mirror the
|
|
200
|
+
// field hint/error into aria-describedby. Idempotent.
|
|
201
|
+
_wireLabels(root) {
|
|
202
|
+
if (!root) return;
|
|
203
|
+
root.querySelectorAll("label").forEach((label) => {
|
|
204
|
+
const slot = label.querySelector("slot");
|
|
205
|
+
const field = label.closest(".c-field") || label.parentElement;
|
|
206
|
+
const host = this;
|
|
207
|
+
const controls = [];
|
|
208
|
+
if (slot) {
|
|
209
|
+
for (const n of slot.assignedElements({ flatten: true })) {
|
|
210
|
+
if (n.matches("input, textarea, select")) controls.push(n);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// A field's input lives in a named "input" slot, not inside the label.
|
|
214
|
+
if (!controls.length && field) {
|
|
215
|
+
const inputSlot = field.querySelector('slot[name="input"]');
|
|
216
|
+
if (inputSlot) {
|
|
217
|
+
for (const n of inputSlot.assignedElements({ flatten: true })) {
|
|
218
|
+
if (n.matches("input, textarea, select")) controls.push(n);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (const input of controls) {
|
|
223
|
+
if (!input.id) input.id = contourId("shadow-input");
|
|
224
|
+
if (!label.hasAttribute("for")) label.setAttribute("for", input.id);
|
|
225
|
+
// R31-19: `for` does not cross the shadow boundary — the label is in
|
|
226
|
+
// the shadow tree, the input in the light tree, so `label.control`
|
|
227
|
+
// is null and `input.labels` is empty. Give the input an explicit
|
|
228
|
+
// accessible name from the label's slotted text, and forward a label
|
|
229
|
+
// click to the control, so the visible label actually toggles it.
|
|
230
|
+
if (!input.hasAttribute("aria-label") && !input.hasAttribute("aria-labelledby")) {
|
|
231
|
+
const text = assignedText(label);
|
|
232
|
+
if (text) input.setAttribute("aria-label", text);
|
|
233
|
+
}
|
|
234
|
+
if (!input._contourLabelWired) {
|
|
235
|
+
input._contourLabelWired = true;
|
|
236
|
+
label.addEventListener("click", (e) => {
|
|
237
|
+
if (e.target.closest("a, button, input, textarea, select")) return;
|
|
238
|
+
try { input.click(); } catch (err) { /* control without click */ }
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
// Mirror the field hint/error into aria-describedby (once).
|
|
242
|
+
if (field && !input.hasAttribute("aria-describedby")) {
|
|
243
|
+
const hint = field.querySelector(".c-field__hint");
|
|
244
|
+
const err = field.querySelector(".c-field__error");
|
|
245
|
+
const ids = [];
|
|
246
|
+
if (hint) { if (!hint.id) hint.id = contourId("hint"); ids.push(hint.id); }
|
|
247
|
+
if (err) { if (!err.id) err.id = contourId("err"); ids.push(err.id); }
|
|
248
|
+
if (ids.length) input.setAttribute("aria-describedby", ids.join(" "));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
connectedCallback() {
|
|
254
|
+
this.applyMods();
|
|
255
|
+
try { this.enhance(this.shadowRoot); } catch (e) { /* ignore */ }
|
|
256
|
+
this._applyLabels();
|
|
257
|
+
// Wire behaviours now that slotted content is present.
|
|
258
|
+
try {
|
|
259
|
+
wcReset(this);
|
|
260
|
+
wireTabs(this); wireSegmented(this); wireCombobox(this); wireTree(this);
|
|
261
|
+
wireMenu(this); wirePagination(this); wireCopy(this); wireRemovable(this);
|
|
262
|
+
} catch (e) { /* ignore */ }
|
|
263
|
+
// Re-wire behaviour when slotted content changes. Reset the wired set so
|
|
264
|
+
if (this.shadowRoot && !this._wiredSlotchange) {
|
|
265
|
+
this._wiredSlotchange = true;
|
|
266
|
+
this.shadowRoot.addEventListener("slotchange", () => {
|
|
267
|
+
try { this.enhance(this.shadowRoot);
|
|
268
|
+
this._applyLabels();
|
|
269
|
+
wireTabs(this); wireSegmented(this); wireCombobox(this); wireTree(this);
|
|
270
|
+
wireMenu(this); wirePagination(this); wireCopy(this); wireRemovable(this);
|
|
271
|
+
} catch (e) { /* ignore */ }
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
attributeChangedCallback() { this.applyMods(); this._applyLabels(); }
|
|
276
|
+
// R30-33: localise the static aria-labels/copy baked into shadow templates.
|
|
277
|
+
// The light-DOM sweep (R30-13) routes literals through labelText(); the
|
|
278
|
+
// shadow layer has no such path because its markup is a static template
|
|
279
|
+
// string. This post-processes the rendered shadowRoot, replacing the
|
|
280
|
+
// English defaults with the host's `labels` overrides where present. It is
|
|
281
|
+
// idempotent and only touches nodes that still carry the default text, so
|
|
282
|
+
// an author's own shadow content (slotted) is never overwritten.
|
|
283
|
+
_applyLabels() {
|
|
284
|
+
const root = this.shadowRoot;
|
|
285
|
+
if (!root) return;
|
|
286
|
+
const set = (selector, key, fallback) => {
|
|
287
|
+
const node = root.querySelector(selector);
|
|
288
|
+
if (node) node.setAttribute("aria-label", labelText(this, key, fallback));
|
|
289
|
+
};
|
|
290
|
+
set(".c-callout__close", "close", "Close");
|
|
291
|
+
set(".c-alert__close", "close", "Close");
|
|
292
|
+
set(".c-tag__remove", "remove", "Remove");
|
|
293
|
+
set(".c-filter-pill__remove", "remove-filter", "Remove filter");
|
|
294
|
+
set(".c-breadcrumb", "breadcrumb", "Breadcrumb");
|
|
295
|
+
set(".c-spinner", "loading", "Loading");
|
|
296
|
+
set(".c-install__copy", "copy-command", "Copy command");
|
|
297
|
+
// The install/code copy buttons carry visible "Copy" text as well as an
|
|
298
|
+
// aria-label; localise both, but only while they still read "Copy" so a
|
|
299
|
+
// consumer who set their own label keeps it.
|
|
300
|
+
const copyBtn = root.querySelector(".c-install__copy, .c-code__copy");
|
|
301
|
+
if (copyBtn && copyBtn.textContent.trim() === "Copy") {
|
|
302
|
+
copyBtn.textContent = labelText(this, "copy", "Copy");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
applyMods() {
|
|
306
|
+
const el = this.shadowRoot.firstElementChild;
|
|
307
|
+
if (!el || !this._shadowBase) return;
|
|
308
|
+
const mods = [];
|
|
309
|
+
for (const attr of MODS) {
|
|
310
|
+
const val = this.getAttribute(attr);
|
|
311
|
+
if (!val) continue;
|
|
312
|
+
for (const cls of this._shadowBase.split(/\s+/)) {
|
|
313
|
+
if (cls) mods.push("c-" + cls.replace(/^c-/, "") + "--" + val);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
el.className = (this._shadowBase + " " + mods.join(" ")).trim();
|
|
317
|
+
// An alert's live region follows its variant, and rewriting the class above
|
|
318
|
+
if (el.classList.contains("c-alert")) {
|
|
319
|
+
const urgent = el.classList.contains("c-alert--warning") || el.classList.contains("c-alert--danger");
|
|
320
|
+
const role = urgent ? "alert" : "status";
|
|
321
|
+
const live = urgent ? "assertive" : "polite";
|
|
322
|
+
if (!el.hasAttribute("role") || el.dataset.contourAutoRole) {
|
|
323
|
+
el.setAttribute("role", role);
|
|
324
|
+
el.dataset.contourAutoRole = role;
|
|
325
|
+
}
|
|
326
|
+
if (!el.hasAttribute("aria-live") || el.dataset.contourAutoLive) {
|
|
327
|
+
el.setAttribute("aria-live", live);
|
|
328
|
+
el.dataset.contourAutoLive = live;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Registers a wrapper. The template markup is always recorded so Node/SSR
|
|
335
|
+
const SHADOW_PARTS_PUBLIC = {};
|
|
336
|
+
const SHADOW_MARKUP = {};
|
|
337
|
+
const recordParts = (name, markup) => {
|
|
338
|
+
SHADOW_PARTS_PUBLIC[name] = [...markup.matchAll(/part="([^"]+)"/g)]
|
|
339
|
+
.flatMap((m) => m[1].split(/\s+/))
|
|
340
|
+
.filter(Boolean);
|
|
341
|
+
};
|
|
342
|
+
const shadow = (name, markup) => {
|
|
343
|
+
SHADOW_MARKUP[name] = markup;
|
|
344
|
+
recordParts(name, markup);
|
|
345
|
+
if (typeof document === "undefined") return;
|
|
346
|
+
const tpl = document.createElement("template");
|
|
347
|
+
setHTML(tpl, markup);
|
|
348
|
+
const cls = class extends ContourShadow {};
|
|
349
|
+
cls.contourTemplate = tpl;
|
|
350
|
+
define(name, cls);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// A wrapper that is one default slot needs no markup of its own: the element,
|
|
354
|
+
const slotWrapper = (tag, block, part) =>
|
|
355
|
+
"<" + tag + ' class="c-' + block + '" part="' + (part || block) + '"' + (tag === "button" ? ' type="button"' : "") + "><slot></slot></" + tag + ">";
|
|
356
|
+
const SHADOW_SLOTS = {
|
|
357
|
+
div: [
|
|
358
|
+
"accordion", "activity", "advanced", "affordance", "affordances", "anchor", "app", "app-body", "box", "btn-group", "chart-grid",
|
|
359
|
+
"catchment", "cluster", "command", "compare", "comparison", "compass",
|
|
360
|
+
"contour-plot", "cta", "dash-freshness", "dash-kpi-row", "dash-shell", "dash-toolbar", "datamark", "delta", "draggable", "drag-handle", "drop-indicator", "drop-zone",
|
|
361
|
+
"destructive-hint", "docs-shell", "drawer", "elevation",
|
|
362
|
+
"filter-bar", "flow-map", "heatmap", "hexbin", "icon-badge", "input-group", "instrument", "range-scale", "resizable", "table-wrap", "time-range", "toast-host", "scroll-area",
|
|
363
|
+
"isochrone", "landing-features", "landing-hero", "layer-panel", "layer-switch", "legend", "logos", "map-area",
|
|
364
|
+
"map-footer", "map-panel", "map-slot", "measure", "metrics", "minimap", "pager", "pagination",
|
|
365
|
+
"panel", "plate", "price", "pricing", "progress", "prose", "pullquote", "radio-list",
|
|
366
|
+
"range", "readout", "recipe", "reserved", "resize-handle", "row",
|
|
367
|
+
"row-action", "scale-bar", "scrim", "segmented", "sheet", "shortcut-list", "shortcut-overlay", "shortcut-panel",
|
|
368
|
+
"shortcut-row", "showcase", "sidebar", "skeleton", "sparkline", "stack", "steps", "streaming-chart",
|
|
369
|
+
"survey", "theme-scope", "time-slider", "timeline", "toast",
|
|
370
|
+
"toolbar", "tooltip", "tree", "version", "version-banner"
|
|
371
|
+
],
|
|
372
|
+
button: [
|
|
373
|
+
"btn", "pressable"
|
|
374
|
+
],
|
|
375
|
+
span: [
|
|
376
|
+
"avatar", "badge", "chip", "dot", "flag", "live-badge", "mark", "pill"
|
|
377
|
+
],
|
|
378
|
+
a: [
|
|
379
|
+
"link", "linkish"
|
|
380
|
+
],
|
|
381
|
+
kbd: [
|
|
382
|
+
"kbd"
|
|
383
|
+
],
|
|
384
|
+
label: [
|
|
385
|
+
"check", "radio", "switch"
|
|
386
|
+
],
|
|
387
|
+
nav: [
|
|
388
|
+
"docs-nav"
|
|
389
|
+
],
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// Two wrappers name their part after the region it holds rather than after
|
|
393
|
+
const SHADOW_PARTS = {
|
|
394
|
+
"version-banner": "banner",
|
|
395
|
+
"contour-plot": "plot",
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// The slot wrappers are recorded wherever this module runs. `shadow()` already
|
|
399
|
+
for (const [tag, blocks] of Object.entries(SHADOW_SLOTS)) {
|
|
400
|
+
for (const block of blocks) {
|
|
401
|
+
const part = SHADOW_PARTS[block] || block;
|
|
402
|
+
shadow("contour-shadow-" + block, slotWrapper(tag, block, part));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Structured templates: more than one slot, or a class that is not the block's
|
|
407
|
+
// own, so the markup is the definition and is written out.
|
|
408
|
+
shadow("contour-shadow-callout", `<div class="c-callout" part="callout"><div class="c-callout__title" part="title"><slot name="title"></slot></div><div class="c-callout__body" part="body"><slot></slot></div><button class="c-callout__close" part="close" type="button" aria-label="Close">×</button></div>`);
|
|
409
|
+
shadow("contour-shadow-empty", `<div class="c-empty" part="empty"><div class="c-empty__illustration" part="illustration"><slot name="illustration"></slot></div><div class="c-empty__title" part="title"><slot name="title"></slot></div><div class="c-empty__text" part="body"><slot></slot></div><div><slot name="action"></slot></div></div>`);
|
|
410
|
+
shadow("contour-shadow-feature", `<div class="c-feature" part="feature"><span class="c-feature__index" part="index"><slot name="index"></slot></span><div class="c-feature__title" part="title"><slot name="title"></slot></div><div class="c-feature__body" part="body"><slot></slot></div></div>`);
|
|
411
|
+
shadow("contour-shadow-kpi", `<div class="c-kpi" part="kpi"><div class="c-kpi__value" part="value"><slot name="value"></slot></div><div class="c-kpi__label" part="label"><slot name="label"></slot></div><div class="c-kpi__foot" part="foot"><slot name="delta"></slot><slot name="note"></slot></div></div>`);
|
|
412
|
+
shadow("contour-shadow-stat", `<div class="c-stat" part="stat"><div class="c-stat__value" part="value"><slot name="value"></slot></div><div class="c-stat__label" part="label"><slot name="label"></slot></div><div class="c-stat__delta" part="delta"><slot name="delta"></slot></div></div>`);
|
|
413
|
+
shadow("contour-shadow-meter", `<div class="c-meter" part="meter"><span class="c-meter__core" part="core"><span class="c-meter__value" part="value"><slot name="value"></slot></span><span class="c-meter__label" part="label"><slot name="label"></slot></span></span></div>`);
|
|
414
|
+
shadow("contour-shadow-brownout", `<div class="c-brownout" part="brownout" data-level="0"><slot></slot></div>`);
|
|
415
|
+
shadow("contour-shadow-widget-state", `<div class="c-widget-state" part="widget-state"><slot></slot></div>`);
|
|
416
|
+
shadow("contour-shadow-donut", `<div class="c-donut" part="donut"><svg class="c-donut__ring" part="ring" viewBox="0 0 32 32"></svg><div class="c-donut__center" part="center"><slot name="value"></slot></div><div class="c-donut__list" part="list"><slot></slot></div></div>`);
|
|
417
|
+
shadow("contour-shadow-axis", `<div class="c-axis" part="axis"><slot></slot></div>`);
|
|
418
|
+
shadow("contour-shadow-brush", `<div class="c-brush" part="brush"><div class="c-brush__selection" part="selection"></div></div>`);
|
|
419
|
+
shadow("contour-shadow-stacked-bar", `<div class="c-stacked-bar" part="bar"><slot></slot></div>`);
|
|
420
|
+
shadow("contour-shadow-category", `<div class="c-category" part="category" role="listbox"><slot></slot></div>`);
|
|
421
|
+
shadow("contour-shadow-gauge", `<div class="c-gauge" part="gauge"><svg class="c-gauge__dial" part="dial" aria-hidden="true" viewBox="0 0 100 100"><path class="c-gauge__track" part="track" d="M 10 90 A 40 40 0 0 1 90 90"></path><g class="c-gauge__zones"></g><line class="c-gauge__needle" part="needle" x1="50" y1="50" x2="50" y2="15"></line><circle class="c-gauge__hub" part="hub" cx="50" cy="50" r="4"></circle></svg><div class="c-gauge__value" part="value"><slot name="value"></slot></div><div class="c-gauge__band" part="band"><slot name="band"></slot></div></div>`);
|
|
422
|
+
shadow("contour-shadow-diff", `<div class="c-diff" part="diff"><div class="c-diff__line c-diff__line--del" part="before"><span class="c-diff__sign">−</span><span><slot name="before"></slot></span></div><div class="c-diff__line c-diff__line--add" part="after"><span class="c-diff__sign">+</span><span><slot name="after"></slot></span></div></div>`);
|
|
423
|
+
shadow("contour-shadow-do-dont", `<div class="c-do-dont" part="do-dont"><div class="c-do-dont__item c-do-dont__item--do" part="do"><slot name="do"></slot></div><div class="c-do-dont__item c-do-dont__item--dont" part="dont"><slot name="dont"></slot></div></div>`);
|
|
424
|
+
shadow("contour-shadow-threshold", `<div class="c-threshold" part="threshold"><span class="c-threshold__fill" part="value"></span><span class="c-threshold__target" part="target"></span></div>`);
|
|
425
|
+
shadow("contour-shadow-coordinate", `<div class="c-coordinate" part="coordinate"><span class="c-coordinate__value" part="value"><slot name="value"></slot></span></div>`);
|
|
426
|
+
shadow("contour-shadow-install", `<div class="c-install" part="install"><span class="c-install__prompt" part="prompt"><slot name="prompt"></slot></span><span class="c-install__command" part="command"><slot name="command"></slot></span><button class="c-btn c-btn--ghost c-btn--sm c-install__copy" part="copy" type="button" aria-label="Copy command">Copy</button></div>`);
|
|
427
|
+
shadow("contour-shadow-toc", `<div class="c-toc" part="toc"><div class="c-toc__title" part="title"><slot name="title"></slot></div><nav class="c-toc__list" part="list"><slot></slot></nav></div>`);
|
|
428
|
+
shadow("contour-shadow-testimonial", `<div class="c-testimonial" part="testimonial"><blockquote class="c-testimonial__quote" part="quote"><slot name="quote"></slot></blockquote><div class="c-testimonial__byline" part="byline"><span class="c-testimonial__name" part="name"><slot name="name"></slot></span><span class="c-testimonial__role" part="role"><slot name="role"></slot></span></div></div>`);
|
|
429
|
+
shadow("contour-shadow-search-trigger", `<button class="c-search-trigger" part="trigger"><span><slot name="label"></slot></span><kbd class="c-search-trigger__kbd"><slot name="shortcut"></slot></kbd></button>`);
|
|
430
|
+
shadow("contour-shadow-brand", `<a class="c-brand" part="brand"><span class="c-mark" part="mark"><slot name="mark"></slot></span><span class="c-brand__name" part="name"><slot name="name"></slot></span></a>`);
|
|
431
|
+
shadow("contour-shadow-section-head", `<div class="c-section-head" part="head"><h2 class="c-section-head__title" part="title"><slot name="title"></slot></h2><p class="c-section-head__desc" part="desc"><slot name="desc"></slot></p></div>`);
|
|
432
|
+
shadow("contour-shadow-loading", `<div class="c-loading" part="loading"><span class="c-spinner" part="spinner"></span><slot></slot></div>`);
|
|
433
|
+
// Shadow tabs are a styling shell: panels are authored by the consumer
|
|
434
|
+
shadow("contour-shadow-tabs", `<div class="c-tabs" part="tabs"><div class="c-tabs__list" part="list" role="tablist"><slot name="tabs"></slot></div><div class="c-tabs__panels" role="presentation"><slot></slot></div></div>`);
|
|
435
|
+
shadow("contour-shadow-breadcrumb", `<nav class="c-breadcrumb" part="breadcrumb" aria-label="Breadcrumb"><ol><slot></slot></ol></nav>`);
|
|
436
|
+
shadow("contour-shadow-menu", `<div class="c-menu" part="menu" role="menu"><slot></slot></div>`);
|
|
437
|
+
shadow("contour-shadow-combobox", `<div class="c-combobox" part="combobox"><input class="c-combobox__input" part="input" type="text" /><ul class="c-combobox__list" part="list" hidden><slot></slot></ul></div>`);
|
|
438
|
+
shadow("contour-shadow-chart", `<div class="c-chart" part="chart"><div class="c-chart__plot" part="plot"><slot></slot></div></div>`);
|
|
439
|
+
shadow("contour-shadow-bivariate", `<div class="c-bivariate" part="bivariate"><div class="c-bivariate__matrix" part="matrix"><slot></slot></div></div>`);
|
|
440
|
+
shadow("contour-shadow-bivariate-legend", `<div class="c-bivariate-legend" part="legend"><div class="c-bivariate-legend__matrix" part="matrix"><slot></slot></div></div>`);
|
|
441
|
+
shadow("contour-shadow-expandable", `<details class="c-expandable" part="expandable"><summary class="c-expandable__summary"><slot name="summary"></slot></summary><div class="c-expandable__body" part="body"><slot></slot></div></details>`);
|
|
442
|
+
shadow("contour-shadow-disclosure", `<details class="c-disclosure" part="disclosure"><summary><slot name="summary"></slot></summary><div class="c-disclosure__body" part="body"><slot></slot></div></details>`);
|
|
443
|
+
shadow("contour-shadow-faq", `<div class="c-faq" part="faq"><div class="c-faq__question" part="question"><slot name="question"></slot></div><div class="c-faq__answer" part="answer"><slot name="answer"></slot></div></div>`);
|
|
444
|
+
shadow("contour-shadow-code-tabs", `<div class="c-code-tabs" part="code-tabs"><div class="c-code-tabs__list" part="list" role="tablist"><slot name="tabs"></slot></div><div class="c-code-tabs__panels"><slot></slot></div></div>`);
|
|
445
|
+
shadow("contour-shadow-header", `<header class="c-header" part="header"><div class="container c-header__inner" part="inner"><a class="c-brand" part="brand"><slot name="brand"></slot></a><nav class="c-header__nav" part="nav"><slot name="nav"></slot></nav><div class="c-header__actions" part="actions"><slot name="actions"></slot></div></div></header>`);
|
|
446
|
+
shadow("contour-shadow-footer", `<footer class="c-footer" part="footer"><div class="container c-footer__grid" part="grid"><div class="c-footer__brand" part="brand"><slot name="brand"></slot></div><div class="c-footer__links" part="links"><slot name="links"></slot></div></div></footer>`);
|
|
447
|
+
shadow("contour-shadow-container", `<div class="container" part="container"><slot></slot></div>`);
|
|
448
|
+
// R31-03: contour-shadow-input / -textarea / -select / -table / -scheme-toggle
|
|
449
|
+
// were removed. Each was a control shell that could not hold the state its
|
|
450
|
+
// light twin holds (no value, no options, no rows, no toggle), so it was a
|
|
451
|
+
// trap rather than a styling seam. Use the light <contour-input>,
|
|
452
|
+
// <contour-select>, <contour-table> and <contour-scheme-toggle>, or style a
|
|
453
|
+
// native control with the `.c-input` / `.c-table` classes directly.
|
|
454
|
+
shadow("contour-shadow-spinner", `<span class="c-spinner" part="spinner" aria-label="Loading"></span>`);
|
|
455
|
+
shadow("contour-shadow-code", `<pre class="c-code" part="code"><code><slot></slot></code><button class="c-code__copy" part="copy" type="button">Copy</button></pre>`);
|
|
456
|
+
// (folded in from the hand-written component that used to carry its own copy of the sheet logic)
|
|
457
|
+
shadow("contour-shadow-card", `<div class="c-card" part="card"> <div class="c-card__title" part="head"><slot name="title">Untitled</slot></div> <div class="c-card__body" part="body"><slot></slot></div> <div class="c-card__foot" part="foot"><slot name="foot"></slot></div> </div>`);
|
|
458
|
+
// (folded in from the hand-written component that used to carry its own copy of the sheet logic)
|
|
459
|
+
// (folded in from the hand-written component that used to carry its own copy of the sheet logic)
|
|
460
|
+
shadow("contour-shadow-field", `<div class="c-field" part="field"> <label class="c-field__label" part="label"><slot name="label">Label</slot></label> <slot name="input"></slot> <span class="c-field__hint" part="hint"><slot name="hint"></slot></span> </div>`);
|
|
461
|
+
// (folded in from the hand-written component that used to carry its own copy of the sheet logic)
|
|
462
|
+
shadow("contour-shadow-alert", `<div class="c-alert" part="alert"> <div class="c-alert__title" part="title"><slot name="title"></slot></div> <div class="c-alert__body" part="body"><slot></slot></div> <div class="c-alert__actions" part="actions"><slot name="action"></slot></div> <button class="c-alert__close" part="close" type="button" aria-label="Close">×</button> </div>`);
|
|
463
|
+
shadow("contour-shadow-tag", `<span class="c-tag" part="tag"><slot></slot><button class="c-tag__remove" part="remove" type="button" aria-label="Remove">×</button></span>`);
|
|
464
|
+
shadow("contour-shadow-filter-pill", `<span class="c-filter-pill" part="filter-pill"><slot></slot><button class="c-filter-pill__remove" part="remove" type="button" aria-label="Remove filter">×</button></span>`);
|
|
465
|
+
|
|
466
|
+
// The dialog is the one wrapper with behaviour, because a close button that
|
|
467
|
+
// does not close is a control the reader cannot use.
|
|
468
|
+
const DIALOG_MARKUP = "<dialog class=\"c-dialog\" part=\"dialog\"> <div class=\"c-dialog__head\" part=\"head\"> <slot name=\"title\"></slot> <button class=\"c-btn c-btn--icon c-btn--ghost\" part=\"close\" type=\"button\" aria-label=\"Close\"> <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\"><path d=\"m18 6-12 12M6 6l12 12\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.7\" stroke-linecap=\"round\"/></svg> </button> </div> <div class=\"c-dialog__body\" part=\"body\"><slot></slot></div> <div class=\"c-dialog__foot\" part=\"foot\"><slot name=\"foot\"></slot></div> </dialog>";
|
|
469
|
+
SHADOW_MARKUP["contour-shadow-dialog"] = DIALOG_MARKUP;
|
|
470
|
+
recordParts("contour-shadow-dialog", DIALOG_MARKUP);
|
|
471
|
+
class ContourShadowDialog extends ContourShadow {
|
|
472
|
+
static contourTemplate = (typeof document !== "undefined") ? (() => {
|
|
473
|
+
const tpl = document.createElement("template");
|
|
474
|
+
setHTML(tpl, DIALOG_MARKUP);
|
|
475
|
+
return tpl;
|
|
476
|
+
})() : null;
|
|
477
|
+
enhance(root) {
|
|
478
|
+
super.enhance(root);
|
|
479
|
+
// The base constructor AND connectedCallback both call enhance; the base's
|
|
480
|
+
if (this._wiredShadowDialog) return;
|
|
481
|
+
this._wiredShadowDialog = true;
|
|
482
|
+
const dlg = root.querySelector("dialog");
|
|
483
|
+
const close = root.querySelector('[part="close"]');
|
|
484
|
+
if (close) close.addEventListener("click", () => dlg?.close());
|
|
485
|
+
if (dlg) {
|
|
486
|
+
let lastFocus = null;
|
|
487
|
+
// `<dialog>` fires no `show` or `close` events in this engine (measured
|
|
488
|
+
dlg.addEventListener("beforetoggle", (e) => {
|
|
489
|
+
if (e.newState === "open") {
|
|
490
|
+
lastFocus = document.activeElement;
|
|
491
|
+
// beforetoggle fires BEFORE dialog.open flips, so the attribute
|
|
492
|
+
// write must not re-enter attributeChangedCallback's show() path.
|
|
493
|
+
this._syncingOpenAttr = true;
|
|
494
|
+
if (!this.hasAttribute("open")) this.setAttribute("open", "");
|
|
495
|
+
this._syncingOpenAttr = false;
|
|
496
|
+
this.dispatchEvent(new CustomEvent("contour:open", { bubbles: true, composed: true }));
|
|
497
|
+
// Focus the close button or first focusable element
|
|
498
|
+
const focusable = dlg.querySelector("button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])");
|
|
499
|
+
if (focusable) try { focusable.focus(); } catch (e) {}
|
|
500
|
+
} else if (e.newState === "closed") {
|
|
501
|
+
this._syncingOpenAttr = true;
|
|
502
|
+
if (this.hasAttribute("open")) this.removeAttribute("open");
|
|
503
|
+
this._syncingOpenAttr = false;
|
|
504
|
+
this.dispatchEvent(new CustomEvent("contour:close", { bubbles: true, composed: true }));
|
|
505
|
+
// Restore focus
|
|
506
|
+
if (lastFocus && typeof lastFocus.focus === "function") {
|
|
507
|
+
try { lastFocus.focus(); } catch (e) {}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
// Focus trap: keep Tab within the dialog. R31-04: the focusables are not
|
|
512
|
+
// only in the shadow tree — slotted light-DOM controls (the dialog body
|
|
513
|
+
// is a <slot>) are real tab stops too, and `document.activeElement` is
|
|
514
|
+
// the shadow host, not the control. Collect both, and read the
|
|
515
|
+
// shadow-aware active element.
|
|
516
|
+
const collectFocusable = () => {
|
|
517
|
+
const sel = "button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])";
|
|
518
|
+
const out = Array.from(dlg.querySelectorAll(sel));
|
|
519
|
+
for (const slot of dlg.querySelectorAll("slot")) {
|
|
520
|
+
for (const n of slot.assignedElements({ flatten: true })) {
|
|
521
|
+
if (n.matches(sel)) out.push(n);
|
|
522
|
+
for (const sub of n.querySelectorAll(sel)) out.push(sub);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return out;
|
|
526
|
+
};
|
|
527
|
+
const activeEl = () => (this.shadowRoot && this.shadowRoot.activeElement) || document.activeElement;
|
|
528
|
+
dlg.addEventListener("keydown", (e) => {
|
|
529
|
+
if (e.key !== "Tab") return;
|
|
530
|
+
const focusables = collectFocusable();
|
|
531
|
+
if (!focusables.length) return;
|
|
532
|
+
const first = focusables[0];
|
|
533
|
+
const last = focusables[focusables.length - 1];
|
|
534
|
+
if (e.shiftKey && activeEl() === first) {
|
|
535
|
+
e.preventDefault();
|
|
536
|
+
last.focus();
|
|
537
|
+
} else if (!e.shiftKey && activeEl() === last) {
|
|
538
|
+
e.preventDefault();
|
|
539
|
+
first.focus();
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
static get observedAttributes() { return [...super.observedAttributes, "open"]; }
|
|
545
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
546
|
+
super.attributeChangedCallback(name, oldValue, newValue);
|
|
547
|
+
// The host's `open` attribute drives the inner native dialog, matching
|
|
548
|
+
// the light family; without this the attribute was inert (R25-12).
|
|
549
|
+
if (name === "open" && this.shadowRoot && !this._syncingOpenAttr) {
|
|
550
|
+
const dlg = this.shadowRoot.querySelector("dialog");
|
|
551
|
+
if (dlg) {
|
|
552
|
+
if (newValue !== null && !dlg.open) this.show();
|
|
553
|
+
else if (newValue === null && dlg.open) this.close();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
connectedCallback() {
|
|
558
|
+
super.connectedCallback();
|
|
559
|
+
// R31-33: an `open` attribute set before the element was connected (or a
|
|
560
|
+
// show() called while disconnected) must engage the dialog once it lands
|
|
561
|
+
// in the document; showModal() on a disconnected dialog throws.
|
|
562
|
+
if (this.hasAttribute("open")) this.show();
|
|
563
|
+
}
|
|
564
|
+
show() {
|
|
565
|
+
// R31-33: showModal() throws on a disconnected element. If not yet
|
|
566
|
+
// connected, leave the `open` attribute set; connectedCallback applies it.
|
|
567
|
+
if (!this.isConnected) { if (!this.hasAttribute("open")) this.setAttribute("open", ""); return; }
|
|
568
|
+
const d = this.shadowRoot?.querySelector("dialog");
|
|
569
|
+
if (d && !d.open) d.showModal();
|
|
570
|
+
}
|
|
571
|
+
close() { const d = this.shadowRoot?.querySelector("dialog"); if (d && d.open) d.close(); }
|
|
572
|
+
// Native-dialog parity with the light family (R20 T-32): showModal() is
|
|
573
|
+
showModal() { this.show(); }
|
|
574
|
+
get open() { return !!this.shadowRoot?.querySelector("dialog")?.open; }
|
|
575
|
+
set open(v) { if (v) this.show(); else this.close(); }
|
|
576
|
+
get returnValue() { return this.shadowRoot?.querySelector("dialog")?.returnValue ?? ""; }
|
|
577
|
+
set returnValue(v) { const d = this.shadowRoot?.querySelector("dialog"); if (d) d.returnValue = String(v); }
|
|
578
|
+
}
|
|
579
|
+
define("contour-shadow-dialog", ContourShadowDialog);
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Render a Declarative Shadow DOM string for a registered contour-shadow-* element.
|
|
583
|
+
* Intended for SSR/string emission; returns null for unknown tags.
|
|
584
|
+
*/
|
|
585
|
+
const renderShadowDSD = (tag, attrs = "", light = "") => {
|
|
586
|
+
const markup = SHADOW_MARKUP[tag];
|
|
587
|
+
if (typeof markup !== "string") return null;
|
|
588
|
+
const attrStr = attrs ? (attrs.startsWith(" ") ? attrs : " " + attrs) : "";
|
|
589
|
+
return `<${tag}${attrStr}><template shadowrootmode="open">${markup}</template>${light}</${tag}>`;
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
// Published once, at the end, so the array holds what the browser was actually
|
|
593
|
+
const FROZEN_SHADOW_MARKUP = Object.freeze({ ...SHADOW_MARKUP });
|
|
594
|
+
if (typeof globalThis !== "undefined") {
|
|
595
|
+
globalThis.ContourShadowTemplates = FROZEN_SHADOW_MARKUP;
|
|
596
|
+
}
|
|
597
|
+
// Only the SHADOW vocabulary publishes here (R25-06). ContourElements and
|
|
598
|
+
if (typeof window !== "undefined") {
|
|
599
|
+
window.ContourShadowParts = Object.freeze(Object.fromEntries(
|
|
600
|
+
Object.entries(SHADOW_PARTS_PUBLIC).map(([name, parts]) => [name, Object.freeze([...new Set(parts)])]),
|
|
601
|
+
));
|
|
602
|
+
if (!window.Contour) window.Contour = {};
|
|
603
|
+
try { window.Contour.shadowTemplates = FROZEN_SHADOW_MARKUP; } catch {}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export { SHADOW_MARKUP, renderShadowDSD };
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Forms family barrel — the form-control web components, split into semantic
|
|
610
|
+
* fragments. Importing this module registers every form component (the
|
|
611
|
+
* fragments are side-effectful: each defines its own custom elements). The
|
|
612
|
+
* build concatenates the fragments for the IIFE/ESM bundles; this barrel keeps
|
|
613
|
+
* `contour-ds/wc/forms.mjs` working for consumers who import the family by name.
|
|
614
|
+
*/
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Combobox: a filterable, keyboard-navigable listbox backed by a JSON `options`
|
|
624
|
+
* attribute or authored `<li>` options. Supports single and multi-select
|
|
625
|
+
* (`multiple`), fixed-row virtualization (`virtual`), async/remote options
|
|
626
|
+
* (`src`, `loading`, `error`, `retry`, `empty`, `skeleton`), a clear button and
|
|
627
|
+
* form association. Emits `contour:select` on commit and `contour:input` on
|
|
628
|
+
* filter. ArrowUp/Down/Home/End rove the cursor; Enter commits; Escape closes.
|
|
629
|
+
*/
|
|
630
|
+
class ContourCombobox extends ContourBase {
|
|
631
|
+
static get observed() { return ["options", "items", "placeholder", "label", "value", "multiple", "loading", "error", "retry", "src", "lazy", "refresh", "timeout", "empty", "skeleton", "virtual", "disabled", "required", "name", "cache-key", "no-cache"]; }
|
|
632
|
+
static get formAssociated() { return true; }
|
|
633
|
+
static get properties() { return { options: { attribute: "options" }, items: { attribute: "items" }, placeholder: { attribute: "placeholder" }, label: { attribute: "label" }, value: { attribute: "value" }, multiple: { attribute: "multiple", type: Boolean }, virtual: { attribute: "virtual", type: Boolean }, src: { attribute: "src" }, disabled: { attribute: "disabled", type: Boolean }, required: { attribute: "required", type: Boolean }, name: { attribute: "name" } }; }
|
|
634
|
+
render() {
|
|
635
|
+
this.classList.add("c-combobox");
|
|
636
|
+
// Authored default captured before commit; form reset restores this.
|
|
637
|
+
if (this._comboboxDefault === undefined) this._comboboxDefault = this.getAttribute("value");
|
|
638
|
+
wireStates(this);
|
|
639
|
+
loadRemote(this, "options");
|
|
640
|
+
if (renderStates(this, "combobox")) return;
|
|
641
|
+
const options = remoteData(this, "options") || this.getAttribute("options") || this.getAttribute("items");
|
|
642
|
+
// Slot composition: authored <li> wins over JSON attribute.
|
|
643
|
+
const authored = Array.from(this.children).filter((n) => !n.classList || (!n.classList.contains("c-combobox__input") && !n.classList.contains("c-combobox__list")));
|
|
644
|
+
if ((options) && !authored.length) {
|
|
645
|
+
try {
|
|
646
|
+
const parsed = options ? JSON.parse(options) : [];
|
|
647
|
+
this._comboboxOptions = parsed;
|
|
648
|
+
const input = this.ensure(".c-combobox__input", () => { const el = document.createElement("input"); el.className = "c-input c-combobox__input"; el.type = "text"; return el; });
|
|
649
|
+
input.type = "text";
|
|
650
|
+
input.placeholder = this.getAttribute("placeholder") || labelText(this, "search", "Search…");
|
|
651
|
+
input.setAttribute("aria-label", this.getAttribute("label") || labelText(this, "search", "Search"));
|
|
652
|
+
// Reflect native constraints so platform validates and FormData sees name.
|
|
653
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
654
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
655
|
+
if (this.hasAttribute("required")) { input.setAttribute("required", ""); input.setAttribute("aria-required", "true"); }
|
|
656
|
+
else { input.removeAttribute("required"); input.removeAttribute("aria-required"); }
|
|
657
|
+
if (this.hasAttribute("name")) input.setAttribute("name", this.getAttribute("name"));
|
|
658
|
+
const val = this.getAttribute("value");
|
|
659
|
+
if (val !== null) {
|
|
660
|
+
// Attribute holds submitted value; field shows option label, so they can differ.
|
|
661
|
+
const values = String(val).split(",").map((s) => s.trim()).filter(Boolean);
|
|
662
|
+
const labels = values.map((v) => {
|
|
663
|
+
const match = parsed.find((o) => String(typeof o === "string" ? o : o.value) === v);
|
|
664
|
+
return match ? String(typeof match === "string" ? match : match.label) : v;
|
|
665
|
+
});
|
|
666
|
+
if (this.hasAttribute("multiple")) {
|
|
667
|
+
// R31-05: a multi-select must not put "Alpha,Charlie" in the
|
|
668
|
+
// search field — that string filters out every option. Keep the
|
|
669
|
+
// query empty and show the chosen labels as the placeholder.
|
|
670
|
+
input.value = "";
|
|
671
|
+
input.placeholder = labels.join(", ") || (this.getAttribute("placeholder") || labelText(this, "search", "Search…"));
|
|
672
|
+
} else {
|
|
673
|
+
const match = parsed.find((o) => String(typeof o === "string" ? o : o.value) === String(val));
|
|
674
|
+
input.value = match ? String(typeof match === "string" ? match : match.label) : val;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
const list = this.ensure(".c-combobox__list", () => { const el = document.createElement("ul"); el.className = "c-combobox__list"; el.setAttribute("hidden", ""); return el; });
|
|
678
|
+
list.innerHTML = "";
|
|
679
|
+
list.setAttribute("hidden", "");
|
|
680
|
+
this._comboboxVirtual = this.hasAttribute("virtual") && parsed.length > 100;
|
|
681
|
+
if (this._comboboxVirtual) {
|
|
682
|
+
list.classList.add("c-combobox__list--virtual");
|
|
683
|
+
list.style.maxBlockSize = "20rem";
|
|
684
|
+
list.style.overflow = "auto";
|
|
685
|
+
this._renderComboboxWindow("", true);
|
|
686
|
+
} else {
|
|
687
|
+
list.classList.remove("c-combobox__list--virtual");
|
|
688
|
+
list.style.maxBlockSize = "";
|
|
689
|
+
list.style.overflow = "";
|
|
690
|
+
this._renderComboboxWindow("", true);
|
|
691
|
+
}
|
|
692
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
693
|
+
} else if (!authored.length && !options) {
|
|
694
|
+
renderEmpty(this, "combobox");
|
|
695
|
+
} else if (authored.length) {
|
|
696
|
+
// R31-18: authored <li> options. Build the input + list shell and adopt
|
|
697
|
+
// the authored <li> as options (role=option), so a consumer can write
|
|
698
|
+
// the list in markup instead of a JSON `options` attribute.
|
|
699
|
+
const input = this.ensure(".c-combobox__input", () => { const el = document.createElement("input"); el.className = "c-input c-combobox__input"; el.type = "text"; return el; });
|
|
700
|
+
input.type = "text";
|
|
701
|
+
input.placeholder = this.getAttribute("placeholder") || labelText(this, "search", "Search…");
|
|
702
|
+
input.setAttribute("aria-label", this.getAttribute("label") || labelText(this, "search", "Search"));
|
|
703
|
+
if (this.hasAttribute("required")) { input.setAttribute("required", ""); input.setAttribute("aria-required", "true"); }
|
|
704
|
+
else { input.removeAttribute("required"); input.removeAttribute("aria-required"); }
|
|
705
|
+
if (this.hasAttribute("name")) input.setAttribute("name", this.getAttribute("name"));
|
|
706
|
+
const list = this.ensure(".c-combobox__list", () => { const el = document.createElement("ul"); el.className = "c-combobox__list"; el.setAttribute("hidden", ""); return el; });
|
|
707
|
+
for (const li of authored) {
|
|
708
|
+
if (li.tagName === "LI") { list.appendChild(li); li.setAttribute("role", "option"); }
|
|
709
|
+
}
|
|
710
|
+
this._comboboxOptions = Array.from(list.querySelectorAll("li")).map((li) => ({
|
|
711
|
+
label: li.textContent.trim(),
|
|
712
|
+
value: li.dataset.value != null ? li.dataset.value : li.textContent.trim(),
|
|
713
|
+
}));
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (this.getAttribute("value")) {
|
|
717
|
+
this.ensure(".c-combobox__clear", () => {
|
|
718
|
+
const btn = document.createElement("button");
|
|
719
|
+
btn.className = "c-combobox__clear";
|
|
720
|
+
btn.type = "button";
|
|
721
|
+
btn.setAttribute("aria-label", labelText(this, "clear", "Clear"));
|
|
722
|
+
btn.textContent = "×";
|
|
723
|
+
return btn;
|
|
724
|
+
});
|
|
725
|
+
} else {
|
|
726
|
+
const btn = this.querySelector(".c-combobox__clear");
|
|
727
|
+
if (btn) btn.remove();
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
_renderComboboxWindow(query = "", reset = false) {
|
|
731
|
+
const list = this.querySelector(".c-combobox__list");
|
|
732
|
+
const options = this._comboboxOptions || [];
|
|
733
|
+
if (!list) return;
|
|
734
|
+
const q = String(query || "").toLowerCase();
|
|
735
|
+
const filtered = options.map((opt, index) => ({
|
|
736
|
+
opt,
|
|
737
|
+
index,
|
|
738
|
+
label: typeof opt === "string" ? opt : opt.label,
|
|
739
|
+
value: typeof opt === "string" ? opt : opt.value,
|
|
740
|
+
})).filter((item) => !q || String(item.label).toLowerCase().includes(q));
|
|
741
|
+
this._comboboxFiltered = filtered;
|
|
742
|
+
const rowHeight = cssLength("--combobox-row-height", 32);
|
|
743
|
+
const windowSize = 20;
|
|
744
|
+
const overscan = 6;
|
|
745
|
+
const oldScrollTop = list.scrollTop;
|
|
746
|
+
const start = this._comboboxVirtual && !reset
|
|
747
|
+
? Math.min(Math.max(0, filtered.length - windowSize), Math.max(0, Math.floor(oldScrollTop / rowHeight) - overscan))
|
|
748
|
+
: 0;
|
|
749
|
+
this._comboboxWindowStart = start;
|
|
750
|
+
const end = this._comboboxVirtual ? Math.min(filtered.length, start + windowSize + overscan * 2) : filtered.length;
|
|
751
|
+
const selectedVals = (this.getAttribute("value") || "").split(",").map((v) => v.trim()).filter(Boolean);
|
|
752
|
+
list.innerHTML = "";
|
|
753
|
+
const addSpacer = (height) => {
|
|
754
|
+
const spacer = document.createElement("li");
|
|
755
|
+
spacer.setAttribute("aria-hidden", "true");
|
|
756
|
+
spacer.style.blockSize = `${height}px`;
|
|
757
|
+
list.appendChild(spacer);
|
|
758
|
+
};
|
|
759
|
+
if (this._comboboxVirtual) addSpacer(start * rowHeight);
|
|
760
|
+
if (!filtered.length) {
|
|
761
|
+
const empty = document.createElement("li");
|
|
762
|
+
empty.className = "c-combobox__empty";
|
|
763
|
+
// Disabled option, not a live region: a listbox may only contain
|
|
764
|
+
// `option`, and `role="status"` inside one is an axe violation.
|
|
765
|
+
empty.setAttribute("role", "option");
|
|
766
|
+
empty.setAttribute("aria-disabled", "true");
|
|
767
|
+
empty.setAttribute("aria-selected", "false");
|
|
768
|
+
empty.textContent = this.getAttribute("empty") || labelText(this, "no-matches", "No matches");
|
|
769
|
+
list.appendChild(empty);
|
|
770
|
+
}
|
|
771
|
+
if (this.hasAttribute("multiple")) list.setAttribute("aria-multiselectable", "true");
|
|
772
|
+
else list.removeAttribute("aria-multiselectable");
|
|
773
|
+
filtered.slice(start, end).forEach((item) => {
|
|
774
|
+
const li = document.createElement("li");
|
|
775
|
+
li.className = "c-combobox__option";
|
|
776
|
+
li.dataset.optionIndex = String(item.index);
|
|
777
|
+
li.dataset.optionValue = String(item.value != null ? item.value : item.label);
|
|
778
|
+
li.setAttribute("role", "option");
|
|
779
|
+
const label = String(item.label);
|
|
780
|
+
const q = String(query || "").toLowerCase();
|
|
781
|
+
if (q && label.toLowerCase().includes(q)) {
|
|
782
|
+
const idx = label.toLowerCase().indexOf(q);
|
|
783
|
+
const before = label.slice(0, idx);
|
|
784
|
+
const match = label.slice(idx, idx + q.length);
|
|
785
|
+
const after = label.slice(idx + q.length);
|
|
786
|
+
li.innerHTML = "";
|
|
787
|
+
if (before) li.appendChild(document.createTextNode(before));
|
|
788
|
+
const mark = document.createElement("mark");
|
|
789
|
+
mark.className = "c-combobox__match";
|
|
790
|
+
mark.textContent = match;
|
|
791
|
+
li.appendChild(mark);
|
|
792
|
+
if (after) li.appendChild(document.createTextNode(after));
|
|
793
|
+
} else {
|
|
794
|
+
li.textContent = label;
|
|
795
|
+
}
|
|
796
|
+
const isSelected = this.hasAttribute("multiple") && selectedVals.includes(String(item.value));
|
|
797
|
+
li.setAttribute("aria-selected", String(isSelected));
|
|
798
|
+
if (isSelected) li.classList.add("c-combobox__option--selected");
|
|
799
|
+
list.appendChild(li);
|
|
800
|
+
});
|
|
801
|
+
if (this._comboboxVirtual) addSpacer(Math.max(0, filtered.length - end) * rowHeight);
|
|
802
|
+
if (this._comboboxVirtual && !reset) list.scrollTop = oldScrollTop;
|
|
803
|
+
}
|
|
804
|
+
_syncFormValue() {
|
|
805
|
+
if (!this._internals) return;
|
|
806
|
+
// Committed value on host attribute; form submits value, field shows label.
|
|
807
|
+
const raw = this.getAttribute("value");
|
|
808
|
+
const value = raw != null && raw !== "" ? raw : null;
|
|
809
|
+
this._internals.setFormValue(value);
|
|
810
|
+
this._syncValidity(this.hasAttribute("required") && !value);
|
|
811
|
+
}
|
|
812
|
+
enhance() {
|
|
813
|
+
wireCombobox(this);
|
|
814
|
+
// Mirror committed input value into form association so host submits.
|
|
815
|
+
if (this._internals && !this._wiredComboboxForm) {
|
|
816
|
+
this._wiredComboboxForm = true;
|
|
817
|
+
this._syncFormValue();
|
|
818
|
+
this.addEventListener("contour:select", (e) => {
|
|
819
|
+
const v = e && e.detail && (e.detail.value || e.detail.label);
|
|
820
|
+
if (v != null) this.setAttribute("value", String(v));
|
|
821
|
+
this._syncFormValue();
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
if (!this._wiredComboboxClear) {
|
|
825
|
+
this._wiredComboboxClear = true;
|
|
826
|
+
this.addEventListener("click", (e) => {
|
|
827
|
+
const btn = e.target && e.target.closest ? e.target.closest(".c-combobox__clear") : null;
|
|
828
|
+
if (!btn || !this.contains(btn)) return;
|
|
829
|
+
e.preventDefault();
|
|
830
|
+
this.removeAttribute("value");
|
|
831
|
+
const input = this.querySelector(".c-combobox__input");
|
|
832
|
+
if (input) input.value = "";
|
|
833
|
+
this._syncFormValue();
|
|
834
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { name: this.getAttribute("name"), value: null, checked: null } }));
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
formResetCallback() {
|
|
839
|
+
// Restore authored default; render maps to option label, base reset writes raw values.
|
|
840
|
+
this._contourResetting = true;
|
|
841
|
+
try {
|
|
842
|
+
const def = this._comboboxDefault ?? null;
|
|
843
|
+
if (def === null) this.removeAttribute("value");
|
|
844
|
+
else this.setAttribute("value", def);
|
|
845
|
+
this._syncFormValue();
|
|
846
|
+
} finally {
|
|
847
|
+
this._contourResetting = false;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
define("contour-combobox", ContourCombobox);
|
|
852
|
+
/**
|
|
853
|
+
* Field: a labelled form control wrapper. Renders an `<input>`/`<textarea>`/
|
|
854
|
+
* `<select>` (or adopts an authored one), associates a label via `for`/`id`,
|
|
855
|
+
* and attaches hint and error text with `aria-describedby`. Drives inline
|
|
856
|
+
* validation on blur (`aria-invalid`, `is-invalid`) and keeps the form value in
|
|
857
|
+
* sync via `_internals.setFormValue`. Emits `contour:input` / `contour:change`.
|
|
858
|
+
*/
|
|
859
|
+
class ContourField extends ContourBase {
|
|
860
|
+
static get observed() { return ["label", "hint", "error", "placeholder", "type", "required", "name", "value", "disabled", "custom-error"]; }
|
|
861
|
+
static get formAssociated() { return true; }
|
|
862
|
+
static get properties() { return { label: { attribute: "label" }, hint: { attribute: "hint" }, error: { attribute: "error" }, placeholder: { attribute: "placeholder" }, type: { attribute: "type" }, required: { attribute: "required", type: Boolean }, name: { attribute: "name" }, value: { attribute: "value" }, disabled: { attribute: "disabled", type: Boolean }, customError: { attribute: "custom-error" } }; }
|
|
863
|
+
render() {
|
|
864
|
+
this.classList.add("c-field");
|
|
865
|
+
const label = this.getAttribute("label");
|
|
866
|
+
const hint = this.getAttribute("hint");
|
|
867
|
+
const error = this.getAttribute("error");
|
|
868
|
+
const placeholder = this.getAttribute("placeholder") || "";
|
|
869
|
+
if (label) {
|
|
870
|
+
const lbl = this.ensure(".c-field__label", () => { const el = document.createElement("label"); el.className = "c-field__label"; return el; });
|
|
871
|
+
lbl.textContent = label;
|
|
872
|
+
} else {
|
|
873
|
+
const lbl = this.querySelector(".c-field__label");
|
|
874
|
+
if (lbl) lbl.remove();
|
|
875
|
+
}
|
|
876
|
+
// Skip generating input when nested form host submits its own value.
|
|
877
|
+
const nestedFormHost = Array.from(this.children).find((c) => c.constructor && c.constructor.formAssociated);
|
|
878
|
+
const input = nestedFormHost ? null : this.ensure("input.c-input, textarea, select", () => {
|
|
879
|
+
const el = document.createElement("input");
|
|
880
|
+
el.className = "c-input";
|
|
881
|
+
return el;
|
|
882
|
+
});
|
|
883
|
+
if (input) {
|
|
884
|
+
input.type = this.getAttribute("type") || "text";
|
|
885
|
+
input.placeholder = placeholder;
|
|
886
|
+
// Stable id across renders; fresh id breaks keyed value/focus matching.
|
|
887
|
+
if (!input.id) input.id = this._fieldInputId || (this._fieldInputId = contourId("contour-field"));
|
|
888
|
+
|
|
889
|
+
const lbl = this.querySelector(".c-field__label");
|
|
890
|
+
if (lbl) { if (!input.id) input.id = contourId("contour-field"); lbl.setAttribute("for", input.id); input.setAttribute("aria-label", lbl.textContent); }
|
|
891
|
+
else if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "field", "Field"));
|
|
892
|
+
if (hint) {
|
|
893
|
+
const h = this.ensure(".c-field__hint", () => { const el = document.createElement("span"); el.className = "c-field__hint"; return el; });
|
|
894
|
+
h.textContent = hint;
|
|
895
|
+
} else {
|
|
896
|
+
const h = this.querySelector(".c-field__hint");
|
|
897
|
+
if (h) h.remove();
|
|
898
|
+
}
|
|
899
|
+
if (error) {
|
|
900
|
+
const e = this.ensure(".c-field__error", () => { const el = document.createElement("span"); el.className = "c-field__error"; return el; });
|
|
901
|
+
e.textContent = error;
|
|
902
|
+
} else {
|
|
903
|
+
const e = this.querySelector(".c-field__error");
|
|
904
|
+
if (e) e.remove();
|
|
905
|
+
}
|
|
906
|
+
// R22 WC4-26: custom-error attribute — declarative setCustomValidity.
|
|
907
|
+
const customError = this.getAttribute("custom-error");
|
|
908
|
+
if (customError !== null) {
|
|
909
|
+
input.setAttribute("aria-invalid", "true");
|
|
910
|
+
this.classList.add("is-invalid");
|
|
911
|
+
this.setAttribute("data-invalid", "");
|
|
912
|
+
let errEl = this.querySelector(".c-field__error");
|
|
913
|
+
if (!errEl) {
|
|
914
|
+
errEl = document.createElement("span");
|
|
915
|
+
errEl.className = "c-field__error";
|
|
916
|
+
this.appendChild(errEl);
|
|
917
|
+
}
|
|
918
|
+
errEl.textContent = customError;
|
|
919
|
+
if (!errEl.id) errEl.id = contourId("err");
|
|
920
|
+
input.setAttribute("aria-describedby", (input.getAttribute("aria-describedby") || "").split(" ").filter((x) => x && x !== errEl.id).concat(errEl.id).join(" "));
|
|
921
|
+
}
|
|
922
|
+
// Link input to hint/error via aria-describedby; generated ids ensure both are announced.
|
|
923
|
+
const hintEl = this.querySelector(".c-field__hint");
|
|
924
|
+
const errEl = this.querySelector(".c-field__error");
|
|
925
|
+
const describedBy = [];
|
|
926
|
+
if (hintEl) { if (!hintEl.id) hintEl.id = contourId("hint"); describedBy.push(hintEl.id); }
|
|
927
|
+
if (errEl) { if (!errEl.id) errEl.id = contourId("err"); describedBy.push(errEl.id); }
|
|
928
|
+
if (describedBy.length) input.setAttribute("aria-describedby", describedBy.join(" "));
|
|
929
|
+
// Reflect native constraints: checkValidity and FormData must see required/name/value.
|
|
930
|
+
if (this.hasAttribute("required")) { input.setAttribute("aria-required", "true"); input.setAttribute("required", ""); }
|
|
931
|
+
else { input.removeAttribute("aria-required"); input.removeAttribute("required"); }
|
|
932
|
+
if (this.hasAttribute("name")) input.setAttribute("name", this.getAttribute("name"));
|
|
933
|
+
if (this.hasAttribute("value")) input.value = this.getAttribute("value");
|
|
934
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
935
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
936
|
+
} // end if (input)
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
ContourField.prototype.enhance = function() {
|
|
940
|
+
if (this._wiredField) return;
|
|
941
|
+
this._wiredField = true;
|
|
942
|
+
const sync = () => {
|
|
943
|
+
const input = this.querySelector("input, textarea, select");
|
|
944
|
+
if (!input || !this._internals) return;
|
|
945
|
+
this._internals.setFormValue(input.value);
|
|
946
|
+
// Mirror required-missing into form association (not just ARIA) so checkValidity fails.
|
|
947
|
+
this._syncValidity(this.hasAttribute("required") && !input.value);
|
|
948
|
+
};
|
|
949
|
+
this.addEventListener("input", (e) => {
|
|
950
|
+
const input = this.querySelector("input, textarea, select");
|
|
951
|
+
if (!input || e.target !== input) return;
|
|
952
|
+
sync();
|
|
953
|
+
this._emitForm("contour:input");
|
|
954
|
+
});
|
|
955
|
+
this.addEventListener("change", (e) => {
|
|
956
|
+
const input = this.querySelector("input, textarea, select");
|
|
957
|
+
if (!input || e.target !== input) return;
|
|
958
|
+
sync();
|
|
959
|
+
this._emitForm("contour:change");
|
|
960
|
+
});
|
|
961
|
+
// Inline validation on blur: reflect validity via aria-invalid; focusout bubbles, blur doesn't.
|
|
962
|
+
this.addEventListener("focusout", (e) => {
|
|
963
|
+
const input = this.querySelector("input, textarea, select");
|
|
964
|
+
if (!input || e.target !== input) return;
|
|
965
|
+
const valid = input.checkValidity();
|
|
966
|
+
if (valid) {
|
|
967
|
+
input.setAttribute("aria-invalid", "false");
|
|
968
|
+
this.classList.remove("is-invalid");
|
|
969
|
+
this.removeAttribute("data-invalid");
|
|
970
|
+
// Clear transient validation error, not author-set error.
|
|
971
|
+
if (!this.getAttribute("error")) {
|
|
972
|
+
const stale = this.querySelector(".c-field__error");
|
|
973
|
+
if (stale) {
|
|
974
|
+
const id = stale.id;
|
|
975
|
+
stale.remove();
|
|
976
|
+
if (id) input.setAttribute("aria-describedby", (input.getAttribute("aria-describedby") || "").split(" ").filter((x) => x && x !== id).join(" "));
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
} else {
|
|
980
|
+
input.setAttribute("aria-invalid", "true");
|
|
981
|
+
this.classList.add("is-invalid");
|
|
982
|
+
this.setAttribute("data-invalid", "");
|
|
983
|
+
// R22 WC4-26: emit cancelable contour:invalid; listener can suppress invalid state.
|
|
984
|
+
const invalidEvent = new CustomEvent("contour:invalid", {
|
|
985
|
+
bubbles: true,
|
|
986
|
+
composed: true,
|
|
987
|
+
cancelable: true,
|
|
988
|
+
detail: { field: this, input, message: input.validationMessage || "Invalid value" },
|
|
989
|
+
});
|
|
990
|
+
const notCancelled = this.dispatchEvent(invalidEvent);
|
|
991
|
+
if (notCancelled) {
|
|
992
|
+
// Surface browser validation message when no explicit error/custom-error attr.
|
|
993
|
+
if (!this.getAttribute("error") && !this.getAttribute("custom-error")) {
|
|
994
|
+
const msg = input.validationMessage || "Invalid value";
|
|
995
|
+
let errEl = this.querySelector(".c-field__error");
|
|
996
|
+
if (!errEl) {
|
|
997
|
+
errEl = document.createElement("span");
|
|
998
|
+
errEl.className = "c-field__error";
|
|
999
|
+
this.appendChild(errEl);
|
|
1000
|
+
}
|
|
1001
|
+
errEl.textContent = msg;
|
|
1002
|
+
if (!errEl.id) errEl.id = contourId("err");
|
|
1003
|
+
input.setAttribute("aria-describedby", (input.getAttribute("aria-describedby") || "").split(" ").filter((x) => x && x !== errEl.id).concat(errEl.id).join(" "));
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
sync();
|
|
1008
|
+
}, true);
|
|
1009
|
+
sync();
|
|
1010
|
+
};
|
|
1011
|
+
define("contour-field", ContourField);
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Field-group: a grouped field with a shared label, optional hint and error
|
|
1015
|
+
* text. Sets `role="group"` and `aria-required` when required; toggles
|
|
1016
|
+
* `is-error` when an error is present. Child fields are labelled by the group.
|
|
1017
|
+
*/
|
|
1018
|
+
class ContourFieldGroup extends ContourBase {
|
|
1019
|
+
static get observed() { return ["label", "hint", "error", "required"]; }
|
|
1020
|
+
static get properties() { return { label: { attribute: "label" }, hint: { attribute: "hint" }, error: { attribute: "error" }, required: { attribute: "required", type: Boolean } }; }
|
|
1021
|
+
render() {
|
|
1022
|
+
this.classList.add("c-field-group");
|
|
1023
|
+
this.setAttribute("role", "group");
|
|
1024
|
+
const label = this.getAttribute("label");
|
|
1025
|
+
if (label) {
|
|
1026
|
+
const lbl = this.ensure(".c-field-group__label", () => { const el = document.createElement("div"); el.className = "c-field-group__label"; return el; });
|
|
1027
|
+
lbl.textContent = label;
|
|
1028
|
+
} else {
|
|
1029
|
+
const lbl = this.querySelector(".c-field-group__label");
|
|
1030
|
+
if (lbl) lbl.remove();
|
|
1031
|
+
}
|
|
1032
|
+
const hint = this.getAttribute("hint");
|
|
1033
|
+
if (hint) {
|
|
1034
|
+
const h = this.ensure(".c-field-group__hint", () => { const el = document.createElement("span"); el.className = "c-field-group__hint"; return el; });
|
|
1035
|
+
h.textContent = hint;
|
|
1036
|
+
} else {
|
|
1037
|
+
const h = this.querySelector(".c-field-group__hint");
|
|
1038
|
+
if (h) h.remove();
|
|
1039
|
+
}
|
|
1040
|
+
const error = this.getAttribute("error");
|
|
1041
|
+
if (error) {
|
|
1042
|
+
const e = this.ensure(".c-field-group__error", () => { const el = document.createElement("span"); el.className = "c-field-group__error"; return el; });
|
|
1043
|
+
e.textContent = error;
|
|
1044
|
+
} else {
|
|
1045
|
+
const e = this.querySelector(".c-field-group__error");
|
|
1046
|
+
if (e) e.remove();
|
|
1047
|
+
}
|
|
1048
|
+
if (error) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1049
|
+
// aria-required not allowed on role="group"; required semantics go on inner controls.
|
|
1050
|
+
const required = this.hasAttribute("required");
|
|
1051
|
+
this.querySelectorAll("input, select, textarea").forEach((el) => {
|
|
1052
|
+
if (required) { if (!el.hasAttribute("aria-required")) el.setAttribute("aria-required", "true"); }
|
|
1053
|
+
else if (el.getAttribute("aria-required") === "true") el.removeAttribute("aria-required");
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
enhance() {
|
|
1057
|
+
if (this._wiredFieldGroup) return;
|
|
1058
|
+
this._wiredFieldGroup = true;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
define("contour-field-group", ContourFieldGroup);
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* Form: a form wrapper with submission state and a validation summary. Adds
|
|
1065
|
+
* `is-submitting` and disables the submit button while `submitting` is set.
|
|
1066
|
+
* On invalid submit, prevents default and renders a `role="alert"` summary
|
|
1067
|
+
* listing the invalid fields. Emits `contour:submit` on valid submit.
|
|
1068
|
+
*
|
|
1069
|
+
* Nesting contract: when `contour-form` is nested inside a real `<form>`,
|
|
1070
|
+
* submission listens on that enclosing form so `contour:submit` fires and
|
|
1071
|
+
* the real form is prevented from navigating. When standalone, the host
|
|
1072
|
+
* itself is the submit target. The component's own submit button (rendered
|
|
1073
|
+
* when `submit-label` is given) only handles the standalone case; inside a
|
|
1074
|
+
* real form the native button drives the form's submit event which the
|
|
1075
|
+
* enclosing-form listener catches.
|
|
1076
|
+
*/
|
|
1077
|
+
class ContourForm extends ContourBase {
|
|
1078
|
+
static get observed() { return ["submitting", "error", "summary", "title", "submit-label"]; }
|
|
1079
|
+
static get properties() { return { submitting: { attribute: "submitting", type: Boolean }, error: { attribute: "error" }, summary: { attribute: "summary" }, title: { attribute: "title" }, submitLabel: { attribute: "submit-label" } }; }
|
|
1080
|
+
render() {
|
|
1081
|
+
this.classList.add("c-form");
|
|
1082
|
+
if (this.hasAttribute("submitting")) this.classList.add("is-submitting"); else this.classList.remove("is-submitting");
|
|
1083
|
+
const title = this.getAttribute("title");
|
|
1084
|
+
let titleEl = this.querySelector(".c-form__title");
|
|
1085
|
+
if (title) {
|
|
1086
|
+
if (!titleEl) { titleEl = document.createElement("h2"); titleEl.className = "c-form__title"; this.prepend(titleEl); }
|
|
1087
|
+
titleEl.textContent = title;
|
|
1088
|
+
} else if (titleEl) {
|
|
1089
|
+
titleEl.remove();
|
|
1090
|
+
}
|
|
1091
|
+
const summaryText = this.getAttribute("summary") || this.getAttribute("error");
|
|
1092
|
+
let summary = this.querySelector(".c-form__summary");
|
|
1093
|
+
if (summaryText) {
|
|
1094
|
+
if (!summary) { summary = document.createElement("div"); summary.className = "c-form__summary"; summary.setAttribute("role", "alert"); this.prepend(summary); }
|
|
1095
|
+
summary.textContent = summaryText;
|
|
1096
|
+
summary.removeAttribute("hidden");
|
|
1097
|
+
} else if (summary) {
|
|
1098
|
+
summary.setAttribute("hidden", "");
|
|
1099
|
+
}
|
|
1100
|
+
// Component owns submission: renders submit control when submit-label given.
|
|
1101
|
+
const submitLabel = this.getAttribute("submit-label");
|
|
1102
|
+
let submit = this.querySelector(".c-form__submit");
|
|
1103
|
+
if (!submit) {
|
|
1104
|
+
submit = document.createElement("button");
|
|
1105
|
+
submit.type = "submit";
|
|
1106
|
+
submit.className = "c-btn c-btn--primary c-form__submit";
|
|
1107
|
+
this.appendChild(submit);
|
|
1108
|
+
}
|
|
1109
|
+
// submit-label is authored override; labels.submit is localised default; applyLabel re-applies default each render.
|
|
1110
|
+
if (submitLabel) {
|
|
1111
|
+
submit.textContent = submitLabel;
|
|
1112
|
+
if (submit.dataset) delete submit.dataset.contourLabel;
|
|
1113
|
+
} else {
|
|
1114
|
+
applyLabel(submit, this, "submit", "Submit");
|
|
1115
|
+
}
|
|
1116
|
+
if (this.hasAttribute("submitting")) submit.setAttribute("disabled", "");
|
|
1117
|
+
else submit.removeAttribute("disabled");
|
|
1118
|
+
}
|
|
1119
|
+
enhance() {
|
|
1120
|
+
if (this._wiredForm) return;
|
|
1121
|
+
this._wiredForm = true;
|
|
1122
|
+
// Nesting contract: submit bubbles to enclosing form; listen on closest form or host.
|
|
1123
|
+
const closestForm = this.closest("form");
|
|
1124
|
+
const submitTarget = closestForm || this;
|
|
1125
|
+
|
|
1126
|
+
const onSubmit = (e) => {
|
|
1127
|
+
const form = e.target;
|
|
1128
|
+
// Component claims submission ownership: never let nested form navigate.
|
|
1129
|
+
e.preventDefault();
|
|
1130
|
+
if (form && typeof form.checkValidity === "function" && !form.checkValidity()) {
|
|
1131
|
+
const invalid = Array.from(form.querySelectorAll(":invalid"));
|
|
1132
|
+
const names = invalid.map((el) => el.getAttribute("aria-label") || el.name || el.id || "field").join(", ");
|
|
1133
|
+
let summary = this.querySelector(".c-form__summary");
|
|
1134
|
+
if (!summary) { summary = document.createElement("div"); summary.className = "c-form__summary"; summary.setAttribute("role", "alert"); this.prepend(summary); }
|
|
1135
|
+
summary.textContent = labelText(this, "validation-summary", "Please fix") + ": " + (names || labelText(this, "invalid-fields", "invalid fields"));
|
|
1136
|
+
summary.removeAttribute("hidden");
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
this._submit();
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
// Click on component's submit control is submission path when no nested form.
|
|
1143
|
+
this.addEventListener("click", (e) => {
|
|
1144
|
+
const btn = e.target && e.target.closest ? e.target.closest(".c-form__submit") : null;
|
|
1145
|
+
if (!btn || !this.contains(btn)) return;
|
|
1146
|
+
if (e.target.closest("form")) return; // a nested real form handles it
|
|
1147
|
+
e.preventDefault();
|
|
1148
|
+
this._submit();
|
|
1149
|
+
});
|
|
1150
|
+
submitTarget.addEventListener("submit", onSubmit);
|
|
1151
|
+
onDispose(this, () => {
|
|
1152
|
+
if (submitTarget !== this) {
|
|
1153
|
+
submitTarget.removeEventListener("submit", onSubmit);
|
|
1154
|
+
}
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
_submit() {
|
|
1158
|
+
this.classList.add("is-submitting");
|
|
1159
|
+
const submit = this.querySelector("[type='submit'], .c-form__submit");
|
|
1160
|
+
if (submit) submit.setAttribute("disabled", "");
|
|
1161
|
+
this.dispatchEvent(new CustomEvent("contour:submit", { bubbles: true, composed: true, detail: { submitting: true } }));
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
define("contour-form", ContourForm);
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* Range: a native `<input type=range>` wrapper with optional dual-handle range
|
|
1171
|
+
* mode (`range`, `low`, `high`) and a sparkline canvas (`sparkline`). Form
|
|
1172
|
+
* associated; emits `contour:input` / `contour:change` with the current value
|
|
1173
|
+
* (or `{low, high}` in range mode). Renders a `--fill` custom property for the
|
|
1174
|
+
* filled track.
|
|
1175
|
+
*/
|
|
1176
|
+
class ContourRange extends ContourBase {
|
|
1177
|
+
static get observed() { return ["min", "max", "value", "low", "high", "range", "sparkline", "label", "disabled", "error", "help", "name", "step"]; }
|
|
1178
|
+
static get formAssociated() { return true; }
|
|
1179
|
+
static get properties() { return { min: { attribute: "min" }, max: { attribute: "max" }, value: { attribute: "value" }, label: { attribute: "label" }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" }, name: { attribute: "name" }, step: { attribute: "step" } }; }
|
|
1180
|
+
render() {
|
|
1181
|
+
this.classList.add("c-range");
|
|
1182
|
+
const min = this.getAttribute("min") || "0";
|
|
1183
|
+
const max = this.getAttribute("max") || "100";
|
|
1184
|
+
const rangeMode = this.hasAttribute("range") || this.hasAttribute("low") || this.hasAttribute("high");
|
|
1185
|
+
// sparkline canvas
|
|
1186
|
+
const spark = this.getAttribute("sparkline");
|
|
1187
|
+
if (spark) {
|
|
1188
|
+
let canvas = this.querySelector(".c-range__sparkline");
|
|
1189
|
+
if (!canvas) { canvas = document.createElement("canvas"); canvas.className = "c-range__sparkline"; canvas.setAttribute("part", "sparkline-canvas"); canvas.setAttribute("aria-hidden", "true"); this.insertBefore(canvas, this.firstChild); }
|
|
1190
|
+
// R31-67: the canvas was created but never painted. Draw the series.
|
|
1191
|
+
this._drawRangeSpark(canvas, spark);
|
|
1192
|
+
} else {
|
|
1193
|
+
const c = this.querySelector(".c-range__sparkline");
|
|
1194
|
+
if (c) c.remove();
|
|
1195
|
+
}
|
|
1196
|
+
if (rangeMode) {
|
|
1197
|
+
// remove single input if present
|
|
1198
|
+
const single = this.querySelector("input.c-range:not(.c-range__low):not(.c-range__high)");
|
|
1199
|
+
// keep generic input? We'll use two named inputs
|
|
1200
|
+
let lowInput = this.querySelector("input.c-range__low");
|
|
1201
|
+
if (!lowInput) { lowInput = document.createElement("input"); lowInput.type = "range"; lowInput.className = "c-range c-range__low"; lowInput.setAttribute("part", "input"); this.appendChild(lowInput); }
|
|
1202
|
+
let highInput = this.querySelector("input.c-range__high");
|
|
1203
|
+
if (!highInput) { highInput = document.createElement("input"); highInput.type = "range"; highInput.className = "c-range c-range__high"; highInput.setAttribute("part", "input"); this.appendChild(highInput); }
|
|
1204
|
+
lowInput.min = min; lowInput.max = max;
|
|
1205
|
+
highInput.min = min; highInput.max = max;
|
|
1206
|
+
const step = this.getAttribute("step");
|
|
1207
|
+
if (step) { lowInput.step = step; highInput.step = step; } else { lowInput.removeAttribute("step"); highInput.removeAttribute("step"); }
|
|
1208
|
+
if (this.hasAttribute("disabled")) { lowInput.setAttribute("disabled", ""); highInput.setAttribute("disabled", ""); } else { lowInput.removeAttribute("disabled"); highInput.removeAttribute("disabled"); }
|
|
1209
|
+
const lowVal = this.getAttribute("low") || min;
|
|
1210
|
+
const highVal = this.getAttribute("high") || max;
|
|
1211
|
+
if (this._rangeUserLow !== null && this._rangeAttrLowAtDirty === (this.getAttribute("low") || min)) lowInput.value = this._rangeUserLow;
|
|
1212
|
+
else { lowInput.value = lowVal; this._rangeUserLow = null; this._rangeAttrLowAtDirty = null; }
|
|
1213
|
+
if (this._rangeUserHigh !== null && this._rangeAttrHighAtDirty === (this.getAttribute("high") || max)) highInput.value = this._rangeUserHigh;
|
|
1214
|
+
else { highInput.value = highVal; this._rangeUserHigh = null; this._rangeAttrHighAtDirty = null; }
|
|
1215
|
+
const label = this.getAttribute("label");
|
|
1216
|
+
if (label) { lowInput.setAttribute("aria-label", label + " low"); highInput.setAttribute("aria-label", label + " high"); }
|
|
1217
|
+
// Remove hidden mirrors from previous dual render.
|
|
1218
|
+
} else {
|
|
1219
|
+
// remove dual inputs and any leftover mirrors from a dual render
|
|
1220
|
+
Array.from(this.querySelectorAll("input.c-range__low, input.c-range__high, input[type='hidden'][data-range]")).forEach((n) => n.remove());
|
|
1221
|
+
const input = this.ensure("input.c-range", () => { const el = document.createElement("input"); el.type = "range"; el.setAttribute("part", "input"); return el; });
|
|
1222
|
+
// ensure class
|
|
1223
|
+
input.className = "c-range";
|
|
1224
|
+
input.type = "range";
|
|
1225
|
+
input.min = min;
|
|
1226
|
+
input.max = max;
|
|
1227
|
+
const step = this.getAttribute("step");
|
|
1228
|
+
if (step) input.step = step; else input.removeAttribute("step");
|
|
1229
|
+
if (this.hasAttribute("disabled")) input.setAttribute("disabled", ""); else input.removeAttribute("disabled");
|
|
1230
|
+
const attrVal = this.getAttribute("value") || "50";
|
|
1231
|
+
if (this._rangeUserValue !== null && this._rangeAttrAtDirty === (this.getAttribute("value") || "50")) {
|
|
1232
|
+
input.value = this._rangeUserValue;
|
|
1233
|
+
} else {
|
|
1234
|
+
input.value = attrVal;
|
|
1235
|
+
this._rangeUserValue = null;
|
|
1236
|
+
this._rangeAttrAtDirty = null;
|
|
1237
|
+
}
|
|
1238
|
+
const label = this.getAttribute("label");
|
|
1239
|
+
if (label) input.setAttribute("aria-label", label);
|
|
1240
|
+
else if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "range", "Range"));
|
|
1241
|
+
const updateFill = () => {
|
|
1242
|
+
const mn = Number(input.min) || 0;
|
|
1243
|
+
const mx = Number(input.max) || 100;
|
|
1244
|
+
const val = Number(input.value) || 0;
|
|
1245
|
+
const pct = mx > mn ? ((val - mn) / (mx - mn)) * 100 : 0;
|
|
1246
|
+
input.style.setProperty("--fill", pct + "%");
|
|
1247
|
+
};
|
|
1248
|
+
updateFill();
|
|
1249
|
+
if (!input._fillWired) {
|
|
1250
|
+
input._fillWired = true;
|
|
1251
|
+
input.addEventListener("input", updateFill);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
// R31-67: paint the sparkline series onto the canvas. The canvas has no
|
|
1256
|
+
// box until it is laid out, so the first draw waits a frame; a 0-width
|
|
1257
|
+
// canvas silently draws nothing.
|
|
1258
|
+
_drawRangeSpark(canvas, raw) {
|
|
1259
|
+
let nums = [];
|
|
1260
|
+
try { const p = JSON.parse(raw); if (Array.isArray(p)) nums = p.map(Number).filter(Number.isFinite); } catch (e) { /* not JSON */ }
|
|
1261
|
+
if (nums.length < 2) return;
|
|
1262
|
+
const draw = () => {
|
|
1263
|
+
if (!canvas.isConnected) return;
|
|
1264
|
+
const w = canvas.clientWidth, h = canvas.clientHeight;
|
|
1265
|
+
if (!w || !h) return;
|
|
1266
|
+
const dpr = (typeof window !== "undefined" && window.devicePixelRatio) || 1;
|
|
1267
|
+
canvas.width = Math.round(w * dpr);
|
|
1268
|
+
canvas.height = Math.round(h * dpr);
|
|
1269
|
+
const ctx = canvas.getContext && canvas.getContext("2d");
|
|
1270
|
+
if (!ctx) return;
|
|
1271
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
1272
|
+
ctx.clearRect(0, 0, w, h);
|
|
1273
|
+
const lo = Math.min.apply(null, nums), hi = Math.max.apply(null, nums), range = (hi - lo) || 1;
|
|
1274
|
+
const pad = 2;
|
|
1275
|
+
const x = (i) => (i / (nums.length - 1)) * (w - pad * 2) + pad;
|
|
1276
|
+
const y = (v) => h - pad - ((v - lo) / range) * (h - pad * 2);
|
|
1277
|
+
let stroke = "";
|
|
1278
|
+
try { stroke = getComputedStyle(this).getPropertyValue("--chart-1").trim(); } catch (e) { /* no computed style */ }
|
|
1279
|
+
if (!stroke || /^(var|light-dark)\(/.test(stroke)) stroke = "#1f77b4";
|
|
1280
|
+
ctx.beginPath();
|
|
1281
|
+
ctx.lineWidth = 1.5;
|
|
1282
|
+
ctx.strokeStyle = stroke;
|
|
1283
|
+
nums.forEach((v, i) => { const px = x(i), py = y(v); if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); });
|
|
1284
|
+
ctx.stroke();
|
|
1285
|
+
};
|
|
1286
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(draw); else draw();
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
// patch enhance onto ContourRange
|
|
1290
|
+
const _origRangeRender = ContourRange.prototype.render;
|
|
1291
|
+
ContourRange.prototype.attributeChangedCallback = function(name, oldValue, newValue) {
|
|
1292
|
+
if (name === "value") { this._rangeUserValue = null; this._rangeAttrAtDirty = null; }
|
|
1293
|
+
if (name === "low") { this._rangeUserLow = null; this._rangeAttrLowAtDirty = null; }
|
|
1294
|
+
if (name === "high") { this._rangeUserHigh = null; this._rangeAttrHighAtDirty = null; }
|
|
1295
|
+
return ContourBase.prototype.attributeChangedCallback.call(this, name, oldValue, newValue);
|
|
1296
|
+
};
|
|
1297
|
+
// Form value (R20 T-08): single submits handle value; dual submits two entries via FormData.
|
|
1298
|
+
ContourRange.prototype._syncFormValue = function() {
|
|
1299
|
+
if (!this._internals) return;
|
|
1300
|
+
const low = this.querySelector("input.c-range__low");
|
|
1301
|
+
const high = this.querySelector("input.c-range__high");
|
|
1302
|
+
if (low && high) {
|
|
1303
|
+
const name = this.getAttribute("name");
|
|
1304
|
+
if (name) {
|
|
1305
|
+
const fd = new FormData();
|
|
1306
|
+
fd.append(name, low.value);
|
|
1307
|
+
fd.append(name, high.value);
|
|
1308
|
+
this._internals.setFormValue(fd);
|
|
1309
|
+
} else {
|
|
1310
|
+
this._internals.setFormValue(null);
|
|
1311
|
+
}
|
|
1312
|
+
} else {
|
|
1313
|
+
const single = this.querySelector("input.c-range:not(.c-range__low):not(.c-range__high)");
|
|
1314
|
+
if (single) this._internals.setFormValue(single.value);
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
ContourRange.prototype.formResetCallback = function() {
|
|
1318
|
+
// Clear user-drag tracking so re-render reads authored defaults under resetting flag.
|
|
1319
|
+
this._contourResetting = true;
|
|
1320
|
+
try {
|
|
1321
|
+
this._rangeUserValue = null; this._rangeAttrAtDirty = null;
|
|
1322
|
+
this._rangeUserLow = null; this._rangeAttrLowAtDirty = null;
|
|
1323
|
+
this._rangeUserHigh = null; this._rangeAttrHighAtDirty = null;
|
|
1324
|
+
if (this.isConnected && this._contourAuthored) this.attributeChangedCallback("__rerender__", null, "");
|
|
1325
|
+
this._syncFormValue();
|
|
1326
|
+
} finally {
|
|
1327
|
+
this._contourResetting = false;
|
|
1328
|
+
}
|
|
1329
|
+
};
|
|
1330
|
+
ContourRange.prototype.formStateRestoreCallback = function(state) {
|
|
1331
|
+
if (!this._internals || state === null || state === undefined) return;
|
|
1332
|
+
const low = this.querySelector("input.c-range__low");
|
|
1333
|
+
const high = this.querySelector("input.c-range__high");
|
|
1334
|
+
if (low && high && state instanceof FormData) {
|
|
1335
|
+
const vals = state.getAll(this.getAttribute("name"));
|
|
1336
|
+
if (vals.length >= 2) { low.value = String(vals[0]); high.value = String(vals[1]); }
|
|
1337
|
+
} else {
|
|
1338
|
+
const single = this.querySelector("input.c-range:not(.c-range__low):not(.c-range__high)");
|
|
1339
|
+
if (single) single.value = String(state);
|
|
1340
|
+
}
|
|
1341
|
+
this._syncFormValue();
|
|
1342
|
+
};
|
|
1343
|
+
ContourRange.prototype.enhance = function() {
|
|
1344
|
+
if (this._wiredRange) return;
|
|
1345
|
+
this._wiredRange = true;
|
|
1346
|
+
// R24-59: sync form value on every render, not only after _wiredRange guard.
|
|
1347
|
+
this._syncFormValue();
|
|
1348
|
+
const sync = () => { this._syncFormValue(); };
|
|
1349
|
+
this.addEventListener("input", (e) => {
|
|
1350
|
+
const low = this.querySelector("input.c-range__low");
|
|
1351
|
+
const high = this.querySelector("input.c-range__high");
|
|
1352
|
+
const single = this.querySelector("input.c-range:not(.c-range__low):not(.c-range__high)");
|
|
1353
|
+
if (low && high && (e.target === low || e.target === high)) {
|
|
1354
|
+
this._rangeUserLow = low.value;
|
|
1355
|
+
this._rangeUserHigh = high.value;
|
|
1356
|
+
this._rangeAttrLowAtDirty = this.getAttribute("low") || (this.getAttribute("min") || "0");
|
|
1357
|
+
this._rangeAttrHighAtDirty = this.getAttribute("high") || (this.getAttribute("max") || "100");
|
|
1358
|
+
sync();
|
|
1359
|
+
this._emitForm("contour:input", { low: Number(low.value), high: Number(high.value) });
|
|
1360
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { low: Number(low.value), high: Number(high.value) } }));
|
|
1361
|
+
} else if (single && e.target === single) {
|
|
1362
|
+
this._rangeUserValue = single.value;
|
|
1363
|
+
this._rangeAttrAtDirty = this.getAttribute("value") || "50";
|
|
1364
|
+
sync();
|
|
1365
|
+
this._emitForm("contour:input");
|
|
1366
|
+
}
|
|
1367
|
+
});
|
|
1368
|
+
this.addEventListener("change", (e) => {
|
|
1369
|
+
const low = this.querySelector("input.c-range__low");
|
|
1370
|
+
const high = this.querySelector("input.c-range__high");
|
|
1371
|
+
const single = this.querySelector("input.c-range:not(.c-range__low):not(.c-range__high)");
|
|
1372
|
+
if (low && high && (e.target === low || e.target === high)) {
|
|
1373
|
+
sync();
|
|
1374
|
+
this._emitForm("contour:change", { low: Number(low.value), high: Number(high.value) });
|
|
1375
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { low: Number(low.value), high: Number(high.value) } }));
|
|
1376
|
+
} else if (single && e.target === single) {
|
|
1377
|
+
sync();
|
|
1378
|
+
this._emitForm("contour:change");
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
sync();
|
|
1382
|
+
};
|
|
1383
|
+
define("contour-range", ContourRange);
|
|
1384
|
+
/**
|
|
1385
|
+
* Switch: a `role="switch"` checkbox control with label, required, disabled,
|
|
1386
|
+
* error and help states. Form associated; emits `contour:change` on toggle.
|
|
1387
|
+
* The input's ::after draws the track; there is no separate track element.
|
|
1388
|
+
*/
|
|
1389
|
+
class ContourSwitch extends ContourBase {
|
|
1390
|
+
static get observed() { return ["label", "required", "checked", "disabled", "error", "help"]; }
|
|
1391
|
+
static get properties() { return { label: { attribute: "label" }, required: { attribute: "required", type: Boolean }, checked: { attribute: "checked", type: Boolean }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" } }; }
|
|
1392
|
+
static get formAssociated() { return true; }
|
|
1393
|
+
render() {
|
|
1394
|
+
this.classList.add("c-switch");
|
|
1395
|
+
// Authored default captured before toggle reflects into checked.
|
|
1396
|
+
if (this._switchDefaultChecked === undefined) this._switchDefaultChecked = this.hasAttribute("checked");
|
|
1397
|
+
const label = this.getAttribute("label");
|
|
1398
|
+
// Guide's control is label.c-switch wrapping input; sheet paints track on input.
|
|
1399
|
+
const input = this.ensure("input.c-switch__input", () => { const el = document.createElement("input"); el.className = "c-switch__input"; el.type = "checkbox"; return el; });
|
|
1400
|
+
input.checked = this.hasAttribute("checked");
|
|
1401
|
+
input.setAttribute("role", "switch");
|
|
1402
|
+
input.setAttribute("aria-checked", String(input.checked));
|
|
1403
|
+
this._setCustomState("data-checked", input.checked);
|
|
1404
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1405
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1406
|
+
const err = this.getAttribute("error");
|
|
1407
|
+
const help = this.getAttribute("help");
|
|
1408
|
+
let msg = this.querySelector(".c-switch__message");
|
|
1409
|
+
if (err || help) {
|
|
1410
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-switch__message"; this.appendChild(msg); }
|
|
1411
|
+
msg.textContent = err || help || "";
|
|
1412
|
+
msg.classList.toggle("c-switch__message--error", !!err);
|
|
1413
|
+
} else if (msg) { msg.remove(); }
|
|
1414
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1415
|
+
// Input's ::after draws track; separate track span would be unstyled (R18-41).
|
|
1416
|
+
if (label) {
|
|
1417
|
+
input.setAttribute("aria-label", label);
|
|
1418
|
+
const txt = this.querySelector(":scope > span:not([class])") || (() => { const el = document.createElement("span"); this.appendChild(el); return el; })();
|
|
1419
|
+
txt.textContent = label;
|
|
1420
|
+
}
|
|
1421
|
+
if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "switch", "Switch"));
|
|
1422
|
+
}
|
|
1423
|
+
enhance() {
|
|
1424
|
+
const input = this.querySelector("input");
|
|
1425
|
+
if (input && this._internals) {
|
|
1426
|
+
input.setAttribute("aria-checked", String(input.checked));
|
|
1427
|
+
this._setCustomState("data-checked", input.checked);
|
|
1428
|
+
this._internals.setFormValue(input.checked ? (input.value || "on") : null);
|
|
1429
|
+
this._syncValidity(this.hasAttribute("required") && !input.checked);
|
|
1430
|
+
}
|
|
1431
|
+
if (this._wiredSwitch) return;
|
|
1432
|
+
this._wiredSwitch = true;
|
|
1433
|
+
// Label activation: host not native label, click text/track toggles input; input click left to platform.
|
|
1434
|
+
this.addEventListener("click", (e) => {
|
|
1435
|
+
if (e.target && e.target.closest && e.target.closest("input")) return;
|
|
1436
|
+
const inp = this.querySelector("input");
|
|
1437
|
+
if (!inp || inp.disabled) return;
|
|
1438
|
+
e.preventDefault();
|
|
1439
|
+
inp.click();
|
|
1440
|
+
});
|
|
1441
|
+
this.addEventListener("change", (e) => {
|
|
1442
|
+
const inp = this.querySelector("input");
|
|
1443
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
1444
|
+
inp.setAttribute("aria-checked", String(inp.checked));
|
|
1445
|
+
this._setCustomState("data-checked", inp.checked);
|
|
1446
|
+
this._setCustomState("data-indeterminate", false);
|
|
1447
|
+
this.removeAttribute("indeterminate");
|
|
1448
|
+
this.toggleAttribute("checked", inp.checked);
|
|
1449
|
+
this._internals.setFormValue(inp.checked ? (inp.value || "on") : null);
|
|
1450
|
+
this._emitForm("contour:change");
|
|
1451
|
+
this._syncValidity(this.hasAttribute("required") && !inp.checked);
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
formResetCallback() {
|
|
1455
|
+
// checked reflected on toggle; authored default from first-render capture (R20 T-08).
|
|
1456
|
+
this._contourResetting = true;
|
|
1457
|
+
try {
|
|
1458
|
+
this.toggleAttribute("checked", !!this._switchDefaultChecked);
|
|
1459
|
+
const inp = this.querySelector("input");
|
|
1460
|
+
if (inp) inp.checked = this.hasAttribute("checked");
|
|
1461
|
+
this._syncFormValue();
|
|
1462
|
+
} finally {
|
|
1463
|
+
this._contourResetting = false;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
define("contour-switch", ContourSwitch);
|
|
1468
|
+
/**
|
|
1469
|
+
* Check: a checkbox (or `type="radio"` when `type` is set) with label, name,
|
|
1470
|
+
* value, required, checked, disabled, error and help states. The input's
|
|
1471
|
+
* ::after draws the box; there is no separate box element. Form associated;
|
|
1472
|
+
* emits `contour:change` on toggle.
|
|
1473
|
+
*/
|
|
1474
|
+
class ContourCheck extends ContourBase {
|
|
1475
|
+
static get observed() { return ["label", "name", "value", "required", "type", "checked", "indeterminate", "disabled", "error", "help"]; }
|
|
1476
|
+
static get properties() { return { label: { attribute: "label" }, name: { attribute: "name" }, value: { attribute: "value" }, required: { attribute: "required", type: Boolean }, type: { attribute: "type" }, checked: { attribute: "checked", type: Boolean }, indeterminate: { attribute: "indeterminate", type: Boolean }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" } }; }
|
|
1477
|
+
static get formAssociated() { return true; }
|
|
1478
|
+
render() {
|
|
1479
|
+
this.classList.add("c-check");
|
|
1480
|
+
// Authored default captured before toggle reflects into checked.
|
|
1481
|
+
if (this._checkDefaultChecked === undefined) this._checkDefaultChecked = this.hasAttribute("checked");
|
|
1482
|
+
const label = this.getAttribute("label");
|
|
1483
|
+
const input = this.ensure("input.c-check__input", () => { const el = document.createElement("input"); el.className = "c-check__input"; el.type = this.getAttribute("type") === "radio" ? "radio" : "checkbox"; return el; });
|
|
1484
|
+
input.checked = this.hasAttribute("checked");
|
|
1485
|
+
input.indeterminate = this.hasAttribute("indeterminate") && !input.checked;
|
|
1486
|
+
// Input's ::after draws box; separate box span would be unstyled (R18-41).
|
|
1487
|
+
if (label) {
|
|
1488
|
+
input.setAttribute("aria-label", label);
|
|
1489
|
+
const txt = this.querySelector(":scope > span:not([class])") || (() => { const el = document.createElement("span"); this.appendChild(el); return el; })();
|
|
1490
|
+
txt.textContent = label;
|
|
1491
|
+
}
|
|
1492
|
+
if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "checkbox", "Checkbox"));
|
|
1493
|
+
if (this.hasAttribute("name")) input.name = this.getAttribute("name");
|
|
1494
|
+
if (this.hasAttribute("value")) input.value = this.getAttribute("value");
|
|
1495
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1496
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1497
|
+
const err = this.getAttribute("error");
|
|
1498
|
+
const help = this.getAttribute("help");
|
|
1499
|
+
let msg = this.querySelector(".c-check__message");
|
|
1500
|
+
if (err || help) {
|
|
1501
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-check__message"; this.appendChild(msg); }
|
|
1502
|
+
msg.textContent = err || help || "";
|
|
1503
|
+
msg.classList.toggle("c-check__message--error", !!err);
|
|
1504
|
+
} else if (msg) { msg.remove(); }
|
|
1505
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1506
|
+
}
|
|
1507
|
+
enhance() {
|
|
1508
|
+
const input = this.querySelector("input");
|
|
1509
|
+
if (input && this._internals) {
|
|
1510
|
+
this._setCustomState("data-checked", input.checked);
|
|
1511
|
+
this._setCustomState("data-indeterminate", input.indeterminate);
|
|
1512
|
+
this._internals.setFormValue(input.checked ? (input.value || "on") : null);
|
|
1513
|
+
this._syncValidity(this.hasAttribute("required") && !input.checked);
|
|
1514
|
+
}
|
|
1515
|
+
if (this._wiredCheck) return;
|
|
1516
|
+
this._wiredCheck = true;
|
|
1517
|
+
// Label activation: host not native label, click text/box toggles input; input click left to platform.
|
|
1518
|
+
this.addEventListener("click", (e) => {
|
|
1519
|
+
if (e.target && e.target.closest && e.target.closest("input")) return;
|
|
1520
|
+
const inp = this.querySelector("input");
|
|
1521
|
+
if (!inp || inp.disabled) return;
|
|
1522
|
+
e.preventDefault();
|
|
1523
|
+
inp.click();
|
|
1524
|
+
});
|
|
1525
|
+
this.addEventListener("change", (e) => {
|
|
1526
|
+
const inp = this.querySelector("input");
|
|
1527
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
1528
|
+
this._setCustomState("data-checked", inp.checked);
|
|
1529
|
+
this._setCustomState("data-indeterminate", false);
|
|
1530
|
+
this.removeAttribute("indeterminate");
|
|
1531
|
+
this.toggleAttribute("checked", inp.checked);
|
|
1532
|
+
this._internals.setFormValue(inp.checked ? (inp.value || "on") : null);
|
|
1533
|
+
this._emitForm("contour:change");
|
|
1534
|
+
this._syncValidity(this.hasAttribute("required") && !inp.checked);
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
formResetCallback() {
|
|
1538
|
+
// Same default-capture contract as contour-switch (R20 T-08).
|
|
1539
|
+
this._contourResetting = true;
|
|
1540
|
+
try {
|
|
1541
|
+
this.toggleAttribute("checked", !!this._checkDefaultChecked);
|
|
1542
|
+
const inp = this.querySelector("input");
|
|
1543
|
+
if (inp) inp.checked = this.hasAttribute("checked");
|
|
1544
|
+
this._syncFormValue();
|
|
1545
|
+
} finally {
|
|
1546
|
+
this._contourResetting = false;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
define("contour-check", ContourCheck);
|
|
1551
|
+
/**
|
|
1552
|
+
* Radio: a radio button with label, name, value, required, checked, disabled,
|
|
1553
|
+
* error and help states. Form associated; unchecking clears the stale FormData
|
|
1554
|
+
* entry. Emits `contour:change` on toggle.
|
|
1555
|
+
*/
|
|
1556
|
+
class ContourRadio extends ContourBase {
|
|
1557
|
+
static get observed() { return ["label", "name", "value", "required", "checked", "disabled", "error", "help"]; }
|
|
1558
|
+
static get properties() { return { label: { attribute: "label" }, name: { attribute: "name" }, value: { attribute: "value" }, required: { attribute: "required", type: Boolean }, checked: { attribute: "checked", type: Boolean }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" } }; }
|
|
1559
|
+
static get formAssociated() { return true; }
|
|
1560
|
+
render() {
|
|
1561
|
+
this.classList.add("c-radio");
|
|
1562
|
+
const label = this.getAttribute("label");
|
|
1563
|
+
const input = this.ensure("input.c-radio__input", () => { const el = document.createElement("input"); el.className = "c-radio__input"; el.type = "radio"; return el; });
|
|
1564
|
+
input.checked = this.hasAttribute("checked");
|
|
1565
|
+
if (this.hasAttribute("name")) input.name = this.getAttribute("name");
|
|
1566
|
+
if (this.hasAttribute("value")) input.value = this.getAttribute("value");
|
|
1567
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1568
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1569
|
+
const err = this.getAttribute("error");
|
|
1570
|
+
const help = this.getAttribute("help");
|
|
1571
|
+
let msg = this.querySelector(".c-radio__message");
|
|
1572
|
+
if (err || help) {
|
|
1573
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-radio__message"; this.appendChild(msg); }
|
|
1574
|
+
msg.textContent = err || help || "";
|
|
1575
|
+
msg.classList.toggle("c-radio__message--error", !!err);
|
|
1576
|
+
} else if (msg) { msg.remove(); }
|
|
1577
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1578
|
+
if (label) {
|
|
1579
|
+
input.setAttribute("aria-label", label);
|
|
1580
|
+
const txt = this.querySelector(":scope > span:not([class])") || (() => { const el = document.createElement("span"); this.appendChild(el); return el; })();
|
|
1581
|
+
txt.textContent = label;
|
|
1582
|
+
}
|
|
1583
|
+
if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "radio", "Radio"));
|
|
1584
|
+
}
|
|
1585
|
+
enhance() {
|
|
1586
|
+
const input = this.querySelector("input");
|
|
1587
|
+
if (input && this._internals) {
|
|
1588
|
+
this._setCustomState("data-checked", input.checked);
|
|
1589
|
+
this._setCustomState("data-indeterminate", input.indeterminate);
|
|
1590
|
+
this._internals.setFormValue(input.checked ? (input.value || "on") : null);
|
|
1591
|
+
this._syncValidity(this.hasAttribute("required") && !input.checked);
|
|
1592
|
+
}
|
|
1593
|
+
if (this._wiredRadio) return;
|
|
1594
|
+
this._wiredRadio = true;
|
|
1595
|
+
// Label activation: host not native label, click text selects radio; input click left to platform.
|
|
1596
|
+
this.addEventListener("click", (e) => {
|
|
1597
|
+
if (e.target && e.target.closest && e.target.closest("input")) return;
|
|
1598
|
+
const inp = this.querySelector("input");
|
|
1599
|
+
if (!inp || inp.disabled) return;
|
|
1600
|
+
e.preventDefault();
|
|
1601
|
+
inp.click();
|
|
1602
|
+
});
|
|
1603
|
+
this.addEventListener("change", (e) => {
|
|
1604
|
+
const inp = this.querySelector("input");
|
|
1605
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
1606
|
+
// Unchecking (group movement) must clear stale FormData entry.
|
|
1607
|
+
this._setCustomState("data-checked", inp.checked);
|
|
1608
|
+
this._internals.setFormValue(inp.checked ? (inp.value || "on") : null);
|
|
1609
|
+
this._emitForm("contour:change");
|
|
1610
|
+
this._syncValidity(this.hasAttribute("required") && !inp.checked);
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
define("contour-radio", ContourRadio);
|
|
1615
|
+
/**
|
|
1616
|
+
* Select: a native `<select>` wrapper populated from a JSON `options` attribute
|
|
1617
|
+
* (with optgroup support) or an authored `<select>`. Supports label, placeholder,
|
|
1618
|
+
* required, disabled, error and help. Form associated; emits `contour:change`.
|
|
1619
|
+
*/
|
|
1620
|
+
class ContourSelect extends ContourBase {
|
|
1621
|
+
static get observed() { return ["options", "items", "required", "label", "placeholder", "disabled", "error", "help", "value", "name", "multiple", "autocomplete"]; }
|
|
1622
|
+
static get properties() { return { options: { attribute: "options" }, items: { attribute: "items" }, required: { attribute: "required", type: Boolean }, label: { attribute: "label" }, placeholder: { attribute: "placeholder" }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" }, value: { attribute: "value" }, name: { attribute: "name" }, multiple: { attribute: "multiple", type: Boolean }, autocomplete: { attribute: "autocomplete" } }; }
|
|
1623
|
+
static get formAssociated() { return true; }
|
|
1624
|
+
render() {
|
|
1625
|
+
this.classList.add("c-select");
|
|
1626
|
+
// Authored default captured before commit reflects into value.
|
|
1627
|
+
if (this._selectDefault === undefined) this._selectDefault = this.getAttribute("value");
|
|
1628
|
+
const options = this.getAttribute("options") || this.getAttribute("items");
|
|
1629
|
+
// Slot composition: authored <select> wins over JSON attribute.
|
|
1630
|
+
const generated = new Set(this._contourGenerated || []);
|
|
1631
|
+
const authored = Array.from(this.children).filter((n) => !generated.has(n));
|
|
1632
|
+
if (options && !authored.length) {
|
|
1633
|
+
try {
|
|
1634
|
+
const parsed = JSON.parse(options);
|
|
1635
|
+
const select = this.ensure("select.c-select", () => { const el = document.createElement("select"); el.className = "c-select"; return el; });
|
|
1636
|
+
select.innerHTML = "";
|
|
1637
|
+
const label = this.getAttribute("label");
|
|
1638
|
+
if (label) {
|
|
1639
|
+
select.setAttribute("aria-label", label);
|
|
1640
|
+
}
|
|
1641
|
+
const placeholder = this.getAttribute("placeholder");
|
|
1642
|
+
if (placeholder) {
|
|
1643
|
+
const ph = document.createElement("option");
|
|
1644
|
+
ph.value = "";
|
|
1645
|
+
ph.textContent = placeholder;
|
|
1646
|
+
ph.disabled = true;
|
|
1647
|
+
ph.selected = true;
|
|
1648
|
+
select.appendChild(ph);
|
|
1649
|
+
}
|
|
1650
|
+
// Group support.
|
|
1651
|
+
const groups = {};
|
|
1652
|
+
parsed.forEach((opt) => {
|
|
1653
|
+
const g = (typeof opt === "object" && opt !== null) ? opt.group : null;
|
|
1654
|
+
if (g) {
|
|
1655
|
+
if (!groups[g]) { groups[g] = document.createElement("optgroup"); groups[g].label = g; select.appendChild(groups[g]); }
|
|
1656
|
+
const option = document.createElement("option");
|
|
1657
|
+
option.value = typeof opt === "string" ? opt : opt.value;
|
|
1658
|
+
option.textContent = typeof opt === "string" ? opt : opt.label;
|
|
1659
|
+
groups[g].appendChild(option);
|
|
1660
|
+
} else {
|
|
1661
|
+
const option = document.createElement("option");
|
|
1662
|
+
option.value = typeof opt === "string" ? opt : opt.value;
|
|
1663
|
+
option.textContent = typeof opt === "string" ? opt : opt.label;
|
|
1664
|
+
select.appendChild(option);
|
|
1665
|
+
}
|
|
1666
|
+
});
|
|
1667
|
+
if (select.parentNode !== this) this.appendChild(select);
|
|
1668
|
+
this.classList.remove("c-select");
|
|
1669
|
+
select.classList.add("c-select");
|
|
1670
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
1671
|
+
}
|
|
1672
|
+
// Ensure authored select gets c-select styling.
|
|
1673
|
+
const authoredSel = this.querySelector("select");
|
|
1674
|
+
if (authoredSel && !authoredSel.classList.contains("c-select")) {
|
|
1675
|
+
authoredSel.classList.add("c-select");
|
|
1676
|
+
this.classList.remove("c-select");
|
|
1677
|
+
}
|
|
1678
|
+
// Disabled / error / help contract (applies to authored or generated select).
|
|
1679
|
+
const sel = this.querySelector("select");
|
|
1680
|
+
if (sel) {
|
|
1681
|
+
if (this.hasAttribute("disabled")) { sel.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1682
|
+
else { sel.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1683
|
+
// Reflect native attrs: FormData needs name, multi-select needs multiple, value is committed selection.
|
|
1684
|
+
if (this.hasAttribute("name")) sel.setAttribute("name", this.getAttribute("name"));
|
|
1685
|
+
if (this.hasAttribute("multiple")) sel.setAttribute("multiple", ""); else sel.removeAttribute("multiple");
|
|
1686
|
+
if (this.hasAttribute("autocomplete")) sel.setAttribute("autocomplete", this.getAttribute("autocomplete"));
|
|
1687
|
+
if (this.hasAttribute("required")) sel.setAttribute("required", ""); else sel.removeAttribute("required");
|
|
1688
|
+
const val = this.getAttribute("value");
|
|
1689
|
+
if (val !== null) {
|
|
1690
|
+
if (sel.multiple) {
|
|
1691
|
+
const wanted = new Set(String(val).split(",").map((v) => v.trim()).filter(Boolean));
|
|
1692
|
+
Array.from(sel.options).forEach((o) => { o.selected = wanted.has(o.value); });
|
|
1693
|
+
} else {
|
|
1694
|
+
sel.value = val;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
// Label (applies to both JSON and authored options).
|
|
1699
|
+
const label = this.getAttribute("label");
|
|
1700
|
+
if (label) {
|
|
1701
|
+
let lbl = this.querySelector("label.c-select__label");
|
|
1702
|
+
if (!lbl) { lbl = document.createElement("label"); lbl.className = "c-select__label"; this.insertBefore(lbl, sel || this.firstChild); }
|
|
1703
|
+
lbl.textContent = label;
|
|
1704
|
+
if (sel) {
|
|
1705
|
+
if (!sel.id) sel.id = contourId("contour-select");
|
|
1706
|
+
lbl.setAttribute("for", sel.id);
|
|
1707
|
+
sel.setAttribute("aria-label", label);
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
if (!this.querySelector("select")?.hasAttribute("aria-label")) {
|
|
1711
|
+
const sel = this.querySelector("select");
|
|
1712
|
+
if (sel) sel.setAttribute("aria-label", labelText(this, "select", "Select"));
|
|
1713
|
+
}
|
|
1714
|
+
const err = this.getAttribute("error");
|
|
1715
|
+
const help = this.getAttribute("help");
|
|
1716
|
+
let msg = this.querySelector(".c-select__message");
|
|
1717
|
+
if (err || help) {
|
|
1718
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-select__message"; this.appendChild(msg); }
|
|
1719
|
+
msg.textContent = err || help || "";
|
|
1720
|
+
msg.classList.toggle("c-select__message--error", !!err);
|
|
1721
|
+
} else if (msg) { msg.remove(); }
|
|
1722
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1723
|
+
}
|
|
1724
|
+
enhance() {
|
|
1725
|
+
if (this._internals) this._syncFormValue();
|
|
1726
|
+
if (this._wiredSelect) return;
|
|
1727
|
+
this._wiredSelect = true;
|
|
1728
|
+
this.addEventListener("input", (e) => {
|
|
1729
|
+
const sel = this.querySelector("select");
|
|
1730
|
+
if (!sel || e.target !== sel) return;
|
|
1731
|
+
this._emitForm("contour:input");
|
|
1732
|
+
});
|
|
1733
|
+
this.addEventListener("change", (e) => {
|
|
1734
|
+
const sel = this.querySelector("select");
|
|
1735
|
+
if (!sel || e.target !== sel || !this._internals) return;
|
|
1736
|
+
// Reflect whole selection: multi-select sel.value is first only; reflected attr rebuilds on re-render (R20 T-06).
|
|
1737
|
+
const committed = sel.multiple ? Array.from(sel.selectedOptions).map((o) => o.value).join(",") : sel.value;
|
|
1738
|
+
this.setAttribute("value", committed);
|
|
1739
|
+
this._syncFormValue();
|
|
1740
|
+
this._emitForm("contour:change");
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
// Multi-select submits every selected option under the host name (not select.value); base generic must not collapse to one entry.
|
|
1744
|
+
_syncFormValue() {
|
|
1745
|
+
if (!this._internals) return;
|
|
1746
|
+
const sel = this.querySelector("select");
|
|
1747
|
+
if (!sel) return;
|
|
1748
|
+
if (sel.multiple) {
|
|
1749
|
+
const chosen = Array.from(sel.selectedOptions).map((o) => o.value);
|
|
1750
|
+
const name = this.getAttribute("name");
|
|
1751
|
+
if (name && chosen.length) {
|
|
1752
|
+
const fd = new FormData();
|
|
1753
|
+
for (const v of chosen) fd.append(name, v);
|
|
1754
|
+
this._internals.setFormValue(fd);
|
|
1755
|
+
} else {
|
|
1756
|
+
this._internals.setFormValue(null);
|
|
1757
|
+
}
|
|
1758
|
+
this._syncValidity(this.hasAttribute("required") && !chosen.length);
|
|
1759
|
+
} else {
|
|
1760
|
+
this._internals.setFormValue(sel.value);
|
|
1761
|
+
this._syncValidity(this.hasAttribute("required") && !sel.value);
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
formResetCallback() {
|
|
1765
|
+
// Restore authored default (not first option or last commit); resetting flag keeps base restore out.
|
|
1766
|
+
this._contourResetting = true;
|
|
1767
|
+
try {
|
|
1768
|
+
const def = this._selectDefault ?? null;
|
|
1769
|
+
if (def === null) this.removeAttribute("value");
|
|
1770
|
+
else this.setAttribute("value", def);
|
|
1771
|
+
this._syncFormValue();
|
|
1772
|
+
} finally {
|
|
1773
|
+
this._contourResetting = false;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
formStateRestoreCallback(state) {
|
|
1777
|
+
const sel = this.querySelector("select");
|
|
1778
|
+
if (!sel || !this._internals) return;
|
|
1779
|
+
const values = state instanceof FormData
|
|
1780
|
+
? Array.from(state.getAll(this.getAttribute("name"))).map(String)
|
|
1781
|
+
: (state == null ? [] : String(state).split(",").map((v) => v.trim()).filter(Boolean));
|
|
1782
|
+
this._contourResetting = true;
|
|
1783
|
+
try {
|
|
1784
|
+
if (sel.multiple) {
|
|
1785
|
+
const wanted = new Set(values);
|
|
1786
|
+
for (const o of sel.options) o.selected = wanted.has(o.value);
|
|
1787
|
+
this.setAttribute("value", values.join(","));
|
|
1788
|
+
} else {
|
|
1789
|
+
sel.value = values[0] || "";
|
|
1790
|
+
this.setAttribute("value", sel.value);
|
|
1791
|
+
}
|
|
1792
|
+
this._syncFormValue();
|
|
1793
|
+
} finally {
|
|
1794
|
+
this._contourResetting = false;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
define("contour-select", ContourSelect);
|
|
1799
|
+
|
|
1800
|
+
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* Version: a `<select>` of version options from a JSON `options` attribute,
|
|
1805
|
+
* with `selected` support per option. Form associated; emits `contour:change`.
|
|
1806
|
+
*/
|
|
1807
|
+
class ContourVersionControl extends ContourBase {
|
|
1808
|
+
static get observed() { return ["options", "items", "value", "name"]; }
|
|
1809
|
+
static get formAssociated() { return true; }
|
|
1810
|
+
static get properties() { return { options: { attribute: "options" }, items: { attribute: "items" }, value: { attribute: "value" }, name: { attribute: "name" } }; }
|
|
1811
|
+
render() {
|
|
1812
|
+
this.classList.add("c-version");
|
|
1813
|
+
// Authored default captured before any commit reflects into `value`.
|
|
1814
|
+
if (this._versionDefault === undefined) this._versionDefault = this.getAttribute("value");
|
|
1815
|
+
const options = this.getAttribute("options") || this.getAttribute("items");
|
|
1816
|
+
const generated = new Set(this._contourGenerated || []);
|
|
1817
|
+
const authored = Array.from(this.children).filter((n) => !generated.has(n));
|
|
1818
|
+
if (options && !authored.length) {
|
|
1819
|
+
try {
|
|
1820
|
+
const parsed = JSON.parse(options);
|
|
1821
|
+
const select = this.ensure("select.c-version", () => { const el = document.createElement("select"); el.className = "c-version"; return el; });
|
|
1822
|
+
select.innerHTML = "";
|
|
1823
|
+
if (!select.hasAttribute("aria-label")) select.setAttribute("aria-label", labelText(this, "version", "Version"));
|
|
1824
|
+
parsed.forEach((opt) => {
|
|
1825
|
+
const option = document.createElement("option");
|
|
1826
|
+
if (typeof opt === "string") { option.value = opt; option.textContent = opt; }
|
|
1827
|
+
else { option.value = opt.value || opt.label; option.textContent = opt.label || opt.value; if (opt.selected) option.selected = true; }
|
|
1828
|
+
select.appendChild(option);
|
|
1829
|
+
});
|
|
1830
|
+
if (select.parentNode !== this) this.appendChild(select);
|
|
1831
|
+
this.classList.remove("c-version");
|
|
1832
|
+
select.classList.add("c-version");
|
|
1833
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
1834
|
+
}
|
|
1835
|
+
// The committed value selects the option (R20 T-07): without this the
|
|
1836
|
+
// host always submitted the first option regardless of `value`.
|
|
1837
|
+
const sel = this.querySelector("select");
|
|
1838
|
+
if (sel) {
|
|
1839
|
+
if (this.hasAttribute("name")) sel.setAttribute("name", this.getAttribute("name"));
|
|
1840
|
+
const val = this.getAttribute("value");
|
|
1841
|
+
if (val !== null) sel.value = val;
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
enhance() {
|
|
1845
|
+
const sync = () => {
|
|
1846
|
+
const select = this.querySelector("select");
|
|
1847
|
+
if (!select || !this._internals) return;
|
|
1848
|
+
this._internals.setFormValue(select.value);
|
|
1849
|
+
};
|
|
1850
|
+
sync();
|
|
1851
|
+
if (this._wiredVersion) return;
|
|
1852
|
+
this._wiredVersion = true;
|
|
1853
|
+
this.addEventListener("change", (e) => {
|
|
1854
|
+
const select = this.querySelector("select");
|
|
1855
|
+
if (!select || e.target !== select) return;
|
|
1856
|
+
// Reflect the committed selection so a consumer reading `value` sees
|
|
1857
|
+
// what the user picked, and a re-render rebuilds it.
|
|
1858
|
+
this.setAttribute("value", select.value);
|
|
1859
|
+
sync();
|
|
1860
|
+
this._emitForm("contour:change");
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
formResetCallback() {
|
|
1864
|
+
// Restore the authored default, not the first option (R20 T-08).
|
|
1865
|
+
this._contourResetting = true;
|
|
1866
|
+
try {
|
|
1867
|
+
const def = this._versionDefault ?? null;
|
|
1868
|
+
if (def === null) this.removeAttribute("value");
|
|
1869
|
+
else this.setAttribute("value", def);
|
|
1870
|
+
this._syncFormValue();
|
|
1871
|
+
} finally {
|
|
1872
|
+
this._contourResetting = false;
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
define("contour-version", ContourVersionControl);
|
|
1877
|
+
/**
|
|
1878
|
+
* Textarea: a `<textarea>` wrapper with placeholder, required, disabled, error
|
|
1879
|
+
* and help states. Form associated; emits `contour:input` / `contour:change`.
|
|
1880
|
+
*/
|
|
1881
|
+
class ContourTextarea extends ContourBase {
|
|
1882
|
+
static get observed() { return ["placeholder", "required", "disabled", "error", "help", "value", "name", "label"]; }
|
|
1883
|
+
static get properties() { return { placeholder: { attribute: "placeholder" }, required: { attribute: "required", type: Boolean }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" }, value: { attribute: "value" }, name: { attribute: "name" }, label: { attribute: "label" } }; }
|
|
1884
|
+
static get formAssociated() { return true; }
|
|
1885
|
+
render() {
|
|
1886
|
+
const ta = this.ensure("textarea", () => { const el = document.createElement("textarea"); el.className = "c-textarea"; return el; });
|
|
1887
|
+
ta.className = "c-textarea";
|
|
1888
|
+
// value and name reach the generated control; typed text survives re-renders via base user-dirty tracking.
|
|
1889
|
+
if (this.hasAttribute("name")) ta.name = this.getAttribute("name");
|
|
1890
|
+
else ta.removeAttribute("name");
|
|
1891
|
+
if (this.hasAttribute("value")) ta.value = this.getAttribute("value");
|
|
1892
|
+
if (this.hasAttribute("aria-label") && !ta.hasAttribute("aria-label")) ta.setAttribute("aria-label", this.getAttribute("aria-label"));
|
|
1893
|
+
if (this.hasAttribute("label") && !ta.hasAttribute("aria-label")) ta.setAttribute("aria-label", this.getAttribute("label"));
|
|
1894
|
+
if (!ta.hasAttribute("aria-label")) ta.setAttribute("aria-label", labelText(this, "textarea", "Textarea"));
|
|
1895
|
+
if (this.hasAttribute("placeholder")) ta.placeholder = this.getAttribute("placeholder");
|
|
1896
|
+
else ta.removeAttribute("placeholder");
|
|
1897
|
+
if (this.hasAttribute("required")) ta.setAttribute("required", "");
|
|
1898
|
+
else ta.removeAttribute("required");
|
|
1899
|
+
if (this.hasAttribute("disabled")) { ta.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1900
|
+
else { ta.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1901
|
+
const err = this.getAttribute("error");
|
|
1902
|
+
const help = this.getAttribute("help");
|
|
1903
|
+
let msg = this.querySelector(".c-textarea__message");
|
|
1904
|
+
if (err || help) {
|
|
1905
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-textarea__message"; this.appendChild(msg); }
|
|
1906
|
+
msg.textContent = err || help || "";
|
|
1907
|
+
msg.classList.toggle("c-textarea__message--error", !!err);
|
|
1908
|
+
} else if (msg) { msg.remove(); }
|
|
1909
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1910
|
+
}
|
|
1911
|
+
enhance() {
|
|
1912
|
+
const ta = this.querySelector("textarea");
|
|
1913
|
+
if (ta && this._internals) {
|
|
1914
|
+
this._internals.setFormValue(ta.value);
|
|
1915
|
+
this._syncValidity(this.hasAttribute("required") && !ta.value.trim());
|
|
1916
|
+
}
|
|
1917
|
+
if (this._wiredTextarea) return;
|
|
1918
|
+
this._wiredTextarea = true;
|
|
1919
|
+
this.addEventListener("input", (e) => {
|
|
1920
|
+
const t = this.querySelector("textarea");
|
|
1921
|
+
if (!t || e.target !== t || !this._internals) return;
|
|
1922
|
+
this._internals.setFormValue(t.value);
|
|
1923
|
+
this._emitForm("contour:input");
|
|
1924
|
+
this._syncValidity(this.hasAttribute("required") && !t.value.trim());
|
|
1925
|
+
});
|
|
1926
|
+
this.addEventListener("change", (e) => {
|
|
1927
|
+
const t = this.querySelector("textarea");
|
|
1928
|
+
if (!t || e.target !== t || !this._internals) return;
|
|
1929
|
+
this._internals.setFormValue(t.value);
|
|
1930
|
+
this._emitForm("contour:change");
|
|
1931
|
+
this._syncValidity(this.hasAttribute("required") && !t.value.trim());
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
define("contour-textarea", ContourTextarea);
|
|
1936
|
+
/**
|
|
1937
|
+
* Input: an `<input>` wrapper with type, placeholder, required, value, disabled,
|
|
1938
|
+
* error and help states. Form associated; emits `contour:input` / `contour:change`.
|
|
1939
|
+
*/
|
|
1940
|
+
class ContourInputFull extends ContourBase {
|
|
1941
|
+
static get observed() { return ["type", "placeholder", "required", "value", "disabled", "error", "help", "label", "name"]; }
|
|
1942
|
+
static get properties() { return { type: { attribute: "type" }, placeholder: { attribute: "placeholder" }, required: { attribute: "required", type: Boolean }, value: { attribute: "value" }, disabled: { attribute: "disabled", type: Boolean }, error: { attribute: "error" }, help: { attribute: "help" }, label: { attribute: "label" }, name: { attribute: "name" } }; }
|
|
1943
|
+
static get formAssociated() { return true; }
|
|
1944
|
+
render() {
|
|
1945
|
+
this.classList.add("c-input");
|
|
1946
|
+
const input = this.ensure("input", () => { const el = document.createElement("input"); el.className = "c-input"; return el; });
|
|
1947
|
+
input.className = "c-input";
|
|
1948
|
+
input.type = this.getAttribute("type") || "text";
|
|
1949
|
+
input.placeholder = this.getAttribute("placeholder") || "";
|
|
1950
|
+
// The name keys the base class's value/focus preservation and matches
|
|
1951
|
+
// the native control's own contract (R20 T-02/T-08).
|
|
1952
|
+
if (this.hasAttribute("name")) input.name = this.getAttribute("name");
|
|
1953
|
+
else input.removeAttribute("name");
|
|
1954
|
+
let label = this.getAttribute("label");
|
|
1955
|
+
if (!label) {
|
|
1956
|
+
const field = this.closest("contour-field");
|
|
1957
|
+
if (field) label = field.getAttribute("label");
|
|
1958
|
+
}
|
|
1959
|
+
if (label) input.setAttribute("aria-label", label);
|
|
1960
|
+
else if (!input.getAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "input", "Input"));
|
|
1961
|
+
if (this.hasAttribute("value")) input.value = this.getAttribute("value");
|
|
1962
|
+
if (this.hasAttribute("required")) input.setAttribute("required", "");
|
|
1963
|
+
else input.removeAttribute("required");
|
|
1964
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
1965
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
1966
|
+
// Error/help text
|
|
1967
|
+
const err = this.getAttribute("error");
|
|
1968
|
+
const help = this.getAttribute("help");
|
|
1969
|
+
let msg = this.querySelector(".c-input__message");
|
|
1970
|
+
if (err || help) {
|
|
1971
|
+
if (!msg) { msg = document.createElement("div"); msg.className = "c-input__message"; this.appendChild(msg); }
|
|
1972
|
+
msg.textContent = err || help || "";
|
|
1973
|
+
msg.classList.toggle("c-input__message--error", !!err);
|
|
1974
|
+
} else if (msg) { msg.remove(); }
|
|
1975
|
+
if (err) this.classList.add("is-error"); else this.classList.remove("is-error");
|
|
1976
|
+
}
|
|
1977
|
+
enhance() {
|
|
1978
|
+
const input = this.querySelector("input");
|
|
1979
|
+
if (input && this._internals) {
|
|
1980
|
+
this._internals.setFormValue(input.value);
|
|
1981
|
+
this._syncValidity(this.hasAttribute("required") && !input.value.trim());
|
|
1982
|
+
}
|
|
1983
|
+
if (this._wiredInput) return;
|
|
1984
|
+
this._wiredInput = true;
|
|
1985
|
+
this.addEventListener("input", (e) => {
|
|
1986
|
+
const inp = this.querySelector("input");
|
|
1987
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
1988
|
+
this._internals.setFormValue(inp.value);
|
|
1989
|
+
this._emitForm("contour:input");
|
|
1990
|
+
this._syncValidity(this.hasAttribute("required") && !inp.value.trim());
|
|
1991
|
+
});
|
|
1992
|
+
this.addEventListener("change", (e) => {
|
|
1993
|
+
const inp = this.querySelector("input");
|
|
1994
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
1995
|
+
this._internals.setFormValue(inp.value);
|
|
1996
|
+
this._emitForm("contour:change");
|
|
1997
|
+
this._syncValidity(this.hasAttribute("required") && !inp.value.trim());
|
|
1998
|
+
});
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
define("contour-input", ContourInputFull);
|
|
2002
|
+
/**
|
|
2003
|
+
* InputGroup: a text input with an optional search icon, a clear (×) affordance
|
|
2004
|
+
* and a submit (→) affordance. Attributes: `label`, `placeholder`, `clearable`,
|
|
2005
|
+
* `submit`, `value`, `name`, `required`, `disabled`. Form associated: the host
|
|
2006
|
+
* submits its value through ElementInternals. Emits `contour:input` /
|
|
2007
|
+
* `contour:change`, and `contour:change` (bubbles, composed) with `{ value: "" }`
|
|
2008
|
+
* on clear.
|
|
2009
|
+
*/
|
|
2010
|
+
class ContourInputGroup extends ContourBase {
|
|
2011
|
+
static get observed() { return ["label", "placeholder", "clearable", "submit", "value", "name", "required", "disabled"]; }
|
|
2012
|
+
static get properties() { return { label: { attribute: "label" }, placeholder: { attribute: "placeholder" }, clearable: { attribute: "clearable", type: Boolean }, submit: { attribute: "submit", type: Boolean }, value: { attribute: "value" }, name: { attribute: "name" }, required: { attribute: "required", type: Boolean }, disabled: { attribute: "disabled", type: Boolean } }; }
|
|
2013
|
+
static get formAssociated() { return true; }
|
|
2014
|
+
render() {
|
|
2015
|
+
this.classList.add("c-input-group");
|
|
2016
|
+
const icon = this.ensure("svg", () => { const el = document.createElementNS("http://www.w3.org/2000/svg", "svg"); el.setAttribute("aria-hidden", "true"); const use = document.createElementNS("http://www.w3.org/2000/svg", "use"); use.setAttribute("href", "#i-search"); el.appendChild(use); return el; });
|
|
2017
|
+
const input = this.ensure("input.c-input", () => { const el = document.createElement("input"); el.className = "c-input"; el.type = "text"; return el; });
|
|
2018
|
+
input.placeholder = this.getAttribute("placeholder") || this.getAttribute("label") || "";
|
|
2019
|
+
// Form participation (R20 T-05): the host submits through internals,
|
|
2020
|
+
// and the inner control carries the name/value the author wrote.
|
|
2021
|
+
if (this.hasAttribute("name")) input.name = this.getAttribute("name");
|
|
2022
|
+
else input.removeAttribute("name");
|
|
2023
|
+
if (this.hasAttribute("value")) input.value = this.getAttribute("value");
|
|
2024
|
+
if (this.hasAttribute("required")) input.setAttribute("required", "");
|
|
2025
|
+
else input.removeAttribute("required");
|
|
2026
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
2027
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
2028
|
+
const label = this.getAttribute("label");
|
|
2029
|
+
if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", label || "Search");
|
|
2030
|
+
// Clear affordance.
|
|
2031
|
+
const oldClear = this.querySelector(".c-input-group__clear");
|
|
2032
|
+
if (oldClear) oldClear.remove();
|
|
2033
|
+
if (this.hasAttribute("clearable")) {
|
|
2034
|
+
const btn = document.createElement("button");
|
|
2035
|
+
btn.className = "c-input-group__clear";
|
|
2036
|
+
btn.type = "button";
|
|
2037
|
+
btn.setAttribute("aria-label", labelText(this, "clear", "Clear"));
|
|
2038
|
+
btn.textContent = "×";
|
|
2039
|
+
this.appendChild(btn);
|
|
2040
|
+
}
|
|
2041
|
+
// Submit affordance.
|
|
2042
|
+
const oldSubmit = this.querySelector(".c-input-group__submit");
|
|
2043
|
+
if (oldSubmit) oldSubmit.remove();
|
|
2044
|
+
if (this.hasAttribute("submit")) {
|
|
2045
|
+
const btn = document.createElement("button");
|
|
2046
|
+
btn.className = "c-input-group__submit";
|
|
2047
|
+
btn.type = "submit";
|
|
2048
|
+
btn.setAttribute("aria-label", labelText(this, "submit", "Submit"));
|
|
2049
|
+
btn.textContent = "→";
|
|
2050
|
+
this.appendChild(btn);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
enhance() {
|
|
2054
|
+
const input = this.querySelector("input.c-input");
|
|
2055
|
+
if (input && this._internals) {
|
|
2056
|
+
this._internals.setFormValue(input.value);
|
|
2057
|
+
this._syncValidity(this.hasAttribute("required") && !input.value.trim());
|
|
2058
|
+
}
|
|
2059
|
+
if (this._wiredInputGroup) return;
|
|
2060
|
+
this._wiredInputGroup = true;
|
|
2061
|
+
this.addEventListener("input", (e) => {
|
|
2062
|
+
const inp = this.querySelector("input.c-input");
|
|
2063
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
2064
|
+
this._internals.setFormValue(inp.value);
|
|
2065
|
+
this._emitForm("contour:input");
|
|
2066
|
+
this._syncValidity(this.hasAttribute("required") && !inp.value.trim());
|
|
2067
|
+
});
|
|
2068
|
+
this.addEventListener("change", (e) => {
|
|
2069
|
+
const inp = this.querySelector("input.c-input");
|
|
2070
|
+
if (!inp || e.target !== inp || !this._internals) return;
|
|
2071
|
+
this._internals.setFormValue(inp.value);
|
|
2072
|
+
this._emitForm("contour:change");
|
|
2073
|
+
this._syncValidity(this.hasAttribute("required") && !inp.value.trim());
|
|
2074
|
+
});
|
|
2075
|
+
this.addEventListener("click", (e) => {
|
|
2076
|
+
const clear = e.target && e.target.closest ? e.target.closest(".c-input-group__clear") : null;
|
|
2077
|
+
if (clear && this.contains(clear)) {
|
|
2078
|
+
e.preventDefault();
|
|
2079
|
+
const input = this.querySelector("input.c-input");
|
|
2080
|
+
if (input) { input.value = ""; input.focus(); }
|
|
2081
|
+
if (this._internals) {
|
|
2082
|
+
this._internals.setFormValue("");
|
|
2083
|
+
this._syncValidity(this.hasAttribute("required"));
|
|
2084
|
+
}
|
|
2085
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { value: "" } }));
|
|
2086
|
+
}
|
|
2087
|
+
});
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
define("contour-input-group", ContourInputGroup);
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
/**
|
|
2096
|
+
* RadioList: a `role="radiogroup"` of radio options from a JSON `items`
|
|
2097
|
+
* attribute (or remote `src`). Supports roving tabindex, Home/End navigation,
|
|
2098
|
+
* RTL, empty/loading/error states, and `formResetCallback` that re-renders from
|
|
2099
|
+
* the authored `items` payload. Form associated; emits `contour:change`.
|
|
2100
|
+
*/
|
|
2101
|
+
class ContourRadioList extends ContourBase {
|
|
2102
|
+
static get observed() { return ["items", "options", "name", "value", "required", "empty", "loading", "error", "retry", "src", "lazy", "refresh", "timeout", "skeleton", "cache-key", "dir", "no-cache"]; }
|
|
2103
|
+
static get properties() { return { items: { attribute: "items" }, options: { attribute: "options" }, name: { attribute: "name" }, value: { attribute: "value" }, required: { attribute: "required", type: Boolean }, src: { attribute: "src" } }; }
|
|
2104
|
+
static get formAssociated() { return true; }
|
|
2105
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
2106
|
+
// A `value` written by sync() reflects the user's pick; the radios already
|
|
2107
|
+
// show it, so a re-render would only rebuild the list and detach the
|
|
2108
|
+
// focused input (losing focus mid-interaction). The attribute still sets,
|
|
2109
|
+
// so el.value reads it and a later render restores from it.
|
|
2110
|
+
if (name === "value" && this._radioSync) return;
|
|
2111
|
+
super.attributeChangedCallback(name, oldValue, newValue);
|
|
2112
|
+
}
|
|
2113
|
+
render() {
|
|
2114
|
+
this.classList.add("c-radio-list");
|
|
2115
|
+
this.setAttribute("role", "radiogroup");
|
|
2116
|
+
if (!this.hasAttribute("aria-label")) this.setAttribute("aria-label", this.getAttribute("name") || "Options");
|
|
2117
|
+
// R31-74: required is a group constraint; mirror it to the radiogroup.
|
|
2118
|
+
if (this.hasAttribute("required")) this.setAttribute("aria-required", "true"); else this.removeAttribute("aria-required");
|
|
2119
|
+
wireStates(this);
|
|
2120
|
+
loadRemote(this, "items");
|
|
2121
|
+
if (renderStates(this, "radio-list")) return;
|
|
2122
|
+
const items = remoteData(this, "items") || this.getAttribute("options") || this.getAttribute("items");
|
|
2123
|
+
// R31-74: keep the current selection across a re-render. The radios are
|
|
2124
|
+
// rebuilt from `items` every render, so without this a host attribute
|
|
2125
|
+
// change (or a form reset) drops the checked item. The authored `value`
|
|
2126
|
+
// attribute seeds it; otherwise the previously checked item is kept.
|
|
2127
|
+
const prevChecked = this.querySelector("input.c-radio__input:checked");
|
|
2128
|
+
const desired = this.hasAttribute("value") ? this.getAttribute("value") : (prevChecked ? prevChecked.value : null);
|
|
2129
|
+
Array.from(this.querySelectorAll("label.c-radio")).forEach((l) => l.remove());
|
|
2130
|
+
if (items) {
|
|
2131
|
+
try {
|
|
2132
|
+
const parsed = JSON.parse(items);
|
|
2133
|
+
const groupName = this.getAttribute("name") || contourId("radio-list");
|
|
2134
|
+
parsed.forEach((it) => {
|
|
2135
|
+
const lbl = document.createElement("label");
|
|
2136
|
+
lbl.className = "c-radio";
|
|
2137
|
+
const input = document.createElement("input");
|
|
2138
|
+
input.type = "radio";
|
|
2139
|
+
input.className = "c-radio__input";
|
|
2140
|
+
input.name = groupName;
|
|
2141
|
+
input.tabIndex = -1;
|
|
2142
|
+
if (typeof it === "string") { input.value = it; if (it === desired) input.checked = true; lbl.appendChild(input); lbl.appendChild(document.createTextNode(" " + it)); }
|
|
2143
|
+
else { input.value = it.value || it.label; if (it.checked || (desired != null && String(it.value || it.label) === String(desired))) input.checked = true; lbl.appendChild(input); const span = document.createElement("span"); span.textContent = it.label || it.value; lbl.appendChild(span); }
|
|
2144
|
+
this.appendChild(lbl);
|
|
2145
|
+
});
|
|
2146
|
+
// Roving tabindex: only the checked item (or the first) is in the tab
|
|
2147
|
+
// order, exactly like a native radiogroup.
|
|
2148
|
+
const radios = Array.from(this.querySelectorAll("input.c-radio__input"));
|
|
2149
|
+
const checked = radios.find((r) => r.checked) || radios[0];
|
|
2150
|
+
if (checked) checked.tabIndex = 0;
|
|
2151
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
2152
|
+
} else {
|
|
2153
|
+
renderEmpty(this, "radio-list");
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
enhance() {
|
|
2157
|
+
const sync = () => {
|
|
2158
|
+
const checked = this.querySelector("input.c-radio__input:checked");
|
|
2159
|
+
const val = checked ? checked.value : null;
|
|
2160
|
+
if (this._internals) this._internals.setFormValue(val);
|
|
2161
|
+
// R31-74: reflect the selection into `value` so el.value reads it and a
|
|
2162
|
+
// re-render restores it; required reports group validity. The self-write
|
|
2163
|
+
// is flagged so it does not re-render and detach the focused radio.
|
|
2164
|
+
if (val != null && this.getAttribute("value") !== val) {
|
|
2165
|
+
this._radioSync = true;
|
|
2166
|
+
try { this.setAttribute("value", val); } finally { this._radioSync = false; }
|
|
2167
|
+
}
|
|
2168
|
+
this._syncValidity(this.hasAttribute("required") && val == null);
|
|
2169
|
+
};
|
|
2170
|
+
sync();
|
|
2171
|
+
if (this._wiredRadioList) return;
|
|
2172
|
+
this._wiredRadioList = true;
|
|
2173
|
+
// Form life + group semantics, delegated on the host (rule 12): the
|
|
2174
|
+
// inputs are rebuilt on every render, so node-bound listeners would die.
|
|
2175
|
+
this.addEventListener("change", (e) => {
|
|
2176
|
+
const input = e.target && e.target.closest ? e.target.closest("input.c-radio__input") : null;
|
|
2177
|
+
if (!input || !this.contains(input)) return;
|
|
2178
|
+
sync();
|
|
2179
|
+
this._emitForm("contour:change");
|
|
2180
|
+
});
|
|
2181
|
+
this.addEventListener("keydown", (e) => {
|
|
2182
|
+
const radios = Array.from(this.querySelectorAll("input.c-radio__input"));
|
|
2183
|
+
if (!radios.length) return;
|
|
2184
|
+
const idx = radios.indexOf(e.target);
|
|
2185
|
+
if (idx === -1) return;
|
|
2186
|
+
const rtl = (this.getAttribute("dir") || document.documentElement.getAttribute("dir") || "").toLowerCase() === "rtl";
|
|
2187
|
+
let next = -1;
|
|
2188
|
+
if (e.key === "ArrowDown") next = (idx + 1) % radios.length;
|
|
2189
|
+
else if (e.key === "ArrowUp") next = (idx - 1 + radios.length) % radios.length;
|
|
2190
|
+
else if (e.key === "ArrowRight") next = rtl ? (idx - 1 + radios.length) % radios.length : (idx + 1) % radios.length;
|
|
2191
|
+
else if (e.key === "ArrowLeft") next = rtl ? (idx + 1) % radios.length : (idx - 1 + radios.length) % radios.length;
|
|
2192
|
+
else if (e.key === "Home") next = 0;
|
|
2193
|
+
else if (e.key === "End") next = radios.length - 1;
|
|
2194
|
+
else return;
|
|
2195
|
+
e.preventDefault();
|
|
2196
|
+
const before = radios.find((r) => r.checked);
|
|
2197
|
+
for (const r of radios) r.tabIndex = -1;
|
|
2198
|
+
radios[next].tabIndex = 0;
|
|
2199
|
+
radios[next].checked = true;
|
|
2200
|
+
radios[next].focus();
|
|
2201
|
+
sync();
|
|
2202
|
+
// Native radio groups fire change when an arrow moves the selection;
|
|
2203
|
+
// wrap-around onto the same radio is not a change.
|
|
2204
|
+
if (before !== radios[next]) this._emitForm("contour:change");
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
formResetCallback() {
|
|
2208
|
+
// Authored default lives in the `items` payload (it.checked); re-render from data under the resetting flag so base checked-restore doesn't resurrect the user's selection.
|
|
2209
|
+
this._contourResetting = true;
|
|
2210
|
+
try {
|
|
2211
|
+
if (this.isConnected && this._contourAuthored) this.attributeChangedCallback("__rerender__", null, "");
|
|
2212
|
+
this._syncFormValue();
|
|
2213
|
+
} finally {
|
|
2214
|
+
this._contourResetting = false;
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
define("contour-radio-list", ContourRadioList);
|
|
2219
|
+
|
|
2220
|
+
// Fieldset: a <fieldset> with a <legend> for grouping controls; platform draws the boundary.
|
|
2221
|
+
class ContourFieldset extends ContourBase {
|
|
2222
|
+
static get observed() { return ["legend", "disabled"]; }
|
|
2223
|
+
static get properties() { return { legend: { attribute: "legend" }, disabled: { attribute: "disabled", type: Boolean } }; }
|
|
2224
|
+
render() {
|
|
2225
|
+
const fs = this.ensure("fieldset", () => { const el = document.createElement("fieldset"); el.className = "c-fieldset"; return el; });
|
|
2226
|
+
if (this.hasAttribute("disabled")) fs.setAttribute("disabled", ""); else fs.removeAttribute("disabled");
|
|
2227
|
+
const legend = this.getAttribute("legend");
|
|
2228
|
+
let lg = fs.querySelector("legend");
|
|
2229
|
+
if (legend) {
|
|
2230
|
+
if (!lg) { lg = document.createElement("legend"); lg.className = "c-fieldset__legend"; fs.prepend(lg); }
|
|
2231
|
+
lg.textContent = legend;
|
|
2232
|
+
} else if (lg) lg.remove();
|
|
2233
|
+
// Move authored children into the fieldset (light DOM).
|
|
2234
|
+
const authored = Array.from(this.children).filter((c) => c !== fs);
|
|
2235
|
+
if (authored.length) { authored.forEach((c) => fs.appendChild(c)); }
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
define("contour-fieldset", ContourFieldset);
|
|
2239
|
+
|
|
2240
|
+
// Checkbox group: a fieldset of checkboxes with a shared name. Form associated: host owns group value, value reflects comma-joined selection, required reports group validity, emits contour:change { value, values }.
|
|
2241
|
+
// R31-36: the option payload answers to both spellings this system uses — `options` (a select's word) and `items` (a list's word) — so one vocabulary does not stop at the component boundary.
|
|
2242
|
+
class ContourCheckboxGroup extends ContourBase {
|
|
2243
|
+
static get observed() { return ["legend", "name", "options", "items", "value", "required", "disabled"]; }
|
|
2244
|
+
static get properties() { return { legend: { attribute: "legend" }, name: { attribute: "name" }, options: { attribute: "options" }, items: { attribute: "items" }, value: { attribute: "value" }, required: { attribute: "required", type: Boolean }, disabled: { attribute: "disabled", type: Boolean } }; }
|
|
2245
|
+
static get formAssociated() { return true; }
|
|
2246
|
+
render() {
|
|
2247
|
+
this.classList.add("c-checkbox-group");
|
|
2248
|
+
// Authored default captured before any toggle reflects into `value`.
|
|
2249
|
+
if (this._checkboxGroupDefault === undefined) this._checkboxGroupDefault = this.getAttribute("value");
|
|
2250
|
+
const fs = this.ensure("fieldset", () => { const el = document.createElement("fieldset"); el.className = "c-checkbox-group__fieldset"; return el; });
|
|
2251
|
+
if (this.hasAttribute("disabled")) fs.setAttribute("disabled", ""); else fs.removeAttribute("disabled");
|
|
2252
|
+
if (this.hasAttribute("required")) fs.setAttribute("aria-required", "true"); else fs.removeAttribute("aria-required");
|
|
2253
|
+
const legend = this.getAttribute("legend");
|
|
2254
|
+
let lg = fs.querySelector("legend");
|
|
2255
|
+
if (legend) {
|
|
2256
|
+
if (!lg) { lg = document.createElement("legend"); lg.className = "c-checkbox-group__legend"; fs.prepend(lg); }
|
|
2257
|
+
lg.textContent = legend;
|
|
2258
|
+
} else if (lg) lg.remove();
|
|
2259
|
+
const name = this.getAttribute("name") || "group";
|
|
2260
|
+
const options = this.getAttribute("options") || this.getAttribute("items");
|
|
2261
|
+
const selected = (this.getAttribute("value") || "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
2262
|
+
if (options) {
|
|
2263
|
+
try {
|
|
2264
|
+
const parsed = JSON.parse(options);
|
|
2265
|
+
Array.from(fs.querySelectorAll(".c-check")).forEach((n) => n.remove());
|
|
2266
|
+
parsed.forEach((opt) => {
|
|
2267
|
+
const label = typeof opt === "string" ? opt : (opt.label || opt.value || "");
|
|
2268
|
+
const value = typeof opt === "string" ? opt : (opt.value || opt.label || "");
|
|
2269
|
+
const wrap = document.createElement("label");
|
|
2270
|
+
wrap.className = "c-check";
|
|
2271
|
+
const input = document.createElement("input");
|
|
2272
|
+
input.type = "checkbox";
|
|
2273
|
+
input.className = "c-check__input";
|
|
2274
|
+
input.name = name;
|
|
2275
|
+
input.value = value;
|
|
2276
|
+
// Boxes share the group name, so name-based focus matching lands on the first; per-option preserve key restores focus to the clicked box.
|
|
2277
|
+
input.setAttribute("data-contour-preserve-key", value);
|
|
2278
|
+
if (selected.includes(value)) input.checked = true;
|
|
2279
|
+
const span = document.createElement("span");
|
|
2280
|
+
span.textContent = label;
|
|
2281
|
+
wrap.appendChild(input);
|
|
2282
|
+
wrap.appendChild(span);
|
|
2283
|
+
fs.appendChild(wrap);
|
|
2284
|
+
});
|
|
2285
|
+
} catch (e) { warnBadJson(this, "attribute", e); }
|
|
2286
|
+
}
|
|
2287
|
+
// R31-62: apply `value` to authored checkbox children too, not only the JSON
|
|
2288
|
+
// `options` branch. A consumer who authors <input type="checkbox" value="a">
|
|
2289
|
+
// with value="a,c" must see both boxes checked and submit both entries.
|
|
2290
|
+
// Authored <contour-check> children upgrade after this render and only then
|
|
2291
|
+
// create their inner input, so re-apply on the next microtask and frame.
|
|
2292
|
+
if (selected.length) {
|
|
2293
|
+
const apply = () => {
|
|
2294
|
+
if (!this.isConnected) return;
|
|
2295
|
+
for (const input of this.querySelectorAll("input[type=checkbox]")) {
|
|
2296
|
+
if (selected.includes(input.value)) input.checked = true;
|
|
2297
|
+
}
|
|
2298
|
+
if (this._internals) this._syncFormValue();
|
|
2299
|
+
};
|
|
2300
|
+
apply();
|
|
2301
|
+
if (typeof queueMicrotask === "function") queueMicrotask(apply);
|
|
2302
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(apply);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
enhance() {
|
|
2306
|
+
if (this._internals) this._syncFormValue();
|
|
2307
|
+
if (this._wiredCheckboxGroup) return;
|
|
2308
|
+
this._wiredCheckboxGroup = true;
|
|
2309
|
+
this.addEventListener("change", (e) => {
|
|
2310
|
+
const t = e.target;
|
|
2311
|
+
if (t && t.matches && t.matches("input[type=checkbox]")) {
|
|
2312
|
+
const values = Array.from(this.querySelectorAll("input[type=checkbox]:checked")).map((i) => i.value);
|
|
2313
|
+
// Reflect the group value so `el.value` reads the live selection and
|
|
2314
|
+
// a re-render rebuilds it; the attribute write re-renders synchronously.
|
|
2315
|
+
this.setAttribute("value", values.join(","));
|
|
2316
|
+
this._syncFormValue();
|
|
2317
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { value: values.join(","), values } }));
|
|
2318
|
+
}
|
|
2319
|
+
});
|
|
2320
|
+
}
|
|
2321
|
+
// Each checked box is its own entry under the host name (native repeated-entry shape); host owns reset/validity; formdata re-sync calls this.
|
|
2322
|
+
_syncFormValue() {
|
|
2323
|
+
if (!this._internals) return;
|
|
2324
|
+
const checked = Array.from(this.querySelectorAll("input[type=checkbox]:checked"));
|
|
2325
|
+
const name = this.getAttribute("name");
|
|
2326
|
+
if (name && checked.length) {
|
|
2327
|
+
const fd = new FormData();
|
|
2328
|
+
for (const c of checked) fd.append(name, c.value);
|
|
2329
|
+
this._internals.setFormValue(fd);
|
|
2330
|
+
} else {
|
|
2331
|
+
this._internals.setFormValue(null);
|
|
2332
|
+
}
|
|
2333
|
+
this._syncValidity(this.hasAttribute("required") && !checked.length);
|
|
2334
|
+
}
|
|
2335
|
+
formResetCallback() {
|
|
2336
|
+
// Restore the authored default, not the last reflected selection.
|
|
2337
|
+
this._contourResetting = true;
|
|
2338
|
+
try {
|
|
2339
|
+
const def = this._checkboxGroupDefault ?? null;
|
|
2340
|
+
if (def === null) this.removeAttribute("value");
|
|
2341
|
+
else this.setAttribute("value", def);
|
|
2342
|
+
this._syncFormValue();
|
|
2343
|
+
} finally {
|
|
2344
|
+
this._contourResetting = false;
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
formStateRestoreCallback(state) {
|
|
2348
|
+
const values = state instanceof FormData
|
|
2349
|
+
? Array.from(state.getAll(this.getAttribute("name"))).map(String)
|
|
2350
|
+
: (state == null ? [] : String(state).split(",").map((v) => v.trim()).filter(Boolean));
|
|
2351
|
+
this._contourResetting = true;
|
|
2352
|
+
try {
|
|
2353
|
+
this.setAttribute("value", values.join(","));
|
|
2354
|
+
this._syncFormValue();
|
|
2355
|
+
} finally {
|
|
2356
|
+
this._contourResetting = false;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
define("contour-checkbox-group", ContourCheckboxGroup);
|
|
2361
|
+
|
|
2362
|
+
// Search field: type=search with a search icon and accessible name; form associated via ElementInternals with reset/required validity.
|
|
2363
|
+
class ContourSearchField extends ContourBase {
|
|
2364
|
+
static get observed() { return ["value", "placeholder", "label", "name", "required", "disabled"]; }
|
|
2365
|
+
static get properties() { return { value: { attribute: "value" }, placeholder: { attribute: "placeholder" }, label: { attribute: "label" }, name: { attribute: "name" }, required: { attribute: "required", type: Boolean }, disabled: { attribute: "disabled", type: Boolean } }; }
|
|
2366
|
+
static get formAssociated() { return true; }
|
|
2367
|
+
render() {
|
|
2368
|
+
this.classList.add("c-search-field");
|
|
2369
|
+
const input = this.ensure("input", () => { const el = document.createElement("input"); el.type = "search"; el.className = "c-search-field__input"; return el; });
|
|
2370
|
+
input.placeholder = this.getAttribute("placeholder") || "Search";
|
|
2371
|
+
if (this.getAttribute("value") !== null) input.value = this.getAttribute("value");
|
|
2372
|
+
if (this.hasAttribute("name")) input.name = this.getAttribute("name");
|
|
2373
|
+
if (this.hasAttribute("required")) input.setAttribute("required", "");
|
|
2374
|
+
else input.removeAttribute("required");
|
|
2375
|
+
if (this.hasAttribute("disabled")) { input.setAttribute("disabled", ""); this.classList.add("is-disabled"); }
|
|
2376
|
+
else { input.removeAttribute("disabled"); this.classList.remove("is-disabled"); }
|
|
2377
|
+
const label = this.getAttribute("label");
|
|
2378
|
+
if (label) input.setAttribute("aria-label", label);
|
|
2379
|
+
else if (!input.hasAttribute("aria-label")) input.setAttribute("aria-label", labelText(this, "search", "Search"));
|
|
2380
|
+
}
|
|
2381
|
+
enhance() {
|
|
2382
|
+
const input = this.querySelector("input");
|
|
2383
|
+
if (input && this._internals) {
|
|
2384
|
+
this._internals.setFormValue(input.value);
|
|
2385
|
+
this._syncValidity(this.hasAttribute("required") && !input.value.trim());
|
|
2386
|
+
}
|
|
2387
|
+
if (this._wiredSearchField) return;
|
|
2388
|
+
this._wiredSearchField = true;
|
|
2389
|
+
this.addEventListener("input", (e) => {
|
|
2390
|
+
const t = e.target;
|
|
2391
|
+
if (t && t.matches && t.matches("input")) {
|
|
2392
|
+
if (this._internals) {
|
|
2393
|
+
this._internals.setFormValue(t.value);
|
|
2394
|
+
this._syncValidity(this.hasAttribute("required") && !t.value.trim());
|
|
2395
|
+
}
|
|
2396
|
+
this._emitForm("contour:input");
|
|
2397
|
+
}
|
|
2398
|
+
});
|
|
2399
|
+
this.addEventListener("change", (e) => {
|
|
2400
|
+
const t = e.target;
|
|
2401
|
+
if (!t || !t.matches || !t.matches("input") || !this._internals) return;
|
|
2402
|
+
this._internals.setFormValue(t.value);
|
|
2403
|
+
this._emitForm("contour:change");
|
|
2404
|
+
this._syncValidity(this.hasAttribute("required") && !t.value.trim());
|
|
2405
|
+
});
|
|
2406
|
+
}
|
|
2407
|
+
}
|
|
2408
|
+
define("contour-search-field", ContourSearchField);
|
|
2409
|
+
|
|
2410
|
+
// Density toggle: a two-state control (comfortable / compact) that sets a
|
|
2411
|
+
// data-density attribute on the host. The consumer's CSS reads it.
|
|
2412
|
+
class ContourDensityToggle extends ContourBase {
|
|
2413
|
+
static get observed() { return ["value", "label"]; }
|
|
2414
|
+
static get properties() { return { value: { attribute: "value" }, label: { attribute: "label" } }; }
|
|
2415
|
+
render() {
|
|
2416
|
+
this.classList.add("c-density-toggle");
|
|
2417
|
+
const value = this.getAttribute("value") || "comfortable";
|
|
2418
|
+
this.setAttribute("data-density", value);
|
|
2419
|
+
const btn = this.ensure("button", () => { const el = document.createElement("button"); el.className = "c-btn c-btn--sm c-density-toggle__btn"; el.type = "button"; return el; });
|
|
2420
|
+
const label = this.getAttribute("label") || (value === "compact" ? "Compact" : "Comfortable");
|
|
2421
|
+
btn.textContent = label;
|
|
2422
|
+
btn.setAttribute("aria-pressed", value === "compact" ? "true" : "false");
|
|
2423
|
+
}
|
|
2424
|
+
enhance() {
|
|
2425
|
+
if (this._wiredDensityToggle) return;
|
|
2426
|
+
this._wiredDensityToggle = true;
|
|
2427
|
+
this.addEventListener("click", () => {
|
|
2428
|
+
const next = this.getAttribute("data-density") === "compact" ? "comfortable" : "compact";
|
|
2429
|
+
this.setAttribute("value", next);
|
|
2430
|
+
this.setAttribute("data-density", next);
|
|
2431
|
+
const btn = this.querySelector(".c-density-toggle__btn");
|
|
2432
|
+
if (btn) { btn.textContent = next === "compact" ? "Compact" : "Comfortable"; btn.setAttribute("aria-pressed", next === "compact" ? "true" : "false"); }
|
|
2433
|
+
this.dispatchEvent(new CustomEvent("contour:change", { bubbles: true, composed: true, detail: { value: next } }));
|
|
2434
|
+
});
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
define("contour-density-toggle", ContourDensityToggle);
|