@thednp/shorty 1.0.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.
Files changed (234) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +340 -0
  3. package/dist/shorty.esm.js +2540 -0
  4. package/dist/shorty.esm.min.js +2 -0
  5. package/dist/shorty.js +2563 -0
  6. package/dist/shorty.min.js +2 -0
  7. package/package.json +59 -0
  8. package/src/attr/getAttribute.js +9 -0
  9. package/src/attr/getAttributeNS.js +10 -0
  10. package/src/attr/hasAttribute.js +9 -0
  11. package/src/attr/hasAttributeNS.js +10 -0
  12. package/src/attr/removeAttribute.js +9 -0
  13. package/src/attr/removeAttributeNS.js +10 -0
  14. package/src/attr/setAttribute.js +10 -0
  15. package/src/attr/setAttributeNS.js +11 -0
  16. package/src/blocks/documentBody.js +5 -0
  17. package/src/blocks/documentElement.js +5 -0
  18. package/src/blocks/documentHead.js +5 -0
  19. package/src/boolean/isApple.js +13 -0
  20. package/src/boolean/isFirefox.js +9 -0
  21. package/src/boolean/isMobile.js +20 -0
  22. package/src/boolean/support3DTransform.js +8 -0
  23. package/src/boolean/supportAnimation.js +8 -0
  24. package/src/boolean/supportPassive.js +28 -0
  25. package/src/boolean/supportTouch.js +6 -0
  26. package/src/boolean/supportTransform.js +8 -0
  27. package/src/boolean/supportTransition.js +8 -0
  28. package/src/class/addClass.js +10 -0
  29. package/src/class/hasClass.js +10 -0
  30. package/src/class/removeClass.js +10 -0
  31. package/src/event/off.js +13 -0
  32. package/src/event/on.js +13 -0
  33. package/src/event/one.js +26 -0
  34. package/src/get/getBoundingClientRect.js +33 -0
  35. package/src/get/getDocument.js +11 -0
  36. package/src/get/getDocumentBody.js +10 -0
  37. package/src/get/getDocumentElement.js +11 -0
  38. package/src/get/getDocumentHead.js +10 -0
  39. package/src/get/getElementAnimationDelay.js +21 -0
  40. package/src/get/getElementAnimationDelayLegacy.js +21 -0
  41. package/src/get/getElementAnimationDuration.js +20 -0
  42. package/src/get/getElementAnimationDurationLegacy.js +21 -0
  43. package/src/get/getElementStyle.js +18 -0
  44. package/src/get/getElementTransitionDelay.js +21 -0
  45. package/src/get/getElementTransitionDelayLegacy.js +21 -0
  46. package/src/get/getElementTransitionDuration.js +20 -0
  47. package/src/get/getElementTransitionDurationLegacy.js +21 -0
  48. package/src/get/getNodeScroll.js +16 -0
  49. package/src/get/getParentNode.js +24 -0
  50. package/src/get/getRectRelativeToOffsetParent.js +30 -0
  51. package/src/get/getUID.js +34 -0
  52. package/src/get/getWindow.js +20 -0
  53. package/src/index.js +475 -0
  54. package/src/is/isArray.js +9 -0
  55. package/src/is/isCustomElement.js +8 -0
  56. package/src/is/isDocument.js +9 -0
  57. package/src/is/isElement.js +9 -0
  58. package/src/is/isElementInScrollRange.js +18 -0
  59. package/src/is/isElementInViewport.js +22 -0
  60. package/src/is/isElementsArray.js +10 -0
  61. package/src/is/isFunction.js +9 -0
  62. package/src/is/isHTMLCollection.js +8 -0
  63. package/src/is/isHTMLElement.js +8 -0
  64. package/src/is/isHTMLImageElement.js +8 -0
  65. package/src/is/isMedia.js +12 -0
  66. package/src/is/isNode.js +8 -0
  67. package/src/is/isNodeList.js +8 -0
  68. package/src/is/isRTL.js +9 -0
  69. package/src/is/isSVGElement.js +8 -0
  70. package/src/is/isScaledElement.js +15 -0
  71. package/src/is/isShadowRoot.js +12 -0
  72. package/src/is/isString.js +8 -0
  73. package/src/is/isTableElement.js +7 -0
  74. package/src/is/isWindow.js +9 -0
  75. package/src/misc/ArrayFrom.js +8 -0
  76. package/src/misc/Float32ArrayFrom.js +7 -0
  77. package/src/misc/Float64ArrayFrom.js +7 -0
  78. package/src/misc/ObjectAssign.js +7 -0
  79. package/src/misc/ObjectKeys.js +7 -0
  80. package/src/misc/ObjectValues.js +7 -0
  81. package/src/misc/OriginalEvent.js +18 -0
  82. package/src/misc/createElement.js +25 -0
  83. package/src/misc/createElementNS.js +26 -0
  84. package/src/misc/data.js +78 -0
  85. package/src/misc/dispatchEvent.js +8 -0
  86. package/src/misc/distinct.js +10 -0
  87. package/src/misc/emulateAnimationEnd.js +37 -0
  88. package/src/misc/emulateAnimationEndLegacy.js +38 -0
  89. package/src/misc/emulateTransitionEnd.js +37 -0
  90. package/src/misc/emulateTransitionEndLegacy.js +38 -0
  91. package/src/misc/focus.js +8 -0
  92. package/src/misc/noop.js +3 -0
  93. package/src/misc/normalizeOptions.js +49 -0
  94. package/src/misc/normalizeValue.js +32 -0
  95. package/src/misc/passiveHandler.js +7 -0
  96. package/src/misc/passiveHandlerLegacy.js +9 -0
  97. package/src/misc/reflow.js +9 -0
  98. package/src/misc/setElementStyle.js +10 -0
  99. package/src/misc/timer.js +78 -0
  100. package/src/misc/toLowerCase.js +8 -0
  101. package/src/misc/toUpperCase.js +8 -0
  102. package/src/misc/tryWrapper.js +11 -0
  103. package/src/misc/version.js +9 -0
  104. package/src/selectors/closest.js +16 -0
  105. package/src/selectors/documentAll.js +8 -0
  106. package/src/selectors/elementNodes.js +5 -0
  107. package/src/selectors/getCustomElements.js +18 -0
  108. package/src/selectors/getElementById.js +11 -0
  109. package/src/selectors/getElementsByClassName.js +15 -0
  110. package/src/selectors/getElementsByTagName.js +15 -0
  111. package/src/selectors/matches.js +10 -0
  112. package/src/selectors/matchesLegacy.js +23 -0
  113. package/src/selectors/parentNodes.js +5 -0
  114. package/src/selectors/querySelector.js +15 -0
  115. package/src/selectors/querySelectorAll.js +14 -0
  116. package/src/strings/DOMContentLoadedEvent.js +6 -0
  117. package/src/strings/DOMMouseScrollEvent.js +6 -0
  118. package/src/strings/abortEvent.js +6 -0
  119. package/src/strings/addEventListener.js +6 -0
  120. package/src/strings/animationDelay.js +6 -0
  121. package/src/strings/animationDelayLegacy.js +8 -0
  122. package/src/strings/animationDuration.js +6 -0
  123. package/src/strings/animationDurationLegacy.js +8 -0
  124. package/src/strings/animationEndEvent.js +6 -0
  125. package/src/strings/animationEndEventLegacy.js +8 -0
  126. package/src/strings/animationName.js +6 -0
  127. package/src/strings/animationNameLegacy.js +8 -0
  128. package/src/strings/ariaChecked.js +6 -0
  129. package/src/strings/ariaDescribedBy.js +6 -0
  130. package/src/strings/ariaDescription.js +6 -0
  131. package/src/strings/ariaExpanded.js +6 -0
  132. package/src/strings/ariaHasPopup.js +6 -0
  133. package/src/strings/ariaHidden.js +6 -0
  134. package/src/strings/ariaLabel.js +6 -0
  135. package/src/strings/ariaLabelledBy.js +6 -0
  136. package/src/strings/ariaModal.js +6 -0
  137. package/src/strings/ariaPressed.js +6 -0
  138. package/src/strings/ariaSelected.js +6 -0
  139. package/src/strings/ariaValueMax.js +6 -0
  140. package/src/strings/ariaValueMin.js +6 -0
  141. package/src/strings/ariaValueNow.js +6 -0
  142. package/src/strings/ariaValueText.js +6 -0
  143. package/src/strings/beforeunloadEvent.js +6 -0
  144. package/src/strings/bezierEasings.js +33 -0
  145. package/src/strings/blurEvent.js +6 -0
  146. package/src/strings/changeEvent.js +6 -0
  147. package/src/strings/contextmenuEvent.js +6 -0
  148. package/src/strings/errorEvent.js +6 -0
  149. package/src/strings/focusEvent.js +6 -0
  150. package/src/strings/focusEvents.js +6 -0
  151. package/src/strings/focusinEvent.js +6 -0
  152. package/src/strings/focusoutEvent.js +6 -0
  153. package/src/strings/gesturechangeEvent.js +6 -0
  154. package/src/strings/gestureendEvent.js +6 -0
  155. package/src/strings/gesturestartEvent.js +6 -0
  156. package/src/strings/keyAlt.js +7 -0
  157. package/src/strings/keyArrowDown.js +7 -0
  158. package/src/strings/keyArrowLeft.js +7 -0
  159. package/src/strings/keyArrowRight.js +7 -0
  160. package/src/strings/keyArrowUp.js +7 -0
  161. package/src/strings/keyBackspace.js +7 -0
  162. package/src/strings/keyCapsLock.js +7 -0
  163. package/src/strings/keyControl.js +7 -0
  164. package/src/strings/keyDelete.js +7 -0
  165. package/src/strings/keyEnter.js +7 -0
  166. package/src/strings/keyEscape.js +7 -0
  167. package/src/strings/keyInsert.js +7 -0
  168. package/src/strings/keyMeta.js +7 -0
  169. package/src/strings/keyPause.js +7 -0
  170. package/src/strings/keyScrollLock.js +7 -0
  171. package/src/strings/keyShift.js +7 -0
  172. package/src/strings/keySpace.js +7 -0
  173. package/src/strings/keyTab.js +7 -0
  174. package/src/strings/keyboardEventKeys.js +27 -0
  175. package/src/strings/keydownEvent.js +6 -0
  176. package/src/strings/keypressEvent.js +6 -0
  177. package/src/strings/keyupEvent.js +6 -0
  178. package/src/strings/loadEvent.js +6 -0
  179. package/src/strings/loadstartEvent.js +6 -0
  180. package/src/strings/mouseClickEvents.js +6 -0
  181. package/src/strings/mouseHoverEvents.js +6 -0
  182. package/src/strings/mouseSwipeEvents.js +8 -0
  183. package/src/strings/mouseclickEvent.js +6 -0
  184. package/src/strings/mousedblclickEvent.js +6 -0
  185. package/src/strings/mousedownEvent.js +6 -0
  186. package/src/strings/mouseenterEvent.js +6 -0
  187. package/src/strings/mousehoverEvent.js +6 -0
  188. package/src/strings/mouseinEvent.js +6 -0
  189. package/src/strings/mouseleaveEvent.js +6 -0
  190. package/src/strings/mousemoveEvent.js +6 -0
  191. package/src/strings/mouseoutEvent.js +6 -0
  192. package/src/strings/mouseoverEvent.js +6 -0
  193. package/src/strings/mouseupEvent.js +6 -0
  194. package/src/strings/mousewheelEvent.js +6 -0
  195. package/src/strings/moveEvent.js +6 -0
  196. package/src/strings/nativeEvents.js +108 -0
  197. package/src/strings/offsetHeight.js +6 -0
  198. package/src/strings/offsetWidth.js +6 -0
  199. package/src/strings/orientationchangeEvent.js +6 -0
  200. package/src/strings/pointercancelEvent.js +6 -0
  201. package/src/strings/pointerdownEvent.js +6 -0
  202. package/src/strings/pointerleaveEvent.js +6 -0
  203. package/src/strings/pointermoveEvent.js +6 -0
  204. package/src/strings/pointerupEvent.js +6 -0
  205. package/src/strings/readystatechangeEvent.js +6 -0
  206. package/src/strings/removeEventListener.js +6 -0
  207. package/src/strings/resetEvent.js +6 -0
  208. package/src/strings/resizeEvent.js +6 -0
  209. package/src/strings/scrollEvent.js +6 -0
  210. package/src/strings/scrollHeight.js +6 -0
  211. package/src/strings/scrollWidth.js +6 -0
  212. package/src/strings/selectEvent.js +6 -0
  213. package/src/strings/selectendEvent.js +6 -0
  214. package/src/strings/selectstartEvent.js +6 -0
  215. package/src/strings/submitEvent.js +6 -0
  216. package/src/strings/touchEvents.js +8 -0
  217. package/src/strings/touchcancelEvent.js +6 -0
  218. package/src/strings/touchendEvent.js +6 -0
  219. package/src/strings/touchmoveEvent.js +6 -0
  220. package/src/strings/touchstartEvent.js +6 -0
  221. package/src/strings/transitionDelay.js +6 -0
  222. package/src/strings/transitionDelayLegacy.js +8 -0
  223. package/src/strings/transitionDuration.js +6 -0
  224. package/src/strings/transitionDurationLegacy.js +8 -0
  225. package/src/strings/transitionEndEvent.js +6 -0
  226. package/src/strings/transitionEndEventLegacy.js +8 -0
  227. package/src/strings/transitionProperty.js +7 -0
  228. package/src/strings/transitionPropertyLegacy.js +12 -0
  229. package/src/strings/unloadEvent.js +6 -0
  230. package/src/strings/userAgent.js +7 -0
  231. package/src/strings/userAgentData.js +8 -0
  232. package/types/index.d.ts +252 -0
  233. package/types/module/shorty.ts +249 -0
  234. package/types/shorty.d.ts +2317 -0
@@ -0,0 +1,12 @@
1
+ import getWindow from '../get/getWindow';
2
+ /**
3
+ * Check if target is a `ShadowRoot`.
4
+ *
5
+ * @param {any} element target
6
+ * @returns {boolean} the query result
7
+ */
8
+ const isShadowRoot = (element) => {
9
+ const OwnElement = getWindow(element).ShadowRoot;
10
+ return element instanceof OwnElement || element instanceof ShadowRoot;
11
+ };
12
+ export default isShadowRoot;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shortcut for `typeof SOMETHING === string` static method.
3
+ *
4
+ * @param {any} str array-like iterable object
5
+ * @returns {boolean} the query result
6
+ */
7
+ const isString = (str) => typeof str === 'string';
8
+ export default isString;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Check if a target element is a `<table>`, `<td>` or `<th>`.
3
+ * @param {any} element the target element
4
+ * @returns {boolean} the query result
5
+ */
6
+ const isTableElement = (element) => ['TABLE', 'TD', 'TH'].includes(element.tagName);
7
+ export default isTableElement;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Check if a target node is `window`.
3
+ *
4
+ * @param {any} node the target node
5
+ * @returns {boolean} the query result
6
+ */
7
+ export default function isWindow(node) {
8
+ return node instanceof Window;
9
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shortcut for `Array.from()` static method.
3
+ *
4
+ * @param {any[] | HTMLCollection | NodeList | Map<any, any>} arr array-like iterable object
5
+ * @returns {Array<any>}
6
+ */
7
+ const ArrayFrom = (arr) => Array.from(arr);
8
+ export default ArrayFrom;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Shortcut for `Float32Array.from()` static method.
3
+ * @param {any[] | HTMLCollection | NodeList} arr array-like iterable object
4
+ * @returns {Float32Array}
5
+ */
6
+ const Float32ArrayFrom = (arr) => Float32Array.from(Array.from(arr));
7
+ export default Float32ArrayFrom;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Shortcut for `Float64Array.from()` static method.
3
+ * @param {any[] | HTMLCollection | NodeList} arr array-like iterable object
4
+ * @returns {Float64Array}
5
+ */
6
+ const Float64ArrayFrom = (arr) => Float64Array.from(Array.from(arr));
7
+ export default Float64ArrayFrom;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Shortcut for `Object.assign()` static method.
3
+ * @param {Record<string, any>} obj a target object
4
+ * @param {Record<string, any>} source a source object
5
+ */
6
+ const ObjectAssign = (obj, source) => Object.assign(obj, source);
7
+ export default ObjectAssign;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Shortcut for `Object.keys()` static method.
3
+ * @param {Record<string, any>} obj a target object
4
+ * @returns {string[]}
5
+ */
6
+ const ObjectKeys = (obj) => Object.keys(obj);
7
+ export default ObjectKeys;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Shortcut for `Object.values()` static method.
3
+ * @param {Record<string, any>} obj a target object
4
+ * @returns {any[]}
5
+ */
6
+ const ObjectValues = (obj) => Object.values(obj);
7
+ export default ObjectValues;
@@ -0,0 +1,18 @@
1
+ import ObjectAssign from './ObjectAssign';
2
+
3
+ /**
4
+ * Returns a namespaced `CustomEvent` specific to each component.
5
+ * @param {string} EventType Event.type
6
+ * @param {Record<string, any>=} config Event.options | Event.properties
7
+ * @returns {SHORTER.OriginalEvent} a new namespaced event
8
+ */
9
+ export default function OriginalEvent(EventType, config) {
10
+ const OriginalCustomEvent = new CustomEvent(EventType, {
11
+ cancelable: true, bubbles: true,
12
+ });
13
+
14
+ if (config instanceof Object) {
15
+ ObjectAssign(OriginalCustomEvent, config);
16
+ }
17
+ return OriginalCustomEvent;
18
+ }
@@ -0,0 +1,25 @@
1
+ import getDocument from '../get/getDocument';
2
+ import ObjectAssign from './ObjectAssign';
3
+
4
+ /**
5
+ * This is a shortie for `document.createElement` method
6
+ * which allows you to create a new `HTMLElement` for a given `tagName`
7
+ * or based on an object with specific non-readonly attributes:
8
+ * `id`, `className`, `textContent`, `style`, etc.
9
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
10
+ *
11
+ * @param {Record<string, string> | string} param `tagName` or object
12
+ * @return {HTMLElement | Element} a new `HTMLElement` or `Element`
13
+ */
14
+ export default function createElement(param) {
15
+ if (typeof param === 'string') {
16
+ return getDocument().createElement(param);
17
+ }
18
+
19
+ const { tagName } = param;
20
+ const attr = { ...param };
21
+ const newElement = createElement(tagName);
22
+ delete attr.tagName;
23
+ ObjectAssign(newElement, attr);
24
+ return newElement;
25
+ }
@@ -0,0 +1,26 @@
1
+ import getDocument from '../get/getDocument';
2
+ import ObjectAssign from './ObjectAssign';
3
+
4
+ /**
5
+ * This is a shortie for `document.createElementNS` method
6
+ * which allows you to create a new `HTMLElement` for a given `tagName`
7
+ * or based on an object with specific non-readonly attributes:
8
+ * `id`, `className`, `textContent`, `style`, etc.
9
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS
10
+ *
11
+ * @param {string} namespace `namespaceURI` to associate with the new `HTMLElement`
12
+ * @param {Record<string, string> | string} param `tagName` or object
13
+ * @return {HTMLElement | Element} a new `HTMLElement` or `Element`
14
+ */
15
+ export default function createElementNS(namespace, param) {
16
+ if (typeof param === 'string') {
17
+ return getDocument().createElementNS(namespace, param);
18
+ }
19
+
20
+ const { tagName } = param;
21
+ const attr = { ...param };
22
+ const newElement = createElementNS(namespace, tagName);
23
+ delete attr.tagName;
24
+ ObjectAssign(newElement, attr);
25
+ return newElement;
26
+ }
@@ -0,0 +1,78 @@
1
+ import querySelector from '../selectors/querySelector';
2
+
3
+ /** @type {Map<string, Map<HTMLElement | Element, Record<string, any>>>} */
4
+ const componentData = new Map();
5
+ /**
6
+ * An interface for web components background data.
7
+ * @see https://github.com/thednp/bootstrap.native/blob/master/src/components/base-component.js
8
+ */
9
+ const Data = {
10
+ /**
11
+ * Sets web components data.
12
+ * @param {HTMLElement | Element | string} target target element
13
+ * @param {string} component the component's name or a unique key
14
+ * @param {Record<string, any>} instance the component instance
15
+ */
16
+ set: (target, component, instance) => {
17
+ const element = querySelector(target);
18
+ if (!element) return;
19
+
20
+ if (!componentData.has(component)) {
21
+ componentData.set(component, new Map());
22
+ }
23
+
24
+ const instanceMap = componentData.get(component);
25
+ // @ts-ignore - not undefined, but defined right above
26
+ instanceMap.set(element, instance);
27
+ },
28
+
29
+ /**
30
+ * Returns all instances for specified component.
31
+ * @param {string} component the component's name or a unique key
32
+ * @returns {Map<HTMLElement | Element, Record<string, any>>?} all the component instances
33
+ */
34
+ getAllFor: (component) => {
35
+ const instanceMap = componentData.get(component);
36
+
37
+ return instanceMap || null;
38
+ },
39
+
40
+ /**
41
+ * Returns the instance associated with the target.
42
+ * @param {HTMLElement | Element | string} target target element
43
+ * @param {string} component the component's name or a unique key
44
+ * @returns {Record<string, any>?} the instance
45
+ */
46
+ get: (target, component) => {
47
+ const element = querySelector(target);
48
+ const allForC = Data.getAllFor(component);
49
+ const instance = element && allForC && allForC.get(element);
50
+
51
+ return instance || null;
52
+ },
53
+
54
+ /**
55
+ * Removes web components data.
56
+ * @param {HTMLElement | Element | string} target target element
57
+ * @param {string} component the component's name or a unique key
58
+ */
59
+ remove: (target, component) => {
60
+ const element = querySelector(target);
61
+ const instanceMap = componentData.get(component);
62
+ if (!instanceMap || !element) return;
63
+
64
+ instanceMap.delete(element);
65
+
66
+ if (instanceMap.size === 0) {
67
+ componentData.delete(component);
68
+ }
69
+ },
70
+ };
71
+
72
+ /**
73
+ * An alias for `Data.get()`.
74
+ * @type {SHORTER.getInstance<any>}
75
+ */
76
+ export const getInstance = (target, component) => Data.get(target, component);
77
+
78
+ export default Data;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shortcut for the `Element.dispatchEvent(Event)` method.
3
+ *
4
+ * @param {HTMLElement | Element} element is the target
5
+ * @param {Event} event is the `Event` object
6
+ */
7
+ const dispatchEvent = (element, event) => element.dispatchEvent(event);
8
+ export default dispatchEvent;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * JavaScript `Array` distinct.
3
+ * @see https://codeburst.io/javascript-array-distinct-5edc93501dc4
4
+ * @param {any} value
5
+ * @param {number} index
6
+ * @param {any} self
7
+ * @returns {boolean}
8
+ */
9
+ const distinct = (value, index, self) => self.indexOf(value) === index;
10
+ export default distinct;
@@ -0,0 +1,37 @@
1
+ import animationEndEvent from '../strings/animationEndEvent';
2
+ import getElementAnimationDelay from '../get/getElementAnimationDelay';
3
+ import getElementAnimationDuration from '../get/getElementAnimationDuration';
4
+
5
+ /**
6
+ * Utility to make sure callbacks are consistently
7
+ * called when animation ends.
8
+ *
9
+ * @param {HTMLElement | Element} element target
10
+ * @param {EventListener} handler `animationend` callback
11
+ */
12
+ export default function emulateAnimationEnd(element, handler) {
13
+ let called = 0;
14
+ const endEvent = new Event(animationEndEvent);
15
+ const duration = getElementAnimationDuration(element);
16
+ const delay = getElementAnimationDelay(element);
17
+
18
+ if (duration) {
19
+ /**
20
+ * Wrap the handler in on -> off callback
21
+ * @type {EventListener}
22
+ */
23
+ const animationEndWrapper = (e) => {
24
+ if (e.target === element) {
25
+ handler.apply(element, [e]);
26
+ element.removeEventListener(animationEndEvent, animationEndWrapper);
27
+ called = 1;
28
+ }
29
+ };
30
+ element.addEventListener(animationEndEvent, animationEndWrapper);
31
+ setTimeout(() => {
32
+ if (!called) element.dispatchEvent(endEvent);
33
+ }, duration + delay + 17);
34
+ } else {
35
+ handler.apply(element, [endEvent]);
36
+ }
37
+ }
@@ -0,0 +1,38 @@
1
+ import supportAnimation from '../boolean/supportAnimation';
2
+ import animationEndEvent from '../strings/animationEndEventLegacy';
3
+ import getElementAnimationDelay from '../get/getElementAnimationDelayLegacy';
4
+ import getElementAnimationDuration from '../get/getElementAnimationDurationLegacy';
5
+
6
+ /**
7
+ * Utility to make sure callbacks are consistently
8
+ * called when animation ends.
9
+ *
10
+ * @param {HTMLElement | Element} element target
11
+ * @param {EventListener} handler `animationend` callback
12
+ */
13
+ export default function emulateAnimationEnd(element, handler) {
14
+ let called = 0;
15
+ const endEvent = new Event(animationEndEvent);
16
+ const duration = getElementAnimationDuration(element);
17
+ const delay = getElementAnimationDelay(element);
18
+
19
+ if (supportAnimation && duration) {
20
+ /**
21
+ * Wrap the handler in on -> off callback
22
+ * @param {Event} e Event object
23
+ */
24
+ const animationEndWrapper = (e) => {
25
+ if (e.target === element) {
26
+ handler.apply(element, [e]);
27
+ element.removeEventListener(animationEndEvent, animationEndWrapper);
28
+ called = 1;
29
+ }
30
+ };
31
+ element.addEventListener(animationEndEvent, animationEndWrapper);
32
+ setTimeout(() => {
33
+ if (!called) element.dispatchEvent(endEvent);
34
+ }, duration + delay + 17);
35
+ } else {
36
+ handler.apply(element, [endEvent]);
37
+ }
38
+ }
@@ -0,0 +1,37 @@
1
+ import transitionEndEvent from '../strings/transitionEndEvent';
2
+ import getElementTransitionDelay from '../get/getElementTransitionDelay';
3
+ import getElementTransitionDuration from '../get/getElementTransitionDuration';
4
+
5
+ /**
6
+ * Utility to make sure callbacks are consistently
7
+ * called when transition ends.
8
+ *
9
+ * @param {HTMLElement | Element} element target
10
+ * @param {EventListener} handler `transitionend` callback
11
+ */
12
+ export default function emulateTransitionEnd(element, handler) {
13
+ let called = 0;
14
+ const endEvent = new Event(transitionEndEvent);
15
+ const duration = getElementTransitionDuration(element);
16
+ const delay = getElementTransitionDelay(element);
17
+
18
+ if (duration) {
19
+ /**
20
+ * Wrap the handler in on -> off callback
21
+ * @type {EventListener} e Event object
22
+ */
23
+ const transitionEndWrapper = (e) => {
24
+ if (e.target === element) {
25
+ handler.apply(element, [e]);
26
+ element.removeEventListener(transitionEndEvent, transitionEndWrapper);
27
+ called = 1;
28
+ }
29
+ };
30
+ element.addEventListener(transitionEndEvent, transitionEndWrapper);
31
+ setTimeout(() => {
32
+ if (!called) element.dispatchEvent(endEvent);
33
+ }, duration + delay + 17);
34
+ } else {
35
+ handler.apply(element, [endEvent]);
36
+ }
37
+ }
@@ -0,0 +1,38 @@
1
+ import supportTransition from '../boolean/supportTransition';
2
+ import transitionEndEvent from '../strings/transitionEndEventLegacy';
3
+ import getElementTransitionDelay from '../get/getElementTransitionDelayLegacy';
4
+ import getElementTransitionDuration from '../get/getElementTransitionDurationLegacy';
5
+
6
+ /**
7
+ * Utility to make sure callbacks are consistently
8
+ * called when transition ends.
9
+ *
10
+ * @param {HTMLElement | Element} element target
11
+ * @param {EventListener} handler `transitionend` callback
12
+ */
13
+ export default function emulateTransitionEnd(element, handler) {
14
+ let called = 0;
15
+ const endEvent = new Event(transitionEndEvent);
16
+ const duration = getElementTransitionDuration(element);
17
+ const delay = getElementTransitionDelay(element);
18
+
19
+ if (supportTransition && duration) {
20
+ /**
21
+ * Wrap the handler in on -> off callback
22
+ * @param {Event} e Event object
23
+ */
24
+ const transitionEndWrapper = (e) => {
25
+ if (e.target === element) {
26
+ handler.apply(element, [e]);
27
+ element.removeEventListener(transitionEndEvent, transitionEndWrapper);
28
+ called = 1;
29
+ }
30
+ };
31
+ element.addEventListener(transitionEndEvent, transitionEndWrapper);
32
+ setTimeout(() => {
33
+ if (!called) element.dispatchEvent(endEvent);
34
+ }, duration + delay + 17);
35
+ } else {
36
+ handler.apply(element, [endEvent]);
37
+ }
38
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Utility to focus an `HTMLElement` target.
3
+ *
4
+ * @param {HTMLElement | Element} element is the target
5
+ */
6
+ // @ts-ignore -- `Element`s resulted from querySelector can focus too
7
+ const focus = (element) => element.focus();
8
+ export default focus;
@@ -0,0 +1,3 @@
1
+ /** A generic function with empty body. */
2
+ const noop = () => {};
3
+ export default noop;
@@ -0,0 +1,49 @@
1
+ import getAttribute from '../attr/getAttribute';
2
+ import normalizeValue from './normalizeValue';
3
+ import ObjectKeys from './ObjectKeys';
4
+ import toLowerCase from './toLowerCase';
5
+
6
+ /**
7
+ * Utility to normalize component options.
8
+ *
9
+ * @param {HTMLElement | Element} element target
10
+ * @param {Record<string, any>} defaultOps component default options
11
+ * @param {Record<string, any>} inputOps component instance options
12
+ * @param {string=} ns component namespace
13
+ * @return {Record<string, any>} normalized component options object
14
+ */
15
+ export default function normalizeOptions(element, defaultOps, inputOps, ns) {
16
+ // @ts-ignore -- our targets are always `HTMLElement`
17
+ const data = { ...element.dataset };
18
+ /** @type {Record<string, any>} */
19
+ const normalOps = {};
20
+ /** @type {Record<string, any>} */
21
+ const dataOps = {};
22
+ const title = 'title';
23
+
24
+ ObjectKeys(data).forEach((k) => {
25
+ const key = ns && k.includes(ns)
26
+ ? k.replace(ns, '').replace(/[A-Z]/, (match) => toLowerCase(match))
27
+ : k;
28
+
29
+ dataOps[key] = normalizeValue(data[k]);
30
+ });
31
+
32
+ ObjectKeys(inputOps).forEach((k) => {
33
+ inputOps[k] = normalizeValue(inputOps[k]);
34
+ });
35
+
36
+ ObjectKeys(defaultOps).forEach((k) => {
37
+ if (k in inputOps) {
38
+ normalOps[k] = inputOps[k];
39
+ } else if (k in dataOps) {
40
+ normalOps[k] = dataOps[k];
41
+ } else {
42
+ normalOps[k] = k === title
43
+ ? getAttribute(element, title)
44
+ : defaultOps[k];
45
+ }
46
+ });
47
+
48
+ return normalOps;
49
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The raw value or a given component option.
3
+ *
4
+ * @typedef {string | HTMLElement | Function | number | boolean | null} niceValue
5
+ */
6
+
7
+ /**
8
+ * Utility to normalize component options
9
+ *
10
+ * @param {any} value the input value
11
+ * @return {niceValue} the normalized value
12
+ */
13
+ export default function normalizeValue(value) {
14
+ if (value === 'true') { // boolean
15
+ return true;
16
+ }
17
+
18
+ if (value === 'false') { // boolean
19
+ return false;
20
+ }
21
+
22
+ if (!Number.isNaN(+value)) { // number
23
+ return +value;
24
+ }
25
+
26
+ if (value === '' || value === 'null') { // null
27
+ return null;
28
+ }
29
+
30
+ // string / function / HTMLElement / object
31
+ return value;
32
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A global namespace for most scroll event listeners.
3
+ * @type {Partial<AddEventListenerOptions>}
4
+ */
5
+ const passiveHandler = { passive: true };
6
+
7
+ export default passiveHandler;
@@ -0,0 +1,9 @@
1
+ import supportPassive from '../boolean/supportPassive';
2
+
3
+ /**
4
+ * A global namespace for most scroll event listeners in legacy browsers.
5
+ * @type {Partial<AddEventListenerOptions> | boolean}
6
+ */
7
+ const passiveHandler = supportPassive ? { passive: true } : false;
8
+
9
+ export default passiveHandler;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Utility to force re-paint of an `HTMLElement` target.
3
+ *
4
+ * @param {HTMLElement | Element} element is the target
5
+ * @return {number} the `Element.offsetHeight` value
6
+ */
7
+ // @ts-ignore
8
+ const reflow = (element) => element.offsetHeight;
9
+ export default reflow;
@@ -0,0 +1,10 @@
1
+ import ObjectAssign from './ObjectAssign';
2
+
3
+ /**
4
+ * Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
5
+ * @param {HTMLElement | Element} element target element
6
+ * @param {Partial<CSSStyleDeclaration>} styles attribute value
7
+ */
8
+ // @ts-ignore
9
+ const setElementStyle = (element, styles) => { ObjectAssign(element.style, styles); };
10
+ export default setElementStyle;
@@ -0,0 +1,78 @@
1
+ import querySelector from '../selectors/querySelector';
2
+
3
+ /** @type {Map<HTMLElement | Element, any>} */
4
+ const TimeCache = new Map();
5
+ /**
6
+ * An interface for one or more `TimerHandler`s per `Element`.
7
+ * @see https://github.com/thednp/navbar.js/
8
+ */
9
+ const Timer = {
10
+ /**
11
+ * Sets a new timeout timer for an element, or element -> key association.
12
+ * @param {HTMLElement | Element | string} target target element
13
+ * @param {ReturnType<TimerHandler>} callback the callback
14
+ * @param {number} delay the execution delay
15
+ * @param {string=} key a unique key
16
+ */
17
+ set: (target, callback, delay, key) => {
18
+ const element = querySelector(target);
19
+
20
+ if (!element) return;
21
+
22
+ if (key && key.length) {
23
+ if (!TimeCache.has(element)) {
24
+ TimeCache.set(element, new Map());
25
+ }
26
+ const keyTimers = TimeCache.get(element);
27
+ keyTimers.set(key, setTimeout(callback, delay));
28
+ } else {
29
+ TimeCache.set(element, setTimeout(callback, delay));
30
+ }
31
+ },
32
+
33
+ /**
34
+ * Returns the timer associated with the target.
35
+ * @param {HTMLElement | Element | string} target target element
36
+ * @param {string=} key a unique
37
+ * @returns {number?} the timer
38
+ */
39
+ get: (target, key) => {
40
+ const element = querySelector(target);
41
+
42
+ if (!element) return null;
43
+ const keyTimers = TimeCache.get(element);
44
+
45
+ if (key && key.length && keyTimers && keyTimers.get) {
46
+ return keyTimers.get(key) || null;
47
+ }
48
+ return keyTimers || null;
49
+ },
50
+
51
+ /**
52
+ * Clears the element's timer.
53
+ * @param {HTMLElement | Element | string} target target element
54
+ * @param {string=} key a unique key
55
+ */
56
+ clear: (target, key) => {
57
+ const element = querySelector(target);
58
+
59
+ if (!element) return;
60
+
61
+ if (key && key.length) {
62
+ const keyTimers = TimeCache.get(element);
63
+
64
+ if (keyTimers && keyTimers.get) {
65
+ clearTimeout(keyTimers.get(key));
66
+ keyTimers.delete(key);
67
+ if (keyTimers.size === 0) {
68
+ TimeCache.delete(element);
69
+ }
70
+ }
71
+ } else {
72
+ clearTimeout(TimeCache.get(element));
73
+ TimeCache.delete(element);
74
+ }
75
+ },
76
+ };
77
+
78
+ export default Timer;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shortcut for `String.toLowerCase()`.
3
+ *
4
+ * @param {string} source input string
5
+ * @returns {string} lowercase output string
6
+ */
7
+ const toLowerCase = (source) => source.toLowerCase();
8
+ export default toLowerCase;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shortcut for `String.toUpperCase()`.
3
+ *
4
+ * @param {string} source input string
5
+ * @returns {string} uppercase output string
6
+ */
7
+ const toUpperCase = (source) => source.toUpperCase();
8
+ export default toUpperCase;