@internetarchive/modal-manager 0.3.4 → 0.9.1-alpha.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.
@@ -0,0 +1,221 @@
1
+ // Cached compute style calls. This is specifically for browsers that dont support `checkVisibility()`.
2
+ // computedStyle calls are "live" so they only need to be retrieved once for an element.
3
+ const computedStyleMap = new WeakMap<Element, CSSStyleDeclaration>();
4
+
5
+ function getCachedComputedStyle(el: HTMLElement): CSSStyleDeclaration {
6
+ let computedStyle: undefined | CSSStyleDeclaration = computedStyleMap.get(el);
7
+
8
+ if (!computedStyle) {
9
+ computedStyle = window.getComputedStyle(el, null);
10
+ computedStyleMap.set(el, computedStyle);
11
+ }
12
+
13
+ return computedStyle;
14
+ }
15
+
16
+ function isVisible(el: HTMLElement): boolean {
17
+ // This is the fastest check, but isn't supported in Safari.
18
+ if ('checkVisibility' in el && typeof el['checkVisibility'] === 'function') {
19
+ // Opacity is focusable, visibility is not.
20
+ return (el as any)['checkVisibility']({
21
+ checkOpacity: false,
22
+ checkVisibilityCSS: true,
23
+ });
24
+ }
25
+
26
+ // Fallback "polyfill" for "checkVisibility"
27
+ const computedStyle = getCachedComputedStyle(el as HTMLElement);
28
+
29
+ return (
30
+ computedStyle.visibility !== 'hidden' && computedStyle.display !== 'none'
31
+ );
32
+ }
33
+
34
+ // While this behavior isn't standard in Safari / Chrome yet, I think it's the most reasonable
35
+ // way of handling tabbable overflow areas. Browser sniffing seems gross, and it's the most
36
+ // accessible way of handling overflow areas. [Konnor]
37
+ function isOverflowingAndTabbable(el: HTMLElement): boolean {
38
+ const computedStyle = getCachedComputedStyle(el);
39
+
40
+ const { overflowY, overflowX } = computedStyle;
41
+
42
+ if (overflowY === 'scroll' || overflowX === 'scroll') {
43
+ return true;
44
+ }
45
+
46
+ if (overflowY !== 'auto' || overflowX !== 'auto') {
47
+ return false;
48
+ }
49
+
50
+ // Always overflow === "auto" by this point
51
+ const isOverflowingY = el.scrollHeight > el.clientHeight;
52
+
53
+ if (isOverflowingY && overflowY === 'auto') {
54
+ return true;
55
+ }
56
+
57
+ const isOverflowingX = el.scrollWidth > el.clientWidth;
58
+
59
+ if (isOverflowingX && overflowX === 'auto') {
60
+ return true;
61
+ }
62
+
63
+ return false;
64
+ }
65
+
66
+ /** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */
67
+ function isTabbable(el: HTMLElement) {
68
+ const tag = el.tagName.toLowerCase();
69
+
70
+ const tabindex = Number(el.getAttribute('tabindex'));
71
+ const hasTabindex = el.hasAttribute('tabindex');
72
+
73
+ // elements with a tabindex attribute that is either NaN or <= -1 are not tabbable
74
+ if (hasTabindex && (isNaN(tabindex) || tabindex <= -1)) {
75
+ return false;
76
+ }
77
+
78
+ // Elements with a disabled attribute are not tabbable
79
+ if (el.hasAttribute('disabled')) {
80
+ return false;
81
+ }
82
+
83
+ // If any parents have "inert", we aren't "tabbable"
84
+ if (el.closest('[inert]')) {
85
+ return false;
86
+ }
87
+
88
+ // Radios without a checked attribute are not tabbable
89
+ if (
90
+ tag === 'input' &&
91
+ el.getAttribute('type') === 'radio' &&
92
+ !el.hasAttribute('checked')
93
+ ) {
94
+ return false;
95
+ }
96
+
97
+ if (!isVisible(el)) {
98
+ return false;
99
+ }
100
+
101
+ // Audio and video elements with the controls attribute are tabbable
102
+ if ((tag === 'audio' || tag === 'video') && el.hasAttribute('controls')) {
103
+ return true;
104
+ }
105
+
106
+ // Elements with a tabindex other than -1 are tabbable
107
+ if (el.hasAttribute('tabindex')) {
108
+ return true;
109
+ }
110
+
111
+ // Elements with a contenteditable attribute are tabbable
112
+ if (
113
+ el.hasAttribute('contenteditable') &&
114
+ el.getAttribute('contenteditable') !== 'false'
115
+ ) {
116
+ return true;
117
+ }
118
+
119
+ // At this point, the following elements are considered tabbable
120
+ const isNativelyTabbable = [
121
+ 'button',
122
+ 'input',
123
+ 'select',
124
+ 'textarea',
125
+ 'a',
126
+ 'audio',
127
+ 'video',
128
+ 'summary',
129
+ 'iframe',
130
+ ].includes(tag);
131
+
132
+ if (isNativelyTabbable) {
133
+ return true;
134
+ }
135
+
136
+ // We save the overflow checks for last, because they're the most expensive
137
+ return isOverflowingAndTabbable(el);
138
+ }
139
+
140
+ /**
141
+ * Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
142
+ * element because it short-circuits after finding the first and last ones.
143
+ */
144
+ export function getTabbableBoundary(root: HTMLElement | ShadowRoot) {
145
+ const tabbableElements = getTabbableElements(root);
146
+
147
+ // Find the first and last tabbable elements
148
+ const start = tabbableElements[0] ?? null;
149
+ const end = tabbableElements[tabbableElements.length - 1] ?? null;
150
+
151
+ return { start, end };
152
+ }
153
+
154
+ /**
155
+ * This looks funky. Basically a slot's children will always be picked up *if* they're within the `root` element.
156
+ * However, there is an edge case when, if the `root` is wrapped by another shadow DOM, it won't grab the children.
157
+ * This fixes that fun edge case.
158
+ */
159
+ function getSlottedChildrenOutsideRootElement(
160
+ slotElement: HTMLSlotElement,
161
+ root: HTMLElement | ShadowRoot
162
+ ) {
163
+ return (
164
+ (slotElement.getRootNode({ composed: true }) as ShadowRoot | null)?.host !==
165
+ root
166
+ );
167
+ }
168
+
169
+ export function getTabbableElements(root: HTMLElement | ShadowRoot) {
170
+ const walkedEls = new WeakMap();
171
+ const tabbableElements: HTMLElement[] = [];
172
+
173
+ function walk(el: HTMLElement | ShadowRoot) {
174
+ if (el instanceof Element) {
175
+ // if the element has "inert" we can just no-op it.
176
+ if (el.hasAttribute('inert') || el.closest('[inert]')) {
177
+ return;
178
+ }
179
+
180
+ if (walkedEls.has(el)) {
181
+ return;
182
+ }
183
+ walkedEls.set(el, true);
184
+
185
+ if (!tabbableElements.includes(el) && isTabbable(el)) {
186
+ tabbableElements.push(el);
187
+ }
188
+
189
+ if (
190
+ el instanceof HTMLSlotElement &&
191
+ getSlottedChildrenOutsideRootElement(el, root)
192
+ ) {
193
+ el.assignedElements({ flatten: true }).forEach(
194
+ (assignedEl: Element) => {
195
+ walk(assignedEl as HTMLElement | ShadowRoot);
196
+ }
197
+ );
198
+ }
199
+
200
+ if (el.shadowRoot !== null && el.shadowRoot.mode === 'open') {
201
+ walk(el.shadowRoot);
202
+ }
203
+ }
204
+
205
+ for (const e of Array.from(el.children) as HTMLElement[]) {
206
+ walk(e);
207
+ }
208
+ }
209
+
210
+ // Collect all elements including the root
211
+ walk(root);
212
+
213
+ // Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.
214
+ // So is it worth being right? Or fast?
215
+ return tabbableElements.sort((a, b) => {
216
+ // Make sure we sort by tabindex.
217
+ const aTabindex = Number(a.getAttribute('tabindex')) || 0;
218
+ const bTabindex = Number(b.getAttribute('tabindex')) || 0;
219
+ return bTabindex - aTabindex;
220
+ });
221
+ }
@@ -21,7 +21,7 @@ describe('Modal Manager', () => {
21
21
  <modal-manager .mode=${ModalManagerMode.Open}></modal-manager>
22
22
  `)) as ModalManager;
23
23
 
24
- el.customModalContent = ('foo' as unknown) as TemplateResult;
24
+ el.customModalContent = 'foo' as unknown as TemplateResult;
25
25
  await elementUpdated(el);
26
26
 
27
27
  expect(el.customModalContent).to.equal('foo');
@@ -56,7 +56,7 @@ describe('Modal Manager', () => {
56
56
  setTimeout(() => {
57
57
  el.showModal({ config });
58
58
  });
59
- const response = await oneEvent(el, 'modeChanged');
59
+ const response = await oneEvent(el, 'modeChanged', false);
60
60
  expect(response.detail.mode).to.equal(ModalManagerMode.Open);
61
61
  });
62
62
 
@@ -72,7 +72,7 @@ describe('Modal Manager', () => {
72
72
  setTimeout(() => {
73
73
  el.closeModal();
74
74
  });
75
- const response = await oneEvent(el, 'modeChanged');
75
+ const response = await oneEvent(el, 'modeChanged', false);
76
76
  expect(response.detail.mode).to.equal(ModalManagerMode.Closed);
77
77
  });
78
78
 
@@ -40,7 +40,7 @@ describe('Modal Template', () => {
40
40
  setTimeout(() => {
41
41
  closeButton?.dispatchEvent(clickEvent);
42
42
  });
43
- const response = await oneEvent(el, 'closeButtonPressed');
43
+ const response = await oneEvent(el, 'closeButtonPressed', false);
44
44
  expect(response).to.exist;
45
45
  });
46
46