@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.
Files changed (225) hide show
  1. package/README.md +5 -5
  2. package/dist/shorty.cjs +18 -1
  3. package/dist/shorty.cjs.map +1 -1
  4. package/dist/shorty.d.ts +31 -18
  5. package/dist/shorty.js +18 -1
  6. package/dist/shorty.js.map +1 -1
  7. package/dist/shorty.mjs +451 -386
  8. package/dist/shorty.mjs.map +1 -1
  9. package/dts.config.ts +1 -7
  10. package/package.json +16 -28
  11. package/src/attr/hasAttribute.ts +2 -1
  12. package/src/attr/hasAttributeNS.ts +5 -2
  13. package/src/attr/removeAttribute.ts +2 -1
  14. package/src/attr/removeAttributeNS.ts +5 -2
  15. package/src/attr/setAttributeNS.ts +6 -2
  16. package/src/boolean/isApple.ts +6 -4
  17. package/src/boolean/isFirefox.ts +2 -2
  18. package/src/boolean/isMobile.ts +4 -4
  19. package/src/boolean/support3DTransform.ts +4 -2
  20. package/src/boolean/supportAnimation.ts +4 -2
  21. package/src/boolean/supportPassive.ts +6 -6
  22. package/src/boolean/supportTouch.ts +3 -2
  23. package/src/boolean/supportTransform.ts +4 -2
  24. package/src/boolean/supportTransition.ts +4 -2
  25. package/src/event/off.ts +5 -1
  26. package/src/event/on.ts +5 -1
  27. package/src/event/one.ts +5 -5
  28. package/src/get/getBoundingClientRect.ts +10 -5
  29. package/src/get/getDocument.ts +4 -4
  30. package/src/get/getDocumentBody.ts +1 -1
  31. package/src/get/getDocumentElement.ts +1 -1
  32. package/src/get/getDocumentHead.ts +4 -2
  33. package/src/get/getElementAnimationDelay.ts +9 -6
  34. package/src/get/getElementAnimationDuration.ts +9 -6
  35. package/src/get/getElementStyle.ts +2 -2
  36. package/src/get/getElementTransitionDelay.ts +9 -8
  37. package/src/get/getElementTransitionDuration.ts +9 -8
  38. package/src/get/getNodeScroll.ts +4 -2
  39. package/src/get/getParentNode.ts +5 -5
  40. package/src/get/getRectRelativeToOffsetParent.ts +9 -6
  41. package/src/get/getUID.ts +1 -1
  42. package/src/get/getWindow.ts +2 -2
  43. package/src/index.ts +425 -415
  44. package/src/interface/event.d.ts +49 -41
  45. package/src/interface/fn.ts +1 -0
  46. package/src/interface/originalEvent.d.ts +1 -1
  47. package/src/is/isArray.ts +2 -1
  48. package/src/is/isCanvas.ts +2 -2
  49. package/src/is/isCustomElement.ts +5 -3
  50. package/src/is/isDocument.ts +3 -2
  51. package/src/is/isElement.ts +3 -2
  52. package/src/is/isElementInScrollRange.ts +3 -3
  53. package/src/is/isElementInViewport.ts +5 -4
  54. package/src/is/isElementsArray.ts +2 -2
  55. package/src/is/isFunction.ts +4 -2
  56. package/src/is/isHTMLCollection.ts +2 -2
  57. package/src/is/isHTMLElement.ts +1 -1
  58. package/src/is/isHTMLImageElement.ts +2 -2
  59. package/src/is/isJSON.ts +2 -2
  60. package/src/is/isMap.ts +3 -3
  61. package/src/is/isMedia.ts +9 -3
  62. package/src/is/isNode.ts +5 -3
  63. package/src/is/isNodeList.ts +2 -2
  64. package/src/is/isNumber.ts +2 -1
  65. package/src/is/isObject.ts +1 -1
  66. package/src/is/isRTL.ts +2 -2
  67. package/src/is/isSVGElement.ts +2 -2
  68. package/src/is/isScaledElement.ts +4 -3
  69. package/src/is/isShadowRoot.ts +2 -2
  70. package/src/is/isString.ts +2 -1
  71. package/src/is/isTableElement.ts +6 -3
  72. package/src/is/isWeakMap.ts +3 -3
  73. package/src/is/isWindow.ts +2 -2
  74. package/src/misc/Float32ArrayFrom.ts +3 -2
  75. package/src/misc/Float64ArrayFrom.ts +3 -2
  76. package/src/misc/ObjectAssign.ts +15 -4
  77. package/src/misc/ObjectEntries.ts +1 -1
  78. package/src/misc/ObjectHasOwn.ts +3 -2
  79. package/src/misc/ObjectKeys.ts +2 -1
  80. package/src/misc/ObjectValues.ts +3 -2
  81. package/src/misc/createCustomEvent.ts +8 -5
  82. package/src/misc/createElement.ts +6 -4
  83. package/src/misc/createElementNS.ts +3 -3
  84. package/src/misc/data.ts +4 -7
  85. package/src/misc/dispatchEvent.ts +2 -1
  86. package/src/misc/distinct.ts +2 -1
  87. package/src/misc/emulateAnimationEnd.ts +12 -9
  88. package/src/misc/emulateTransitionEnd.ts +11 -8
  89. package/src/misc/focus.ts +2 -1
  90. package/src/misc/focusTrap.ts +67 -0
  91. package/src/misc/getInstance.ts +1 -1
  92. package/src/misc/normalizeOptions.ts +16 -13
  93. package/src/misc/normalizeValue.ts +8 -6
  94. package/src/misc/setElementStyle.ts +9 -6
  95. package/src/misc/timer.ts +15 -8
  96. package/src/selectors/closest.ts +2 -2
  97. package/src/selectors/getCustomElements.ts +4 -5
  98. package/src/selectors/getElementById.ts +3 -3
  99. package/src/selectors/getElementsByClassName.ts +5 -5
  100. package/src/selectors/getElementsByTagName.ts +7 -5
  101. package/src/selectors/matches.ts +2 -1
  102. package/src/selectors/querySelector.ts +9 -6
  103. package/src/selectors/querySelectorAll.ts +6 -3
  104. package/src/strings/DOMContentLoadedEvent.ts +1 -1
  105. package/src/strings/DOMMouseScrollEvent.ts +1 -1
  106. package/src/strings/abortEvent.ts +1 -1
  107. package/src/strings/addEventListener.ts +1 -1
  108. package/src/strings/animationDelay.ts +1 -1
  109. package/src/strings/animationDuration.ts +1 -1
  110. package/src/strings/animationEndEvent.ts +1 -1
  111. package/src/strings/animationName.ts +1 -1
  112. package/src/strings/ariaChecked.ts +1 -1
  113. package/src/strings/ariaDescribedBy.ts +1 -1
  114. package/src/strings/ariaDescription.ts +1 -1
  115. package/src/strings/ariaExpanded.ts +1 -1
  116. package/src/strings/ariaHasPopup.ts +1 -1
  117. package/src/strings/ariaHidden.ts +1 -1
  118. package/src/strings/ariaLabel.ts +1 -1
  119. package/src/strings/ariaLabelledBy.ts +1 -1
  120. package/src/strings/ariaModal.ts +1 -1
  121. package/src/strings/ariaPressed.ts +1 -1
  122. package/src/strings/ariaSelected.ts +1 -1
  123. package/src/strings/ariaValueMax.ts +1 -1
  124. package/src/strings/ariaValueMin.ts +1 -1
  125. package/src/strings/ariaValueNow.ts +1 -1
  126. package/src/strings/ariaValueText.ts +1 -1
  127. package/src/strings/beforeunloadEvent.ts +1 -1
  128. package/src/strings/bezierEasings.ts +25 -25
  129. package/src/strings/blurEvent.ts +1 -1
  130. package/src/strings/changeEvent.ts +1 -1
  131. package/src/strings/contextmenuEvent.ts +1 -1
  132. package/src/strings/dragEvent.ts +1 -1
  133. package/src/strings/dragendEvent.ts +1 -1
  134. package/src/strings/dragenterEvent.ts +1 -1
  135. package/src/strings/dragleaveEvent.ts +1 -1
  136. package/src/strings/dragoverEvent.ts +1 -1
  137. package/src/strings/dragstartEvent.ts +1 -1
  138. package/src/strings/errorEvent.ts +1 -1
  139. package/src/strings/focusEvent.ts +1 -1
  140. package/src/strings/focusEvents.ts +1 -1
  141. package/src/strings/focusableSelector.ts +4 -0
  142. package/src/strings/focusinEvent.ts +1 -1
  143. package/src/strings/focusoutEvent.ts +1 -1
  144. package/src/strings/gesturechangeEvent.ts +1 -1
  145. package/src/strings/gestureendEvent.ts +1 -1
  146. package/src/strings/gesturestartEvent.ts +1 -1
  147. package/src/strings/keyAlt.ts +1 -1
  148. package/src/strings/keyArrowDown.ts +1 -1
  149. package/src/strings/keyArrowLeft.ts +1 -1
  150. package/src/strings/keyArrowRight.ts +1 -1
  151. package/src/strings/keyArrowUp.ts +1 -1
  152. package/src/strings/keyBackspace.ts +1 -1
  153. package/src/strings/keyCapsLock.ts +1 -1
  154. package/src/strings/keyControl.ts +1 -1
  155. package/src/strings/keyDelete.ts +1 -1
  156. package/src/strings/keyEnter.ts +1 -1
  157. package/src/strings/keyEscape.ts +1 -1
  158. package/src/strings/keyInsert.ts +1 -1
  159. package/src/strings/keyMeta.ts +1 -1
  160. package/src/strings/keyNumpadEnter.ts +1 -1
  161. package/src/strings/keyPause.ts +1 -1
  162. package/src/strings/keyScrollLock.ts +1 -1
  163. package/src/strings/keyShift.ts +1 -1
  164. package/src/strings/keySpace.ts +1 -1
  165. package/src/strings/keyTab.ts +1 -1
  166. package/src/strings/keyboardEventKeys.ts +19 -19
  167. package/src/strings/keydownEvent.ts +1 -1
  168. package/src/strings/keypressEvent.ts +1 -1
  169. package/src/strings/keyupEvent.ts +1 -1
  170. package/src/strings/loadEvent.ts +1 -1
  171. package/src/strings/loadstartEvent.ts +1 -1
  172. package/src/strings/mouseClickEvents.ts +1 -1
  173. package/src/strings/mouseHoverEvents.ts +3 -4
  174. package/src/strings/mouseSwipeEvents.ts +4 -4
  175. package/src/strings/mouseclickEvent.ts +1 -1
  176. package/src/strings/mousedblclickEvent.ts +1 -1
  177. package/src/strings/mousedownEvent.ts +1 -1
  178. package/src/strings/mouseenterEvent.ts +1 -1
  179. package/src/strings/mousehoverEvent.ts +1 -1
  180. package/src/strings/mouseinEvent.ts +1 -1
  181. package/src/strings/mouseleaveEvent.ts +1 -1
  182. package/src/strings/mousemoveEvent.ts +1 -1
  183. package/src/strings/mouseoutEvent.ts +1 -1
  184. package/src/strings/mouseoverEvent.ts +1 -1
  185. package/src/strings/mouseupEvent.ts +1 -1
  186. package/src/strings/mousewheelEvent.ts +1 -1
  187. package/src/strings/moveEvent.ts +1 -1
  188. package/src/strings/nativeEvents.ts +50 -50
  189. package/src/strings/offsetHeight.ts +1 -1
  190. package/src/strings/offsetWidth.ts +1 -1
  191. package/src/strings/orientationchangeEvent.ts +1 -1
  192. package/src/strings/pointercancelEvent.ts +1 -1
  193. package/src/strings/pointerdownEvent.ts +1 -1
  194. package/src/strings/pointerleaveEvent.ts +1 -1
  195. package/src/strings/pointermoveEvent.ts +1 -1
  196. package/src/strings/pointerupEvent.ts +1 -1
  197. package/src/strings/readystatechangeEvent.ts +1 -1
  198. package/src/strings/removeEventListener.ts +1 -1
  199. package/src/strings/resetEvent.ts +1 -1
  200. package/src/strings/resizeEvent.ts +1 -1
  201. package/src/strings/scrollEvent.ts +1 -1
  202. package/src/strings/scrollHeight.ts +1 -1
  203. package/src/strings/scrollWidth.ts +1 -1
  204. package/src/strings/selectEvent.ts +1 -1
  205. package/src/strings/selectendEvent.ts +1 -1
  206. package/src/strings/selectstartEvent.ts +1 -1
  207. package/src/strings/submitEvent.ts +1 -1
  208. package/src/strings/tabindex.ts +1 -1
  209. package/src/strings/touchEvents.ts +4 -4
  210. package/src/strings/touchcancelEvent.ts +1 -1
  211. package/src/strings/touchendEvent.ts +1 -1
  212. package/src/strings/touchmoveEvent.ts +1 -1
  213. package/src/strings/touchstartEvent.ts +1 -1
  214. package/src/strings/transitionDelay.ts +1 -1
  215. package/src/strings/transitionDuration.ts +1 -1
  216. package/src/strings/transitionEndEvent.ts +1 -1
  217. package/src/strings/transitionProperty.ts +1 -1
  218. package/src/strings/unloadEvent.ts +1 -1
  219. package/src/strings/userAgentData.ts +2 -2
  220. package/test/fixtures/getExampleDom.ts +12 -8
  221. package/test/is.test.ts +15 -11
  222. package/test/misc.test.ts +65 -17
  223. package/{vite.config.ts → vite.config.mts} +4 -8
  224. package/{vitest.config-ui.ts → vitest.config-ui.mts} +5 -0
  225. package/{vitest.config.ts → vitest.config.mts} +5 -0
@@ -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<string, any>>(obj: O) => Object.keys(obj) as (keyof O)[];
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;
@@ -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>>(obj: O): O[keyof O][] =>
9
- Object.values(obj) as O[keyof O][];
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 '../interface/originalEvent';
2
- import isObject from '../is/isObject';
3
- import ObjectAssign from './ObjectAssign';
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 = <O extends Record<string, unknown>, T extends OriginalEvent>(
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
- /* istanbul ignore else @preserve */
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 '../is/isString';
2
- import getDocument from '../get/getDocument';
3
- import ObjectAssign from './ObjectAssign';
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>(param?: string | Partial<T>): T | undefined => {
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 '../get/getDocument';
2
- import ObjectAssign from './ObjectAssign';
3
- import isString from '../is/isString';
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 '../is/isHTMLElement';
1
+ import isHTMLElement from "../is/isHTMLElement";
2
2
 
3
- const componentData = new Map<string, Map<HTMLElement, any>>();
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
- /* istanbul ignore else */
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
- /* istanbul ignore else */
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 => element.dispatchEvent(event);
7
+ const dispatchEvent = (element: EventTarget, event: Event): boolean =>
8
+ element.dispatchEvent(event);
8
9
 
9
10
  export default dispatchEvent;
@@ -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 => arr.indexOf(value) === index;
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 '../strings/animationEndEvent';
2
- import getElementAnimationDelay from '../get/getElementAnimationDelay';
3
- import getElementAnimationDuration from '../get/getElementAnimationDuration';
4
- import dispatchEvent from './dispatchEvent';
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 = (element: HTMLElement, handler: EventListener): void => {
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
- /** Wrap the handler in on -> off callback */
23
+ // Wrap the handler in on -> off callback
21
24
  const animationEndWrapper = (e: Event): void => {
22
- /* istanbul ignore else @preserve */
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
- /* istanbul ignore next @preserve */
34
+ // istanbul ignore next @preserve
32
35
  if (!called) dispatchEvent(element, endEvent);
33
36
  }, duration + delay + 17);
34
37
  } else {
35
- /* istanbul ignore next @preserve */
38
+ // istanbul ignore next @preserve
36
39
  handler.apply(element, [endEvent]);
37
40
  }
38
41
  };
@@ -1,7 +1,7 @@
1
- import transitionEndEvent from '../strings/transitionEndEvent';
2
- import getElementTransitionDelay from '../get/getElementTransitionDelay';
3
- import getElementTransitionDuration from '../get/getElementTransitionDuration';
4
- import dispatchEvent from './dispatchEvent';
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 = (element: HTMLElement, handler: EventListener): void => {
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
- /** Wrap the handler in on -> off callback */
23
+ // Wrap the handler in on -> off callback
21
24
  const transitionEndWrapper = (e: Event): void => {
22
- /* istanbul ignore else @preserve */
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
- /* istanbul ignore next @preserve */
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 => element.focus(options);
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
+ };
@@ -1,4 +1,4 @@
1
- import Data from './data';
1
+ import Data from "./data";
2
2
 
3
3
  /**
4
4
  * An alias for `Data.get()`.
@@ -1,7 +1,7 @@
1
- import getAttribute from '../attr/getAttribute';
2
- import normalizeValue from './normalizeValue';
3
- import ObjectEntries from './ObjectEntries';
4
- import toLowerCase from './toLowerCase';
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]: any }>(
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 = 'title';
26
+ const title = "title";
27
27
 
28
28
  ObjectEntries(data).forEach(([k, v]) => {
29
- const key: keyof T =
30
- ns && typeof k === 'string' && k.includes(ns)
31
- ? k.replace(ns, '').replace(/[A-Z]/g, (match: string) => toLowerCase(match))
32
- : k;
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
- /* istanbul ignore else */
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] = (k === title ? getAttribute(element, title) : v) as T[keyof T];
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
- type NormalValue = boolean | number | string | ((...args: any[]) => any) | null;
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?: boolean | number | string): NormalValue => {
10
- if (['true', true].includes(value as boolean)) {
11
+ const normalizeValue = (value?: unknown): NormalValue => {
12
+ if (["true", true].includes(value as boolean)) {
11
13
  return true;
12
14
  }
13
15
 
14
- if (['false', false].includes(value as boolean)) {
16
+ if (["false", false].includes(value as boolean)) {
15
17
  return false;
16
18
  }
17
19
 
18
- if (['null', '', null, undefined].includes(value as string | undefined)) {
20
+ if (["null", "", null, undefined].includes(value as string | undefined)) {
19
21
  return null;
20
22
  }
21
23
 
22
- if (value !== '' && !Number.isNaN(+(value as string))) {
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 './ObjectAssign';
2
- import ObjectEntries from './ObjectEntries';
3
- import isString from '../is/isString';
4
- import type { CSS4Declaration } from '../interface/css4Declaration';
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 = (element: HTMLElement, styles: Partial<CSS4Declaration>): void => {
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 '../is/isMap';
2
- import isHTMLElement from '../is/isHTMLElement';
3
- import isNumber from '../is/isNumber';
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: (element: HTMLElement, callback: TimerHandler, delay: number, key?: string): void => {
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
- /* istanbul ignore else */
31
+ // istanbul ignore else @preserve
27
32
  if (key && key.length) {
28
- /* istanbul ignore else */
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) || /* istanbul ignore next */ null;
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
- /* istanbul ignore else */
80
+ // istanbul ignore else @preserve
74
81
  if ((keyTimers as KeyMap).size === 0) {
75
82
  TimeCache.delete(element);
76
83
  }
@@ -15,8 +15,8 @@ const closest = <T extends Element = HTMLElement>(
15
15
  ): HTMLElement | null => {
16
16
  return element
17
17
  ? element.closest(selector) ||
18
- // break out of `ShadowRoot`
19
- closest((element.getRootNode() as ShadowRoot).host, selector)
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 '../is/isCustomElement';
2
- import getElementsByTagName from './getElementsByTagName';
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): CustomElement[] => {
15
- const collection = getElementsByTagName('*', parent);
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 '../get/getDocument';
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): HTMLElement | null => {
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 '../get/getDocument';
2
- import isNode from '../is/isNode';
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
- ): HTMLCollectionOf<HTMLElement> => {
15
+ ) => {
16
16
  const lookUp = parent && isNode(parent) ? parent : getDocument();
17
17
  return (lookUp as HTMLElement | Document).getElementsByClassName(
18
18
  selector,
19
- ) as HTMLCollectionOf<HTMLElement>;
19
+ ) as HTMLCollectionOf<T>;
20
20
  };
21
21
 
22
22
  export default getElementsByClassName;
@@ -1,5 +1,5 @@
1
- import getDocument from '../get/getDocument';
2
- import isNode from '../is/isNode';
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
- ): HTMLCollectionOf<HTMLElement> => {
15
+ ) => {
16
16
  const lookUp = isNode(parent) ? parent : getDocument();
17
- return (lookUp as Document).getElementsByTagName(selector) as HTMLCollectionOf<HTMLElement>;
17
+ return (lookUp as Document).getElementsByTagName(
18
+ selector,
19
+ ) as HTMLCollectionOf<T>;
18
20
  };
19
21
 
20
22
  export default getElementsByTagName;
@@ -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 => target.matches(selector);
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 '../get/getDocument';
2
- import isNode from '../is/isNode';
3
- import isHTMLElement from '../is/isHTMLElement';
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 = (selector: HTMLElement | string, parent?: ParentNode): HTMLElement | null => {
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;