-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
utils.ts
61 lines (49 loc) · 1.72 KB
/
utils.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
import * as React from 'react';
/* Use it instead of .includes method for IE support */
export function arrayIncludes<T>(array: T[] | readonly T[], itemOrItems: T | T[]) {
if (Array.isArray(itemOrItems)) {
return itemOrItems.every((item) => array.indexOf(item) !== -1);
}
return array.indexOf(itemOrItems) !== -1;
}
export const onSpaceOrEnter =
(
innerFn: (ev: React.MouseEvent<any> | React.KeyboardEvent<any>) => void,
externalEvent?: (event: React.KeyboardEvent<any>) => void,
) =>
(event: React.KeyboardEvent) => {
if (event.key === 'Enter' || event.key === ' ') {
innerFn(event);
// prevent any side effects
event.preventDefault();
event.stopPropagation();
}
if (externalEvent) {
externalEvent(event);
}
};
export const executeInTheNextEventLoopTick = (fn: () => void) => {
setTimeout(fn, 0);
};
// https://www.abeautifulsite.net/posts/finding-the-active-element-in-a-shadow-root/
export const getActiveElement = (root: Document | ShadowRoot = document): Element | null => {
const activeEl = root.activeElement;
if (!activeEl) {
return null;
}
if (activeEl.shadowRoot) {
return getActiveElement(activeEl.shadowRoot);
}
return activeEl;
};
/**
* Gets the index of the focused list item in a given ul list element.
*
* @param {HTMLUListElement} listElement - The list element to search within.
* @returns {number} The index of the focused list item, or -1 if none is focused.
*/
export const getFocusedListItemIndex = (listElement: HTMLUListElement): number => {
const children = Array.from(listElement.children);
return children.indexOf(getActiveElement(document)!);
};
export const DEFAULT_DESKTOP_MODE_MEDIA_QUERY = '@media (pointer: fine)';