@oscarpalmer/oui 0.17.0 → 0.18.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.
Files changed (58) hide show
  1. package/dist/css/badge.css +3 -3
  2. package/dist/css/content.css +9 -11
  3. package/dist/css/form.css +36 -22
  4. package/dist/css/message.css +4 -0
  5. package/dist/css/reset.css +176 -56
  6. package/dist/css/tooltip.css +8 -0
  7. package/dist/js/attributable.full.js +48 -0
  8. package/dist/js/attributable.js +30 -45
  9. package/dist/js/floatable.full.js +413 -0
  10. package/dist/js/floatable.js +254 -169
  11. package/dist/js/focus-trap/embedded.full.js +351 -0
  12. package/dist/js/focus-trap/embedded.js +131 -131
  13. package/dist/js/focus-trap/standalone.full.js +402 -0
  14. package/dist/js/focus-trap/standalone.js +4 -4
  15. package/dist/js/index.js +4 -5
  16. package/dist/js/oui.full.js +1596 -0
  17. package/dist/js/popover.full.js +822 -0
  18. package/dist/js/popover.js +83 -162
  19. package/dist/js/splitter.full.js +605 -0
  20. package/dist/js/splitter.js +263 -245
  21. package/dist/js/tooltip.full.js +679 -0
  22. package/dist/js/tooltip.js +159 -150
  23. package/package.json +31 -59
  24. package/src/css/content.scss +1 -2
  25. package/src/css/form.scss +12 -4
  26. package/src/css/message.scss +3 -0
  27. package/src/css/mixins.scss +7 -0
  28. package/src/css/reset.scss +180 -54
  29. package/src/css/tooltip.scss +8 -0
  30. package/src/js/floatable.ts +380 -133
  31. package/src/js/focus-trap/embedded.ts +149 -64
  32. package/src/js/focus-trap/standalone.ts +4 -4
  33. package/src/js/index.ts +1 -0
  34. package/src/js/popover.ts +50 -120
  35. package/src/js/splitter.ts +250 -103
  36. package/src/js/tooltip.ts +155 -68
  37. package/types/floatable.d.ts +17 -10
  38. package/types/focus-trap/embedded.d.ts +18 -5
  39. package/types/index.d.ts +1 -0
  40. package/types/popover.d.ts +2 -0
  41. package/types/splitter.d.ts +13 -1
  42. package/types/tooltip.d.ts +2 -1
  43. package/dist/js/attributable.cjs +0 -51
  44. package/dist/js/floatable.cjs +0 -176
  45. package/dist/js/focus-trap/embedded.cjs +0 -150
  46. package/dist/js/focus-trap/standalone.cjs +0 -8
  47. package/dist/js/index.cjs +0 -6
  48. package/dist/js/popover.cjs +0 -166
  49. package/dist/js/splitter.cjs +0 -272
  50. package/dist/js/tooltip.cjs +0 -166
  51. package/types/attributable.d.cts +0 -5
  52. package/types/floatable.d.cts +0 -20
  53. package/types/focus-trap/embedded.d.cts +0 -15
  54. package/types/focus-trap/standalone.d.cts +0 -3
  55. package/types/index.d.cts +0 -14
  56. package/types/popover.d.cts +0 -14
  57. package/types/splitter.d.cts +0 -10
  58. package/types/tooltip.d.cts +0 -3
@@ -0,0 +1,822 @@
1
+ function isNumber(value) {
2
+ return typeof value === "number" && !Number.isNaN(value);
3
+ }
4
+
5
+ function getString(value) {
6
+ if (typeof value === "string") return value;
7
+ if (value == null) return "";
8
+ if (typeof value !== "object") return String(value);
9
+ const asString = String(value.valueOf?.() ?? value);
10
+ return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
11
+ }
12
+
13
+ function isNullableOrWhitespace(value) {
14
+ return value == null || EXPRESSION_WHITESPACE.test(getString(value));
15
+ }
16
+ var EXPRESSION_WHITESPACE = /^\s*$/;
17
+
18
+ function getFocusable(parent) {
19
+ return getValidElements(parent, FILTERS_FOCUSABLE);
20
+ }
21
+ function getItem(element, tabbable) {
22
+ return {
23
+ element,
24
+ tabIndex: TABINDEX_DEFAULT
25
+ };
26
+ }
27
+ function getValidElements(parent, filters, tabbable) {
28
+ if (!(parent instanceof Element)) return [];
29
+ const elements = [...parent.querySelectorAll(SELECTOR_FULL)];
30
+ const items = [];
31
+ let { length } = elements;
32
+ for (let index = 0; index < length; index += 1) {
33
+ const item = getItem(elements[index]);
34
+ if (!filters.some((filter) => filter(item))) items.push(item);
35
+ }
36
+ return items.map((item) => item.element);
37
+ }
38
+ function isDisabled$1(item) {
39
+ if (EXPRESSION_DISABLEABLE.test(item.element.tagName) && isDisabledFromFieldset(item.element)) return true;
40
+ return item.element.disabled ?? false;
41
+ }
42
+ function isDisabledFromFieldset(element) {
43
+ let parent = element.parentElement;
44
+ while (parent != null) {
45
+ if (parent instanceof HTMLFieldSetElement && parent.disabled) {
46
+ const children = Array.from(parent.children);
47
+ const { length } = children;
48
+ for (let index = 0; index < length; index += 1) {
49
+ const child = children[index];
50
+ if (child instanceof HTMLLegendElement) return parent.matches(SELECTOR_FIELDSET_DISABLED) || !child.contains(element);
51
+ }
52
+ return true;
53
+ }
54
+ parent = parent.parentElement;
55
+ }
56
+ return false;
57
+ }
58
+ function isHidden(item) {
59
+ if ((item.element.hidden ?? false) || item.element instanceof HTMLInputElement && item.element.type === STYLE_HIDDEN) return true;
60
+ if ((item.element.matches(SELECTOR_SUMMARY_FIRST) ? item.element.parentElement : item.element)?.matches(SELECTOR_DETAILS_CLOSED_CHILDREN) ?? false) return true;
61
+ const style = getComputedStyle(item.element);
62
+ if (style.display === STYLE_NONE || style.visibility === STYLE_HIDDEN) return true;
63
+ const { height, width } = item.element.getBoundingClientRect();
64
+ return height === 0 && width === 0;
65
+ }
66
+ function isInert(item) {
67
+ return (item.element.inert ?? false) || EXPRESSION_TRUEISH.test(item.element.getAttribute(ATTRIBUTE_INERT)) || item.element.parentElement != null && isInert({
68
+ element: item.element.parentElement});
69
+ }
70
+ function isSummarised(item) {
71
+ return item.element instanceof HTMLDetailsElement && [...item.element.children].some((child) => EXPRESSION_SUMMARY.test(child.tagName));
72
+ }
73
+ var ATTRIBUTE_INERT = "inert";
74
+ var EXPRESSION_DISABLEABLE = /^(button|input|select|textarea)$/i;
75
+ var EXPRESSION_SUMMARY = /^summary$/i;
76
+ var EXPRESSION_TRUEISH = /^(|true)$/i;
77
+ var FILTERS_FOCUSABLE = [
78
+ isDisabled$1,
79
+ isInert,
80
+ isHidden,
81
+ isSummarised
82
+ ];
83
+ var SELECTOR_DETAILS_CLOSED_CHILDREN = "details:not([open]) *";
84
+ var SELECTOR_FIELDSET_DISABLED = "fieldset[disabled] *";
85
+ var SELECTOR_SUMMARY_FIRST = "details > summary:first-of-type";
86
+ var SELECTOR_FULL = [
87
+ "[contenteditable]:not([contenteditable=\"false\"])",
88
+ "[tabindex]:not(slot)",
89
+ "a[href]",
90
+ "audio[controls]",
91
+ "button",
92
+ "details",
93
+ SELECTOR_SUMMARY_FIRST,
94
+ "input",
95
+ "select",
96
+ "textarea",
97
+ "video[controls]"
98
+ ].map((selector) => `${selector}:not([inert])`).join(",");
99
+ var STYLE_HIDDEN = "hidden";
100
+ var STYLE_NONE = "none";
101
+ var TABINDEX_DEFAULT = -1;
102
+
103
+ function isEventTarget(value) {
104
+ return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
105
+ }
106
+
107
+ function getBoolean(value, defaultValue) {
108
+ return typeof value === "boolean" ? value : defaultValue ?? false;
109
+ }
110
+
111
+ function clamp(value, minimum, maximum, loop) {
112
+ if (![
113
+ value,
114
+ minimum,
115
+ maximum
116
+ ].every(isNumber)) return NaN;
117
+ if (value < minimum) return maximum ;
118
+ return value > maximum ? minimum : value;
119
+ }
120
+
121
+ function calculate() {
122
+ return new Promise((resolve) => {
123
+ const values = [];
124
+ let last;
125
+ function step(now) {
126
+ if (last != null) values.push(now - last);
127
+ last = now;
128
+ if (values.length >= CALCULATION_TOTAL) {
129
+ const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - CALCULATION_TRIM);
130
+ resolve(median);
131
+ } else requestAnimationFrame(step);
132
+ }
133
+ requestAnimationFrame(step);
134
+ });
135
+ }
136
+ function noop() {}
137
+ var CALCULATION_TOTAL = 10;
138
+ var CALCULATION_TRIM = 4;
139
+ calculate().then((value) => {
140
+ });
141
+
142
+ function createEventOptions(options) {
143
+ return {
144
+ capture: getBoolean(options?.capture),
145
+ once: getBoolean(options?.once),
146
+ passive: getBoolean(options?.passive, true),
147
+ signal: options?.signal
148
+ };
149
+ }
150
+ function on(target, type, listener, options) {
151
+ if (!isEventTarget(target) || typeof type !== "string" || typeof listener !== "function") return noop;
152
+ const extended = createEventOptions(options);
153
+ target.addEventListener(type, listener, extended);
154
+ return () => {
155
+ target.removeEventListener(type, listener, extended);
156
+ };
157
+ }
158
+
159
+ function findAncestor(origin, selector) {
160
+ if (!(origin instanceof Element) || selector == null) return null;
161
+ if (typeof selector === "string") {
162
+ if (origin.matches?.(selector)) return origin;
163
+ return origin.closest(selector);
164
+ }
165
+ if (typeof selector !== "function") return null;
166
+ if (selector(origin)) return origin;
167
+ let parent = origin.parentElement;
168
+ while (parent != null && !selector(parent)) {
169
+ if (parent === document.body) return null;
170
+ parent = parent.parentElement;
171
+ }
172
+ return parent;
173
+ }
174
+
175
+ class Floatable {
176
+ options;
177
+ active = false;
178
+ disabled = false;
179
+ frame;
180
+ ignoreFocus = false;
181
+ constructor(options) {
182
+ this.options = options;
183
+ initialize(this);
184
+ }
185
+ destroy() {
186
+ stop(this);
187
+ const state = getState();
188
+ state?.active.delete(this);
189
+ state?.mapped.delete(this.options.anchor);
190
+ state?.mapped.delete(this.options.content);
191
+ this.options.anchor = undefined;
192
+ this.options.content = undefined;
193
+ }
194
+ disable() {
195
+ if (this.disabled) {
196
+ return;
197
+ }
198
+ this.disabled = true;
199
+ stop(this);
200
+ getState()?.active.delete(this);
201
+ }
202
+ enable() {
203
+ if (!this.disabled) {
204
+ return;
205
+ }
206
+ this.disabled = false;
207
+ initialize(this);
208
+ }
209
+ toggle(active, ignoreFocus) {
210
+ if (this.disabled || this.active === active) {
211
+ return;
212
+ }
213
+ const state = getState();
214
+ if (state == null) {
215
+ return;
216
+ }
217
+ this.ignoreFocus = !active && (ignoreFocus ?? false);
218
+ stop(this);
219
+ if (active) {
220
+ state.active.add(this);
221
+ }
222
+ else {
223
+ state.active.delete(this);
224
+ }
225
+ if (this.options.interactive) {
226
+ if (active) {
227
+ state.order.push(this);
228
+ }
229
+ else {
230
+ const index = state.order.indexOf(this);
231
+ if (index > -1) {
232
+ state.order.splice(index, 1);
233
+ }
234
+ }
235
+ }
236
+ if (active) {
237
+ activate(this);
238
+ }
239
+ else {
240
+ deactivate(this);
241
+ }
242
+ }
243
+ }
244
+ //
245
+ function activate(floatable) {
246
+ floatable.active = true;
247
+ document.body.append(floatable.options.content);
248
+ floatable.options.content.hidden = false;
249
+ updatePosition(floatable);
250
+ }
251
+ function closeAbove(element) {
252
+ const state = getState();
253
+ if (state == null) {
254
+ return;
255
+ }
256
+ const index = state.order.findIndex(floatable => floatable.options.anchor === element ||
257
+ floatable.options.content === element);
258
+ if (index === -1 || index === state.order.length - 1) {
259
+ return;
260
+ }
261
+ while (index < state.order.length - 1) {
262
+ void state.order.pop()?.toggle(false, true);
263
+ }
264
+ }
265
+ function closeAll() {
266
+ const state = getState();
267
+ const floatables = [...(state?.active ?? [])];
268
+ state?.active.clear();
269
+ for (const floatable of floatables) {
270
+ void floatable.toggle(false, true);
271
+ }
272
+ }
273
+ function closeNonInteractive(state) {
274
+ for (const floatable of state.active) {
275
+ if (!floatable.options.interactive) {
276
+ void floatable.toggle(false);
277
+ }
278
+ }
279
+ }
280
+ function deactivate(floatable) {
281
+ floatable.active = false;
282
+ floatable.options.content.hidden = true;
283
+ floatable.options.anchor.insertAdjacentElement(INSERT_AFTEREND, floatable.options.content);
284
+ floatable.options.onAfter?.(false);
285
+ }
286
+ function getState() {
287
+ return window._oscarpalmer_oui_floatable;
288
+ }
289
+ function getX(anchor, content, position) {
290
+ if (position.endsWith(POSITION_SUFFIX_END)) {
291
+ const end = anchor.right - content.width;
292
+ return end < 0 ? 0 : end;
293
+ }
294
+ if (position.endsWith(POSITION_SUFFIX_START)) {
295
+ const start = anchor.left;
296
+ return start + content.width > window.innerWidth
297
+ ? window.innerWidth - content.width
298
+ : start;
299
+ }
300
+ if (position.startsWith(POSITION_END)) {
301
+ return anchor.right;
302
+ }
303
+ if (position.startsWith(POSITION_START)) {
304
+ return anchor.left - content.width;
305
+ }
306
+ if (position.startsWith(POSITION_HORIZONTAL)) {
307
+ if (anchor.right + content.width <= window.innerWidth) {
308
+ return anchor.right;
309
+ }
310
+ return anchor.left - content.width;
311
+ }
312
+ const left = anchor.left + (anchor.width - content.width) / 2;
313
+ return left < 0 ? 0 : left;
314
+ }
315
+ function getY(anchor, content, position, preferAbove) {
316
+ if (position.startsWith(POSITION_ABOVE)) {
317
+ return anchor.top - content.height;
318
+ }
319
+ if (position.startsWith(POSITION_BELOW)) {
320
+ return anchor.bottom;
321
+ }
322
+ if (position.endsWith(POSITION_BOTTOM)) {
323
+ const bottom = anchor.bottom - content.height;
324
+ return bottom < 0 ? 0 : bottom;
325
+ }
326
+ if (position.endsWith(POSITION_TOP)) {
327
+ const top = anchor.top;
328
+ return top + content.height > window.innerHeight
329
+ ? window.innerHeight - content.height
330
+ : top;
331
+ }
332
+ if (position.startsWith(POSITION_VERTICAL)) {
333
+ if (preferAbove && anchor.top - content.height >= 0) {
334
+ return anchor.top - content.height;
335
+ }
336
+ if (anchor.bottom + content.height <= document.body.clientHeight) {
337
+ return anchor.bottom;
338
+ }
339
+ return anchor.top - content.height;
340
+ }
341
+ return anchor.top + (anchor.height - content.height) / 2;
342
+ }
343
+ function initialize(floatable) {
344
+ const { anchor, content, interactive } = floatable.options;
345
+ if (interactive) {
346
+ anchor.setAttribute(ATTRIBUTE_ANCHOR, '');
347
+ content.setAttribute(ATTRIBUTE_CONTENT, '');
348
+ }
349
+ content.style.position = 'fixed';
350
+ content.style.inset = '0 auto auto 0';
351
+ content.style.zIndex = '10';
352
+ const state = getState();
353
+ state?.mapped.set(anchor, floatable);
354
+ state?.mapped.set(content, floatable);
355
+ }
356
+ function isDisabled(element) {
357
+ return (element.disabled ||
358
+ element.readOnly ||
359
+ element.getAttribute('aria-disabled') === 'true');
360
+ }
361
+ function onClick(event) {
362
+ const related = findAncestor(event.target, SELECTOR_ALL);
363
+ if (!(related instanceof HTMLElement)) {
364
+ closeAll();
365
+ }
366
+ else if (related.hasAttribute(ATTRIBUTE_CONTENT)) {
367
+ closeAbove(related);
368
+ }
369
+ else if (!isDisabled(related)) {
370
+ toggle(related);
371
+ }
372
+ }
373
+ function onKeyDown(event) {
374
+ if (event.key !== 'Escape') {
375
+ return;
376
+ }
377
+ const state = getState();
378
+ if (state == null || state.active.size === 0) {
379
+ return;
380
+ }
381
+ closeNonInteractive(state);
382
+ const related = findAncestor(event.target, SELECTOR_CONTENT$1);
383
+ if (!(related instanceof HTMLElement)) {
384
+ return;
385
+ }
386
+ closeAbove(related);
387
+ const floatable = state.mapped.get(related);
388
+ if (floatable != null) {
389
+ void floatable.toggle(false);
390
+ }
391
+ }
392
+ function stop(floatable) {
393
+ if (floatable.frame != null) {
394
+ cancelAnimationFrame(floatable.frame);
395
+ }
396
+ floatable.frame = undefined;
397
+ }
398
+ function toggle(anchor) {
399
+ const floatable = getState()?.mapped.get(anchor);
400
+ if (floatable == null) {
401
+ closeAll();
402
+ return;
403
+ }
404
+ const active = Boolean(floatable.active);
405
+ const content = findAncestor(floatable.options.anchor, SELECTOR_CONTENT$1);
406
+ if (content == null) {
407
+ closeAll();
408
+ }
409
+ else {
410
+ closeAbove(content);
411
+ }
412
+ void floatable.toggle(!active);
413
+ }
414
+ function updatePosition(floatable) {
415
+ let position = floatable.options.anchor
416
+ .getAttribute(floatable.options.positionAttribute)
417
+ ?.trim() ?? floatable.options.defaultPosition;
418
+ if (!POSITIONS.has(position)) {
419
+ position = floatable.options.defaultPosition;
420
+ }
421
+ let previousAnchor;
422
+ let previousContent;
423
+ let calculated = false;
424
+ let runOnAfter = false;
425
+ function run() {
426
+ const anchor = floatable.options.anchor.getBoundingClientRect();
427
+ const content = floatable.options.content.getBoundingClientRect();
428
+ if (calculated) {
429
+ if (previousAnchor != null && previousContent != null) {
430
+ if (PROPERTIES.every(property => anchor[property] === previousAnchor[property]) &&
431
+ PROPERTIES.every(property => content[property] === previousContent[property])) {
432
+ floatable.frame = requestAnimationFrame(run);
433
+ return;
434
+ }
435
+ }
436
+ }
437
+ else {
438
+ calculated = true;
439
+ }
440
+ update(anchor, content);
441
+ }
442
+ function update(anchor, content) {
443
+ previousAnchor = anchor;
444
+ previousContent = content;
445
+ const left = getX(anchor, content, position);
446
+ const top = getY(anchor, content, position, floatable.options.preferAbove);
447
+ if (top + content.height > window.innerHeight) {
448
+ floatable.options.content.style.height = `${window.innerHeight - top}px`;
449
+ }
450
+ else {
451
+ floatable.options.content.style.height = '';
452
+ }
453
+ if (left + content.width > window.innerWidth) {
454
+ floatable.options.content.style.width = `${window.innerWidth - left}px`;
455
+ }
456
+ else {
457
+ floatable.options.content.style.width = '';
458
+ }
459
+ // floatable.options.content.style.inset = `${top}px auto auto ${left}px`;
460
+ floatable.options.content.style.transform = `translate3d(${left}px, ${top}px, 0)`;
461
+ floatable.frame = requestAnimationFrame(run);
462
+ if (!runOnAfter) {
463
+ runOnAfter = true;
464
+ floatable.options.onAfter?.(true);
465
+ }
466
+ }
467
+ floatable.frame = requestAnimationFrame(run);
468
+ }
469
+ //
470
+ const ATTRIBUTE_ANCHOR = 'oui-floatable-anchor';
471
+ const ATTRIBUTE_CONTENT = 'oui-floatable-content';
472
+ const INSERT_AFTEREND = 'afterend';
473
+ const POSITION_ABOVE = 'above';
474
+ const POSITION_BELOW = 'below';
475
+ const POSITION_BOTTOM = 'bottom';
476
+ const POSITION_END = 'end';
477
+ const POSITION_HORIZONTAL = 'horizontal';
478
+ const POSITION_START = 'start';
479
+ const POSITION_SUFFIX_END = '-end';
480
+ const POSITION_SUFFIX_START = '-start';
481
+ const POSITION_TOP = 'top';
482
+ const POSITION_VERTICAL = 'vertical';
483
+ const POSITIONS = new Set([
484
+ 'above',
485
+ 'above-end',
486
+ 'above-start',
487
+ 'below',
488
+ 'below-end',
489
+ 'below-start',
490
+ 'end',
491
+ 'end-bottom',
492
+ 'end-top',
493
+ 'horizontal',
494
+ 'horizontal-bottom',
495
+ 'horizontal-top',
496
+ 'start',
497
+ 'start-bottom',
498
+ 'start-top',
499
+ 'vertical',
500
+ 'vertical-end',
501
+ 'vertical-start',
502
+ ]);
503
+ const PROPERTIES = ['bottom', 'left', 'right', 'top'];
504
+ const SELECTOR_ALL = `[${ATTRIBUTE_ANCHOR}], [${ATTRIBUTE_CONTENT}]`;
505
+ const SELECTOR_CONTENT$1 = `[${ATTRIBUTE_CONTENT}]`;
506
+ //
507
+ if (window._oscarpalmer_oui_floatable == null) {
508
+ Object.defineProperty(window, '_oscarpalmer_oui_floatable', {
509
+ enumerable: false,
510
+ value: {
511
+ active: new Set(),
512
+ mapped: new Map(),
513
+ order: [],
514
+ },
515
+ writable: false,
516
+ });
517
+ on(document, 'keydown', event => {
518
+ onKeyDown(event);
519
+ });
520
+ on(document, 'click', event => {
521
+ onClick(event);
522
+ });
523
+ }
524
+
525
+ class FocusTrap {
526
+ disabled = false;
527
+ connected = true;
528
+ element;
529
+ embedded;
530
+ get contain() {
531
+ return this.element.hasAttribute(FOCUS_TRAP_CONTAIN);
532
+ }
533
+ get noescape() {
534
+ return this.element.hasAttribute(FOCUS_TRAP_NOESCAPE);
535
+ }
536
+ constructor(element, options) {
537
+ this.element = element;
538
+ this.embedded = options != null;
539
+ element.setAttribute('tabindex', '-1');
540
+ if (options != null) {
541
+ element.setAttribute(FOCUS_TRAP_SELECTOR, '');
542
+ }
543
+ if (options?.noescape) {
544
+ element.setAttribute(FOCUS_TRAP_NOESCAPE, '');
545
+ }
546
+ }
547
+ connect() {
548
+ setConnected(this, true);
549
+ }
550
+ destroy() {
551
+ this.element = undefined;
552
+ }
553
+ disable() {
554
+ if (!(this.disabled || this.noescape)) {
555
+ setDisabled(this, true);
556
+ }
557
+ }
558
+ disconnect() {
559
+ setConnected(this, false);
560
+ }
561
+ enable() {
562
+ if (this.disabled) {
563
+ setDisabled(this, false);
564
+ }
565
+ }
566
+ focus(last) {
567
+ tabToTrap(this, getFocusable(this.element), this.element, last ? -1 : 0);
568
+ }
569
+ }
570
+ //
571
+ function createFocusTrap(element, options) {
572
+ let focusTrap = FOCUSTRAPS_ALL.get(element);
573
+ if (focusTrap == null) {
574
+ focusTrap = new FocusTrap(element, options);
575
+ FOCUSTRAPS_ALL.set(element, focusTrap);
576
+ }
577
+ return focusTrap;
578
+ }
579
+ function onEscape(event) {
580
+ let element = findAncestor(event.target, ATTRIBUTE_SELECTOR);
581
+ let focusTrap = FOCUSTRAPS_ALL.get(element);
582
+ if (element == null || focusTrap == null) {
583
+ return;
584
+ }
585
+ while (focusTrap.disabled) {
586
+ element = findAncestor(focusTrap.element.parentElement, ATTRIBUTE_SELECTOR);
587
+ focusTrap = FOCUSTRAPS_ALL.get(element);
588
+ if (element == null || focusTrap == null) {
589
+ return;
590
+ }
591
+ if (!focusTrap.disabled) {
592
+ break;
593
+ }
594
+ }
595
+ focusTrap?.disable();
596
+ }
597
+ function onFocusIn(event) {
598
+ const focusTrap = FOCUSTRAPS_ALL.get(findAncestor(event.target, ATTRIBUTE_SELECTOR));
599
+ lastTarget = focusTrap == null ? undefined : event.target;
600
+ }
601
+ function onKeydown(event) {
602
+ switch (event.key) {
603
+ case 'Escape':
604
+ onEscape(event);
605
+ break;
606
+ case 'Tab':
607
+ onTab(event);
608
+ break;
609
+ default:
610
+ return;
611
+ }
612
+ }
613
+ function onPointerdown(event) {
614
+ const lastFocusTrap = findAncestor(lastTarget, ATTRIBUTE_SELECTOR);
615
+ const nextFocusTrap = findAncestor(event.target, ATTRIBUTE_SELECTOR);
616
+ if (lastFocusTrap != null && nextFocusTrap !== lastFocusTrap) {
617
+ if (FOCUSTRAPS_ALL.get(lastFocusTrap)?.contain ?? false) {
618
+ event.preventDefault();
619
+ lastTarget?.focus();
620
+ }
621
+ }
622
+ if (FOCUSTRAPS_DISABLED.size === 0) {
623
+ return;
624
+ }
625
+ for (const focusTrap of FOCUSTRAPS_DISABLED) {
626
+ if (focusTrap.element !== nextFocusTrap) {
627
+ focusTrap.enable();
628
+ }
629
+ }
630
+ }
631
+ function onTab(event) {
632
+ const element = findAncestor(event.target, ATTRIBUTE_SELECTOR);
633
+ const focusTrap = FOCUSTRAPS_ALL.get(element);
634
+ if (focusTrap == null) {
635
+ return;
636
+ }
637
+ if (!focusTrap.disabled) {
638
+ event.preventDefault();
639
+ }
640
+ const elements = getFocusable(element);
641
+ const index = elements.indexOf(event.target);
642
+ if (index === -1 || elements.length === 0) {
643
+ tabToTrap(focusTrap, elements, element);
644
+ }
645
+ else {
646
+ tabToElement(focusTrap, elements, index, event.shiftKey ? -1 : 1);
647
+ }
648
+ }
649
+ function setConnected(focusTrap, value) {
650
+ if (!focusTrap.embedded || focusTrap.connected === value) {
651
+ return;
652
+ }
653
+ focusTrap.connected = value;
654
+ }
655
+ function setDisabled(focusTrap, value) {
656
+ focusTrap.disabled = value;
657
+ if (value) {
658
+ FOCUSTRAPS_DISABLED.add(focusTrap);
659
+ }
660
+ else {
661
+ FOCUSTRAPS_DISABLED.delete(focusTrap);
662
+ }
663
+ }
664
+ function tabToElement(focusTrap, elements, index, modifier) {
665
+ const { length } = elements;
666
+ const next = clamp(index + modifier, 0, length - 1);
667
+ if (focusTrap.disabled) {
668
+ if ((index === length - 1 && next === 0) ||
669
+ (index === 0 && next === length - 1)) {
670
+ lastTarget = undefined;
671
+ focusTrap.enable();
672
+ }
673
+ return;
674
+ }
675
+ const element = elements[next];
676
+ const elementFocusTrap = FOCUSTRAPS_ALL.get(element);
677
+ if (elementFocusTrap != null) {
678
+ elementFocusTrap.focus(modifier === -1);
679
+ }
680
+ else {
681
+ elements[next].focus();
682
+ lastTarget = elements[next];
683
+ }
684
+ }
685
+ function tabToTrap(focusTrap, elements, element, index) {
686
+ if (focusTrap.disabled) {
687
+ lastTarget = undefined;
688
+ focusTrap.enable();
689
+ }
690
+ else {
691
+ const next = elements.length === 0 ? element : elements.at(index ?? 0);
692
+ next.focus();
693
+ lastTarget = next;
694
+ }
695
+ }
696
+ //
697
+ const FOCUS_TRAP_SELECTOR = 'oui-focus-trap';
698
+ const ATTRIBUTE_SELECTOR = `[${FOCUS_TRAP_SELECTOR}]`;
699
+ const EVENT_OPTIONS = {
700
+ capture: true,
701
+ passive: false,
702
+ };
703
+ const FOCUS_TRAP_CONTAIN = `${FOCUS_TRAP_SELECTOR}-contain`;
704
+ const FOCUS_TRAP_NOESCAPE = `${FOCUS_TRAP_SELECTOR}-noescape`;
705
+ const FOCUSTRAPS_ALL = new WeakMap();
706
+ const FOCUSTRAPS_DISABLED = new Set();
707
+ let lastTarget;
708
+ //
709
+ on(document, 'focusin', onFocusIn, EVENT_OPTIONS);
710
+ on(document, 'keydown', onKeydown, EVENT_OPTIONS);
711
+ on(document, 'mousedown', onPointerdown, EVENT_OPTIONS);
712
+ on(document, 'touchstart', onPointerdown, EVENT_OPTIONS);
713
+
714
+ let index = 0;
715
+ class OuiPopoverElement extends HTMLElement {
716
+ #floatable;
717
+ #focusTrap;
718
+ constructor() {
719
+ super();
720
+ const content = this.querySelector(SELECTOR_CONTENT);
721
+ const toggle = this.querySelector(SELECTOR_TOGGLE);
722
+ if (!(content instanceof HTMLElement)) {
723
+ throw new Error('A oui-popover must have a direct child element with the [oui-popover-content]-attribute');
724
+ }
725
+ if (!(toggle instanceof HTMLButtonElement)) {
726
+ throw new Error('A oui-popover must have a direct child element with the [oui-popover-toggle]-attribute and be a button');
727
+ }
728
+ content.hidden = true;
729
+ content.setAttribute('role', 'dialog');
730
+ content.setAttribute('aria-modal', 'true');
731
+ if (isNullableOrWhitespace(content.id)) {
732
+ content.setAttribute('id', `${ATTRIBUTE_ID_PREFIX}_${++index}`);
733
+ }
734
+ toggle.setAttribute('aria-controls', content.id);
735
+ toggle.setAttribute('aria-haspopup', 'dialog');
736
+ this.#floatable = new Floatable({
737
+ content,
738
+ anchor: toggle,
739
+ defaultPosition: 'below-start',
740
+ interactive: true,
741
+ positionAttribute: 'position',
742
+ preferAbove: false,
743
+ onAfter: (active) => {
744
+ if (active) {
745
+ focus(content);
746
+ }
747
+ else if (!this.#floatable.ignoreFocus) {
748
+ toggle.focus();
749
+ }
750
+ },
751
+ });
752
+ this.#focusTrap = createFocusTrap(content, {
753
+ noescape: true,
754
+ });
755
+ POPOVERS.set(toggle, this.#floatable);
756
+ POPOVERS.set(content, this.#floatable);
757
+ }
758
+ close() {
759
+ this.hidePopover();
760
+ }
761
+ connectedCallback() {
762
+ this.#floatable.enable();
763
+ this.#focusTrap.connect();
764
+ }
765
+ disconnectedCallback() {
766
+ this.#floatable.disable();
767
+ this.#focusTrap.disconnect();
768
+ }
769
+ hidePopover() {
770
+ if (this.#floatable.active && this.isConnected) {
771
+ this.#floatable.toggle(false);
772
+ }
773
+ }
774
+ open() {
775
+ this.showPopover();
776
+ }
777
+ showPopover() {
778
+ if (!this.#floatable.active && this.isConnected) {
779
+ this.#floatable.toggle(true);
780
+ }
781
+ }
782
+ toggle() {
783
+ this.togglePopover();
784
+ }
785
+ togglePopover(options) {
786
+ if (typeof options === 'boolean') {
787
+ if (options === true) {
788
+ this.showPopover();
789
+ }
790
+ else {
791
+ this.hidePopover();
792
+ }
793
+ return options;
794
+ }
795
+ if (this.#floatable.active) {
796
+ this.hidePopover();
797
+ return false;
798
+ }
799
+ this.showPopover();
800
+ return true;
801
+ }
802
+ }
803
+ //
804
+ function focus(content) {
805
+ let target = getFocusable(content)[0];
806
+ if (!(target instanceof HTMLElement)) {
807
+ target = content;
808
+ }
809
+ requestAnimationFrame(() => {
810
+ target.focus();
811
+ });
812
+ }
813
+ //
814
+ const ATTRIBUTE_ID_PREFIX = 'oui_popover_content_';
815
+ const POPOVERS = new Map();
816
+ const SELECTOR = 'oui-popover';
817
+ const SELECTOR_CONTENT = `:scope > [${SELECTOR}-content]`;
818
+ const SELECTOR_TOGGLE = `:scope > [${SELECTOR}-toggle]`;
819
+ //
820
+ customElements.define(SELECTOR, OuiPopoverElement);
821
+
822
+ export { OuiPopoverElement };