@oscarpalmer/oui 0.13.0 → 0.15.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 (38) hide show
  1. package/README.md +2 -0
  2. package/dist/css/popover.css +9 -0
  3. package/dist/css/tooltip.css +9 -3
  4. package/dist/css/value.css +1 -0
  5. package/dist/js/floatable.cjs +134 -0
  6. package/dist/js/floatable.js +134 -0
  7. package/dist/js/focus-trap/embedded.cjs +150 -0
  8. package/dist/js/focus-trap/embedded.js +150 -0
  9. package/dist/js/focus-trap/standalone.cjs +8 -0
  10. package/dist/js/focus-trap/standalone.js +7 -0
  11. package/dist/js/index.cjs +5 -1
  12. package/dist/js/index.js +6 -1
  13. package/dist/js/popover.cjs +160 -0
  14. package/dist/js/popover.js +160 -0
  15. package/dist/js/tooltip.cjs +83 -126
  16. package/dist/js/tooltip.js +83 -126
  17. package/package.json +7 -7
  18. package/src/css/popover.scss +9 -0
  19. package/src/css/tooltip.scss +7 -3
  20. package/src/js/floatable.ts +196 -0
  21. package/src/js/focus-trap/embedded.ts +215 -0
  22. package/src/js/focus-trap/standalone.ts +9 -0
  23. package/src/js/index.ts +3 -1
  24. package/src/js/popover.ts +221 -0
  25. package/src/js/tooltip.ts +97 -161
  26. package/types/floatable.d.cts +16 -0
  27. package/types/floatable.d.ts +12 -0
  28. package/types/focus-trap/embedded.d.cts +15 -0
  29. package/types/focus-trap/embedded.d.ts +11 -0
  30. package/types/index.d.cts +11 -0
  31. package/types/index.d.ts +3 -1
  32. package/types/popover.d.cts +14 -0
  33. package/types/popover.d.ts +15 -0
  34. package/dist/js/focus-trap.cjs +0 -104
  35. package/dist/js/focus-trap.js +0 -103
  36. package/src/js/focus-trap.ts +0 -133
  37. /package/types/{focus-trap.d.cts → focus-trap/standalone.d.cts} +0 -0
  38. /package/types/{focus-trap.d.ts → focus-trap/standalone.d.ts} +0 -0
@@ -1,133 +0,0 @@
1
- import {clamp} from '@oscarpalmer/atoms/number';
2
- import {on} from '@oscarpalmer/toretto/event';
3
- import {findAncestor} from '@oscarpalmer/toretto/find';
4
- import {getFocusable} from '@oscarpalmer/toretto/focusable';
5
- import {attributable} from './attributable';
6
-
7
- class FocusTrap {
8
- active = true;
9
-
10
- constructor(private element: HTMLElement) {
11
- element.setAttribute('aria-modal', 'true');
12
- element.setAttribute('tabindex', '-1');
13
- }
14
-
15
- activate() {
16
- this.element.setAttribute('aria-modal', 'true');
17
-
18
- this.active = true;
19
- }
20
-
21
- destroy() {
22
- this.active = false;
23
- this.element = undefined as never;
24
- }
25
-
26
- inactivate() {
27
- this.element.setAttribute('aria-modal', 'false');
28
-
29
- this.active = false;
30
-
31
- inactive.add(this);
32
- }
33
- }
34
-
35
- function addFocusTrap(element: HTMLElement): void {
36
- if (!focusTraps.has(element)) {
37
- focusTraps.set(element, new FocusTrap(element));
38
- }
39
- }
40
-
41
- function onEscape(event: KeyboardEvent): void {
42
- if (event.key !== 'Escape') {
43
- return;
44
- }
45
-
46
- const element = findAncestor(
47
- document.activeElement as never,
48
- `[${selector}]`,
49
- ) as HTMLElement;
50
-
51
- focusTraps.get(element)?.inactivate();
52
- }
53
-
54
- function onFocusOut(): void {
55
- if (inactive.size === 0) {
56
- return;
57
- }
58
-
59
- requestAnimationFrame(() => {
60
- const element = findAncestor(
61
- document.activeElement as never,
62
- `[${selector}]`,
63
- ) as HTMLElement;
64
-
65
- if (!focusTraps.has(element)) {
66
- for (const focusTrap of inactive) {
67
- focusTrap.activate();
68
- }
69
- }
70
- });
71
- }
72
-
73
- function onTab(event: KeyboardEvent): void {
74
- if (event.key !== 'Tab') {
75
- return;
76
- }
77
-
78
- const element = findAncestor(
79
- event.target as never,
80
- `[${selector}]`,
81
- ) as HTMLElement;
82
-
83
- if (!(focusTraps.get(element)?.active ?? false)) {
84
- return;
85
- }
86
-
87
- event.preventDefault();
88
-
89
- const elements = getFocusable(element);
90
- const {length} = elements;
91
-
92
- const index = elements.indexOf(event.target as HTMLElement);
93
-
94
- if (index === -1 || length === 0) {
95
- if (length === 0) {
96
- element.focus();
97
- } else {
98
- (elements[0] as HTMLElement).focus();
99
- }
100
-
101
- return;
102
- }
103
-
104
- const next = clamp(index + (event.shiftKey ? -1 : 1), 0, length - 1, true);
105
-
106
- if (next !== index) {
107
- (elements[next] as HTMLElement).focus();
108
- }
109
- }
110
-
111
- function removeFocusTraps(element: HTMLElement): void {
112
- focusTraps.get(element)?.destroy();
113
- focusTraps.delete(element);
114
- }
115
-
116
- const focusTraps = new WeakMap<HTMLElement, FocusTrap>();
117
- const inactive = new Set<FocusTrap>();
118
- const selector = 'oui-focus-trap';
119
-
120
- attributable(selector, addFocusTrap, removeFocusTraps);
121
-
122
- on(document, 'focusout', onFocusOut, {
123
- capture: true,
124
- });
125
-
126
- on(document, 'keydown', onTab, {
127
- capture: true,
128
- passive: false,
129
- });
130
-
131
- on(document, 'keydown', onEscape, {
132
- passive: false,
133
- });