@digdir/designsystemet-web 1.13.2 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,1069 +1,985 @@
1
- // ../../node_modules/.pnpm/invokers-polyfill@1.0.2_patch_hash=d5677be15320f04cdc552d82cb4a79242bd1cf8b26bcbb0a13e1153e7bed3b5a/node_modules/invokers-polyfill/invoker.js
1
+ import "@u-elements/u-details/polyfill";
2
+ import { autoUpdate, computePosition, flip, limitShift, offset, shift, size } from "@floating-ui/dom";
3
+ import { UHTMLComboboxElement } from "@u-elements/u-combobox";
4
+ import * as UTabs from "@u-elements/u-tabs";
5
+ export * from "@u-elements/u-datalist";
6
+ //#region ../../node_modules/.pnpm/invokers-polyfill@1.0.3/node_modules/invokers-polyfill/invoker.js
2
7
  function isSupported() {
3
- return typeof HTMLButtonElement !== "undefined" && "command" in HTMLButtonElement.prototype && "source" in ((globalThis.CommandEvent || {}).prototype || {});
8
+ return typeof HTMLButtonElement !== "undefined" && "command" in HTMLButtonElement.prototype && "source" in ((globalThis.CommandEvent || {}).prototype || {});
4
9
  }
5
10
  function apply() {
6
- document.addEventListener(
7
- "invoke",
8
- (e) => {
9
- if (e.type == "invoke" && e.isTrusted) {
10
- e.stopImmediatePropagation();
11
- e.preventDefault();
12
- }
13
- },
14
- true
15
- );
16
- document.addEventListener(
17
- "command",
18
- (e) => {
19
- if (e.type == "command" && e.isTrusted) {
20
- e.stopImmediatePropagation();
21
- e.preventDefault();
22
- }
23
- },
24
- true
25
- );
26
- function enumerate(obj, key, enumerable = true) {
27
- Object.defineProperty(obj, key, {
28
- ...Object.getOwnPropertyDescriptor(obj, key),
29
- enumerable
30
- });
31
- }
32
- function getRootNode(node) {
33
- if (node && typeof node.getRootNode === "function") {
34
- return node.getRootNode();
35
- }
36
- if (node && node.parentNode) return getRootNode(node.parentNode);
37
- return node;
38
- }
39
- const commandEventSourceElements = /* @__PURE__ */ new WeakMap();
40
- const commandEventActions = /* @__PURE__ */ new WeakMap();
41
- class CommandEvent extends Event {
42
- constructor(type, invokeEventInit = {}) {
43
- super(type, invokeEventInit);
44
- const { source, command } = invokeEventInit;
45
- if (source != null && !(source instanceof Element)) {
46
- throw new TypeError(`source must be an element`);
47
- }
48
- commandEventSourceElements.set(this, source || null);
49
- commandEventActions.set(
50
- this,
51
- command !== void 0 ? String(command) : ""
52
- );
53
- }
54
- get [Symbol.toStringTag]() {
55
- return "CommandEvent";
56
- }
57
- get source() {
58
- if (!commandEventSourceElements.has(this)) {
59
- throw new TypeError("illegal invocation");
60
- }
61
- const source = commandEventSourceElements.get(this);
62
- if (!(source instanceof Element)) return null;
63
- const invokerRoot = getRootNode(source);
64
- if (invokerRoot !== getRootNode(this.target || document)) {
65
- return invokerRoot.host;
66
- }
67
- return source;
68
- }
69
- get command() {
70
- if (!commandEventActions.has(this)) {
71
- throw new TypeError("illegal invocation");
72
- }
73
- return commandEventActions.get(this);
74
- }
75
- }
76
- enumerate(CommandEvent.prototype, "source");
77
- enumerate(CommandEvent.prototype, "command");
78
- const invokerAssociatedElements = /* @__PURE__ */ new WeakMap();
79
- function applyInvokerMixin(ElementClass) {
80
- Object.defineProperties(ElementClass.prototype, {
81
- commandForElement: {
82
- enumerable: true,
83
- configurable: true,
84
- set(targetElement) {
85
- if (targetElement === null) {
86
- this.removeAttribute("commandfor");
87
- invokerAssociatedElements.delete(this);
88
- } else if (!(targetElement instanceof Element)) {
89
- throw new TypeError(`commandForElement must be an element or null`);
90
- } else {
91
- this.setAttribute("commandfor", "");
92
- const targetRootNode = getRootNode(targetElement);
93
- const thisRootNode = getRootNode(this);
94
- if (thisRootNode === targetRootNode || targetRootNode === this.ownerDocument) {
95
- invokerAssociatedElements.set(this, targetElement);
96
- } else {
97
- invokerAssociatedElements.delete(this);
98
- }
99
- }
100
- },
101
- get() {
102
- if (this.localName !== "button") {
103
- return null;
104
- }
105
- if (this.disabled) {
106
- return null;
107
- }
108
- if (this.form && this.getAttribute("type") !== "button") {
109
- console.warn(
110
- "Element with `commandFor` is a form participant. It should explicitly set `type=button` in order for `commandFor` to work"
111
- );
112
- return null;
113
- }
114
- const targetElement = invokerAssociatedElements.get(this);
115
- if (targetElement) {
116
- if (targetElement.isConnected) {
117
- return targetElement;
118
- } else {
119
- invokerAssociatedElements.delete(this);
120
- return null;
121
- }
122
- }
123
- const root = getRootNode(this);
124
- const idref = this.getAttribute("commandfor");
125
- if ((root instanceof Document || root instanceof ShadowRoot) && idref) {
126
- return root.getElementById(idref) || null;
127
- }
128
- return null;
129
- }
130
- },
131
- command: {
132
- enumerable: true,
133
- configurable: true,
134
- get() {
135
- const value = this.getAttribute("command") || "";
136
- if (value.startsWith("--")) return value;
137
- const valueLower = value.toLowerCase();
138
- switch (valueLower) {
139
- case "show-modal":
140
- case "request-close":
141
- case "close":
142
- case "toggle-popover":
143
- case "hide-popover":
144
- case "show-popover":
145
- return valueLower;
146
- }
147
- return "";
148
- },
149
- set(value) {
150
- this.setAttribute("command", value);
151
- }
152
- }
153
- });
154
- }
155
- const onHandlers = /* @__PURE__ */ new WeakMap();
156
- Object.defineProperties(HTMLElement.prototype, {
157
- oncommand: {
158
- enumerable: true,
159
- configurable: true,
160
- get() {
161
- oncommandObserver.takeRecords();
162
- return onHandlers.get(this) || null;
163
- },
164
- set(handler) {
165
- const existing = onHandlers.get(this) || null;
166
- if (existing) {
167
- this.removeEventListener("command", existing);
168
- }
169
- onHandlers.set(
170
- this,
171
- typeof handler === "object" || typeof handler === "function" ? handler : null
172
- );
173
- if (typeof handler == "function") {
174
- this.addEventListener("command", handler);
175
- }
176
- }
177
- }
178
- });
179
- function applyOnCommandHandler(els) {
180
- for (const el of els) {
181
- el.oncommand = new Function("event", el.getAttribute("oncommand"));
182
- }
183
- }
184
- const oncommandObserver = new MutationObserver((records) => {
185
- for (const record of records) {
186
- const { target } = record;
187
- if (record.type === "childList") {
188
- applyOnCommandHandler(target.querySelectorAll("[oncommand]"));
189
- } else {
190
- applyOnCommandHandler([target]);
191
- }
192
- }
193
- });
194
- oncommandObserver.observe(document, {
195
- subtree: true,
196
- childList: true,
197
- attributeFilter: ["oncommand"]
198
- });
199
- applyOnCommandHandler(document.querySelectorAll("[oncommand]"));
200
- const processedEvents = /* @__PURE__ */ new WeakSet();
201
- function handleInvokerActivation(event) {
202
- if (processedEvents.has(event)) return;
203
- processedEvents.add(event);
204
- if (event.defaultPrevented) return;
205
- if (event.type !== "click") return;
206
- const source = event.composedPath().find((el) => el.matches?.("button[commandfor], button[command]"));
207
- if (!source) return;
208
- if (source.form && source.getAttribute("type") !== "button") {
209
- event.preventDefault();
210
- throw new Error(
211
- "Element with `commandFor` is a form participant. It should explicitly set `type=button` in order for `commandFor` to work. In order for it to act as a Submit button, it must not have command or commandfor attributes"
212
- );
213
- }
214
- if (source.hasAttribute("command") !== source.hasAttribute("commandfor")) {
215
- const attr2 = source.hasAttribute("command") ? "command" : "commandfor";
216
- const missing = source.hasAttribute("command") ? "commandfor" : "command";
217
- throw new Error(
218
- `Element with ${attr2} attribute must also have a ${missing} attribute to function.`
219
- );
220
- }
221
- if (source.command !== "show-popover" && source.command !== "hide-popover" && source.command !== "toggle-popover" && source.command !== "show-modal" && source.command !== "request-close" && source.command !== "close" && !source.command.startsWith("--")) {
222
- console.warn(
223
- `"${source.command}" is not a valid command value. Custom commands must begin with --`
224
- );
225
- return;
226
- }
227
- const invokee = source.commandForElement;
228
- if (!invokee) return;
229
- const invokeEvent = new CommandEvent("command", {
230
- command: source.command,
231
- source,
232
- cancelable: true
233
- });
234
- invokee.dispatchEvent(invokeEvent);
235
- if (invokeEvent.defaultPrevented) return;
236
- const command = invokeEvent.command.toLowerCase();
237
- if (invokee.popover) {
238
- const canShow = !invokee.matches(":popover-open");
239
- const shouldShow = canShow && (command === "toggle-popover" || command === "show-popover");
240
- const shouldHide = !canShow && command === "hide-popover";
241
- if (shouldShow) {
242
- invokee.showPopover({ source });
243
- } else if (shouldHide) {
244
- invokee.hidePopover();
245
- }
246
- } else if (invokee.localName === "dialog") {
247
- const canShow = !invokee.hasAttribute("open");
248
- if (canShow && command == "show-modal") {
249
- invokee.showModal();
250
- } else if (!canShow && command == "close") {
251
- invokee.close(source.value ? source.value : void 0);
252
- } else if (!canShow && command == "request-close") {
253
- if (!HTMLDialogElement.prototype.requestClose) {
254
- HTMLDialogElement.prototype.requestClose = function() {
255
- const cancelEvent = new Event("cancel", { cancelable: true });
256
- this.dispatchEvent(cancelEvent);
257
- if (!cancelEvent.defaultPrevented) {
258
- this.close();
259
- }
260
- };
261
- }
262
- invokee.requestClose(source.value ? source.value : void 0);
263
- }
264
- }
265
- }
266
- function setupInvokeListeners(target) {
267
- target.addEventListener("click", handleInvokerActivation, true);
268
- }
269
- function observeShadowRoots(ElementClass, callback) {
270
- const attachShadow = ElementClass.prototype.attachShadow;
271
- ElementClass.prototype.attachShadow = function(init) {
272
- const shadow = attachShadow.call(this, init);
273
- callback(shadow);
274
- return shadow;
275
- };
276
- const attachInternals = ElementClass.prototype.attachInternals;
277
- ElementClass.prototype.attachInternals = function() {
278
- const internals = attachInternals.call(this);
279
- if (internals.shadowRoot) callback(internals.shadowRoot);
280
- return internals;
281
- };
282
- }
283
- applyInvokerMixin(HTMLButtonElement);
284
- observeShadowRoots(HTMLElement, (shadow) => {
285
- setupInvokeListeners(shadow);
286
- oncommandObserver.observe(shadow, { attributeFilter: ["oncommand"] });
287
- applyOnCommandHandler(shadow.querySelectorAll("[oncommand]"));
288
- });
289
- setupInvokeListeners(document);
290
- Object.assign(globalThis, { CommandEvent });
11
+ document.addEventListener("invoke", (e) => {
12
+ if (e.type == "invoke" && e.isTrusted) {
13
+ e.stopImmediatePropagation();
14
+ e.preventDefault();
15
+ }
16
+ }, true);
17
+ document.addEventListener("command", (e) => {
18
+ if (e.type == "command" && e.isTrusted) {
19
+ e.stopImmediatePropagation();
20
+ e.preventDefault();
21
+ }
22
+ }, true);
23
+ function enumerate(obj, key, enumerable = true) {
24
+ Object.defineProperty(obj, key, {
25
+ ...Object.getOwnPropertyDescriptor(obj, key),
26
+ enumerable
27
+ });
28
+ }
29
+ function getRootNode(node) {
30
+ if (node && typeof node.getRootNode === "function") return node.getRootNode();
31
+ if (node && node.parentNode) return getRootNode(node.parentNode);
32
+ return node;
33
+ }
34
+ const commandEventSourceElements = /* @__PURE__ */ new WeakMap();
35
+ const commandEventActions = /* @__PURE__ */ new WeakMap();
36
+ class CommandEvent extends Event {
37
+ constructor(type, invokeEventInit = {}) {
38
+ super(type, invokeEventInit);
39
+ const { source, command } = invokeEventInit;
40
+ if (source != null && !(source instanceof Element)) throw new TypeError(`source must be an element`);
41
+ commandEventSourceElements.set(this, source || null);
42
+ commandEventActions.set(this, command !== void 0 ? String(command) : "");
43
+ }
44
+ get [Symbol.toStringTag]() {
45
+ return "CommandEvent";
46
+ }
47
+ get source() {
48
+ if (!commandEventSourceElements.has(this)) throw new TypeError("illegal invocation");
49
+ const source = commandEventSourceElements.get(this);
50
+ if (!(source instanceof Element)) return null;
51
+ const invokerRoot = getRootNode(source);
52
+ if (invokerRoot !== getRootNode(this.target || document)) return invokerRoot.host;
53
+ return source;
54
+ }
55
+ get command() {
56
+ if (!commandEventActions.has(this)) throw new TypeError("illegal invocation");
57
+ return commandEventActions.get(this);
58
+ }
59
+ }
60
+ enumerate(CommandEvent.prototype, "source");
61
+ enumerate(CommandEvent.prototype, "command");
62
+ const invokerAssociatedElements = /* @__PURE__ */ new WeakMap();
63
+ function applyInvokerMixin(ElementClass) {
64
+ Object.defineProperties(ElementClass.prototype, {
65
+ commandForElement: {
66
+ enumerable: true,
67
+ configurable: true,
68
+ set(targetElement) {
69
+ if (targetElement === null) {
70
+ this.removeAttribute("commandfor");
71
+ invokerAssociatedElements.delete(this);
72
+ } else if (!(targetElement instanceof Element)) throw new TypeError(`commandForElement must be an element or null`);
73
+ else {
74
+ this.setAttribute("commandfor", "");
75
+ const targetRootNode = getRootNode(targetElement);
76
+ if (getRootNode(this) === targetRootNode || targetRootNode === this.ownerDocument) invokerAssociatedElements.set(this, targetElement);
77
+ else invokerAssociatedElements.delete(this);
78
+ }
79
+ },
80
+ get() {
81
+ if (this.localName !== "button") return null;
82
+ if (this.disabled) return null;
83
+ if (this.form && this.getAttribute("type") !== "button") {
84
+ console.warn("Element with `commandFor` is a form participant. It should explicitly set `type=button` in order for `commandFor` to work");
85
+ return null;
86
+ }
87
+ const targetElement = invokerAssociatedElements.get(this);
88
+ if (targetElement) if (targetElement.isConnected) return targetElement;
89
+ else {
90
+ invokerAssociatedElements.delete(this);
91
+ return null;
92
+ }
93
+ const root = getRootNode(this);
94
+ const idref = this.getAttribute("commandfor");
95
+ if ((root instanceof Document || root instanceof ShadowRoot) && idref) return root.getElementById(idref) || null;
96
+ return null;
97
+ }
98
+ },
99
+ command: {
100
+ enumerable: true,
101
+ configurable: true,
102
+ get() {
103
+ const value = this.getAttribute("command") || "";
104
+ if (value.startsWith("--")) return value;
105
+ const valueLower = value.toLowerCase();
106
+ switch (valueLower) {
107
+ case "show-modal":
108
+ case "request-close":
109
+ case "close":
110
+ case "toggle-popover":
111
+ case "hide-popover":
112
+ case "show-popover": return valueLower;
113
+ }
114
+ return "";
115
+ },
116
+ set(value) {
117
+ this.setAttribute("command", value);
118
+ }
119
+ }
120
+ });
121
+ }
122
+ const onHandlers = /* @__PURE__ */ new WeakMap();
123
+ Object.defineProperties(HTMLElement.prototype, { oncommand: {
124
+ enumerable: true,
125
+ configurable: true,
126
+ get() {
127
+ oncommandObserver.takeRecords();
128
+ return onHandlers.get(this) || null;
129
+ },
130
+ set(handler) {
131
+ const existing = onHandlers.get(this) || null;
132
+ if (existing) this.removeEventListener("command", existing);
133
+ onHandlers.set(this, typeof handler === "object" || typeof handler === "function" ? handler : null);
134
+ if (typeof handler == "function") this.addEventListener("command", handler);
135
+ }
136
+ } });
137
+ function applyOnCommandHandler(els) {
138
+ for (const el of els) el.oncommand = new Function("event", el.getAttribute("oncommand"));
139
+ }
140
+ const oncommandObserver = new MutationObserver((records) => {
141
+ for (const record of records) {
142
+ const { target } = record;
143
+ if (record.type === "childList") applyOnCommandHandler(target.querySelectorAll("[oncommand]"));
144
+ else applyOnCommandHandler([target]);
145
+ }
146
+ });
147
+ oncommandObserver.observe(document, {
148
+ subtree: true,
149
+ childList: true,
150
+ attributeFilter: ["oncommand"]
151
+ });
152
+ applyOnCommandHandler(document.querySelectorAll("[oncommand]"));
153
+ const processedEvents = /* @__PURE__ */ new WeakSet();
154
+ function handleInvokerActivation(event) {
155
+ if (processedEvents.has(event)) return;
156
+ processedEvents.add(event);
157
+ if (event.defaultPrevented) return;
158
+ if (event.type !== "click") return;
159
+ const source = event.composedPath().find((el) => el.matches?.("button[commandfor], button[command]"));
160
+ if (!source) return;
161
+ if (source.form && source.getAttribute("type") !== "button") {
162
+ event.preventDefault();
163
+ throw new Error("Element with `commandFor` is a form participant. It should explicitly set `type=button` in order for `commandFor` to work. In order for it to act as a Submit button, it must not have command or commandfor attributes");
164
+ }
165
+ if (source.hasAttribute("command") !== source.hasAttribute("commandfor")) {
166
+ const attr = source.hasAttribute("command") ? "command" : "commandfor";
167
+ const missing = source.hasAttribute("command") ? "commandfor" : "command";
168
+ throw new Error(`Element with ${attr} attribute must also have a ${missing} attribute to function.`);
169
+ }
170
+ if (source.command !== "show-popover" && source.command !== "hide-popover" && source.command !== "toggle-popover" && source.command !== "show-modal" && source.command !== "request-close" && source.command !== "close" && !source.command.startsWith("--")) {
171
+ console.warn(`"${source.command}" is not a valid command value. Custom commands must begin with --`);
172
+ return;
173
+ }
174
+ const invokee = source.commandForElement;
175
+ if (!invokee) return;
176
+ const invokeEvent = new CommandEvent("command", {
177
+ command: source.command,
178
+ source,
179
+ cancelable: true
180
+ });
181
+ invokee.dispatchEvent(invokeEvent);
182
+ if (invokeEvent.defaultPrevented) return;
183
+ const command = invokeEvent.command.toLowerCase();
184
+ if (invokee.popover) {
185
+ const canShow = !invokee.matches(":popover-open");
186
+ const shouldShow = canShow && (command === "toggle-popover" || command === "show-popover");
187
+ const shouldHide = !canShow && command === "hide-popover";
188
+ if (shouldShow) invokee.showPopover({ source });
189
+ else if (shouldHide) invokee.hidePopover();
190
+ } else if (invokee.localName === "dialog") {
191
+ const canShow = !invokee.hasAttribute("open");
192
+ if (canShow && command == "show-modal") invokee.showModal();
193
+ else if (!canShow && command == "close") invokee.close(source.value ? source.value : void 0);
194
+ else if (!canShow && command == "request-close") {
195
+ if (!HTMLDialogElement.prototype.requestClose) HTMLDialogElement.prototype.requestClose = function() {
196
+ const cancelEvent = new Event("cancel", { cancelable: true });
197
+ this.dispatchEvent(cancelEvent);
198
+ if (!cancelEvent.defaultPrevented) this.close();
199
+ };
200
+ invokee.requestClose(source.value ? source.value : void 0);
201
+ }
202
+ }
203
+ }
204
+ function setupInvokeListeners(target) {
205
+ target.addEventListener("click", handleInvokerActivation, true);
206
+ }
207
+ function observeShadowRoots(ElementClass, callback) {
208
+ const attachShadow = ElementClass.prototype.attachShadow;
209
+ ElementClass.prototype.attachShadow = function(init) {
210
+ const shadow = attachShadow.call(this, init);
211
+ callback(shadow);
212
+ return shadow;
213
+ };
214
+ const attachInternals = ElementClass.prototype.attachInternals;
215
+ ElementClass.prototype.attachInternals = function() {
216
+ const internals = attachInternals.call(this);
217
+ if (internals.shadowRoot) callback(internals.shadowRoot);
218
+ return internals;
219
+ };
220
+ }
221
+ applyInvokerMixin(HTMLButtonElement);
222
+ observeShadowRoots(HTMLElement, (shadow) => {
223
+ setupInvokeListeners(shadow);
224
+ oncommandObserver.observe(shadow, { attributeFilter: ["oncommand"] });
225
+ applyOnCommandHandler(shadow.querySelectorAll("[oncommand]"));
226
+ });
227
+ setupInvokeListeners(document);
228
+ Object.assign(globalThis, { CommandEvent });
291
229
  }
292
-
293
- // src/utils/utils.ts
294
- var QUICK_EVENT = { passive: true, capture: true };
295
- var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
296
- var isWindows = () => isBrowser() && // @ts-expect-error Typescript has not implemented userAgentData yet https://stackoverflow.com/a/71392474
297
- /^Win/i.test(navigator.userAgentData?.platform || navigator.platform);
298
- var DSElement = typeof HTMLElement === "undefined" ? class {
299
- } : HTMLElement;
230
+ //#endregion
231
+ //#region src/utils/utils.ts
232
+ const QUICK_EVENT = {
233
+ passive: true,
234
+ capture: true
235
+ };
236
+ const isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
237
+ const isWindows = () => isBrowser() && /^Win/i.test(navigator.userAgentData?.platform || navigator.platform);
238
+ const DSElement = typeof HTMLElement === "undefined" ? class {} : HTMLElement;
300
239
  function debounce(callback, delay) {
301
- let timer;
302
- return function(...args) {
303
- clearTimeout(timer);
304
- timer = setTimeout(() => callback.apply(this, args), delay);
305
- };
240
+ let timer;
241
+ return function(...args) {
242
+ clearTimeout(timer);
243
+ timer = setTimeout(() => callback.apply(this, args), delay);
244
+ };
306
245
  }
307
- var warn = (message, ...args) => !isBrowser() || window.dsWarnings === false || console.log(`\x1B[1mDesignsystemet:\x1B[m ${message}`, ...args);
308
- var attr = (el, name, value) => {
309
- if (value === void 0) return el.getAttribute(name) ?? null;
310
- if (value === null) el.removeAttribute(name);
311
- else if (el.getAttribute(name) !== value) el.setAttribute(name, value);
312
- return null;
313
- };
314
- var getCSSProp = (el, prop) => getComputedStyle(el).getPropertyValue(prop).trim();
315
- var STRIP_QUOTES = /^["']|["']$/g;
316
- var attrOrCSS = (el, name) => {
317
- let value = attr(el, name);
318
- if (!value)
319
- value = getCSSProp(el, `--_ds-${name}`).replace(STRIP_QUOTES, "").trim();
320
- if (!value) warn(`Missing ${name} on:`, el);
321
- return value || null;
322
- };
323
- var on = (el, ...rest) => {
324
- const [types, ...options] = rest;
325
- for (const type of types.split(" ")) el.addEventListener(type, ...options);
326
- return () => off(el, ...rest);
327
- };
328
- var off = (el, ...rest) => {
329
- const [types, ...options] = rest;
330
- for (const type of types.split(" ")) el.removeEventListener(type, ...options);
331
- };
332
- var onHotReload = (key, setup) => {
333
- if (!isBrowser()) return;
334
- if (!window._dsHotReloadCleanup) window._dsHotReloadCleanup = /* @__PURE__ */ new Map();
335
- window._dsHotReloadCleanup?.get(key)?.map((cleanup) => cleanup());
336
- window._dsHotReloadCleanup?.set(key, setup());
337
- };
338
- var onMutation = (el, callback, options) => {
339
- const cleanup = () => observer.disconnect();
340
- const observer = new MutationObserver((records) => {
341
- if (!isBrowser() || !el.isConnected) return cleanup();
342
- callback(el, records);
343
- });
344
- callback(el);
345
- observer.observe(el, options);
346
- return cleanup;
347
- };
348
- var tag = (tagName, attrs) => {
349
- const el = document.createElement(tagName);
350
- if (attrs) for (const [key, val] of Object.entries(attrs)) attr(el, key, val);
351
- return el;
352
- };
353
- var customElements = {
354
- define: (name, instance) => !isBrowser() || window.customElements.get(name) || window.customElements.define(name, instance)
355
- };
356
- var id = 0;
246
+ const warn = (message, ...args) => !isBrowser() || window.dsWarnings === false || console.log(`\x1B[1mDesignsystemet:\x1B[m ${message}`, ...args);
247
+ /**
248
+ * attr
249
+ * @description Utility to quickly get, set and remove attributes
250
+ * @param el The Element to read/write attributes from
251
+ * @param name The attribute name to get, set or remove, or a object to set multiple attributes
252
+ * @param value A valid attribute value or null to remove attribute
253
+ */
254
+ const attr = (el, name, value) => {
255
+ if (value === void 0) return el.getAttribute(name) ?? null;
256
+ if (value === null) el.removeAttribute(name);
257
+ else if (el.getAttribute(name) !== value) el.setAttribute(name, value);
258
+ return null;
259
+ };
260
+ /**
261
+ * getCSSProp
262
+ * @description Retrieves and CSS property value and trims it
263
+ * @param el The Element to read attributes/CSS from
264
+ * @param name Attribute or CSS property to get
265
+ * @return string CSS property value
266
+ */
267
+ const getCSSProp = (el, prop) => getComputedStyle(el).getPropertyValue(prop).trim();
268
+ const STRIP_QUOTES = /^["']|["']$/g;
269
+ /**
270
+ * attrOrCSS
271
+ * @description Retrieves and updates attribute based on attribute or CSS property value
272
+ * @param el The Element to read attributes/CSS from
273
+ * @param name Attribute or CSS property to get
274
+ * @return string attribute or CSS property value
275
+ */
276
+ const attrOrCSS = (el, name) => {
277
+ let value = attr(el, name);
278
+ if (!value) value = getCSSProp(el, `--_ds-${name}`).replace(STRIP_QUOTES, "").trim();
279
+ if (!value) warn(`Missing ${name} on:`, el);
280
+ return value || null;
281
+ };
282
+ /**
283
+ * on
284
+ * @param el The Element to use as EventTarget
285
+ * @param types A space separated string of event types
286
+ * @param listener An event listener function or listener object
287
+ */
288
+ const on = (el, ...rest) => {
289
+ const [types, ...options] = rest;
290
+ for (const type of types.split(" ")) el.addEventListener(type, ...options);
291
+ return () => off(el, ...rest);
292
+ };
293
+ /**
294
+ * off
295
+ * @param el The Element to use as EventTarget
296
+ * @param types A space separated string of event types
297
+ * @param listener An event listener function or listener object
298
+ */
299
+ const off = (el, ...rest) => {
300
+ const [types, ...options] = rest;
301
+ for (const type of types.split(" ")) el.removeEventListener(type, ...options);
302
+ };
303
+ /**
304
+ * onHotReload
305
+ * @description Runs a callback when window is loaded in browser, and ensures cleanup when hot-reloading
306
+ * @param key The key to identify setup and corresponding cleanup
307
+ * @param callback The callback to run when the page is ready
308
+ */
309
+ const onHotReload = (key, setup) => {
310
+ if (!isBrowser()) return;
311
+ if (!window._dsHotReloadCleanup) window._dsHotReloadCleanup = /* @__PURE__ */ new Map();
312
+ window._dsHotReloadCleanup?.get(key)?.map((cleanup) => cleanup());
313
+ window._dsHotReloadCleanup?.set(key, setup());
314
+ };
315
+ /**
316
+ * MutationObserver wrapper with automatic cleanup
317
+ * @return new MutaionObserver
318
+ */
319
+ const onMutation = (el, callback, options) => {
320
+ const cleanup = () => observer.disconnect();
321
+ const observer = new MutationObserver((records) => {
322
+ if (!isBrowser() || !el.isConnected) return cleanup();
323
+ callback(el, records);
324
+ });
325
+ callback(el);
326
+ observer.observe(el, options);
327
+ return cleanup;
328
+ };
329
+ /**
330
+ * tag
331
+ * @description creates element and assigns properties
332
+ * @param tagName The tagname of element to create
333
+ * @param attrs Optional attributes to add to the element
334
+ * @param text Optional text content to add to the element
335
+ * @return HTMLElement with props
336
+ */
337
+ const tag = (tagName, attrs) => {
338
+ const el = document.createElement(tagName);
339
+ if (attrs) for (const [key, val] of Object.entries(attrs)) attr(el, key, val);
340
+ return el;
341
+ };
342
+ /**
343
+ * customElements.define
344
+ * @description Defines a customElement if running in browser and if not already registered
345
+ * Scoped/named "customElements.define" so @custom-elements-manifest/analyzer can find tag names
346
+ */
347
+ const customElements = { define: (name, instance) => !isBrowser() || window.customElements.get(name) || window.customElements.define(name, instance) };
348
+ let id = 0;
357
349
  function useId(el) {
358
- if (!isBrowser()) return `:ds:${++id}`;
359
- if (!window.dsUseId) window.dsUseId = 0;
360
- if (el && !el.id) el.id = `:ds:${++window.dsUseId}`;
361
- return el?.id || "";
350
+ if (!isBrowser()) return `:ds:${++id}`;
351
+ if (!window.dsUseId) window.dsUseId = 0;
352
+ if (el && !el.id) el.id = `:ds:${++window.dsUseId}`;
353
+ return el?.id || "";
362
354
  }
363
- var LIVE_EL;
364
- var LIVE_FIX = 0;
365
- var LIVE_CLEAR = 0;
366
- var announce = (text) => {
367
- clearTimeout(LIVE_CLEAR);
368
- if (LIVE_EL) LIVE_EL.textContent = `${text}${LIVE_FIX++ % 2 ? "\xA0" : ""}`;
369
- if (text) LIVE_CLEAR = setTimeout(announce, 2e3, "");
370
- };
371
- var announceMount = () => {
372
- if (document.readyState !== "complete") return;
373
- if (!LIVE_EL) {
374
- LIVE_EL = tag("div", { "aria-live": "assertive" });
375
- LIVE_EL.style.overflow = "hidden";
376
- LIVE_EL.style.position = "fixed";
377
- LIVE_EL.style.whiteSpace = "nowrap";
378
- LIVE_EL.style.width = "1px";
379
- }
380
- if (!LIVE_EL.isConnected) document.body.appendChild(LIVE_EL);
381
- };
382
- onHotReload("announce", () => [
383
- on(document, "focus mouseover", announceMount, QUICK_EVENT)
384
- ]);
385
-
386
- // src/index.ts
387
- import "@u-elements/u-details/polyfill";
388
-
389
- // src/clickdelegatefor/clickdelegatefor.ts
390
- var CLASS_HOVER = ":click-delegate-hover";
391
- var ATTR_CLICKDELEGATEFOR = "data-clickdelegatefor";
392
- var SELECTOR_CLICKDELEGATEFOR = `[${ATTR_CLICKDELEGATEFOR}]`;
393
- var SELECTOR_SKIP = 'a,button,label,input,select,textarea,details,dialog,[role="button"],[popover],[contenteditable]';
394
- var handleClickDelegateFor = (event) => {
395
- const isNewTab = event.button === 1 || event.metaKey || event.ctrlKey;
396
- const delegateTarget = event.button < 2 && getDelegateTarget(event);
397
- if (!delegateTarget || delegateTarget.contains(event.target)) return;
398
- if (isNewTab && delegateTarget instanceof HTMLAnchorElement)
399
- return window.open(delegateTarget.href, void 0, delegateTarget.rel);
400
- event.stopImmediatePropagation();
401
- delegateTarget.click();
402
- };
403
- var HOVER;
404
- var handleMouseOver = (event) => {
405
- const delegateTarget = getDelegateTarget(event);
406
- if (HOVER === delegateTarget) return;
407
- if (HOVER) HOVER.classList.remove(CLASS_HOVER);
408
- if (delegateTarget) delegateTarget.classList.add(CLASS_HOVER);
409
- HOVER = delegateTarget;
410
- };
411
- var getDelegateTarget = ({ target: el }) => {
412
- const scope = el instanceof Element ? el.closest(SELECTOR_CLICKDELEGATEFOR) : null;
413
- const id2 = scope?.getAttribute(ATTR_CLICKDELEGATEFOR);
414
- const target = id2 && document.getElementById(id2) || void 0;
415
- const skip = target && el.closest(SELECTOR_SKIP);
416
- return (!skip || skip === target) && !target?.disabled ? target : void 0;
417
- };
418
- onHotReload("clickdelegatefor", () => [
419
- on(window, "click auxclick", handleClickDelegateFor, true),
420
- // Use capture to ensure we run before other click listeners
421
- on(document, "mouseover", handleMouseOver, QUICK_EVENT)
422
- // Use passive for better performance
423
- ]);
424
-
425
- // src/dialog/dialog.ts
426
- var DOWN_INSIDE = false;
427
- var handleClosedbyAny = ({
428
- type,
429
- target: el,
430
- clientX: x = 0,
431
- clientY: y = 0
432
- }) => {
433
- if (type === "pointerdown") {
434
- const r = el?.closest?.("dialog")?.getBoundingClientRect();
435
- const isInside = r && r.top <= y && y <= r.bottom && r.left <= x && x <= r.right;
436
- DOWN_INSIDE = !!isInside;
437
- } else {
438
- const isDialog = el instanceof HTMLDialogElement;
439
- const isClose = isDialog && !DOWN_INSIDE && attr(el, "closedby") === "any";
440
- DOWN_INSIDE = false;
441
- if (isClose) requestAnimationFrame(() => el.open && el.close());
442
- }
443
- };
444
- var BUTTONS = isBrowser() ? document.getElementsByTagName("button") : [];
445
- var handleAriaAttributes = () => {
446
- for (const btn of BUTTONS)
447
- if (btn.getAttribute("command")?.endsWith("-modal"))
448
- btn.setAttribute("aria-haspopup", "dialog");
449
- };
450
- var handleCommand = ({ command, target }) => command === "--show-non-modal" && target instanceof HTMLDialogElement && target.show();
355
+ /**
356
+ * @description Based off speak function from [U-elements](https://github.com/u-elements/u-elements/blob/main/packages/utils.ts#L210)
357
+ * @param text The text to announce
358
+ */
359
+ let LIVE_EL;
360
+ let LIVE_FIX = 0;
361
+ let LIVE_CLEAR = 0;
362
+ const announce = (text) => {
363
+ clearTimeout(LIVE_CLEAR);
364
+ if (LIVE_EL) LIVE_EL.textContent = `${text}${LIVE_FIX++ % 2 ? "\xA0" : ""}`;
365
+ if (text) LIVE_CLEAR = setTimeout(announce, 2e3, "");
366
+ };
367
+ const announceMount = () => {
368
+ if (document.readyState !== "complete") return;
369
+ if (!LIVE_EL) {
370
+ LIVE_EL = tag("div", { "aria-live": "assertive" });
371
+ LIVE_EL.style.overflow = "hidden";
372
+ LIVE_EL.style.position = "fixed";
373
+ LIVE_EL.style.whiteSpace = "nowrap";
374
+ LIVE_EL.style.width = "1px";
375
+ }
376
+ if (!LIVE_EL.isConnected) document.body.appendChild(LIVE_EL);
377
+ };
378
+ onHotReload("announce", () => [on(document, "focus mouseover", announceMount, QUICK_EVENT)]);
379
+ //#endregion
380
+ //#region src/clickdelegatefor/clickdelegatefor.ts
381
+ const CLASS_HOVER = ":click-delegate-hover";
382
+ const ATTR_CLICKDELEGATEFOR = "data-clickdelegatefor";
383
+ const SELECTOR_CLICKDELEGATEFOR = `[${ATTR_CLICKDELEGATEFOR}]`;
384
+ const SELECTOR_SKIP = "a,button,label,input,select,textarea,details,dialog,[role=\"button\"],[popover],[contenteditable]";
385
+ const handleClickDelegateFor = (event) => {
386
+ const isNewTab = event.button === 1 || event.metaKey || event.ctrlKey;
387
+ const delegateTarget = event.button < 2 && getDelegateTarget(event);
388
+ if (!delegateTarget || delegateTarget.contains(event.target)) return;
389
+ if (isNewTab && delegateTarget instanceof HTMLAnchorElement) return window.open(delegateTarget.href, void 0, delegateTarget.rel);
390
+ event.stopImmediatePropagation();
391
+ delegateTarget.click();
392
+ };
393
+ let HOVER;
394
+ const handleMouseOver = (event) => {
395
+ const delegateTarget = getDelegateTarget(event);
396
+ if (HOVER === delegateTarget) return;
397
+ if (HOVER) HOVER.classList.remove(CLASS_HOVER);
398
+ if (delegateTarget) delegateTarget.classList.add(CLASS_HOVER);
399
+ HOVER = delegateTarget;
400
+ };
401
+ const getDelegateTarget = ({ target: el }) => {
402
+ const id = (el instanceof Element ? el.closest(SELECTOR_CLICKDELEGATEFOR) : null)?.getAttribute(ATTR_CLICKDELEGATEFOR);
403
+ const target = id && document.getElementById(id) || void 0;
404
+ const skip = target && el.closest(SELECTOR_SKIP);
405
+ return (!skip || skip === target) && !target?.disabled ? target : void 0;
406
+ };
407
+ onHotReload("clickdelegatefor", () => [on(window, "click auxclick", handleClickDelegateFor, true), on(document, "mouseover", handleMouseOver, QUICK_EVENT)]);
408
+ //#endregion
409
+ //#region src/dialog/dialog.ts
410
+ let DOWN_INSIDE = false;
411
+ const handleClosedbyAny = ({ type, target: el, clientX: x = 0, clientY: y = 0 }) => {
412
+ if (type === "pointerdown") {
413
+ const r = el?.closest?.("dialog")?.getBoundingClientRect();
414
+ DOWN_INSIDE = !!(r && r.top <= y && y <= r.bottom && r.left <= x && x <= r.right);
415
+ } else {
416
+ const isClose = el instanceof HTMLDialogElement && !DOWN_INSIDE && attr(el, "closedby") === "any";
417
+ DOWN_INSIDE = false;
418
+ if (isClose) requestAnimationFrame(() => el.open && el.close());
419
+ }
420
+ };
421
+ const BUTTONS = isBrowser() ? document.getElementsByTagName("button") : [];
422
+ const handleAriaAttributes$2 = () => {
423
+ for (const btn of BUTTONS) if (btn.getAttribute("command")?.endsWith("-modal")) btn.setAttribute("aria-haspopup", "dialog");
424
+ };
425
+ const handleCommand = ({ command, target }) => command === "--show-non-modal" && target instanceof HTMLDialogElement && target.show();
451
426
  onHotReload("dialog", () => [
452
- on(document, "command", handleCommand, QUICK_EVENT),
453
- on(document, "pointerdown pointerup", handleClosedbyAny, QUICK_EVENT),
454
- onMutation(document, handleAriaAttributes, {
455
- attributeFilter: ["command"],
456
- attributes: true,
457
- childList: true,
458
- subtree: true
459
- })
460
- ]);
461
-
462
- // src/fieldset/fieldset.ts
463
- var FIELDSETS = isBrowser() ? document.getElementsByTagName("fieldset") : [];
464
- var handleFieldsetMutations = () => {
465
- for (const el of FIELDSETS) {
466
- if (el.hasAttribute("aria-labelledby")) continue;
467
- const labelledby = `${useId(el.querySelector("legend"))} ${useId(el.querySelector(':scope > :is([data-field="description"],legend + p)'))}`;
468
- attr(el, "aria-labelledby", labelledby.trim() || null);
469
- }
470
- };
471
- onHotReload("fieldset", () => [
472
- onMutation(document, handleFieldsetMutations, {
473
- childList: true,
474
- subtree: true
475
- })
427
+ on(document, "command", handleCommand, QUICK_EVENT),
428
+ on(document, "pointerdown pointerup", handleClosedbyAny, QUICK_EVENT),
429
+ onMutation(document, handleAriaAttributes$2, {
430
+ attributeFilter: ["command"],
431
+ attributes: true,
432
+ childList: true,
433
+ subtree: true
434
+ })
476
435
  ]);
477
-
478
- // src/popover/popover.ts
479
- import {
480
- autoUpdate,
481
- computePosition,
482
- flip,
483
- limitShift,
484
- offset,
485
- shift,
486
- size
487
- } from "@floating-ui/dom";
488
- var ATTR_PLACE = "data-placement";
489
- var ATTR_AUTO = "data-autoplacement";
490
- var POPOVERS = /* @__PURE__ */ new Map();
436
+ //#endregion
437
+ //#region src/fieldset/fieldset.ts
438
+ const FIELDSETS = isBrowser() ? document.getElementsByTagName("fieldset") : [];
439
+ const handleFieldsetMutations = () => {
440
+ for (const el of FIELDSETS) {
441
+ if (el.hasAttribute("aria-labelledby")) continue;
442
+ attr(el, "aria-labelledby", `${useId(el.querySelector("legend"))} ${useId(el.querySelector(":scope > :is([data-field=\"description\"],legend + p)"))}`.trim() || null);
443
+ }
444
+ };
445
+ onHotReload("fieldset", () => [onMutation(document, handleFieldsetMutations, {
446
+ childList: true,
447
+ subtree: true
448
+ })]);
449
+ //#endregion
450
+ //#region src/popover/popover.ts
451
+ const ATTR_PLACE = "data-placement";
452
+ const ATTR_AUTO = "data-autoplacement";
453
+ const POPOVERS = /* @__PURE__ */ new Map();
491
454
  function handleToggle(event) {
492
- let { newState, oldState, target: el, source = event.detail } = event;
493
- const isPopover = el instanceof HTMLElement && attr(el, "popover") !== null;
494
- const float = isPopover && getCSSProp(el, "--_ds-floating");
495
- if (!float) return;
496
- if (newState === "closed") return POPOVERS.get(el)?.();
497
- if (!source) {
498
- const root = el.getRootNode();
499
- const css = `[popovertarget="${el.id}"],[commandfor="${el.id}"]`;
500
- source = el.id && root?.querySelector?.(css) || void 0;
501
- }
502
- if (!source || source === el || oldState && oldState === newState) return;
503
- const padding = 10;
504
- const overscroll = getCSSProp(el, "--_ds-floating-overscroll");
505
- const placement = attr(el, ATTR_PLACE) || attr(source, ATTR_PLACE) || float;
506
- const auto = attr(el, ATTR_AUTO) || attr(source, ATTR_AUTO);
507
- const arrowSize = parseFloat(getComputedStyle(el, "::before").height) || 0;
508
- const shiftProp = placement.match(/left|right/gi) ? "Height" : "Width";
509
- const shiftLimit = source[`offset${shiftProp}`] / 2 + arrowSize;
510
- if (placement === "none") return;
511
- const options = {
512
- strategy: "absolute",
513
- placement,
514
- middleware: [
515
- offset(arrowSize || 0),
516
- // Add space for arrow or default to 8px
517
- shift({
518
- padding,
519
- limiter: limitShift({ offset: { mainAxis: shiftLimit } })
520
- // Prevent from shifing away from source
521
- }),
522
- arrowPseudo(),
523
- ...auto !== "false" ? [flip({ padding, crossAxis: false })] : [],
524
- ...overscroll ? [
525
- size({
526
- apply({ availableHeight }) {
527
- if (overscroll === "fit")
528
- el.style.width = `${source.clientWidth}px`;
529
- el.style.maxHeight = `${Math.max(50, availableHeight - padding * 2)}px`;
530
- }
531
- })
532
- ] : []
533
- ]
534
- };
535
- const unfloat = autoUpdate(source, el, async () => {
536
- if (!source?.isConnected) return POPOVERS.get(el)?.();
537
- const { x, y } = await computePosition(source, el, options);
538
- el.style.translate = `${x}px ${y}px`;
539
- });
540
- POPOVERS.set(el, () => POPOVERS.delete(el) && unfloat());
455
+ let { newState, oldState, target: el, source = event.detail } = event;
456
+ const float = el instanceof HTMLElement && attr(el, "popover") !== null && getCSSProp(el, "--_ds-floating");
457
+ if (!float) return;
458
+ if (newState === "closed") return POPOVERS.get(el)?.();
459
+ if (!source) {
460
+ const root = el.getRootNode();
461
+ const css = `[popovertarget="${el.id}"],[commandfor="${el.id}"]`;
462
+ source = el.id && root?.querySelector?.(css) || void 0;
463
+ }
464
+ if (!source || source === el || oldState && oldState === newState) return;
465
+ const padding = 10;
466
+ const overscroll = getCSSProp(el, "--_ds-floating-overscroll");
467
+ const placement = attr(el, ATTR_PLACE) || attr(source, ATTR_PLACE) || float;
468
+ const auto = attr(el, ATTR_AUTO) || attr(source, ATTR_AUTO);
469
+ const arrowSize = parseFloat(getComputedStyle(el, "::before").height) || 0;
470
+ const shiftProp = placement.match(/left|right/gi) ? "Height" : "Width";
471
+ const shiftLimit = source[`offset${shiftProp}`] / 2 + arrowSize;
472
+ if (placement === "none") return;
473
+ const options = {
474
+ strategy: "absolute",
475
+ placement,
476
+ middleware: [
477
+ offset(arrowSize || 0),
478
+ shift({
479
+ padding,
480
+ limiter: limitShift({ offset: { mainAxis: shiftLimit } })
481
+ }),
482
+ arrowPseudo(),
483
+ ...auto !== "false" ? [flip({
484
+ padding,
485
+ crossAxis: false
486
+ })] : [],
487
+ ...overscroll ? [size({ apply({ availableHeight }) {
488
+ if (overscroll === "fit") el.style.width = `${source.clientWidth}px`;
489
+ el.style.maxHeight = `${Math.max(50, availableHeight - padding * 2)}px`;
490
+ } })] : []
491
+ ]
492
+ };
493
+ const unfloat = autoUpdate(source, el, async () => {
494
+ if (!source?.isConnected) return POPOVERS.get(el)?.();
495
+ const { x, y } = await computePosition(source, el, options);
496
+ el.style.translate = `${x}px ${y}px`;
497
+ });
498
+ POPOVERS.set(el, () => POPOVERS.delete(el) && unfloat());
541
499
  }
542
- var IS_SCROLL;
543
- var handleScrollbar = ({ type }) => {
544
- if (type === "mousedown") IS_SCROLL = false;
545
- if (type === "scroll" && IS_SCROLL === false) IS_SCROLL = true;
546
- if (type === "mouseup" && IS_SCROLL)
547
- for (const [popover] of POPOVERS) popover.showPopover();
548
- };
549
- onHotReload("popover", () => [
550
- on(document, "mousedown scroll mouseup", handleScrollbar, true),
551
- on(document, "toggle ds-toggle-source", handleToggle, QUICK_EVENT)
552
- // Use capture since the toggle event does not bubble
553
- ]);
554
- var arrowPseudo = () => ({
555
- name: "arrowPseudo",
556
- fn(data) {
557
- const target = data.elements.floating;
558
- const source = data.rects.reference;
559
- const x = `${Math.round(source.width / 2 + source.x - data.x)}px`;
560
- const y = `${Math.round(source.height / 2 + source.y - data.y)}px`;
561
- target.style.setProperty("--_ds-floating-arrow-x", x);
562
- target.style.setProperty("--_ds-floating-arrow-y", y);
563
- attr(target, "data-floating", data.placement);
564
- return data;
565
- }
500
+ let IS_SCROLL;
501
+ const handleScrollbar = ({ type }) => {
502
+ if (type === "mousedown") IS_SCROLL = false;
503
+ if (type === "scroll" && IS_SCROLL === false) IS_SCROLL = true;
504
+ if (type === "mouseup" && IS_SCROLL) for (const [popover] of POPOVERS) popover.showPopover();
505
+ };
506
+ onHotReload("popover", () => [on(document, "mousedown scroll mouseup", handleScrollbar, true), on(document, "toggle ds-toggle-source", handleToggle, QUICK_EVENT)]);
507
+ const arrowPseudo = () => ({
508
+ name: "arrowPseudo",
509
+ fn(data) {
510
+ const target = data.elements.floating;
511
+ const source = data.rects.reference;
512
+ const x = `${Math.round(source.width / 2 + source.x - data.x)}px`;
513
+ const y = `${Math.round(source.height / 2 + source.y - data.y)}px`;
514
+ target.style.setProperty("--_ds-floating-arrow-x", x);
515
+ target.style.setProperty("--_ds-floating-arrow-y", y);
516
+ attr(target, "data-floating", data.placement);
517
+ return data;
518
+ }
566
519
  });
567
-
568
- // src/readonly/readonly.ts
569
- var isReadOnly = (el) => (el instanceof HTMLSelectElement || el instanceof HTMLInputElement) && (el.hasAttribute("readonly") || attr(el, "aria-readonly") === "true");
570
- var handleKeyDown = (e) => {
571
- if (e.key !== "Tab" && isReadOnly(e.target)) {
572
- const isArrow = e.key?.startsWith("Arrow");
573
- const isModifier = e.altKey || e.ctrlKey || e.metaKey;
574
- if (isArrow || !isModifier) e.preventDefault();
575
- if (isArrow && attr(e.target, "type") === "radio") {
576
- const all = document.querySelectorAll(`input[name="${e.target.name}"]`);
577
- const move = e.key?.match(/Arrow(Right|Down)/) ? 1 : -1;
578
- const next = all.length + [...all].indexOf(e.target) + move;
579
- all[next % all.length]?.focus();
580
- }
581
- }
582
- };
583
- var handleClick = (e) => {
584
- const input = e.target?.closest?.("label")?.control || e.target;
585
- if (isReadOnly(input)) {
586
- e.preventDefault();
587
- input.focus();
588
- }
589
- };
590
- var handleMouseDown = (e) => {
591
- if (e.target instanceof HTMLSelectElement && isReadOnly(e.target))
592
- e.preventDefault();
520
+ //#endregion
521
+ //#region src/readonly/readonly.ts
522
+ const isReadOnly = (el) => (el instanceof HTMLSelectElement || el instanceof HTMLInputElement) && (el.hasAttribute("readonly") || attr(el, "aria-readonly") === "true");
523
+ const handleKeyDown = (e) => {
524
+ if (e.key !== "Tab" && isReadOnly(e.target)) {
525
+ const isArrow = e.key?.startsWith("Arrow");
526
+ const isModifier = e.altKey || e.ctrlKey || e.metaKey;
527
+ if (isArrow || !isModifier) e.preventDefault();
528
+ if (isArrow && attr(e.target, "type") === "radio") {
529
+ const all = document.querySelectorAll(`input[name="${e.target.name}"]`);
530
+ const move = e.key?.match(/Arrow(Right|Down)/) ? 1 : -1;
531
+ all[(all.length + [...all].indexOf(e.target) + move) % all.length]?.focus();
532
+ }
533
+ }
534
+ };
535
+ const handleClick = (e) => {
536
+ const input = e.target?.closest?.("label")?.control || e.target;
537
+ if (isReadOnly(input)) {
538
+ e.preventDefault();
539
+ input.focus();
540
+ }
541
+ };
542
+ const handleMouseDown = (e) => {
543
+ if (e.target instanceof HTMLSelectElement && isReadOnly(e.target)) e.preventDefault();
593
544
  };
594
545
  onHotReload("readonly", () => [
595
- on(document, "keydown", handleKeyDown),
596
- on(document, "click", handleClick),
597
- // click needed for <label> and <input>
598
- on(document, "mousedown", handleMouseDown)
599
- // mousedown needed for <select>
546
+ on(document, "keydown", handleKeyDown),
547
+ on(document, "click", handleClick),
548
+ on(document, "mousedown", handleMouseDown)
600
549
  ]);
601
-
602
- // src/toggle-group/toggle-group.ts
603
- var ARIA_LABELLEDBY = "aria-labelledby";
604
- var ARIA_LABEL = "aria-label";
605
- var ATTR_TOGGLEGROUP = "data-toggle-group";
606
- var SELECTOR_TOGGLEGROUP = `[${ATTR_TOGGLEGROUP}]`;
607
- var handleAriaAttributes2 = () => {
608
- for (const group of document.querySelectorAll(SELECTOR_TOGGLEGROUP))
609
- attr(group, "aria-label", attrOrCSS(group, ATTR_TOGGLEGROUP));
610
- };
611
- var handleKeydown = (event) => {
612
- const { key, target: el } = event;
613
- const group = el instanceof HTMLInputElement && el.closest(SELECTOR_TOGGLEGROUP);
614
- if (!group) return;
615
- if (!attr(group, ARIA_LABEL) && !attr(group, ARIA_LABELLEDBY))
616
- attr(group, ARIA_LABEL, attrOrCSS(group, ATTR_TOGGLEGROUP));
617
- if (key === "Enter") el.click();
618
- if (key?.startsWith("Arrow")) {
619
- event.preventDefault?.();
620
- const inputs = [...group.getElementsByTagName("input")];
621
- const index = inputs.indexOf(el);
622
- const move = key.match(/Arrow(Right|Down)/) ? 1 : -1;
623
- let nextIndex = index;
624
- for (let i = 0; i < inputs.length; i++) {
625
- nextIndex = (inputs.length + nextIndex + move) % inputs.length;
626
- if (!inputs[nextIndex]?.disabled) {
627
- inputs[nextIndex]?.focus();
628
- break;
629
- }
630
- }
631
- }
632
- };
633
- onHotReload("toggle-group", () => [
634
- on(document, "keydown", handleKeydown),
635
- onMutation(document, handleAriaAttributes2, {
636
- attributeFilter: [ATTR_TOGGLEGROUP],
637
- attributes: true,
638
- childList: true,
639
- subtree: true
640
- })
641
- ]);
642
-
643
- // src/tooltip/tooltip.ts
644
- var TIP;
645
- var SOURCE;
646
- var IS_HOVERING = false;
647
- var HOVER_TIMER = 0;
648
- var SKIP_TIMER = 0;
649
- var IS_IOS = isBrowser() && /iPad|iPhone|iPod/.test(navigator.userAgent);
650
- var ATTR_TOOLTIP = "data-tooltip";
651
- var ATTR_COLOR = "data-color";
652
- var ARIA_LABEL2 = "aria-label";
653
- var ARIA_DESC = "aria-description";
654
- var SELECTOR_COLOR = `[${ATTR_COLOR}]`;
655
- var SELECTOR_TOOLTIP = `[${ATTR_TOOLTIP}]`;
656
- var ATTR_SCHEME = "data-color-scheme";
657
- var SELECTOR_SCHEME = `[${ATTR_SCHEME}]`;
658
- var SELECTOR_INTERACTIVE = "a,button,input,label,select,textarea,[tabindex]";
659
- var DELAY_HOVER = 300;
660
- var DELAY_SKIP = 300;
661
- var setTooltipElement = (el) => {
662
- if (el && !(el instanceof HTMLElement))
663
- warn("setTooltipElement expects an HTMLElement, got: ", el);
664
- clearTimeout(SKIP_TIMER);
665
- clearTimeout(HOVER_TIMER);
666
- SOURCE = void 0;
667
- IS_HOVERING = false;
668
- TIP = el || void 0;
669
- };
670
- var handleAriaAttributes3 = () => {
671
- for (const el of document.querySelectorAll(SELECTOR_TOOLTIP)) {
672
- const text = attrOrCSS(el, ATTR_TOOLTIP);
673
- if (!text) return;
674
- if (text !== (el.getAttribute(ARIA_LABEL2) || el.getAttribute(ARIA_DESC))) {
675
- const hasText = attr(el, "role") !== "img" && el.textContent?.trim();
676
- attr(el, ATTR_TOOLTIP, text);
677
- attr(el, ARIA_LABEL2, hasText ? null : text);
678
- attr(el, ARIA_DESC, hasText ? text : null);
679
- if (!el.matches(SELECTOR_INTERACTIVE))
680
- warn('Missing tabindex="0" attribute on: ', el);
681
- }
682
- const isCurrent = el === SOURCE && TIP?.offsetHeight && TIP?.offsetWidth;
683
- const isChanged = isCurrent && text && TIP?.textContent !== text;
684
- if (isCurrent && isChanged) {
685
- if (TIP) TIP.textContent = text;
686
- if (document.activeElement === el) announce(text);
687
- }
688
- }
689
- };
690
- var handleInterest = ({ type, target }) => {
691
- clearTimeout(HOVER_TIMER);
692
- if (target === TIP) return;
693
- if (type === "mouseover" && !IS_HOVERING && !IS_IOS) {
694
- HOVER_TIMER = setTimeout(handleInterest, DELAY_HOVER, { target });
695
- return;
696
- }
697
- const source = target?.closest?.(`[${ATTR_TOOLTIP}]`);
698
- if (source === SOURCE) return;
699
- if (!source) return hideTooltip();
700
- if (!TIP) TIP = tag("div", { class: "ds-tooltip" });
701
- if (!TIP.isConnected) document.body.appendChild(TIP);
702
- const color = source.closest(SELECTOR_COLOR);
703
- const scheme = source.closest(SELECTOR_SCHEME);
704
- const isReset = color !== scheme && color?.contains(scheme);
705
- clearTimeout(SKIP_TIMER);
706
- attr(TIP, "popover", "manual");
707
- attr(TIP, ATTR_SCHEME, scheme?.getAttribute(ATTR_SCHEME) || null);
708
- attr(TIP, ATTR_COLOR, isReset && color?.getAttribute(ATTR_COLOR) || null);
709
- TIP.textContent = attr(source, ATTR_TOOLTIP);
710
- TIP.showPopover();
711
- TIP.dispatchEvent(new CustomEvent("ds-toggle-source", { detail: source }));
712
- IS_HOVERING = true;
713
- SOURCE = source;
714
- };
715
- var hideTooltip = () => TIP?.isConnected && TIP.popover && TIP.hidePopover();
716
- var handleClose = (event) => {
717
- if (event?.type === "keydown")
718
- return event?.key === "Escape" && hideTooltip();
719
- if (!event) IS_HOVERING = false;
720
- else if (event.target === TIP && event.newState === "closed") {
721
- SOURCE = void 0;
722
- SKIP_TIMER = setTimeout(handleClose, DELAY_SKIP);
723
- }
550
+ //#endregion
551
+ //#region src/toggle-group/toggle-group.ts
552
+ const ARIA_LABELLEDBY = "aria-labelledby";
553
+ const ARIA_LABEL$1 = "aria-label";
554
+ const ATTR_TOGGLEGROUP = "data-toggle-group";
555
+ const SELECTOR_TOGGLEGROUP = `[${ATTR_TOGGLEGROUP}]`;
556
+ const handleAriaAttributes$1 = () => {
557
+ for (const group of document.querySelectorAll(SELECTOR_TOGGLEGROUP)) attr(group, "aria-label", attrOrCSS(group, ATTR_TOGGLEGROUP));
558
+ };
559
+ const handleKeydown = (event) => {
560
+ const { key, target: el } = event;
561
+ const group = el instanceof HTMLInputElement && el.closest(SELECTOR_TOGGLEGROUP);
562
+ if (!group) return;
563
+ if (!attr(group, ARIA_LABEL$1) && !attr(group, ARIA_LABELLEDBY)) attr(group, ARIA_LABEL$1, attrOrCSS(group, ATTR_TOGGLEGROUP));
564
+ if (key === "Enter") el.click();
565
+ if (key?.startsWith("Arrow")) {
566
+ event.preventDefault?.();
567
+ const inputs = [...group.getElementsByTagName("input")];
568
+ const index = inputs.indexOf(el);
569
+ const move = key.match(/Arrow(Right|Down)/) ? 1 : -1;
570
+ let nextIndex = index;
571
+ for (let i = 0; i < inputs.length; i++) {
572
+ nextIndex = (inputs.length + nextIndex + move) % inputs.length;
573
+ if (!inputs[nextIndex]?.disabled) {
574
+ inputs[nextIndex]?.focus();
575
+ break;
576
+ }
577
+ }
578
+ }
579
+ };
580
+ onHotReload("toggle-group", () => [on(document, "keydown", handleKeydown), onMutation(document, handleAriaAttributes$1, {
581
+ attributeFilter: [ATTR_TOGGLEGROUP],
582
+ attributes: true,
583
+ childList: true,
584
+ subtree: true
585
+ })]);
586
+ //#endregion
587
+ //#region src/tooltip/tooltip.ts
588
+ let TIP;
589
+ let SOURCE;
590
+ let IS_HOVERING = false;
591
+ let HOVER_TIMER = 0;
592
+ let SKIP_TIMER = 0;
593
+ const IS_IOS = isBrowser() && /iPad|iPhone|iPod/.test(navigator.userAgent);
594
+ const ATTR_TOOLTIP = "data-tooltip";
595
+ const ATTR_COLOR = "data-color";
596
+ const ARIA_LABEL = "aria-label";
597
+ const ARIA_DESC = "aria-description";
598
+ const SELECTOR_COLOR = `[${ATTR_COLOR}]`;
599
+ const SELECTOR_TOOLTIP = `[${ATTR_TOOLTIP}]`;
600
+ const ATTR_SCHEME = "data-color-scheme";
601
+ const SELECTOR_SCHEME = `[${ATTR_SCHEME}]`;
602
+ const SELECTOR_INTERACTIVE = "a,button,input,label,select,textarea,[tabindex]";
603
+ const DELAY_HOVER = 300;
604
+ const DELAY_SKIP = 300;
605
+ /**
606
+ * setTooltipElement
607
+ * @description Allows setting a custom tooltip element. It does not need to, and should not, be injected to document.body, as we inject on hover to ensure React hydration works as expected.
608
+ * @param el The HTMLElement to use as tooltip
609
+ */
610
+ const setTooltipElement = (el) => {
611
+ if (el && !(el instanceof HTMLElement)) warn("setTooltipElement expects an HTMLElement, got: ", el);
612
+ clearTimeout(SKIP_TIMER);
613
+ clearTimeout(HOVER_TIMER);
614
+ SOURCE = void 0;
615
+ IS_HOVERING = false;
616
+ TIP = el || void 0;
617
+ };
618
+ const handleAriaAttributes = () => {
619
+ for (const el of document.querySelectorAll(SELECTOR_TOOLTIP)) {
620
+ const text = attrOrCSS(el, ATTR_TOOLTIP);
621
+ if (!text) return;
622
+ if (text !== (el.getAttribute(ARIA_LABEL) || el.getAttribute(ARIA_DESC))) {
623
+ const hasText = attr(el, "role") !== "img" && el.textContent?.trim();
624
+ attr(el, ATTR_TOOLTIP, text);
625
+ attr(el, ARIA_LABEL, hasText ? null : text);
626
+ attr(el, ARIA_DESC, hasText ? text : null);
627
+ if (!el.matches(SELECTOR_INTERACTIVE)) warn("Missing tabindex=\"0\" attribute on: ", el);
628
+ }
629
+ const isCurrent = el === SOURCE && TIP?.offsetHeight && TIP?.offsetWidth;
630
+ const isChanged = isCurrent && text && TIP?.textContent !== text;
631
+ if (isCurrent && isChanged) {
632
+ if (TIP) TIP.textContent = text;
633
+ if (document.activeElement === el) announce(text);
634
+ }
635
+ }
636
+ };
637
+ const handleInterest = ({ type, target }) => {
638
+ clearTimeout(HOVER_TIMER);
639
+ if (target === TIP) return;
640
+ if (type === "mouseover" && !IS_HOVERING && !IS_IOS) {
641
+ HOVER_TIMER = setTimeout(handleInterest, DELAY_HOVER, { target });
642
+ return;
643
+ }
644
+ const source = target?.closest?.(`[${ATTR_TOOLTIP}]`);
645
+ if (source === SOURCE) return;
646
+ if (!source) return hideTooltip();
647
+ if (!TIP) TIP = tag("div", { class: "ds-tooltip" });
648
+ if (!TIP.isConnected) document.body.appendChild(TIP);
649
+ const color = source.closest(SELECTOR_COLOR);
650
+ const scheme = source.closest(SELECTOR_SCHEME);
651
+ const isReset = color !== scheme && color?.contains(scheme);
652
+ clearTimeout(SKIP_TIMER);
653
+ attr(TIP, "popover", "manual");
654
+ attr(TIP, ATTR_SCHEME, scheme?.getAttribute(ATTR_SCHEME) || null);
655
+ attr(TIP, ATTR_COLOR, isReset && color?.getAttribute(ATTR_COLOR) || null);
656
+ TIP.textContent = attr(source, ATTR_TOOLTIP);
657
+ TIP.showPopover();
658
+ TIP.dispatchEvent(new CustomEvent("ds-toggle-source", { detail: source }));
659
+ IS_HOVERING = true;
660
+ SOURCE = source;
661
+ };
662
+ const hideTooltip = () => TIP?.isConnected && TIP.popover && TIP.hidePopover();
663
+ const handleClose = (event) => {
664
+ if (event?.type === "keydown") return event?.key === "Escape" && hideTooltip();
665
+ if (!event) IS_HOVERING = false;
666
+ else if (event.target === TIP && event.newState === "closed") {
667
+ SOURCE = void 0;
668
+ SKIP_TIMER = setTimeout(handleClose, DELAY_SKIP);
669
+ }
724
670
  };
725
671
  onHotReload("tooltip", () => [
726
- on(document, "blur focus mouseover", handleInterest, QUICK_EVENT),
727
- on(document, "toggle keydown", handleClose, QUICK_EVENT),
728
- onMutation(document, handleAriaAttributes3, {
729
- attributeFilter: [ATTR_TOOLTIP],
730
- attributes: true,
731
- childList: true,
732
- subtree: true
733
- })
672
+ on(document, "blur focus mouseover", handleInterest, QUICK_EVENT),
673
+ on(document, "toggle keydown", handleClose, QUICK_EVENT),
674
+ onMutation(document, handleAriaAttributes, {
675
+ attributeFilter: [ATTR_TOOLTIP],
676
+ attributes: true,
677
+ childList: true,
678
+ subtree: true
679
+ })
734
680
  ]);
735
-
736
- // src/index.ts
737
- export * from "@u-elements/u-datalist";
738
-
739
- // src/breadcrumbs/breadcrumbs.ts
740
- var ATTR_LABEL = "aria-label";
681
+ //#endregion
682
+ //#region src/breadcrumbs/breadcrumbs.ts
683
+ const ATTR_LABEL$1 = "aria-label";
741
684
  var DSBreadcrumbsElement = class extends DSElement {
742
- _items;
743
- // Using underscore instead of private fields for backwards compatibility
744
- _label = null;
745
- _unresize;
746
- _unmutate;
747
- static get observedAttributes() {
748
- return [ATTR_LABEL];
749
- }
750
- connectedCallback() {
751
- const resize = debounce(() => render(this), 100);
752
- this._label = attrOrCSS(this, ATTR_LABEL);
753
- this._items = this.getElementsByTagName("a");
754
- this._unresize = on(window, "resize", resize);
755
- this._unmutate = onMutation(this, render, {
756
- childList: true,
757
- subtree: true
758
- });
759
- }
760
- attributeChangedCallback(_name, _prev, next) {
761
- if (!this._unmutate || !next) return;
762
- this._label = next;
763
- render(this);
764
- }
765
- disconnectedCallback() {
766
- this._unresize?.();
767
- this._unmutate?.();
768
- this._unresize = this._unmutate = this._items = void 0;
769
- }
770
- };
771
- var render = (self) => {
772
- const lastItem = self._items?.[self._items.length - 1];
773
- const lastItemInList = lastItem?.parentElement === self ? null : lastItem;
774
- const isListHidden = !lastItemInList?.offsetHeight;
775
- attr(self, "role", isListHidden ? null : "navigation");
776
- attr(self, ATTR_LABEL, isListHidden ? null : self._label);
777
- for (const item of self._items || [])
778
- attr(item, "aria-current", item === lastItemInList ? "page" : null);
685
+ _items;
686
+ _label = null;
687
+ _unresize;
688
+ _unmutate;
689
+ static get observedAttributes() {
690
+ return [ATTR_LABEL$1];
691
+ }
692
+ connectedCallback() {
693
+ const resize = debounce(() => render$3(this), 100);
694
+ this._label = attrOrCSS(this, ATTR_LABEL$1);
695
+ this._items = this.getElementsByTagName("a");
696
+ this._unresize = on(window, "resize", resize);
697
+ this._unmutate = onMutation(this, render$3, {
698
+ childList: true,
699
+ subtree: true
700
+ });
701
+ }
702
+ attributeChangedCallback(_name, _prev, next) {
703
+ if (!this._unmutate || !next) return;
704
+ this._label = next;
705
+ render$3(this);
706
+ }
707
+ disconnectedCallback() {
708
+ this._unresize?.();
709
+ this._unmutate?.();
710
+ this._unresize = this._unmutate = this._items = void 0;
711
+ }
712
+ };
713
+ const render$3 = (self) => {
714
+ const lastItem = self._items?.[self._items.length - 1];
715
+ const lastItemInList = lastItem?.parentElement === self ? null : lastItem;
716
+ const isListHidden = !lastItemInList?.offsetHeight;
717
+ attr(self, "role", isListHidden ? null : "navigation");
718
+ attr(self, ATTR_LABEL$1, isListHidden ? null : self._label);
719
+ for (const item of self._items || []) attr(item, "aria-current", item === lastItemInList ? "page" : null);
779
720
  };
780
721
  customElements.define("ds-breadcrumbs", DSBreadcrumbsElement);
781
-
782
- // src/error-summary/error-summary.ts
722
+ //#endregion
723
+ //#region src/error-summary/error-summary.ts
783
724
  var DSErrorSummaryElement = class extends DSElement {
784
- _unmutate;
785
- // Using underscore instead of private fields for backwards compatibility
786
- connectedCallback() {
787
- on(this, "animationend", this, QUICK_EVENT);
788
- attr(this, "tabindex", "-1");
789
- this._unmutate = onMutation(this, render2, {
790
- childList: true,
791
- subtree: true
792
- });
793
- this.focus();
794
- }
795
- handleEvent({ target }) {
796
- if (target === this) this.focus();
797
- }
798
- disconnectedCallback() {
799
- off(this, "animationend", this, QUICK_EVENT);
800
- this._unmutate?.();
801
- this._unmutate = void 0;
802
- }
803
- };
804
- var render2 = (self) => {
805
- const heading = self.querySelector("h2,h3,h4,h5,h6");
806
- if (heading) attr(self, "aria-labelledby", useId(heading));
725
+ _unmutate;
726
+ connectedCallback() {
727
+ on(this, "animationend", this, QUICK_EVENT);
728
+ attr(this, "tabindex", "-1");
729
+ this._unmutate = onMutation(this, render$2, {
730
+ childList: true,
731
+ subtree: true
732
+ });
733
+ this.focus();
734
+ }
735
+ handleEvent({ target }) {
736
+ if (target === this) this.focus();
737
+ }
738
+ disconnectedCallback() {
739
+ off(this, "animationend", this, QUICK_EVENT);
740
+ this._unmutate?.();
741
+ this._unmutate = void 0;
742
+ }
743
+ };
744
+ const render$2 = (self) => {
745
+ const heading = self.querySelector("h2,h3,h4,h5,h6");
746
+ if (heading) attr(self, "aria-labelledby", useId(heading));
807
747
  };
808
748
  customElements.define("ds-error-summary", DSErrorSummaryElement);
809
-
810
- // src/field/field.ts
811
- var ATTR_DESCRIBEDBY = "aria-describedby";
812
- var ATTR_INDETERMINATE = "data-indeterminate";
813
- var COUNTER_DEBOUNCE = isWindows() ? 800 : 200;
814
- var COUNTS = /* @__PURE__ */ new WeakMap();
815
- var FIELDS = /* @__PURE__ */ new Map();
816
- var VALIDATIONS = /* @__PURE__ */ new WeakMap();
817
- var WARNING_MULTIPLE_INPUTS = `Fields should only have one input element. Use <fieldset> to group multiple fields:`;
818
- var handleFieldMutations = (_doc, records = []) => {
819
- for (const { target } of records) {
820
- const isFieldset = target instanceof HTMLFieldSetElement;
821
- for (const [field] of FIELDS)
822
- if (isFieldset ? target.contains(field) : field.contains(target))
823
- handleFieldMutation(field);
824
- }
825
- };
826
- var handleFieldMutation = (field) => {
827
- const labels = [];
828
- const nextDescs = [];
829
- const prevDescs = FIELDS.get(field) || [];
830
- let input;
831
- let counter;
832
- let hasValidation = false;
833
- let invalid = false;
834
- for (const el of field.getElementsByTagName("*")) {
835
- if (el instanceof HTMLLabelElement) labels.push(el);
836
- if (el.hidden) continue;
837
- if (isInputLike(el)) {
838
- if (input) warn(WARNING_MULTIPLE_INPUTS, field);
839
- else input = el;
840
- } else {
841
- const type = el.getAttribute("data-field");
842
- if (type === "counter") counter = el;
843
- if (type === "validation") {
844
- nextDescs.unshift(useId(el));
845
- hasValidation = true;
846
- invalid = invalid || isInvalid(el);
847
- } else if (type) nextDescs.push(useId(el));
848
- }
849
- }
850
- if (!input) return;
851
- if (counter) COUNTS.set(input, counter);
852
- for (const label of labels) attr(label, "for", useId(input));
853
- const fieldsetValidation = field.closest("fieldset")?.querySelector(':scope > [data-field="validation"]');
854
- if (fieldsetValidation && !fieldsetValidation?.hidden) {
855
- hasValidation = true;
856
- invalid = invalid || isInvalid(fieldsetValidation);
857
- nextDescs.unshift(useId(fieldsetValidation));
858
- }
859
- const indeterminate = attr(input, ATTR_INDETERMINATE);
860
- if (indeterminate) input.indeterminate = indeterminate === "true";
861
- const isBoolish = input.type === "radio" || input.type === "checkbox";
862
- if (isBoolish) attr(field, "data-clickdelegatefor", useId(input));
863
- const describedby = attr(input, ATTR_DESCRIBEDBY)?.trim().split(/\s+/);
864
- const keep = describedby?.filter((id2) => !prevDescs.includes(id2)) || [];
865
- attr(input, ATTR_DESCRIBEDBY, [...nextDescs, ...keep].join(" ") || null);
866
- FIELDS.set(field, nextDescs);
867
- const hadValidation = VALIDATIONS.has(input);
868
- if (hasValidation && !hadValidation) {
869
- VALIDATIONS.set(input, attr(input, "aria-invalid"));
870
- attr(input, "aria-invalid", "true");
871
- } else if (!hasValidation && hadValidation) {
872
- attr(input, "aria-invalid", VALIDATIONS.get(input));
873
- VALIDATIONS.delete(input);
874
- }
875
- handleFieldInput(input);
876
- };
877
- var TEXTS = {
878
- over: "%d tegn for mye",
879
- under: "%d tegn igjen"
880
- };
881
- var handleFieldInput = (e) => {
882
- const input = e.target || e;
883
- const counter = COUNTS.get(input);
884
- if (counter?.isConnected) {
885
- const limit = Number(attr(counter, "data-limit")) || 0;
886
- const count = limit - input.value.length;
887
- const state = count < 0 ? "over" : "under";
888
- const label = (attrOrCSS(counter, `data-${state}`) || TEXTS[state])?.replace("%d", `${Math.abs(count)}`);
889
- attr(counter, "data-label", label);
890
- attr(counter, "data-state", state);
891
- attr(counter, "data-color", count < 0 ? "danger" : null);
892
- if (e.type === "input" && label)
893
- debouncedCounterLiveRegion(input, label);
894
- }
895
- if (input instanceof HTMLTextAreaElement) {
896
- input.style.setProperty("--_ds-field-sizing", "auto");
897
- input.style.setProperty("--_ds-field-sizing", `${input.scrollHeight}px`);
898
- }
899
- };
900
- var debouncedCounterLiveRegion = debounce((input, text) => {
901
- if (document.activeElement === input) announce(text);
749
+ //#endregion
750
+ //#region src/field/field.ts
751
+ const ATTR_INVALID = "aria-invalid";
752
+ const ATTR_DESCRIBEDBY = "aria-describedby";
753
+ const ATTR_INDETERMINATE = "data-indeterminate";
754
+ const COUNTER_DEBOUNCE = isWindows() ? 800 : 200;
755
+ const COUNTS = /* @__PURE__ */ new WeakMap();
756
+ const FIELDS = /* @__PURE__ */ new Map();
757
+ const VALIDATIONS = /* @__PURE__ */ new WeakSet();
758
+ const WARNING_MULTIPLE_INPUTS = `Fields should only have one input element. Use <fieldset> to group multiple fields:`;
759
+ const handleFieldMutations = (_doc, records = []) => {
760
+ for (const { target } of records) {
761
+ const isFieldset = target instanceof HTMLFieldSetElement;
762
+ for (const [field] of FIELDS) if (isFieldset ? target.contains(field) : field.contains(target)) handleFieldMutation(field);
763
+ }
764
+ };
765
+ const handleFieldMutation = (field) => {
766
+ const labels = [];
767
+ const nextDescs = [];
768
+ const prevDescs = FIELDS.get(field) || [];
769
+ let input;
770
+ let counter;
771
+ let hasValidation = false;
772
+ let invalid = false;
773
+ for (const el of field.getElementsByTagName("*")) {
774
+ if (el instanceof HTMLLabelElement) labels.push(el);
775
+ if (el.hidden) continue;
776
+ if (isInputLike(el)) if (input) warn(WARNING_MULTIPLE_INPUTS, field);
777
+ else input = el;
778
+ else {
779
+ const type = el.getAttribute("data-field");
780
+ if (type === "counter") counter = el;
781
+ if (type === "validation") {
782
+ nextDescs.unshift(useId(el));
783
+ hasValidation = true;
784
+ invalid = invalid || isInvalid(el);
785
+ } else if (type) nextDescs.push(useId(el));
786
+ }
787
+ }
788
+ if (!input) return;
789
+ if (counter) COUNTS.set(input, counter);
790
+ for (const label of labels) attr(label, "for", useId(input));
791
+ const fieldsetValidation = field.closest("fieldset")?.querySelector(":scope > [data-field=\"validation\"]");
792
+ if (fieldsetValidation && !fieldsetValidation?.hidden) {
793
+ hasValidation = true;
794
+ invalid = invalid || isInvalid(fieldsetValidation);
795
+ nextDescs.unshift(useId(fieldsetValidation));
796
+ }
797
+ const indeterminate = attr(input, ATTR_INDETERMINATE);
798
+ if (indeterminate) input.indeterminate = indeterminate === "true";
799
+ if (input.type === "radio" || input.type === "checkbox") attr(field, "data-clickdelegatefor", useId(input));
800
+ const keep = (attr(input, ATTR_DESCRIBEDBY)?.trim().split(/\s+/))?.filter((id) => !prevDescs.includes(id)) || [];
801
+ attr(input, ATTR_DESCRIBEDBY, [...nextDescs, ...keep].join(" ") || null);
802
+ FIELDS.set(field, nextDescs);
803
+ const hadValidation = VALIDATIONS.has(input);
804
+ if (hasValidation && !hadValidation && !input.hasAttribute(ATTR_INVALID)) {
805
+ attr(input, ATTR_INVALID, "true");
806
+ VALIDATIONS.add(input);
807
+ } else if (!hasValidation && hadValidation) {
808
+ attr(input, ATTR_INVALID, null);
809
+ VALIDATIONS.delete(input);
810
+ }
811
+ handleFieldInput(input);
812
+ };
813
+ const TEXTS = {
814
+ over: "%d tegn for mye",
815
+ under: "%d tegn igjen"
816
+ };
817
+ const handleFieldInput = (e) => {
818
+ const input = e.target || e;
819
+ const counter = COUNTS.get(input);
820
+ if (counter?.isConnected) {
821
+ const count = (Number(attr(counter, "data-limit")) || 0) - input.value.length;
822
+ const state = count < 0 ? "over" : "under";
823
+ const label = (attrOrCSS(counter, `data-${state}`) || TEXTS[state])?.replace("%d", `${Math.abs(count)}`);
824
+ attr(counter, "data-label", label);
825
+ attr(counter, "data-state", state);
826
+ attr(counter, "data-color", count < 0 ? "danger" : null);
827
+ if (e.type === "input" && label) debouncedCounterLiveRegion(input, label);
828
+ }
829
+ if (input instanceof HTMLTextAreaElement) {
830
+ input.style.setProperty("--_ds-field-sizing", "auto");
831
+ input.style.setProperty("--_ds-field-sizing", `${input.scrollHeight}px`);
832
+ }
833
+ };
834
+ const debouncedCounterLiveRegion = debounce((input, text) => {
835
+ if (document.activeElement === input) announce(text);
902
836
  }, COUNTER_DEBOUNCE);
903
- var isInvalid = (el) => el.getAttribute("data-color") !== "success";
904
- var isInputLike = (el) => el instanceof HTMLElement && "validity" in el && // Adds support for custom elements implemeted with attachInternals()
905
- !(el instanceof HTMLButtonElement) && // But skip <button> elements
906
- el.type !== "hidden";
837
+ const isInvalid = (el) => el.getAttribute("data-color") !== "success";
838
+ const isInputLike = (el) => el instanceof HTMLElement && "validity" in el && !(el instanceof HTMLButtonElement) && el.type !== "hidden";
907
839
  var DSFieldElement = class extends DSElement {
908
- connectedCallback() {
909
- FIELDS.set(this, []);
910
- handleFieldMutation(this);
911
- }
912
- disconnectedCallback() {
913
- FIELDS.delete(this);
914
- }
840
+ connectedCallback() {
841
+ FIELDS.set(this, []);
842
+ handleFieldMutation(this);
843
+ }
844
+ disconnectedCallback() {
845
+ FIELDS.delete(this);
846
+ }
915
847
  };
916
848
  customElements.define("ds-field", DSFieldElement);
917
- onHotReload("field", () => [
918
- on(document, "input", handleFieldInput, QUICK_EVENT),
919
- onMutation(document, handleFieldMutations, {
920
- attributeFilter: [
921
- "data-field",
922
- "data-limit",
923
- "hidden",
924
- // Needed to check validation visibility
925
- "id",
926
- // Needed to sync label "for" when ID of input/selec/textarea changes
927
- "value",
928
- // Needed to detect changes in controlled React inputs as they do not trigger input events
929
- ATTR_INDETERMINATE
930
- ],
931
- attributes: true,
932
- childList: true,
933
- subtree: true
934
- })
935
- ]);
936
-
937
- // src/pagination/pagination.ts
938
- var ATTR_LABEL2 = "aria-label";
939
- var ATTR_CURRENT = "data-current";
940
- var ATTR_TOTAL = "data-total";
941
- var ATTR_HREF = "data-href";
942
- var pagination = ({ current = 1, total = 10, show = 7 }) => ({
943
- prev: current > 1 ? current - 1 : 0,
944
- next: current < total ? current + 1 : 0,
945
- pages: getSteps(current, total, show).map((page, index) => ({
946
- current: page === current && "page",
947
- key: `key-${page}-${index}`,
948
- page
949
- }))
849
+ onHotReload("field", () => [on(document, "input", handleFieldInput, QUICK_EVENT), onMutation(document, handleFieldMutations, {
850
+ attributeFilter: [
851
+ "data-field",
852
+ "data-limit",
853
+ "hidden",
854
+ "id",
855
+ "value",
856
+ ATTR_INDETERMINATE
857
+ ],
858
+ attributes: true,
859
+ childList: true,
860
+ subtree: true
861
+ })]);
862
+ //#endregion
863
+ //#region src/pagination/pagination.ts
864
+ const ATTR_LABEL = "aria-label";
865
+ const ATTR_CURRENT = "data-current";
866
+ const ATTR_TOTAL = "data-total";
867
+ const ATTR_HREF = "data-href";
868
+ const pagination = ({ current = 1, total = 10, show = 7 }) => ({
869
+ prev: current > 1 ? current - 1 : 0,
870
+ next: current < total ? current + 1 : 0,
871
+ pages: getSteps(current, total, show).map((page, index) => ({
872
+ current: page === current && "page",
873
+ key: `key-${page}-${index}`,
874
+ page
875
+ }))
950
876
  });
951
877
  var DSPaginationElement = class extends DSElement {
952
- _unmutate;
953
- // Using underscore instead of private fields for backwards compatibility
954
- _render;
955
- static get observedAttributes() {
956
- return [ATTR_LABEL2, ATTR_CURRENT, ATTR_TOTAL, ATTR_HREF];
957
- }
958
- connectedCallback() {
959
- const total = attr(this, ATTR_TOTAL);
960
- const current = attr(this, ATTR_CURRENT);
961
- if (current && !total) warn(`Missing ${ATTR_TOTAL} attribute on:`, this);
962
- if (total && !current) warn(`Missing ${ATTR_CURRENT} attribute on:`, this);
963
- attr(this, ATTR_LABEL2, attrOrCSS(this, ATTR_LABEL2));
964
- attr(this, "role", "navigation");
965
- this._unmutate = onMutation(this, render3, {
966
- childList: true,
967
- subtree: true
968
- });
969
- }
970
- attributeChangedCallback() {
971
- if (this._unmutate) render3(this);
972
- }
973
- disconnectedCallback() {
974
- this._unmutate?.();
975
- this._unmutate = this._render = void 0;
976
- }
977
- };
978
- var render3 = (self) => {
979
- const current = Number(attr(self, ATTR_CURRENT));
980
- const total = Number(attr(self, ATTR_TOTAL));
981
- if (current && total) {
982
- const items = self.querySelectorAll("button,a");
983
- const show = items.length - 2;
984
- const href = attr(self, ATTR_HREF);
985
- const { next, prev, pages } = pagination({ current, total, show });
986
- items.forEach((item, i) => {
987
- const page = i ? items[i + 1] ? pages[i - 1]?.page : next : prev;
988
- attr(item, "aria-current", pages[i - 1]?.current ? "true" : null);
989
- attr(item, "aria-label", `${page ?? "hidden"}`);
990
- attr(item, "role", page ? null : "none");
991
- attr(item, "tabindex", page ? null : "-1");
992
- if (item instanceof HTMLButtonElement) attr(item, "value", `${page}`);
993
- if (href && item instanceof HTMLAnchorElement)
994
- attr(item, "href", href.replace("%d", `${page}`));
995
- });
996
- }
997
- };
998
- var getSteps = (now, max, show = Number.POSITIVE_INFINITY) => {
999
- const offset2 = (show - 1) / 2;
1000
- const start = Math.max(Math.min(now - Math.floor(offset2), max - show + 1), 1);
1001
- const end = Math.min(Math.max(now + Math.ceil(offset2), show), max);
1002
- const pages = Array.from({ length: end + 1 - start }, (_, i) => i + start);
1003
- if (show > 4 && start > 1) pages.splice(0, 2, 1, 0);
1004
- if (show > 3 && end < max) pages.splice(-2, 2, 0, max);
1005
- return pages;
878
+ _unmutate;
879
+ _render;
880
+ static get observedAttributes() {
881
+ return [
882
+ ATTR_LABEL,
883
+ ATTR_CURRENT,
884
+ ATTR_TOTAL,
885
+ ATTR_HREF
886
+ ];
887
+ }
888
+ connectedCallback() {
889
+ const total = attr(this, ATTR_TOTAL);
890
+ const current = attr(this, ATTR_CURRENT);
891
+ if (current && !total) warn(`Missing ${ATTR_TOTAL} attribute on:`, this);
892
+ if (total && !current) warn(`Missing ${ATTR_CURRENT} attribute on:`, this);
893
+ attr(this, ATTR_LABEL, attrOrCSS(this, ATTR_LABEL));
894
+ attr(this, "role", "navigation");
895
+ this._unmutate = onMutation(this, render$1, {
896
+ childList: true,
897
+ subtree: true
898
+ });
899
+ }
900
+ attributeChangedCallback() {
901
+ if (this._unmutate) render$1(this);
902
+ }
903
+ disconnectedCallback() {
904
+ this._unmutate?.();
905
+ this._unmutate = this._render = void 0;
906
+ }
907
+ };
908
+ const render$1 = (self) => {
909
+ const current = Number(attr(self, ATTR_CURRENT));
910
+ const total = Number(attr(self, ATTR_TOTAL));
911
+ if (current && total) {
912
+ const items = self.querySelectorAll("button,a");
913
+ const show = items.length - 2;
914
+ const href = attr(self, ATTR_HREF);
915
+ const { next, prev, pages } = pagination({
916
+ current,
917
+ total,
918
+ show
919
+ });
920
+ items.forEach((item, i) => {
921
+ const page = i ? items[i + 1] ? pages[i - 1]?.page : next : prev;
922
+ attr(item, "aria-current", pages[i - 1]?.current ? "true" : null);
923
+ attr(item, "aria-label", `${page ?? "hidden"}`);
924
+ attr(item, "role", page ? null : "none");
925
+ attr(item, "tabindex", page ? null : "-1");
926
+ if (item instanceof HTMLButtonElement) attr(item, "value", `${page}`);
927
+ if (href && item instanceof HTMLAnchorElement) attr(item, "href", href.replace("%d", `${page}`));
928
+ });
929
+ }
930
+ };
931
+ const getSteps = (now, max, show = Number.POSITIVE_INFINITY) => {
932
+ const offset = (show - 1) / 2;
933
+ const start = Math.max(Math.min(now - Math.floor(offset), max - show + 1), 1);
934
+ const end = Math.min(Math.max(now + Math.ceil(offset), show), max);
935
+ const pages = Array.from({ length: end + 1 - start }, (_, i) => i + start);
936
+ if (show > 4 && start > 1) pages.splice(0, 2, 1, 0);
937
+ if (show > 3 && end < max) pages.splice(-2, 2, 0, max);
938
+ return pages;
1006
939
  };
1007
940
  customElements.define("ds-pagination", DSPaginationElement);
1008
-
1009
- // src/suggestion/suggestion.ts
1010
- import { UHTMLComboboxElement } from "@u-elements/u-combobox";
941
+ //#endregion
942
+ //#region src/suggestion/suggestion.ts
1011
943
  var DSSuggestionElement = class extends UHTMLComboboxElement {
1012
- _render;
1013
- _unmutate;
1014
- // Using underscore instead of private fields for backwards compatibility
1015
- connectedCallback() {
1016
- super.connectedCallback();
1017
- this._unmutate = onMutation(this, render4, { childList: true });
1018
- on(this, "toggle", polyfillToggleSource, QUICK_EVENT);
1019
- }
1020
- disconnectedCallback() {
1021
- super.disconnectedCallback();
1022
- this._unmutate?.();
1023
- this._unmutate = this._render = void 0;
1024
- off(this, "toggle", polyfillToggleSource, QUICK_EVENT);
1025
- }
1026
- };
1027
- var render4 = ({ control, list }) => {
1028
- if (control && !control.placeholder) attr(control, "placeholder", " ");
1029
- if (control) attr(control, "popovertarget", useId(list) || null);
1030
- if (list) attr(list, "popover", "manual");
1031
- };
1032
- var polyfillToggleSource = (event) => {
1033
- const self = event.currentTarget;
1034
- const detail = event.newState === "open" && self.control;
1035
- if (detail)
1036
- self.list?.dispatchEvent(new CustomEvent("ds-toggle-source", { detail }));
944
+ _unmutate;
945
+ connectedCallback() {
946
+ super.connectedCallback();
947
+ this._unmutate = onMutation(this, render, { childList: true });
948
+ on(this, "toggle", polyfillToggleSource, QUICK_EVENT);
949
+ }
950
+ disconnectedCallback() {
951
+ super.disconnectedCallback();
952
+ this._unmutate?.();
953
+ this._unmutate = void 0;
954
+ off(this, "toggle", polyfillToggleSource, QUICK_EVENT);
955
+ }
956
+ };
957
+ const render = ({ control, list }) => {
958
+ if (control && !control.placeholder) attr(control, "placeholder", " ");
959
+ if (control) attr(control, "popovertarget", useId(list) || null);
960
+ if (list) attr(list, "popover", "manual");
961
+ if (list) attr(list, "data-is-floating", "true");
962
+ };
963
+ const polyfillToggleSource = (event) => {
964
+ const self = event.currentTarget;
965
+ const detail = event.newState === "open" && self.control;
966
+ if (detail) self.list?.dispatchEvent(new CustomEvent("ds-toggle-source", { detail }));
1037
967
  };
1038
968
  customElements.define("ds-suggestion", DSSuggestionElement);
1039
-
1040
- // src/tabs/tabs.ts
1041
- import * as UTabs from "@u-elements/u-tabs";
1042
- var DSTabsElement = class extends UTabs.UHTMLTabsElement {
1043
- };
1044
- var DSTabListElement = class extends UTabs.UHTMLTabListElement {
1045
- };
1046
- var DSTabElement = class extends UTabs.UHTMLTabElement {
1047
- };
1048
- var DSTabPanelElement = class extends UTabs.UHTMLTabPanelElement {
1049
- };
969
+ //#endregion
970
+ //#region src/tabs/tabs.ts
971
+ var DSTabsElement = class extends UTabs.UHTMLTabsElement {};
972
+ var DSTabListElement = class extends UTabs.UHTMLTabListElement {};
973
+ var DSTabElement = class extends UTabs.UHTMLTabElement {};
974
+ var DSTabPanelElement = class extends UTabs.UHTMLTabPanelElement {};
1050
975
  customElements.define("ds-tabs", DSTabsElement);
1051
976
  customElements.define("ds-tablist", DSTabListElement);
1052
977
  customElements.define("ds-tab", DSTabElement);
1053
978
  customElements.define("ds-tabpanel", DSTabPanelElement);
1054
-
1055
- // src/index.ts
979
+ //#endregion
980
+ //#region src/index.ts
1056
981
  if (isBrowser() && !isSupported()) apply();
1057
- export {
1058
- DSBreadcrumbsElement,
1059
- DSErrorSummaryElement,
1060
- DSFieldElement,
1061
- DSPaginationElement,
1062
- DSSuggestionElement,
1063
- DSTabElement,
1064
- DSTabListElement,
1065
- DSTabPanelElement,
1066
- DSTabsElement,
1067
- pagination,
1068
- setTooltipElement
1069
- };
982
+ //#endregion
983
+ export { DSBreadcrumbsElement, DSErrorSummaryElement, DSFieldElement, DSPaginationElement, DSSuggestionElement, DSTabElement, DSTabListElement, DSTabPanelElement, DSTabsElement, pagination, setTooltipElement };
984
+
985
+ //# sourceMappingURL=index.js.map