@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,402 @@
1
+ function attributable(attribute, add, remove) {
2
+ const observer = new MutationObserver(entries => {
3
+ const { length } = entries;
4
+ for (let index = 0; index < length; index += 1) {
5
+ const entry = entries[index];
6
+ if (entry.type === 'attributes' && entry.target instanceof HTMLElement) {
7
+ if (entry.target.hasAttribute(attribute)) {
8
+ handleAdded(attribute, [entry.target], add);
9
+ }
10
+ else {
11
+ handleRemoved([entry.target], remove);
12
+ }
13
+ }
14
+ else if (entry.type === 'childList') {
15
+ handleAdded(attribute, entry.addedNodes, add);
16
+ handleRemoved(entry.removedNodes, remove);
17
+ }
18
+ }
19
+ });
20
+ observer.observe(document, {
21
+ attributeFilter: [attribute],
22
+ attributes: true,
23
+ childList: true,
24
+ subtree: true,
25
+ });
26
+ requestAnimationFrame(() => {
27
+ handleAdded(attribute, document.querySelectorAll(`[${attribute}]`), add);
28
+ });
29
+ }
30
+ function handleAdded(attribute, nodes, add) {
31
+ const elements = [...nodes].filter(node => node instanceof HTMLElement);
32
+ const { length } = elements;
33
+ for (let index = 0; index < length; index += 1) {
34
+ const element = elements[index];
35
+ if (element.hasAttribute(attribute)) {
36
+ add(element);
37
+ }
38
+ }
39
+ }
40
+ function handleRemoved(nodes, remove) {
41
+ const elements = [...nodes].filter(node => node instanceof HTMLElement);
42
+ const { length } = elements;
43
+ for (let index = 0; index < length; index += 1) {
44
+ remove(elements[index]);
45
+ }
46
+ }
47
+
48
+ function isNumber(value) {
49
+ return typeof value === "number" && !Number.isNaN(value);
50
+ }
51
+
52
+ function clamp(value, minimum, maximum, loop) {
53
+ if (![
54
+ value,
55
+ minimum,
56
+ maximum
57
+ ].every(isNumber)) return NaN;
58
+ if (value < minimum) return maximum ;
59
+ return value > maximum ? minimum : value;
60
+ }
61
+
62
+ function isEventTarget(value) {
63
+ return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
64
+ }
65
+
66
+ function getBoolean(value, defaultValue) {
67
+ return typeof value === "boolean" ? value : defaultValue ?? false;
68
+ }
69
+
70
+ function calculate() {
71
+ return new Promise((resolve) => {
72
+ const values = [];
73
+ let last;
74
+ function step(now) {
75
+ if (last != null) values.push(now - last);
76
+ last = now;
77
+ if (values.length >= CALCULATION_TOTAL) {
78
+ const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - CALCULATION_TRIM);
79
+ resolve(median);
80
+ } else requestAnimationFrame(step);
81
+ }
82
+ requestAnimationFrame(step);
83
+ });
84
+ }
85
+ function noop() {}
86
+ var CALCULATION_TOTAL = 10;
87
+ var CALCULATION_TRIM = 4;
88
+ calculate().then((value) => {
89
+ });
90
+
91
+ function createEventOptions(options) {
92
+ return {
93
+ capture: getBoolean(options?.capture),
94
+ once: getBoolean(options?.once),
95
+ passive: getBoolean(options?.passive, true),
96
+ signal: options?.signal
97
+ };
98
+ }
99
+ function on(target, type, listener, options) {
100
+ if (!isEventTarget(target) || typeof type !== "string" || typeof listener !== "function") return noop;
101
+ const extended = createEventOptions(options);
102
+ target.addEventListener(type, listener, extended);
103
+ return () => {
104
+ target.removeEventListener(type, listener, extended);
105
+ };
106
+ }
107
+
108
+ function findAncestor(origin, selector) {
109
+ if (!(origin instanceof Element) || selector == null) return null;
110
+ if (typeof selector === "string") {
111
+ if (origin.matches?.(selector)) return origin;
112
+ return origin.closest(selector);
113
+ }
114
+ if (typeof selector !== "function") return null;
115
+ if (selector(origin)) return origin;
116
+ let parent = origin.parentElement;
117
+ while (parent != null && !selector(parent)) {
118
+ if (parent === document.body) return null;
119
+ parent = parent.parentElement;
120
+ }
121
+ return parent;
122
+ }
123
+
124
+ function getFocusable(parent) {
125
+ return getValidElements(parent, FILTERS_FOCUSABLE);
126
+ }
127
+ function getItem(element, tabbable) {
128
+ return {
129
+ element,
130
+ tabIndex: TABINDEX_DEFAULT
131
+ };
132
+ }
133
+ function getValidElements(parent, filters, tabbable) {
134
+ if (!(parent instanceof Element)) return [];
135
+ const elements = [...parent.querySelectorAll(SELECTOR_FULL)];
136
+ const items = [];
137
+ let { length } = elements;
138
+ for (let index = 0; index < length; index += 1) {
139
+ const item = getItem(elements[index]);
140
+ if (!filters.some((filter) => filter(item))) items.push(item);
141
+ }
142
+ return items.map((item) => item.element);
143
+ }
144
+ function isDisabled(item) {
145
+ if (EXPRESSION_DISABLEABLE.test(item.element.tagName) && isDisabledFromFieldset(item.element)) return true;
146
+ return item.element.disabled ?? false;
147
+ }
148
+ function isDisabledFromFieldset(element) {
149
+ let parent = element.parentElement;
150
+ while (parent != null) {
151
+ if (parent instanceof HTMLFieldSetElement && parent.disabled) {
152
+ const children = Array.from(parent.children);
153
+ const { length } = children;
154
+ for (let index = 0; index < length; index += 1) {
155
+ const child = children[index];
156
+ if (child instanceof HTMLLegendElement) return parent.matches(SELECTOR_FIELDSET_DISABLED) || !child.contains(element);
157
+ }
158
+ return true;
159
+ }
160
+ parent = parent.parentElement;
161
+ }
162
+ return false;
163
+ }
164
+ function isHidden(item) {
165
+ if ((item.element.hidden ?? false) || item.element instanceof HTMLInputElement && item.element.type === STYLE_HIDDEN) return true;
166
+ if ((item.element.matches(SELECTOR_SUMMARY_FIRST) ? item.element.parentElement : item.element)?.matches(SELECTOR_DETAILS_CLOSED_CHILDREN) ?? false) return true;
167
+ const style = getComputedStyle(item.element);
168
+ if (style.display === STYLE_NONE || style.visibility === STYLE_HIDDEN) return true;
169
+ const { height, width } = item.element.getBoundingClientRect();
170
+ return height === 0 && width === 0;
171
+ }
172
+ function isInert(item) {
173
+ return (item.element.inert ?? false) || EXPRESSION_TRUEISH.test(item.element.getAttribute(ATTRIBUTE_INERT)) || item.element.parentElement != null && isInert({
174
+ element: item.element.parentElement});
175
+ }
176
+ function isSummarised(item) {
177
+ return item.element instanceof HTMLDetailsElement && [...item.element.children].some((child) => EXPRESSION_SUMMARY.test(child.tagName));
178
+ }
179
+ var ATTRIBUTE_INERT = "inert";
180
+ var EXPRESSION_DISABLEABLE = /^(button|input|select|textarea)$/i;
181
+ var EXPRESSION_SUMMARY = /^summary$/i;
182
+ var EXPRESSION_TRUEISH = /^(|true)$/i;
183
+ var FILTERS_FOCUSABLE = [
184
+ isDisabled,
185
+ isInert,
186
+ isHidden,
187
+ isSummarised
188
+ ];
189
+ var SELECTOR_DETAILS_CLOSED_CHILDREN = "details:not([open]) *";
190
+ var SELECTOR_FIELDSET_DISABLED = "fieldset[disabled] *";
191
+ var SELECTOR_SUMMARY_FIRST = "details > summary:first-of-type";
192
+ var SELECTOR_FULL = [
193
+ "[contenteditable]:not([contenteditable=\"false\"])",
194
+ "[tabindex]:not(slot)",
195
+ "a[href]",
196
+ "audio[controls]",
197
+ "button",
198
+ "details",
199
+ SELECTOR_SUMMARY_FIRST,
200
+ "input",
201
+ "select",
202
+ "textarea",
203
+ "video[controls]"
204
+ ].map((selector) => `${selector}:not([inert])`).join(",");
205
+ var STYLE_HIDDEN = "hidden";
206
+ var STYLE_NONE = "none";
207
+ var TABINDEX_DEFAULT = -1;
208
+
209
+ class FocusTrap {
210
+ disabled = false;
211
+ connected = true;
212
+ element;
213
+ embedded;
214
+ get contain() {
215
+ return this.element.hasAttribute(FOCUS_TRAP_CONTAIN);
216
+ }
217
+ get noescape() {
218
+ return this.element.hasAttribute(FOCUS_TRAP_NOESCAPE);
219
+ }
220
+ constructor(element, options) {
221
+ this.element = element;
222
+ this.embedded = options != null;
223
+ element.setAttribute('tabindex', '-1');
224
+ if (options != null) {
225
+ element.setAttribute(FOCUS_TRAP_SELECTOR, '');
226
+ }
227
+ if (options?.noescape) {
228
+ element.setAttribute(FOCUS_TRAP_NOESCAPE, '');
229
+ }
230
+ }
231
+ connect() {
232
+ setConnected(this, true);
233
+ }
234
+ destroy() {
235
+ this.element = undefined;
236
+ }
237
+ disable() {
238
+ if (!(this.disabled || this.noescape)) {
239
+ setDisabled(this, true);
240
+ }
241
+ }
242
+ disconnect() {
243
+ setConnected(this, false);
244
+ }
245
+ enable() {
246
+ if (this.disabled) {
247
+ setDisabled(this, false);
248
+ }
249
+ }
250
+ focus(last) {
251
+ tabToTrap(this, getFocusable(this.element), this.element, last ? -1 : 0);
252
+ }
253
+ }
254
+ //
255
+ function createFocusTrap(element, options) {
256
+ let focusTrap = FOCUSTRAPS_ALL.get(element);
257
+ if (focusTrap == null) {
258
+ focusTrap = new FocusTrap(element, options);
259
+ FOCUSTRAPS_ALL.set(element, focusTrap);
260
+ }
261
+ return focusTrap;
262
+ }
263
+ function onEscape(event) {
264
+ let element = findAncestor(event.target, ATTRIBUTE_SELECTOR);
265
+ let focusTrap = FOCUSTRAPS_ALL.get(element);
266
+ if (element == null || focusTrap == null) {
267
+ return;
268
+ }
269
+ while (focusTrap.disabled) {
270
+ element = findAncestor(focusTrap.element.parentElement, ATTRIBUTE_SELECTOR);
271
+ focusTrap = FOCUSTRAPS_ALL.get(element);
272
+ if (element == null || focusTrap == null) {
273
+ return;
274
+ }
275
+ if (!focusTrap.disabled) {
276
+ break;
277
+ }
278
+ }
279
+ focusTrap?.disable();
280
+ }
281
+ function onFocusIn(event) {
282
+ const focusTrap = FOCUSTRAPS_ALL.get(findAncestor(event.target, ATTRIBUTE_SELECTOR));
283
+ lastTarget = focusTrap == null ? undefined : event.target;
284
+ }
285
+ function onKeydown(event) {
286
+ switch (event.key) {
287
+ case 'Escape':
288
+ onEscape(event);
289
+ break;
290
+ case 'Tab':
291
+ onTab(event);
292
+ break;
293
+ default:
294
+ return;
295
+ }
296
+ }
297
+ function onPointerdown(event) {
298
+ const lastFocusTrap = findAncestor(lastTarget, ATTRIBUTE_SELECTOR);
299
+ const nextFocusTrap = findAncestor(event.target, ATTRIBUTE_SELECTOR);
300
+ if (lastFocusTrap != null && nextFocusTrap !== lastFocusTrap) {
301
+ if (FOCUSTRAPS_ALL.get(lastFocusTrap)?.contain ?? false) {
302
+ event.preventDefault();
303
+ lastTarget?.focus();
304
+ }
305
+ }
306
+ if (FOCUSTRAPS_DISABLED.size === 0) {
307
+ return;
308
+ }
309
+ for (const focusTrap of FOCUSTRAPS_DISABLED) {
310
+ if (focusTrap.element !== nextFocusTrap) {
311
+ focusTrap.enable();
312
+ }
313
+ }
314
+ }
315
+ function onTab(event) {
316
+ const element = findAncestor(event.target, ATTRIBUTE_SELECTOR);
317
+ const focusTrap = FOCUSTRAPS_ALL.get(element);
318
+ if (focusTrap == null) {
319
+ return;
320
+ }
321
+ if (!focusTrap.disabled) {
322
+ event.preventDefault();
323
+ }
324
+ const elements = getFocusable(element);
325
+ const index = elements.indexOf(event.target);
326
+ if (index === -1 || elements.length === 0) {
327
+ tabToTrap(focusTrap, elements, element);
328
+ }
329
+ else {
330
+ tabToElement(focusTrap, elements, index, event.shiftKey ? -1 : 1);
331
+ }
332
+ }
333
+ function setConnected(focusTrap, value) {
334
+ if (!focusTrap.embedded || focusTrap.connected === value) {
335
+ return;
336
+ }
337
+ focusTrap.connected = value;
338
+ }
339
+ function setDisabled(focusTrap, value) {
340
+ focusTrap.disabled = value;
341
+ if (value) {
342
+ FOCUSTRAPS_DISABLED.add(focusTrap);
343
+ }
344
+ else {
345
+ FOCUSTRAPS_DISABLED.delete(focusTrap);
346
+ }
347
+ }
348
+ function tabToElement(focusTrap, elements, index, modifier) {
349
+ const { length } = elements;
350
+ const next = clamp(index + modifier, 0, length - 1);
351
+ if (focusTrap.disabled) {
352
+ if ((index === length - 1 && next === 0) ||
353
+ (index === 0 && next === length - 1)) {
354
+ lastTarget = undefined;
355
+ focusTrap.enable();
356
+ }
357
+ return;
358
+ }
359
+ const element = elements[next];
360
+ const elementFocusTrap = FOCUSTRAPS_ALL.get(element);
361
+ if (elementFocusTrap != null) {
362
+ elementFocusTrap.focus(modifier === -1);
363
+ }
364
+ else {
365
+ elements[next].focus();
366
+ lastTarget = elements[next];
367
+ }
368
+ }
369
+ function tabToTrap(focusTrap, elements, element, index) {
370
+ if (focusTrap.disabled) {
371
+ lastTarget = undefined;
372
+ focusTrap.enable();
373
+ }
374
+ else {
375
+ const next = elements.length === 0 ? element : elements.at(index ?? 0);
376
+ next.focus();
377
+ lastTarget = next;
378
+ }
379
+ }
380
+ //
381
+ const FOCUS_TRAP_SELECTOR = 'oui-focus-trap';
382
+ const ATTRIBUTE_SELECTOR = `[${FOCUS_TRAP_SELECTOR}]`;
383
+ const EVENT_OPTIONS = {
384
+ capture: true,
385
+ passive: false,
386
+ };
387
+ const FOCUS_TRAP_CONTAIN = `${FOCUS_TRAP_SELECTOR}-contain`;
388
+ const FOCUS_TRAP_NOESCAPE = `${FOCUS_TRAP_SELECTOR}-noescape`;
389
+ const FOCUSTRAPS_ALL = new WeakMap();
390
+ const FOCUSTRAPS_DISABLED = new Set();
391
+ let lastTarget;
392
+ //
393
+ on(document, 'focusin', onFocusIn, EVENT_OPTIONS);
394
+ on(document, 'keydown', onKeydown, EVENT_OPTIONS);
395
+ on(document, 'mousedown', onPointerdown, EVENT_OPTIONS);
396
+ on(document, 'touchstart', onPointerdown, EVENT_OPTIONS);
397
+
398
+ function removeFocusTraps(element) {
399
+ FOCUSTRAPS_ALL.get(element)?.destroy();
400
+ FOCUSTRAPS_ALL.delete(element);
401
+ }
402
+ attributable(FOCUS_TRAP_SELECTOR, createFocusTrap, removeFocusTraps);
@@ -1,7 +1,7 @@
1
1
  import { attributable } from "../attributable.js";
2
- import { createFocusTrap, selector, focusTraps } from "./embedded.js";
2
+ import { FOCUSTRAPS_ALL, FOCUS_TRAP_SELECTOR, createFocusTrap } from "./embedded.js";
3
3
  function removeFocusTraps(element) {
4
- focusTraps.get(element)?.destroy();
5
- focusTraps.delete(element);
4
+ FOCUSTRAPS_ALL.get(element)?.destroy();
5
+ FOCUSTRAPS_ALL.delete(element);
6
6
  }
7
- attributable(selector, createFocusTrap, removeFocusTraps);
7
+ attributable(FOCUS_TRAP_SELECTOR, createFocusTrap, removeFocusTraps);
package/dist/js/index.js CHANGED
@@ -1,6 +1,5 @@
1
- import "./focus-trap/standalone.js";
1
+ import { setDelay } from "./tooltip.js";
2
+ import { OuiSplitterElement } from "./splitter.js";
2
3
  import { OuiPopoverElement } from "./popover.js";
3
- import "./tooltip.js";
4
- export {
5
- OuiPopoverElement
6
- };
4
+ import "./focus-trap/standalone.js";
5
+ export { OuiPopoverElement, OuiSplitterElement, setDelay };