-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdom.ts
400 lines (344 loc) · 11.8 KB
/
dom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { CSS_UTILITY } from "./resources";
import { guid } from "./guid";
import { tabbable } from "tabbable";
export const tabbableOptions = {
getShadowRoot: true
};
/**
* This helper will guarantee an ID on the provided element.
*
* If it already has an ID, it will be preserved, otherwise a unique one will be generated and assigned.
*
* @param el
* @returns {string} The element's ID.
*/
export function ensureId(el: Element): string {
if (!el) {
return "";
}
return (el.id = el.id || `${el.tagName.toLowerCase()}-${guid()}`);
}
export function nodeListToArray<T extends Element>(nodeList: HTMLCollectionOf<T> | NodeListOf<T> | T[]): T[] {
return Array.isArray(nodeList) ? nodeList : Array.from(nodeList);
}
export type Direction = "ltr" | "rtl";
export function getThemeName(el: HTMLElement): "light" | "dark" {
const closestElWithTheme = closestElementCrossShadowBoundary(
el,
`.${CSS_UTILITY.darkTheme}, .${CSS_UTILITY.lightTheme}`
);
return closestElWithTheme?.classList.contains("calcite-theme-dark") ? "dark" : "light";
}
export function getElementDir(el: HTMLElement): Direction {
const prop = "dir";
const selector = `[${prop}]`;
const closest = closestElementCrossShadowBoundary(el, selector);
return closest ? (closest.getAttribute(prop) as Direction) : "ltr";
}
export function getElementProp(el: Element, prop: string, fallbackValue: any): any {
const selector = `[${prop}]`;
const closest = el.closest(selector);
return closest ? closest.getAttribute(prop) : fallbackValue;
}
export function getRootNode(el: Element): Document | ShadowRoot {
return el.getRootNode() as Document | ShadowRoot;
}
export function getHost(root: Document | ShadowRoot): Element | null {
return (root as ShadowRoot).host || null;
}
/**
* This helper queries an element's rootNode and any ancestor rootNodes.
*
* If both an 'id' and 'selector' are supplied, 'id' will take precedence over 'selector'.
*
* @param element
* @param root0
* @param root0.selector
* @param root0.id
* @returns {Element} The element.
*/
export function queryElementRoots<T extends Element = Element>(
element: Element,
{
selector,
id
}: {
selector?: string;
id?: string;
}
): T | null {
// Gets the rootNode and any ancestor rootNodes (shadowRoot or document) of an element and queries them for a selector.
// Based on: https://stackoverflow.com/q/54520554/194216
function queryFrom<T extends Element = Element>(el: Element): T | null {
if (!el) {
return null;
}
if ((el as Slottable).assignedSlot) {
el = (el as Slottable).assignedSlot;
}
const rootNode = getRootNode(el);
const found = id
? "getElementById" in rootNode
? /*
Check to make sure 'getElementById' exists in cases where element is no longer connected to the DOM and getRootNode() returns the element.
https://github.com/Esri/calcite-components/pull/4280
*/
(rootNode.getElementById(id) as Element as T)
: null
: selector
? (rootNode.querySelector(selector) as T)
: null;
const host = getHost(rootNode);
return found ? found : host ? queryFrom(host) : null;
}
return queryFrom(element);
}
export function closestElementCrossShadowBoundary<T extends Element = Element>(
element: Element,
selector: string
): T | null {
// based on https://stackoverflow.com/q/54520554/194216
function closestFrom<T extends Element = Element>(el: Element): T | null {
return el ? el.closest(selector) || closestFrom(getHost(getRootNode(el))) : null;
}
return closestFrom(element);
}
/**
* This utility helps invoke a callback as it traverses a node and its ancestors until reaching the root document.
*
* Returning early or undefined in `onVisit` will continue traversing up the DOM tree. Otherwise, traversal will halt with the returned value as the result of the function
*
* @param element
* @param onVisit
*/
export function walkUpAncestry<T = any>(element: Element, onVisit: (node: Node) => T): T {
return visit(element, onVisit);
}
function visit<T = any>(node: Node, onVisit: (node: Node) => T): T {
if (!node) {
return;
}
const result = onVisit(node);
if (result !== undefined) {
return result;
}
const { parentNode } = node;
return visit(parentNode instanceof ShadowRoot ? parentNode.host : parentNode, onVisit);
}
export function containsCrossShadowBoundary(element: Element, maybeDescendant: Element): boolean {
return !!walkUpAncestry(maybeDescendant, (node) => (node === element ? true : undefined));
}
export interface FocusableElement extends HTMLElement {
setFocus?: () => Promise<void>;
}
export function isCalciteFocusable(el: FocusableElement): boolean {
return typeof el?.setFocus === "function";
}
export async function focusElement(el: FocusableElement): Promise<void> {
if (!el) {
return;
}
return isCalciteFocusable(el) ? el.setFocus() : el.focus();
}
/**
* Helper to focus the first tabbable element.
*
* @param {HTMLElement} element The html element containing tabbable elements.
*/
export function focusFirstTabbable(element: HTMLElement): void {
(tabbable(element, tabbableOptions)[0] || element).focus();
}
interface GetSlottedOptions {
all?: boolean;
direct?: boolean;
matches?: string;
selector?: string;
}
const defaultSlotSelector = ":not([slot])";
/**
* Gets slotted elements for a named slot.
*
* @param element
* @param slotName
* @param options
* @deprecated Use `onSlotchange` event instead.
*
* ```
* <slot onSlotchange={(event) => this.myElements = slotChangeGetAssignedElements(event)} />}
* ```
*/
export function getSlotted<T extends Element = Element>(
element: Element,
slotName: string | string[] | (GetSlottedOptions & { all: true }),
options: GetSlottedOptions & { all: true }
): T[];
export function getSlotted<T extends Element = Element>(
element: Element,
slotName?: string | string[] | GetSlottedOptions,
options?: GetSlottedOptions
): T | null;
export function getSlotted<T extends Element = Element>(
element: Element,
slotName?: string | string[] | GetSlottedOptions,
options?: GetSlottedOptions
): (T | null) | T[] {
if (slotName && !Array.isArray(slotName) && typeof slotName !== "string") {
options = slotName;
slotName = null;
}
const slotSelector = slotName
? Array.isArray(slotName)
? slotName.map((name) => `[slot="${name}"]`).join(",")
: `[slot="${slotName}"]`
: defaultSlotSelector;
if (options?.all) {
return queryMultiple<T>(element, slotSelector, options);
}
return querySingle<T>(element, slotSelector, options);
}
function getDirectChildren<T extends Element = Element>(el: Element, selector: string): T[] {
return el ? (Array.from(el.children || []) as T[]).filter((child) => child?.matches(selector)) : [];
}
function queryMultiple<T extends Element = Element>(
element: Element,
slotSelector: string,
options?: GetSlottedOptions
): T[] {
let matches =
slotSelector === defaultSlotSelector
? getDirectChildren<T>(element, defaultSlotSelector)
: Array.from(element.querySelectorAll<T>(slotSelector));
matches = options && options.direct === false ? matches : matches.filter((el) => el.parentElement === element);
matches = options?.matches ? matches.filter((el) => el?.matches(options.matches)) : matches;
const selector = options?.selector;
return selector
? matches
.map((item) => Array.from(item.querySelectorAll<T>(selector)))
.reduce((previousValue, currentValue) => [...previousValue, ...currentValue], [])
.filter((match) => !!match)
: matches;
}
function querySingle<T extends Element = Element>(
element: Element,
slotSelector: string,
options?: GetSlottedOptions
): T | null {
let match =
slotSelector === defaultSlotSelector
? getDirectChildren<T>(element, defaultSlotSelector)[0] || null
: element.querySelector<T>(slotSelector);
match = options && options.direct === false ? match : match?.parentElement === element ? match : null;
match = options?.matches ? (match?.matches(options.matches) ? match : null) : match;
const selector = options?.selector;
return selector ? match?.querySelector<T>(selector) : match;
}
export function filterDirectChildren<T extends Element>(el: Element, selector: string): T[] {
return Array.from(el.children).filter((child): child is T => child.matches(selector));
}
// set a default icon from a defined set or allow an override with an icon name string
export function setRequestedIcon(
iconObject: Record<string, string>,
iconValue: string | boolean,
matchedValue: string
): string {
if (typeof iconValue === "string" && iconValue !== "") {
return iconValue;
} else if (iconValue === "") {
return iconObject[matchedValue];
}
}
export function intersects(rect1: DOMRect, rect2: DOMRect): boolean {
return !(
rect2.left > rect1.right ||
rect2.right < rect1.left ||
rect2.top > rect1.bottom ||
rect2.bottom < rect1.top
);
}
/**
* This helper makes sure that boolean aria attributes are properly converted to a string.
*
* It should only be used for aria attributes that require a string value of "true" or "false".
*
* @param value
* @returns {string} The string conversion of a boolean value ("true" | "false").
*/
export function toAriaBoolean(value: boolean): string {
return Boolean(value).toString();
}
/**
* This helper returns `true` if the target `slot` element from the `onSlotchange` event has an assigned element.
*
* ```
* <slot onSlotchange={(event) => this.mySlotHasElement = slotChangeHasAssignedElement(event)} />}
* ```
*
* @param event
* @returns {boolean} Whether the slot has any assigned elements.
*/
export function slotChangeHasAssignedElement(event: Event): boolean {
return !!slotChangeGetAssignedElements(event).length;
}
/**
* This helper returns the assigned elements on a `slot` element from the `onSlotchange` event.
*
* ```
* <slot onSlotchange={(event) => this.mySlotElements = slotChangeGetAssignedElements(event)} />}
* ```
*
* @param event
* @returns {boolean} Whether the slot has any assigned elements.
*/
export function slotChangeGetAssignedElements(event: Event): Element[] {
return (event.target as HTMLSlotElement).assignedElements({
flatten: true
});
}
/**
* This helper returns true if the pointer event fired from the primary button of the device.
*
* See https://www.w3.org/TR/pointerevents/#the-button-property.
*
* @param event
* @returns {boolean}
*/
export function isPrimaryPointerButton(event: PointerEvent): boolean {
return !!(event.isPrimary && event.button === 0);
}
/**
* This helper sets focus on and returns a destination element from within a group of provided elements.
*
* @param elements An array of elements
* @param currentElement The current element
* @param destination The target destination element to focus
* @returns {Element} The focused element
*/
export type FocusElementInGroupDestination = "first" | "last" | "next" | "previous";
export const focusElementInGroup = (
elements: Element[],
currentElement: Element,
destination: FocusElementInGroupDestination
): Element => {
const currentIndex = elements.indexOf(currentElement);
const isFirstItem = currentIndex === 0;
const isLastItem = currentIndex === elements.length - 1;
destination =
destination === "previous" && isFirstItem ? "last" : destination === "next" && isLastItem ? "first" : destination;
let focusTarget;
switch (destination) {
case "first":
focusTarget = elements[0];
break;
case "last":
focusTarget = elements[elements.length - 1];
break;
case "next":
focusTarget = elements[currentIndex + 1] || elements[0];
break;
case "previous":
focusTarget = elements[currentIndex - 1] || elements[elements.length - 1];
break;
}
focusElement(focusTarget);
return focusTarget;
};