@thednp/shorty 2.0.4 → 2.0.6
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.
- package/README.md +5 -5
- package/dist/shorty.cjs +18 -1
- package/dist/shorty.cjs.map +1 -1
- package/dist/shorty.d.ts +31 -18
- package/dist/shorty.js +18 -1
- package/dist/shorty.js.map +1 -1
- package/dist/shorty.mjs +451 -386
- package/dist/shorty.mjs.map +1 -1
- package/dts.config.ts +1 -7
- package/package.json +16 -28
- package/src/attr/hasAttribute.ts +2 -1
- package/src/attr/hasAttributeNS.ts +5 -2
- package/src/attr/removeAttribute.ts +2 -1
- package/src/attr/removeAttributeNS.ts +5 -2
- package/src/attr/setAttributeNS.ts +6 -2
- package/src/boolean/isApple.ts +6 -4
- package/src/boolean/isFirefox.ts +2 -2
- package/src/boolean/isMobile.ts +4 -4
- package/src/boolean/support3DTransform.ts +4 -2
- package/src/boolean/supportAnimation.ts +4 -2
- package/src/boolean/supportPassive.ts +6 -6
- package/src/boolean/supportTouch.ts +3 -2
- package/src/boolean/supportTransform.ts +4 -2
- package/src/boolean/supportTransition.ts +4 -2
- package/src/event/off.ts +5 -1
- package/src/event/on.ts +5 -1
- package/src/event/one.ts +5 -5
- package/src/get/getBoundingClientRect.ts +10 -5
- package/src/get/getDocument.ts +4 -4
- package/src/get/getDocumentBody.ts +1 -1
- package/src/get/getDocumentElement.ts +1 -1
- package/src/get/getDocumentHead.ts +4 -2
- package/src/get/getElementAnimationDelay.ts +9 -6
- package/src/get/getElementAnimationDuration.ts +9 -6
- package/src/get/getElementStyle.ts +2 -2
- package/src/get/getElementTransitionDelay.ts +9 -8
- package/src/get/getElementTransitionDuration.ts +9 -8
- package/src/get/getNodeScroll.ts +4 -2
- package/src/get/getParentNode.ts +5 -5
- package/src/get/getRectRelativeToOffsetParent.ts +9 -6
- package/src/get/getUID.ts +1 -1
- package/src/get/getWindow.ts +2 -2
- package/src/index.ts +425 -415
- package/src/interface/event.d.ts +49 -41
- package/src/interface/fn.ts +1 -0
- package/src/interface/originalEvent.d.ts +1 -1
- package/src/is/isArray.ts +2 -1
- package/src/is/isCanvas.ts +2 -2
- package/src/is/isCustomElement.ts +5 -3
- package/src/is/isDocument.ts +3 -2
- package/src/is/isElement.ts +3 -2
- package/src/is/isElementInScrollRange.ts +3 -3
- package/src/is/isElementInViewport.ts +5 -4
- package/src/is/isElementsArray.ts +2 -2
- package/src/is/isFunction.ts +4 -2
- package/src/is/isHTMLCollection.ts +2 -2
- package/src/is/isHTMLElement.ts +1 -1
- package/src/is/isHTMLImageElement.ts +2 -2
- package/src/is/isJSON.ts +2 -2
- package/src/is/isMap.ts +3 -3
- package/src/is/isMedia.ts +9 -3
- package/src/is/isNode.ts +5 -3
- package/src/is/isNodeList.ts +2 -2
- package/src/is/isNumber.ts +2 -1
- package/src/is/isObject.ts +1 -1
- package/src/is/isRTL.ts +2 -2
- package/src/is/isSVGElement.ts +2 -2
- package/src/is/isScaledElement.ts +4 -3
- package/src/is/isShadowRoot.ts +2 -2
- package/src/is/isString.ts +2 -1
- package/src/is/isTableElement.ts +6 -3
- package/src/is/isWeakMap.ts +3 -3
- package/src/is/isWindow.ts +2 -2
- package/src/misc/Float32ArrayFrom.ts +3 -2
- package/src/misc/Float64ArrayFrom.ts +3 -2
- package/src/misc/ObjectAssign.ts +15 -4
- package/src/misc/ObjectEntries.ts +1 -1
- package/src/misc/ObjectHasOwn.ts +3 -2
- package/src/misc/ObjectKeys.ts +2 -1
- package/src/misc/ObjectValues.ts +3 -2
- package/src/misc/createCustomEvent.ts +8 -5
- package/src/misc/createElement.ts +6 -4
- package/src/misc/createElementNS.ts +3 -3
- package/src/misc/data.ts +4 -7
- package/src/misc/dispatchEvent.ts +2 -1
- package/src/misc/distinct.ts +2 -1
- package/src/misc/emulateAnimationEnd.ts +12 -9
- package/src/misc/emulateTransitionEnd.ts +11 -8
- package/src/misc/focus.ts +2 -1
- package/src/misc/focusTrap.ts +67 -0
- package/src/misc/getInstance.ts +1 -1
- package/src/misc/normalizeOptions.ts +16 -13
- package/src/misc/normalizeValue.ts +8 -6
- package/src/misc/setElementStyle.ts +9 -6
- package/src/misc/timer.ts +15 -8
- package/src/selectors/closest.ts +2 -2
- package/src/selectors/getCustomElements.ts +4 -5
- package/src/selectors/getElementById.ts +3 -3
- package/src/selectors/getElementsByClassName.ts +5 -5
- package/src/selectors/getElementsByTagName.ts +7 -5
- package/src/selectors/matches.ts +2 -1
- package/src/selectors/querySelector.ts +9 -6
- package/src/selectors/querySelectorAll.ts +6 -3
- package/src/strings/DOMContentLoadedEvent.ts +1 -1
- package/src/strings/DOMMouseScrollEvent.ts +1 -1
- package/src/strings/abortEvent.ts +1 -1
- package/src/strings/addEventListener.ts +1 -1
- package/src/strings/animationDelay.ts +1 -1
- package/src/strings/animationDuration.ts +1 -1
- package/src/strings/animationEndEvent.ts +1 -1
- package/src/strings/animationName.ts +1 -1
- package/src/strings/ariaChecked.ts +1 -1
- package/src/strings/ariaDescribedBy.ts +1 -1
- package/src/strings/ariaDescription.ts +1 -1
- package/src/strings/ariaExpanded.ts +1 -1
- package/src/strings/ariaHasPopup.ts +1 -1
- package/src/strings/ariaHidden.ts +1 -1
- package/src/strings/ariaLabel.ts +1 -1
- package/src/strings/ariaLabelledBy.ts +1 -1
- package/src/strings/ariaModal.ts +1 -1
- package/src/strings/ariaPressed.ts +1 -1
- package/src/strings/ariaSelected.ts +1 -1
- package/src/strings/ariaValueMax.ts +1 -1
- package/src/strings/ariaValueMin.ts +1 -1
- package/src/strings/ariaValueNow.ts +1 -1
- package/src/strings/ariaValueText.ts +1 -1
- package/src/strings/beforeunloadEvent.ts +1 -1
- package/src/strings/bezierEasings.ts +25 -25
- package/src/strings/blurEvent.ts +1 -1
- package/src/strings/changeEvent.ts +1 -1
- package/src/strings/contextmenuEvent.ts +1 -1
- package/src/strings/dragEvent.ts +1 -1
- package/src/strings/dragendEvent.ts +1 -1
- package/src/strings/dragenterEvent.ts +1 -1
- package/src/strings/dragleaveEvent.ts +1 -1
- package/src/strings/dragoverEvent.ts +1 -1
- package/src/strings/dragstartEvent.ts +1 -1
- package/src/strings/errorEvent.ts +1 -1
- package/src/strings/focusEvent.ts +1 -1
- package/src/strings/focusEvents.ts +1 -1
- package/src/strings/focusableSelector.ts +4 -0
- package/src/strings/focusinEvent.ts +1 -1
- package/src/strings/focusoutEvent.ts +1 -1
- package/src/strings/gesturechangeEvent.ts +1 -1
- package/src/strings/gestureendEvent.ts +1 -1
- package/src/strings/gesturestartEvent.ts +1 -1
- package/src/strings/keyAlt.ts +1 -1
- package/src/strings/keyArrowDown.ts +1 -1
- package/src/strings/keyArrowLeft.ts +1 -1
- package/src/strings/keyArrowRight.ts +1 -1
- package/src/strings/keyArrowUp.ts +1 -1
- package/src/strings/keyBackspace.ts +1 -1
- package/src/strings/keyCapsLock.ts +1 -1
- package/src/strings/keyControl.ts +1 -1
- package/src/strings/keyDelete.ts +1 -1
- package/src/strings/keyEnter.ts +1 -1
- package/src/strings/keyEscape.ts +1 -1
- package/src/strings/keyInsert.ts +1 -1
- package/src/strings/keyMeta.ts +1 -1
- package/src/strings/keyNumpadEnter.ts +1 -1
- package/src/strings/keyPause.ts +1 -1
- package/src/strings/keyScrollLock.ts +1 -1
- package/src/strings/keyShift.ts +1 -1
- package/src/strings/keySpace.ts +1 -1
- package/src/strings/keyTab.ts +1 -1
- package/src/strings/keyboardEventKeys.ts +19 -19
- package/src/strings/keydownEvent.ts +1 -1
- package/src/strings/keypressEvent.ts +1 -1
- package/src/strings/keyupEvent.ts +1 -1
- package/src/strings/loadEvent.ts +1 -1
- package/src/strings/loadstartEvent.ts +1 -1
- package/src/strings/mouseClickEvents.ts +1 -1
- package/src/strings/mouseHoverEvents.ts +3 -4
- package/src/strings/mouseSwipeEvents.ts +4 -4
- package/src/strings/mouseclickEvent.ts +1 -1
- package/src/strings/mousedblclickEvent.ts +1 -1
- package/src/strings/mousedownEvent.ts +1 -1
- package/src/strings/mouseenterEvent.ts +1 -1
- package/src/strings/mousehoverEvent.ts +1 -1
- package/src/strings/mouseinEvent.ts +1 -1
- package/src/strings/mouseleaveEvent.ts +1 -1
- package/src/strings/mousemoveEvent.ts +1 -1
- package/src/strings/mouseoutEvent.ts +1 -1
- package/src/strings/mouseoverEvent.ts +1 -1
- package/src/strings/mouseupEvent.ts +1 -1
- package/src/strings/mousewheelEvent.ts +1 -1
- package/src/strings/moveEvent.ts +1 -1
- package/src/strings/nativeEvents.ts +50 -50
- package/src/strings/offsetHeight.ts +1 -1
- package/src/strings/offsetWidth.ts +1 -1
- package/src/strings/orientationchangeEvent.ts +1 -1
- package/src/strings/pointercancelEvent.ts +1 -1
- package/src/strings/pointerdownEvent.ts +1 -1
- package/src/strings/pointerleaveEvent.ts +1 -1
- package/src/strings/pointermoveEvent.ts +1 -1
- package/src/strings/pointerupEvent.ts +1 -1
- package/src/strings/readystatechangeEvent.ts +1 -1
- package/src/strings/removeEventListener.ts +1 -1
- package/src/strings/resetEvent.ts +1 -1
- package/src/strings/resizeEvent.ts +1 -1
- package/src/strings/scrollEvent.ts +1 -1
- package/src/strings/scrollHeight.ts +1 -1
- package/src/strings/scrollWidth.ts +1 -1
- package/src/strings/selectEvent.ts +1 -1
- package/src/strings/selectendEvent.ts +1 -1
- package/src/strings/selectstartEvent.ts +1 -1
- package/src/strings/submitEvent.ts +1 -1
- package/src/strings/tabindex.ts +1 -1
- package/src/strings/touchEvents.ts +4 -4
- package/src/strings/touchcancelEvent.ts +1 -1
- package/src/strings/touchendEvent.ts +1 -1
- package/src/strings/touchmoveEvent.ts +1 -1
- package/src/strings/touchstartEvent.ts +1 -1
- package/src/strings/transitionDelay.ts +1 -1
- package/src/strings/transitionDuration.ts +1 -1
- package/src/strings/transitionEndEvent.ts +1 -1
- package/src/strings/transitionProperty.ts +1 -1
- package/src/strings/unloadEvent.ts +1 -1
- package/src/strings/userAgentData.ts +2 -2
- package/test/fixtures/getExampleDom.ts +12 -8
- package/test/is.test.ts +15 -11
- package/test/misc.test.ts +65 -17
- package/{vite.config.ts → vite.config.mts} +4 -8
- package/{vitest.config-ui.ts → vitest.config-ui.mts} +5 -0
- package/{vitest.config.ts → vitest.config.mts} +5 -0
package/src/misc/ObjectKeys.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @returns an array with object keys
|
|
6
6
|
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
7
|
*/
|
|
8
|
-
const ObjectKeys = <O extends Record<
|
|
8
|
+
const ObjectKeys = <O extends Record<keyof O, unknown>>(obj: O) =>
|
|
9
|
+
Object.keys(obj) as (keyof O)[];
|
|
9
10
|
|
|
10
11
|
export default ObjectKeys;
|
package/src/misc/ObjectValues.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @returns an array with the object values
|
|
6
6
|
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
7
|
*/
|
|
8
|
-
const ObjectValues = <O extends Record<string, unknown>>(
|
|
9
|
-
|
|
8
|
+
const ObjectValues = <O extends Record<string, unknown>>(
|
|
9
|
+
obj: O,
|
|
10
|
+
): O[keyof O][] => Object.values(obj) as O[keyof O][];
|
|
10
11
|
|
|
11
12
|
export default ObjectValues;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { OriginalEvent } from
|
|
2
|
-
import isObject from
|
|
3
|
-
import ObjectAssign from
|
|
1
|
+
import type { OriginalEvent } from "../interface/originalEvent";
|
|
2
|
+
import isObject from "../is/isObject";
|
|
3
|
+
import ObjectAssign from "./ObjectAssign";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Returns a namespaced `CustomEvent` specific to each component.
|
|
@@ -9,7 +9,10 @@ import ObjectAssign from './ObjectAssign';
|
|
|
9
9
|
* @param config Event.options | Event.properties
|
|
10
10
|
* @returns a new namespaced event
|
|
11
11
|
*/
|
|
12
|
-
const createCustomEvent = <
|
|
12
|
+
const createCustomEvent = <
|
|
13
|
+
O extends unknown & Record<string, unknown>,
|
|
14
|
+
T extends OriginalEvent,
|
|
15
|
+
>(
|
|
13
16
|
eventType: string,
|
|
14
17
|
config?: O,
|
|
15
18
|
): T => {
|
|
@@ -18,7 +21,7 @@ const createCustomEvent = <O extends Record<string, unknown>, T extends Original
|
|
|
18
21
|
bubbles: true,
|
|
19
22
|
}) as T;
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
// istanbul ignore else @preserve
|
|
22
25
|
if (isObject(config)) {
|
|
23
26
|
ObjectAssign(OriginalCustomEvent, config);
|
|
24
27
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import isString from
|
|
2
|
-
import getDocument from
|
|
3
|
-
import ObjectAssign from
|
|
1
|
+
import isString from "../is/isString";
|
|
2
|
+
import getDocument from "../get/getDocument";
|
|
3
|
+
import ObjectAssign from "./ObjectAssign";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Shortie for `document.createElement` method
|
|
@@ -13,7 +13,9 @@ import ObjectAssign from './ObjectAssign';
|
|
|
13
13
|
* @param param `tagName` or object
|
|
14
14
|
* @return a new `HTMLElement`
|
|
15
15
|
*/
|
|
16
|
-
const createElement = <T extends HTMLElement>(
|
|
16
|
+
const createElement = <T extends HTMLElement>(
|
|
17
|
+
param?: string | Partial<T>,
|
|
18
|
+
): T | undefined => {
|
|
17
19
|
if (!param) return undefined;
|
|
18
20
|
|
|
19
21
|
if (isString(param)) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import getDocument from
|
|
2
|
-
import ObjectAssign from
|
|
3
|
-
import isString from
|
|
1
|
+
import getDocument from "../get/getDocument";
|
|
2
|
+
import ObjectAssign from "./ObjectAssign";
|
|
3
|
+
import isString from "../is/isString";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Shortie for `document.createElementNS` method
|
package/src/misc/data.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import isHTMLElement from
|
|
1
|
+
import isHTMLElement from "../is/isHTMLElement";
|
|
2
2
|
|
|
3
|
-
const componentData = new Map<string, Map<HTMLElement,
|
|
3
|
+
const componentData = new Map<string, Map<HTMLElement, unknown>>();
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* An interface for web components background data.
|
|
@@ -19,7 +19,7 @@ const Data = {
|
|
|
19
19
|
set: <T>(element: HTMLElement, component: string, instance: T): void => {
|
|
20
20
|
if (!isHTMLElement(element)) return;
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// istanbul ignore else @preserve
|
|
23
23
|
if (!componentData.has(component)) {
|
|
24
24
|
componentData.set(component, new Map<HTMLElement, T>());
|
|
25
25
|
}
|
|
@@ -51,11 +51,9 @@ const Data = {
|
|
|
51
51
|
get: <T>(element: HTMLElement, component: string): T | null => {
|
|
52
52
|
if (!isHTMLElement(element) || !component) return null;
|
|
53
53
|
const instanceMap = Data.getAllFor<T>(component);
|
|
54
|
-
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
55
54
|
|
|
56
55
|
const instance = element && instanceMap && instanceMap.get(element);
|
|
57
56
|
|
|
58
|
-
// return (instance as T) || null;
|
|
59
57
|
return instance || null;
|
|
60
58
|
},
|
|
61
59
|
|
|
@@ -67,13 +65,12 @@ const Data = {
|
|
|
67
65
|
*/
|
|
68
66
|
remove: <T>(element: HTMLElement, component: string): void => {
|
|
69
67
|
const instanceMap = Data.getAllFor<T>(component);
|
|
70
|
-
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
71
68
|
|
|
72
69
|
if (!instanceMap || !isHTMLElement(element)) return;
|
|
73
70
|
|
|
74
71
|
instanceMap.delete(element);
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
// istanbul ignore else @preserve
|
|
77
74
|
if (instanceMap.size === 0) {
|
|
78
75
|
componentData.delete(component);
|
|
79
76
|
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @param element is the target
|
|
5
5
|
* @param event is the `Event` object
|
|
6
6
|
*/
|
|
7
|
-
const dispatchEvent = (element: EventTarget, event: Event): boolean =>
|
|
7
|
+
const dispatchEvent = (element: EventTarget, event: Event): boolean =>
|
|
8
|
+
element.dispatchEvent(event);
|
|
8
9
|
|
|
9
10
|
export default dispatchEvent;
|
package/src/misc/distinct.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* @param arr a clone of the target array
|
|
14
14
|
* @returns the query result
|
|
15
15
|
*/
|
|
16
|
-
const distinct = <T>(value: T, index: number, arr: T[]): boolean =>
|
|
16
|
+
const distinct = <T>(value: T, index: number, arr: T[]): boolean =>
|
|
17
|
+
arr.indexOf(value) === index;
|
|
17
18
|
|
|
18
19
|
export default distinct;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import animationEndEvent from
|
|
2
|
-
import getElementAnimationDelay from
|
|
3
|
-
import getElementAnimationDuration from
|
|
4
|
-
import dispatchEvent from
|
|
1
|
+
import animationEndEvent from "../strings/animationEndEvent";
|
|
2
|
+
import getElementAnimationDelay from "../get/getElementAnimationDelay";
|
|
3
|
+
import getElementAnimationDuration from "../get/getElementAnimationDuration";
|
|
4
|
+
import dispatchEvent from "./dispatchEvent";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Utility to make sure callbacks are consistently
|
|
@@ -10,16 +10,19 @@ import dispatchEvent from './dispatchEvent';
|
|
|
10
10
|
* @param element target
|
|
11
11
|
* @param handler `animationend` callback
|
|
12
12
|
*/
|
|
13
|
-
const emulateAnimationEnd = (
|
|
13
|
+
const emulateAnimationEnd = (
|
|
14
|
+
element: HTMLElement,
|
|
15
|
+
handler: EventListener,
|
|
16
|
+
): void => {
|
|
14
17
|
let called = 0;
|
|
15
18
|
const endEvent = new Event(animationEndEvent);
|
|
16
19
|
const duration = getElementAnimationDuration(element);
|
|
17
20
|
const delay = getElementAnimationDelay(element);
|
|
18
21
|
|
|
19
22
|
if (duration) {
|
|
20
|
-
|
|
23
|
+
// Wrap the handler in on -> off callback
|
|
21
24
|
const animationEndWrapper = (e: Event): void => {
|
|
22
|
-
|
|
25
|
+
// istanbul ignore else @preserve
|
|
23
26
|
if (e.target === element) {
|
|
24
27
|
handler.apply(element, [e]);
|
|
25
28
|
element.removeEventListener(animationEndEvent, animationEndWrapper);
|
|
@@ -28,11 +31,11 @@ const emulateAnimationEnd = (element: HTMLElement, handler: EventListener): void
|
|
|
28
31
|
};
|
|
29
32
|
element.addEventListener(animationEndEvent, animationEndWrapper);
|
|
30
33
|
setTimeout(() => {
|
|
31
|
-
|
|
34
|
+
// istanbul ignore next @preserve
|
|
32
35
|
if (!called) dispatchEvent(element, endEvent);
|
|
33
36
|
}, duration + delay + 17);
|
|
34
37
|
} else {
|
|
35
|
-
|
|
38
|
+
// istanbul ignore next @preserve
|
|
36
39
|
handler.apply(element, [endEvent]);
|
|
37
40
|
}
|
|
38
41
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import transitionEndEvent from
|
|
2
|
-
import getElementTransitionDelay from
|
|
3
|
-
import getElementTransitionDuration from
|
|
4
|
-
import dispatchEvent from
|
|
1
|
+
import transitionEndEvent from "../strings/transitionEndEvent";
|
|
2
|
+
import getElementTransitionDelay from "../get/getElementTransitionDelay";
|
|
3
|
+
import getElementTransitionDuration from "../get/getElementTransitionDuration";
|
|
4
|
+
import dispatchEvent from "./dispatchEvent";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Utility to make sure callbacks are consistently
|
|
@@ -10,16 +10,19 @@ import dispatchEvent from './dispatchEvent';
|
|
|
10
10
|
* @param element event target
|
|
11
11
|
* @param handler `transitionend` callback
|
|
12
12
|
*/
|
|
13
|
-
const emulateTransitionEnd = (
|
|
13
|
+
const emulateTransitionEnd = (
|
|
14
|
+
element: HTMLElement,
|
|
15
|
+
handler: EventListener,
|
|
16
|
+
): void => {
|
|
14
17
|
let called = 0;
|
|
15
18
|
const endEvent = new Event(transitionEndEvent);
|
|
16
19
|
const duration = getElementTransitionDuration(element);
|
|
17
20
|
const delay = getElementTransitionDelay(element);
|
|
18
21
|
|
|
19
22
|
if (duration) {
|
|
20
|
-
|
|
23
|
+
// Wrap the handler in on -> off callback
|
|
21
24
|
const transitionEndWrapper = (e: Event): void => {
|
|
22
|
-
|
|
25
|
+
// istanbul ignore else @preserve
|
|
23
26
|
if (e.target === element) {
|
|
24
27
|
handler.apply(element, [e]);
|
|
25
28
|
element.removeEventListener(transitionEndEvent, transitionEndWrapper);
|
|
@@ -28,7 +31,7 @@ const emulateTransitionEnd = (element: HTMLElement, handler: EventListener): voi
|
|
|
28
31
|
};
|
|
29
32
|
element.addEventListener(transitionEndEvent, transitionEndWrapper);
|
|
30
33
|
setTimeout(() => {
|
|
31
|
-
|
|
34
|
+
// istanbul ignore next @preserve
|
|
32
35
|
if (!called) dispatchEvent(element, endEvent);
|
|
33
36
|
}, duration + delay + 17);
|
|
34
37
|
} else {
|
package/src/misc/focus.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @param element is the target
|
|
5
5
|
* @param options allows to pass additional options such as `preventScroll: boolean`
|
|
6
6
|
*/
|
|
7
|
-
const focus = (element: HTMLOrSVGElement, options?: FocusOptions): void =>
|
|
7
|
+
const focus = (element: HTMLOrSVGElement, options?: FocusOptions): void =>
|
|
8
|
+
element.focus(options);
|
|
8
9
|
|
|
9
10
|
export default focus;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import ariaHidden from "../strings/ariaHidden";
|
|
2
|
+
import focusableSelector from "../strings/focusableSelector";
|
|
3
|
+
import querySelectorAll from "../selectors/querySelectorAll";
|
|
4
|
+
import getAttribute from "../attr/getAttribute";
|
|
5
|
+
import hasAttribute from "../attr/hasAttribute";
|
|
6
|
+
import off from "../event/off";
|
|
7
|
+
import on from "../event/on";
|
|
8
|
+
import getDocument from "../get/getDocument";
|
|
9
|
+
import { KeyboardEvent } from "../interface/event";
|
|
10
|
+
|
|
11
|
+
const focusTrapMap = new Map<HTMLElement, boolean>();
|
|
12
|
+
|
|
13
|
+
export type FocusableElement =
|
|
14
|
+
| HTMLAnchorElement
|
|
15
|
+
| HTMLButtonElement
|
|
16
|
+
| HTMLInputElement
|
|
17
|
+
| HTMLTextAreaElement
|
|
18
|
+
| HTMLDataListElement
|
|
19
|
+
| HTMLDetailsElement
|
|
20
|
+
| HTMLSelectElement;
|
|
21
|
+
|
|
22
|
+
function handleKeyboardNavigation<T extends HTMLElement & EventTarget>(
|
|
23
|
+
this: T,
|
|
24
|
+
event: KeyboardEvent<T>,
|
|
25
|
+
) {
|
|
26
|
+
const { shiftKey, code } = event;
|
|
27
|
+
const doc = getDocument(this);
|
|
28
|
+
const focusableElements = [
|
|
29
|
+
...querySelectorAll<FocusableElement>(focusableSelector, this),
|
|
30
|
+
].filter(
|
|
31
|
+
(el) => !hasAttribute(el, "disabled") && !getAttribute(el, ariaHidden),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (!focusableElements.length) return;
|
|
35
|
+
const firstFocusable = focusableElements[0];
|
|
36
|
+
const lastFocusable = focusableElements[focusableElements.length - 1];
|
|
37
|
+
|
|
38
|
+
// istanbul ignore else @preserve
|
|
39
|
+
if (code === "Tab") {
|
|
40
|
+
if (shiftKey && doc.activeElement === firstFocusable) {
|
|
41
|
+
lastFocusable.focus();
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
} else if (!shiftKey && doc.activeElement === lastFocusable) {
|
|
44
|
+
firstFocusable.focus();
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Utility to check if a designated element is affected by focus trap;
|
|
52
|
+
* @param target
|
|
53
|
+
*/
|
|
54
|
+
export const hasFocusTrap = (target: HTMLElement) =>
|
|
55
|
+
focusTrapMap.has(target) === true;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Utility to toggle focus trap inside a designated target element;
|
|
59
|
+
* @param target
|
|
60
|
+
*/
|
|
61
|
+
export const toggleFocusTrap = (target: HTMLElement) => {
|
|
62
|
+
const isCurrentlyTrapped = hasFocusTrap(target);
|
|
63
|
+
const action = !isCurrentlyTrapped ? on : off;
|
|
64
|
+
action(target, "keydown", handleKeyboardNavigation);
|
|
65
|
+
if (isCurrentlyTrapped) focusTrapMap.delete(target);
|
|
66
|
+
else focusTrapMap.set(target, true);
|
|
67
|
+
};
|
package/src/misc/getInstance.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import getAttribute from
|
|
2
|
-
import normalizeValue from
|
|
3
|
-
import ObjectEntries from
|
|
4
|
-
import toLowerCase from
|
|
1
|
+
import getAttribute from "../attr/getAttribute";
|
|
2
|
+
import normalizeValue from "./normalizeValue";
|
|
3
|
+
import ObjectEntries from "./ObjectEntries";
|
|
4
|
+
import toLowerCase from "./toLowerCase";
|
|
5
5
|
// import { optionValues } from '../types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -13,23 +13,25 @@ import toLowerCase from './toLowerCase';
|
|
|
13
13
|
* @param ns component namespace
|
|
14
14
|
* @return normalized component options object
|
|
15
15
|
*/
|
|
16
|
-
const normalizeOptions = <T extends { [key: string]:
|
|
16
|
+
const normalizeOptions = <T extends { [key: string]: unknown }>(
|
|
17
17
|
element: HTMLElement,
|
|
18
18
|
defaultOps: T,
|
|
19
19
|
inputOps: Partial<T>,
|
|
20
20
|
ns?: string,
|
|
21
21
|
): T => {
|
|
22
|
-
const INPUT = { ...inputOps };
|
|
22
|
+
const INPUT = { ...inputOps } as T;
|
|
23
23
|
const data = { ...element.dataset };
|
|
24
24
|
const normalOps = { ...defaultOps };
|
|
25
25
|
const dataOps: Partial<T> = {};
|
|
26
|
-
const title =
|
|
26
|
+
const title = "title";
|
|
27
27
|
|
|
28
28
|
ObjectEntries(data).forEach(([k, v]) => {
|
|
29
|
-
const key: keyof T =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
:
|
|
29
|
+
const key: keyof T = ns && typeof k === "string" && k.includes(ns)
|
|
30
|
+
? k.replace(ns, "").replace(
|
|
31
|
+
/[A-Z]/g,
|
|
32
|
+
(match: string) => toLowerCase(match),
|
|
33
|
+
)
|
|
34
|
+
: /* istanbul ignore next @preserve */ k;
|
|
33
35
|
|
|
34
36
|
dataOps[key] = normalizeValue(v) as T[keyof T];
|
|
35
37
|
});
|
|
@@ -39,13 +41,14 @@ const normalizeOptions = <T extends { [key: string]: any }>(
|
|
|
39
41
|
});
|
|
40
42
|
|
|
41
43
|
ObjectEntries(defaultOps).forEach(([k, v]) => {
|
|
42
|
-
|
|
44
|
+
// istanbul ignore else @preserve
|
|
43
45
|
if (k in INPUT) {
|
|
44
46
|
normalOps[k] = INPUT[k] as T[keyof T];
|
|
45
47
|
} else if (k in dataOps) {
|
|
46
48
|
normalOps[k] = dataOps[k] as T[keyof T];
|
|
47
49
|
} else {
|
|
48
|
-
normalOps[k] =
|
|
50
|
+
normalOps[k] =
|
|
51
|
+
(k === title ? getAttribute(element, title) : v) as T[keyof T];
|
|
49
52
|
}
|
|
50
53
|
});
|
|
51
54
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { Fn } from "../interface/fn";
|
|
2
|
+
|
|
3
|
+
type NormalValue = boolean | number | string | Fn | null;
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Utility to normalize component options
|
|
@@ -6,20 +8,20 @@ type NormalValue = boolean | number | string | ((...args: any[]) => any) | null;
|
|
|
6
8
|
* @param value the input value
|
|
7
9
|
* @return the normalized value
|
|
8
10
|
*/
|
|
9
|
-
const normalizeValue = (value?:
|
|
10
|
-
if ([
|
|
11
|
+
const normalizeValue = (value?: unknown): NormalValue => {
|
|
12
|
+
if (["true", true].includes(value as boolean)) {
|
|
11
13
|
return true;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
if ([
|
|
16
|
+
if (["false", false].includes(value as boolean)) {
|
|
15
17
|
return false;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
if ([
|
|
20
|
+
if (["null", "", null, undefined].includes(value as string | undefined)) {
|
|
19
21
|
return null;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
if (value !==
|
|
24
|
+
if (value !== "" && !Number.isNaN(+(value as string))) {
|
|
23
25
|
return +(value as string);
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import ObjectAssign from
|
|
2
|
-
import ObjectEntries from
|
|
3
|
-
import isString from
|
|
4
|
-
import type { CSS4Declaration } from
|
|
1
|
+
import ObjectAssign from "./ObjectAssign";
|
|
2
|
+
import ObjectEntries from "./ObjectEntries";
|
|
3
|
+
import isString from "../is/isString";
|
|
4
|
+
import type { CSS4Declaration } from "../interface/css4Declaration";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
|
|
@@ -9,9 +9,12 @@ import type { CSS4Declaration } from '../interface/css4Declaration';
|
|
|
9
9
|
* @param element target element
|
|
10
10
|
* @param styles attribute value
|
|
11
11
|
*/
|
|
12
|
-
const setElementStyle = (
|
|
12
|
+
const setElementStyle = (
|
|
13
|
+
element: HTMLElement,
|
|
14
|
+
styles: Partial<CSS4Declaration>,
|
|
15
|
+
): void => {
|
|
13
16
|
ObjectEntries(styles).forEach(([key, value]) => {
|
|
14
|
-
if (value && isString(key as string) && (key as string).includes(
|
|
17
|
+
if (value && isString(key as string) && (key as string).includes("--")) {
|
|
15
18
|
element.style.setProperty(key as string, value);
|
|
16
19
|
} else {
|
|
17
20
|
const propObject: Partial<CSS4Declaration> = {};
|
package/src/misc/timer.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import isMap from
|
|
2
|
-
import isHTMLElement from
|
|
3
|
-
import isNumber from
|
|
1
|
+
import isMap from "../is/isMap";
|
|
2
|
+
import isHTMLElement from "../is/isHTMLElement";
|
|
3
|
+
import isNumber from "../is/isNumber";
|
|
4
4
|
|
|
5
5
|
type KeyMap = Map<string, number>;
|
|
6
6
|
type TimeMap = Map<HTMLElement, number | KeyMap>;
|
|
@@ -20,12 +20,17 @@ const Timer = {
|
|
|
20
20
|
* @param delay the execution delay
|
|
21
21
|
* @param key a unique key
|
|
22
22
|
*/
|
|
23
|
-
set: (
|
|
23
|
+
set: (
|
|
24
|
+
element: HTMLElement,
|
|
25
|
+
callback: TimerHandler,
|
|
26
|
+
delay: number,
|
|
27
|
+
key?: string,
|
|
28
|
+
): void => {
|
|
24
29
|
if (!isHTMLElement(element)) return;
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
// istanbul ignore else @preserve
|
|
27
32
|
if (key && key.length) {
|
|
28
|
-
|
|
33
|
+
// istanbul ignore else @preserve
|
|
29
34
|
if (!TimeCache.has(element)) {
|
|
30
35
|
TimeCache.set(element, new Map());
|
|
31
36
|
}
|
|
@@ -49,7 +54,9 @@ const Timer = {
|
|
|
49
54
|
const keyTimers = TimeCache.get(element);
|
|
50
55
|
|
|
51
56
|
if (key && keyTimers && isMap(keyTimers as KeyMap)) {
|
|
52
|
-
return (keyTimers as KeyMap).get(key) ||
|
|
57
|
+
return (keyTimers as KeyMap).get(key) ||
|
|
58
|
+
/* istanbul ignore next @preserve */
|
|
59
|
+
null;
|
|
53
60
|
} else if (isNumber(keyTimers as number)) {
|
|
54
61
|
return keyTimers as number;
|
|
55
62
|
}
|
|
@@ -70,7 +77,7 @@ const Timer = {
|
|
|
70
77
|
if (key && key.length && isMap(keyTimers as KeyMap)) {
|
|
71
78
|
clearTimeout((keyTimers as KeyMap).get(key));
|
|
72
79
|
(keyTimers as KeyMap).delete(key);
|
|
73
|
-
|
|
80
|
+
// istanbul ignore else @preserve
|
|
74
81
|
if ((keyTimers as KeyMap).size === 0) {
|
|
75
82
|
TimeCache.delete(element);
|
|
76
83
|
}
|
package/src/selectors/closest.ts
CHANGED
|
@@ -15,8 +15,8 @@ const closest = <T extends Element = HTMLElement>(
|
|
|
15
15
|
): HTMLElement | null => {
|
|
16
16
|
return element
|
|
17
17
|
? element.closest(selector) ||
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// break out of `ShadowRoot`
|
|
19
|
+
closest((element.getRootNode() as ShadowRoot).host, selector)
|
|
20
20
|
: null;
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import isCustomElement from
|
|
2
|
-
import getElementsByTagName from
|
|
3
|
-
import type { CustomElement } from '../interface/customElement';
|
|
1
|
+
import isCustomElement from "../is/isCustomElement";
|
|
2
|
+
import getElementsByTagName from "./getElementsByTagName";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Returns an `Array` of `Node` elements that are registered as
|
|
@@ -11,8 +10,8 @@ import type { CustomElement } from '../interface/customElement';
|
|
|
11
10
|
* @param parent parent to look into
|
|
12
11
|
* @returns the query result
|
|
13
12
|
*/
|
|
14
|
-
const getCustomElements = (parent?: ParentNode)
|
|
15
|
-
const collection = getElementsByTagName(
|
|
13
|
+
const getCustomElements = (parent?: ParentNode) => {
|
|
14
|
+
const collection = getElementsByTagName("*", parent);
|
|
16
15
|
|
|
17
16
|
return [...collection].filter(isCustomElement);
|
|
18
17
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import getDocument from
|
|
1
|
+
import getDocument from "../get/getDocument";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns an `HTMLElement` that matches the id in the document.
|
|
@@ -9,8 +9,8 @@ import getDocument from '../get/getDocument';
|
|
|
9
9
|
* @param context an element in it's document or document
|
|
10
10
|
* @returns the requested element
|
|
11
11
|
*/
|
|
12
|
-
const getElementById = (id: string, context?: Node)
|
|
13
|
-
return getDocument(context).getElementById(id) || null;
|
|
12
|
+
const getElementById = <T extends HTMLElement>(id: string, context?: Node) => {
|
|
13
|
+
return (getDocument(context).getElementById(id) as T) || null;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export default getElementById;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import getDocument from
|
|
2
|
-
import isNode from
|
|
1
|
+
import getDocument from "../get/getDocument";
|
|
2
|
+
import isNode from "../is/isNode";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Shortcut for `HTMLElement.getElementsByClassName` method. Some `Node` elements
|
|
@@ -9,14 +9,14 @@ import isNode from '../is/isNode';
|
|
|
9
9
|
* @param parent optional Element to look into
|
|
10
10
|
* @return the 'HTMLCollection'
|
|
11
11
|
*/
|
|
12
|
-
const getElementsByClassName = (
|
|
12
|
+
const getElementsByClassName = <T extends HTMLElement>(
|
|
13
13
|
selector: string,
|
|
14
14
|
parent?: ParentNode,
|
|
15
|
-
)
|
|
15
|
+
) => {
|
|
16
16
|
const lookUp = parent && isNode(parent) ? parent : getDocument();
|
|
17
17
|
return (lookUp as HTMLElement | Document).getElementsByClassName(
|
|
18
18
|
selector,
|
|
19
|
-
) as HTMLCollectionOf<
|
|
19
|
+
) as HTMLCollectionOf<T>;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
export default getElementsByClassName;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import getDocument from
|
|
2
|
-
import isNode from
|
|
1
|
+
import getDocument from "../get/getDocument";
|
|
2
|
+
import isNode from "../is/isNode";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Shortcut for `HTMLElement.getElementsByTagName` method. Some `Node` elements
|
|
@@ -9,12 +9,14 @@ import isNode from '../is/isNode';
|
|
|
9
9
|
* @param parent optional Element to look into
|
|
10
10
|
* @return the 'HTMLCollection'
|
|
11
11
|
*/
|
|
12
|
-
const getElementsByTagName = (
|
|
12
|
+
const getElementsByTagName = <T extends HTMLElement>(
|
|
13
13
|
selector: string,
|
|
14
14
|
parent?: ParentNode,
|
|
15
|
-
)
|
|
15
|
+
) => {
|
|
16
16
|
const lookUp = isNode(parent) ? parent : getDocument();
|
|
17
|
-
return (lookUp as Document).getElementsByTagName(
|
|
17
|
+
return (lookUp as Document).getElementsByTagName(
|
|
18
|
+
selector,
|
|
19
|
+
) as HTMLCollectionOf<T>;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
export default getElementsByTagName;
|
package/src/selectors/matches.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @param selector the selector to match
|
|
6
6
|
* @returns the query result
|
|
7
7
|
*/
|
|
8
|
-
const matches = (target: Element, selector: string): boolean =>
|
|
8
|
+
const matches = (target: Element, selector: string): boolean =>
|
|
9
|
+
target.matches(selector);
|
|
9
10
|
|
|
10
11
|
export default matches;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import getDocument from
|
|
2
|
-
import isNode from
|
|
3
|
-
import isHTMLElement from
|
|
1
|
+
import getDocument from "../get/getDocument";
|
|
2
|
+
import isNode from "../is/isNode";
|
|
3
|
+
import isHTMLElement from "../is/isHTMLElement";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Utility to check if target is typeof `HTMLElement`, `Element`, `Node`
|
|
@@ -10,13 +10,16 @@ import isHTMLElement from '../is/isHTMLElement';
|
|
|
10
10
|
* @param parent optional node to look into
|
|
11
11
|
* @return the `HTMLElement` or `querySelector` result
|
|
12
12
|
*/
|
|
13
|
-
const querySelector =
|
|
13
|
+
const querySelector = <T extends HTMLElement>(
|
|
14
|
+
selector: HTMLElement | string,
|
|
15
|
+
parent?: ParentNode,
|
|
16
|
+
): T | null => {
|
|
14
17
|
if (isHTMLElement(selector)) {
|
|
15
|
-
return selector;
|
|
18
|
+
return selector as T;
|
|
16
19
|
}
|
|
17
20
|
const lookUp = isNode(parent) ? parent : getDocument();
|
|
18
21
|
|
|
19
|
-
return lookUp.querySelector(selector);
|
|
22
|
+
return lookUp.querySelector<T>(selector);
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
export default querySelector;
|