@nzyme/dom-utils 0.14.0 → 0.15.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 (94) hide show
  1. package/dist/animateOnce.d.ts +7 -0
  2. package/dist/animateOnce.js +18 -0
  3. package/dist/animateOnce.js.map +1 -0
  4. package/dist/clearFocus.d.ts +3 -1
  5. package/dist/clearFocus.js +3 -1
  6. package/dist/clearFocus.js.map +1 -1
  7. package/dist/downloadFile.d.ts +23 -0
  8. package/dist/downloadFile.js +20 -0
  9. package/dist/downloadFile.js.map +1 -0
  10. package/dist/getBrowserLanguage.d.ts +11 -0
  11. package/dist/getBrowserLanguage.js +31 -0
  12. package/dist/getBrowserLanguage.js.map +1 -0
  13. package/dist/getChildrenHeight.d.ts +6 -2
  14. package/dist/getChildrenHeight.js +7 -3
  15. package/dist/getChildrenHeight.js.map +1 -1
  16. package/dist/getCookie.d.ts +9 -0
  17. package/dist/getCookie.js +22 -0
  18. package/dist/getCookie.js.map +1 -0
  19. package/dist/getOs.d.ts +5 -0
  20. package/dist/getOs.js +39 -0
  21. package/dist/getOs.js.map +1 -0
  22. package/dist/getOuterHeight.d.ts +6 -0
  23. package/dist/getOuterHeight.js +8 -2
  24. package/dist/getOuterHeight.js.map +1 -1
  25. package/dist/getScrollBarWidth.d.ts +6 -1
  26. package/dist/getScrollBarWidth.js +14 -3
  27. package/dist/getScrollBarWidth.js.map +1 -1
  28. package/dist/index.d.ts +17 -9
  29. package/dist/index.js +17 -9
  30. package/dist/index.js.map +1 -1
  31. package/dist/isAncestorOf.d.ts +6 -1
  32. package/dist/isAncestorOf.js +6 -1
  33. package/dist/isAncestorOf.js.map +1 -1
  34. package/dist/isBrowser.d.ts +6 -2
  35. package/dist/isBrowser.js +6 -2
  36. package/dist/isBrowser.js.map +1 -1
  37. package/dist/isProbablyMobile.d.ts +5 -0
  38. package/dist/isProbablyMobile.js +30 -0
  39. package/dist/isProbablyMobile.js.map +1 -0
  40. package/dist/loadScript.d.ts +7 -2
  41. package/dist/loadScript.js +19 -7
  42. package/dist/loadScript.js.map +1 -1
  43. package/dist/loadStyles.d.ts +8 -1
  44. package/dist/loadStyles.js +20 -13
  45. package/dist/loadStyles.js.map +1 -1
  46. package/dist/localStorageUtils.d.ts +13 -1
  47. package/dist/localStorageUtils.js +12 -0
  48. package/dist/localStorageUtils.js.map +1 -1
  49. package/dist/lockBodyScroll.d.ts +6 -2
  50. package/dist/lockBodyScroll.js +7 -3
  51. package/dist/lockBodyScroll.js.map +1 -1
  52. package/dist/navigateToUrl.d.ts +24 -0
  53. package/dist/navigateToUrl.js +55 -0
  54. package/dist/navigateToUrl.js.map +1 -0
  55. package/dist/requestIdleCallback.js +2 -2
  56. package/dist/requestIdleCallback.js.map +1 -1
  57. package/dist/scrollToTopElement.d.ts +7 -3
  58. package/dist/scrollToTopElement.js +19 -8
  59. package/dist/scrollToTopElement.js.map +1 -1
  60. package/dist/setCookie.d.ts +26 -0
  61. package/dist/setCookie.js +26 -0
  62. package/dist/setCookie.js.map +1 -0
  63. package/dist/virtualHistory.d.ts +18 -3
  64. package/dist/virtualHistory.js +12 -5
  65. package/dist/virtualHistory.js.map +1 -1
  66. package/package.json +31 -38
  67. package/dist/clearFocus.cjs +0 -13
  68. package/dist/clearFocus.cjs.map +0 -1
  69. package/dist/getChildrenHeight.cjs +0 -22
  70. package/dist/getChildrenHeight.cjs.map +0 -1
  71. package/dist/getOuterHeight.cjs +0 -12
  72. package/dist/getOuterHeight.cjs.map +0 -1
  73. package/dist/getScrollBarWidth.cjs +0 -29
  74. package/dist/getScrollBarWidth.cjs.map +0 -1
  75. package/dist/index.cjs +0 -17
  76. package/dist/index.cjs.map +0 -1
  77. package/dist/isAncestorOf.cjs +0 -17
  78. package/dist/isAncestorOf.cjs.map +0 -1
  79. package/dist/isBrowser.cjs +0 -11
  80. package/dist/isBrowser.cjs.map +0 -1
  81. package/dist/loadScript.cjs +0 -38
  82. package/dist/loadScript.cjs.map +0 -1
  83. package/dist/loadStyles.cjs +0 -41
  84. package/dist/loadStyles.cjs.map +0 -1
  85. package/dist/localStorageUtils.cjs +0 -22
  86. package/dist/localStorageUtils.cjs.map +0 -1
  87. package/dist/lockBodyScroll.cjs +0 -37
  88. package/dist/lockBodyScroll.cjs.map +0 -1
  89. package/dist/requestIdleCallback.cjs +0 -17
  90. package/dist/requestIdleCallback.cjs.map +0 -1
  91. package/dist/scrollToTopElement.cjs +0 -31
  92. package/dist/scrollToTopElement.cjs.map +0 -1
  93. package/dist/virtualHistory.cjs +0 -86
  94. package/dist/virtualHistory.cjs.map +0 -1
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Animates an element once by adding and removing a class.
3
+ * @util
4
+ * @param element - The element to animate.
5
+ * @param animationClass - The class to add and remove.
6
+ */
7
+ export declare function animateOnce(element: HTMLElement, animationClass: string): void;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Animates an element once by adding and removing a class.
3
+ * @util
4
+ * @param element - The element to animate.
5
+ * @param animationClass - The class to add and remove.
6
+ */
7
+ export function animateOnce(element, animationClass) {
8
+ element.classList.remove(animationClass);
9
+ // Force browser to reflow → ensures animation restarts
10
+ void element.offsetWidth;
11
+ element.classList.add(animationClass);
12
+ const listener = () => {
13
+ element.classList.remove(animationClass);
14
+ element.removeEventListener('animationend', listener);
15
+ };
16
+ element.addEventListener('animationend', listener);
17
+ }
18
+ //# sourceMappingURL=animateOnce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"animateOnce.js","sourceRoot":"","sources":["../src/animateOnce.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,OAAoB,EAAE,cAAsB;IACpE,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAEzC,uDAAuD;IACvD,KAAK,OAAO,CAAC,WAAW,CAAC;IAEzB,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAEtC,MAAM,QAAQ,GAAG,GAAG,EAAE;QAClB,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;QACzC,OAAO,CAAC,mBAAmB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;IAC1D,CAAC,CAAC;IAEF,OAAO,CAAC,gBAAgB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;AACvD,CAAC"}
@@ -1,4 +1,6 @@
1
1
  /**
2
- * Clear the focus from the current element.
2
+ * Clears the focus from the currently focused element in the document.
3
+ * This is useful to hide focus rings or blur input elements programmatically.
4
+ * @util
3
5
  */
4
6
  export declare function clearFocus(): void;
@@ -1,5 +1,7 @@
1
1
  /**
2
- * Clear the focus from the current element.
2
+ * Clears the focus from the currently focused element in the document.
3
+ * This is useful to hide focus rings or blur input elements programmatically.
4
+ * @util
3
5
  */
4
6
  export function clearFocus() {
5
7
  const active = document.activeElement;
@@ -1 +1 @@
1
- {"version":3,"file":"clearFocus.js","sourceRoot":"","sources":["../src/clearFocus.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,UAAU;IACtB,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC;IACtC,IAAI,MAAM,YAAY,WAAW,EAAE,CAAC;QAChC,MAAM,CAAC,IAAI,EAAE,CAAC;IAClB,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"clearFocus.js","sourceRoot":"","sources":["../src/clearFocus.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,UAAU;IACtB,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC;IACtC,IAAI,MAAM,YAAY,WAAW,EAAE,CAAC;QAChC,MAAM,CAAC,IAAI,EAAE,CAAC;IAClB,CAAC;AACL,CAAC"}
@@ -0,0 +1,23 @@
1
+ /** Options for triggering a browser file download. */
2
+ export interface DownloadFileOptions {
3
+ /**
4
+ * The content to download.
5
+ */
6
+ content: BlobPart;
7
+ /**
8
+ * The filename for the downloaded file.
9
+ */
10
+ filename: string;
11
+ /**
12
+ * The MIME type of the file.
13
+ * @default 'application/octet-stream'
14
+ */
15
+ type?: string;
16
+ }
17
+ /**
18
+ * Triggers a file download in the browser.
19
+ * @util
20
+ *
21
+ * @param options - The download options
22
+ */
23
+ export declare function downloadFile(options: DownloadFileOptions): void;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Triggers a file download in the browser.
3
+ * @util
4
+ *
5
+ * @param options - The download options
6
+ */
7
+ export function downloadFile(options) {
8
+ const { content, filename, type = 'application/octet-stream' } = options;
9
+ const blob = new Blob([content], { type });
10
+ const url = URL.createObjectURL(blob);
11
+ const link = document.createElement('a');
12
+ link.href = url;
13
+ link.download = filename;
14
+ link.style.display = 'none';
15
+ document.body.append(link);
16
+ link.click();
17
+ link.remove();
18
+ setTimeout(() => URL.revokeObjectURL(url), 100);
19
+ }
20
+ //# sourceMappingURL=downloadFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"downloadFile.js","sourceRoot":"","sources":["../src/downloadFile.ts"],"names":[],"mappings":"AAiBA;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,OAA4B;IACrD,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,GAAG,0BAA0B,EAAE,GAAG,OAAO,CAAC;IAEzE,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3C,MAAM,GAAG,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IAEtC,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IAChB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACzB,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;IAE5B,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,IAAI,CAAC,KAAK,EAAE,CAAC;IACb,IAAI,CAAC,MAAM,EAAE,CAAC;IAEd,UAAU,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Gets the preferred language from browser languages based on available languages.
3
+ * Falls back to the first available language if no match is found.
4
+ * Caches the result for performance.
5
+ * @util
6
+ *
7
+ * @param availableLanguages - Array of available languages from the provider
8
+ * @returns The best matching language code
9
+ * @__NO_SIDE_EFFECTS__
10
+ */
11
+ export declare function getBrowserLanguage<const L extends readonly string[]>(availableLanguages: L): L[number] | undefined;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Gets the preferred language from browser languages based on available languages.
3
+ * Falls back to the first available language if no match is found.
4
+ * Caches the result for performance.
5
+ * @util
6
+ *
7
+ * @param availableLanguages - Array of available languages from the provider
8
+ * @returns The best matching language code
9
+ * @__NO_SIDE_EFFECTS__
10
+ */
11
+ export function getBrowserLanguage(availableLanguages) {
12
+ if (typeof navigator === 'undefined') {
13
+ return undefined;
14
+ }
15
+ const browserLanguages = (navigator.languages || [navigator.language]);
16
+ // First try exact matches
17
+ for (const browserLang of browserLanguages) {
18
+ if (availableLanguages.includes(browserLang)) {
19
+ return browserLang;
20
+ }
21
+ }
22
+ // Then try partial matches (e.g., 'en-US' matches 'en')
23
+ for (const browserLang of browserLanguages) {
24
+ const langCode = browserLang.split('-')[0];
25
+ if (langCode && availableLanguages.includes(langCode)) {
26
+ return langCode;
27
+ }
28
+ }
29
+ return undefined;
30
+ }
31
+ //# sourceMappingURL=getBrowserLanguage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getBrowserLanguage.js","sourceRoot":"","sources":["../src/getBrowserLanguage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAoC,kBAAqB;IACvF,IAAI,OAAO,SAAS,KAAK,WAAW,EAAE,CAAC;QACnC,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,gBAAgB,GAAG,CAAC,SAAS,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAgB,CAAC;IAEtF,0BAA0B;IAC1B,KAAK,MAAM,WAAW,IAAI,gBAAgB,EAAE,CAAC;QACzC,IAAI,kBAAkB,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YAC3C,OAAO,WAAW,CAAC;QACvB,CAAC;IACL,CAAC;IAED,wDAAwD;IACxD,KAAK,MAAM,WAAW,IAAI,gBAAgB,EAAE,CAAC;QACzC,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAA0B,CAAC;QACpE,IAAI,QAAQ,IAAI,kBAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;YACpD,OAAO,QAAQ,CAAC;QACpB,CAAC;IACL,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC"}
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Returns height of element's children.
3
- * Even if element itself overflows.
2
+ * Returns the total height of an element's children.
3
+ * Calculates the maximum bottom position of all children, even if the parent element has overflow.
4
+ * @util
5
+ *
6
+ * @param element The parent HTML element whose children's height to measure
7
+ * @returns The maximum height in pixels needed to contain all children
4
8
  */
5
9
  export declare function getChildrenHeight(element: HTMLElement): number;
@@ -1,6 +1,10 @@
1
1
  /**
2
- * Returns height of element's children.
3
- * Even if element itself overflows.
2
+ * Returns the total height of an element's children.
3
+ * Calculates the maximum bottom position of all children, even if the parent element has overflow.
4
+ * @util
5
+ *
6
+ * @param element The parent HTML element whose children's height to measure
7
+ * @returns The maximum height in pixels needed to contain all children
4
8
  */
5
9
  export function getChildrenHeight(element) {
6
10
  let height = 0;
@@ -8,7 +12,7 @@ export function getChildrenHeight(element) {
8
12
  if (!(child instanceof HTMLElement)) {
9
13
  continue;
10
14
  }
11
- const marginBottom = parseInt(getComputedStyle(child).marginBottom, 10);
15
+ const marginBottom = Number.parseInt(getComputedStyle(child).marginBottom, 10);
12
16
  const bottom = child.offsetHeight + child.offsetTop + marginBottom;
13
17
  if (bottom > height) {
14
18
  height = bottom;
@@ -1 +1 @@
1
- {"version":3,"file":"getChildrenHeight.js","sourceRoot":"","sources":["../src/getChildrenHeight.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAoB;IAClD,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACnC,IAAI,CAAC,CAAC,KAAK,YAAY,WAAW,CAAC,EAAE,CAAC;YAClC,SAAS;QACb,CAAC;QAED,MAAM,YAAY,GAAG,QAAQ,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QACxE,MAAM,MAAM,GAAG,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,SAAS,GAAG,YAAY,CAAC;QAEnE,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC;YAClB,MAAM,GAAG,MAAM,CAAC;QACpB,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
1
+ {"version":3,"file":"getChildrenHeight.js","sourceRoot":"","sources":["../src/getChildrenHeight.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAoB;IAClD,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACnC,IAAI,CAAC,CAAC,KAAK,YAAY,WAAW,CAAC,EAAE,CAAC;YAClC,SAAS;QACb,CAAC;QAED,MAAM,YAAY,GAAG,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QAC/E,MAAM,MAAM,GAAG,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,SAAS,GAAG,YAAY,CAAC;QAEnE,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC;YAClB,MAAM,GAAG,MAAM,CAAC;QACpB,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Gets the value of a cookie if available.
3
+ * @util
4
+ *
5
+ * @param name - The name of the cookie
6
+ * @returns The value of the cookie if available, undefined otherwise
7
+ * @__NO_SIDE_EFFECTS__
8
+ */
9
+ export declare function getCookie(name: string): string | undefined;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Gets the value of a cookie if available.
3
+ * @util
4
+ *
5
+ * @param name - The name of the cookie
6
+ * @returns The value of the cookie if available, undefined otherwise
7
+ * @__NO_SIDE_EFFECTS__
8
+ */
9
+ export function getCookie(name) {
10
+ if (typeof document === 'undefined') {
11
+ return undefined;
12
+ }
13
+ const cookies = document.cookie.split(';');
14
+ for (const cookie of cookies) {
15
+ const [cookieName, value] = cookie.trim().split('=');
16
+ if (cookieName === name && value) {
17
+ return value;
18
+ }
19
+ }
20
+ return undefined;
21
+ }
22
+ //# sourceMappingURL=getCookie.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getCookie.js","sourceRoot":"","sources":["../src/getCookie.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY;IAClC,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3C,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC3B,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrD,IAAI,UAAU,KAAK,IAAI,IAAI,KAAK,EAAE,CAAC;YAC/B,OAAO,KAAK,CAAC;QACjB,CAAC;IACL,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Returns the operating system of the browser.
3
+ * @util
4
+ */
5
+ export declare function getOS(): string;
package/dist/getOs.js ADDED
@@ -0,0 +1,39 @@
1
+ let os;
2
+ /**
3
+ * Returns the operating system of the browser.
4
+ * @util
5
+ */
6
+ export function getOS() {
7
+ if (os !== undefined) {
8
+ return os;
9
+ }
10
+ return (os = getOSCore());
11
+ }
12
+ function getOSCore() {
13
+ if (typeof navigator === 'undefined' || typeof window === 'undefined') {
14
+ return 'Unknown';
15
+ }
16
+ const userAgent = navigator.userAgent || navigator.vendor;
17
+ const platform = navigator.platform || 'unknown';
18
+ // Use userAgentData if available (modern browsers)
19
+ if (navigator.userAgentData?.platform) {
20
+ return navigator.userAgentData.platform;
21
+ }
22
+ if (platform.includes('Mac')) {
23
+ return 'macOS';
24
+ }
25
+ if (platform.includes('Win')) {
26
+ return 'Windows';
27
+ }
28
+ if (platform.includes('Linux')) {
29
+ return 'Linux';
30
+ }
31
+ if (/iPhone|iPad|iPod/.test(userAgent)) {
32
+ return 'iOS';
33
+ }
34
+ if (userAgent.includes('Android')) {
35
+ return 'Android';
36
+ }
37
+ return 'Unknown';
38
+ }
39
+ //# sourceMappingURL=getOs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getOs.js","sourceRoot":"","sources":["../src/getOs.ts"],"names":[],"mappings":"AAIA,IAAI,EAAqC,CAAC;AAE1C;;;GAGG;AACH,MAAM,UAAU,KAAK;IACjB,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC;IACd,CAAC;IAED,OAAO,CAAC,EAAE,GAAG,SAAS,EAAE,CAAC,CAAC;AAC9B,CAAC;AAED,SAAS,SAAS;IACd,IAAI,OAAO,SAAS,KAAK,WAAW,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QACpE,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,IAAI,SAAS,CAAC,MAAM,CAAC;IAC1D,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,IAAI,SAAS,CAAC;IAEjD,mDAAmD;IACnD,IAAI,SAAS,CAAC,aAAa,EAAE,QAAQ,EAAE,CAAC;QACpC,OAAO,SAAS,CAAC,aAAa,CAAC,QAAQ,CAAC;IAC5C,CAAC;IAED,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC;IACnB,CAAC;IACD,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7B,OAAO,OAAO,CAAC;IACnB,CAAC;IACD,IAAI,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QACrC,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAChC,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC"}
@@ -1 +1,7 @@
1
+ /**
2
+ * Calculates the total outer height of an element including margins.
3
+ * @util
4
+ * @param element The HTML element to measure
5
+ * @returns The total height including the element's height plus top and bottom margins
6
+ */
1
7
  export declare function getOuterHeight(element: HTMLElement): number;
@@ -1,9 +1,15 @@
1
+ /**
2
+ * Calculates the total outer height of an element including margins.
3
+ * @util
4
+ * @param element The HTML element to measure
5
+ * @returns The total height including the element's height plus top and bottom margins
6
+ */
1
7
  export function getOuterHeight(element) {
2
8
  const window = element.ownerDocument.defaultView;
3
9
  const style = window.getComputedStyle(element);
4
10
  const height = element.offsetHeight;
5
- const marginTop = parseInt(style.getPropertyValue('margin-top'), 10);
6
- const marginBottom = parseInt(style.getPropertyValue('margin-bottom'), 10);
11
+ const marginTop = Number.parseInt(style.getPropertyValue('margin-top'), 10);
12
+ const marginBottom = Number.parseInt(style.getPropertyValue('margin-bottom'), 10);
7
13
  return height + marginBottom + marginTop;
8
14
  }
9
15
  //# sourceMappingURL=getOuterHeight.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getOuterHeight.js","sourceRoot":"","sources":["../src/getOuterHeight.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,cAAc,CAAC,OAAoB;IAC/C,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,WAAY,CAAC;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IACpC,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,YAAY,CAAC,EAAE,EAAE,CAAC,CAAC;IACrE,MAAM,YAAY,GAAG,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,eAAe,CAAC,EAAE,EAAE,CAAC,CAAC;IAE3E,OAAO,MAAM,GAAG,YAAY,GAAG,SAAS,CAAC;AAC7C,CAAC"}
1
+ {"version":3,"file":"getOuterHeight.js","sourceRoot":"","sources":["../src/getOuterHeight.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,OAAoB;IAC/C,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,WAAY,CAAC;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IACpC,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,YAAY,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5E,MAAM,YAAY,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,eAAe,CAAC,EAAE,EAAE,CAAC,CAAC;IAElF,OAAO,MAAM,GAAG,YAAY,GAAG,SAAS,CAAC;AAC7C,CAAC"}
@@ -1,4 +1,9 @@
1
1
  /**
2
- * Get the width of the scrollbar.
2
+ * Gets the width of the browser's scrollbar in pixels.
3
+ * This is useful for calculating layout adjustments when scrollbars appear or disappear.
4
+ * The result is cached for subsequent calls.
5
+ * @util
6
+ *
7
+ * @returns The width of the scrollbar in pixels
3
8
  */
4
9
  export declare function getScrollBarWidth(): number;
@@ -1,6 +1,11 @@
1
1
  let scrollBarWidth;
2
2
  /**
3
- * Get the width of the scrollbar.
3
+ * Gets the width of the browser's scrollbar in pixels.
4
+ * This is useful for calculating layout adjustments when scrollbars appear or disappear.
5
+ * The result is cached for subsequent calls.
6
+ * @util
7
+ *
8
+ * @returns The width of the scrollbar in pixels
4
9
  */
5
10
  export function getScrollBarWidth() {
6
11
  if (scrollBarWidth != null) {
@@ -8,15 +13,21 @@ export function getScrollBarWidth() {
8
13
  }
9
14
  return (scrollBarWidth = getScrollbarWidthCore());
10
15
  }
16
+ /**
17
+ * Internal implementation that measures the scrollbar width by creating temporary DOM elements.
18
+ * Creates a container with scrollbar and measures the difference between outer and inner widths.
19
+ *
20
+ * @returns The measured scrollbar width in pixels
21
+ */
11
22
  function getScrollbarWidthCore() {
12
23
  // Creating invisible container
13
24
  const outer = document.createElement('div');
14
25
  outer.style.visibility = 'hidden';
15
26
  outer.style.overflow = 'scroll'; // forcing scrollbar to appear
16
- document.body.appendChild(outer);
27
+ document.body.append(outer);
17
28
  // Creating inner element and placing it in the container
18
29
  const inner = document.createElement('div');
19
- outer.appendChild(inner);
30
+ outer.append(inner);
20
31
  // Calculating difference between container's full width and the child width
21
32
  const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
22
33
  // Removing temporary elements from the DOM
@@ -1 +1 @@
1
- {"version":3,"file":"getScrollBarWidth.js","sourceRoot":"","sources":["../src/getScrollBarWidth.ts"],"names":[],"mappings":"AAAA,IAAI,cAAsB,CAAC;AAE3B;;GAEG;AACH,MAAM,UAAU,iBAAiB;IAC7B,IAAI,cAAc,IAAI,IAAI,EAAE,CAAC;QACzB,OAAO,cAAc,CAAC;IAC1B,CAAC;IAED,OAAO,CAAC,cAAc,GAAG,qBAAqB,EAAE,CAAC,CAAC;AACtD,CAAC;AAED,SAAS,qBAAqB;IAC1B,+BAA+B;IAC/B,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5C,KAAK,CAAC,KAAK,CAAC,UAAU,GAAG,QAAQ,CAAC;IAClC,KAAK,CAAC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC,CAAC,8BAA8B;IAC/D,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAEjC,yDAAyD;IACzD,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5C,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAEzB,4EAA4E;IAC5E,MAAM,cAAc,GAAG,KAAK,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW,CAAC;IAE7D,2CAA2C;IAC3C,KAAK,CAAC,MAAM,EAAE,CAAC;IAEf,OAAO,cAAc,CAAC;AAC1B,CAAC"}
1
+ {"version":3,"file":"getScrollBarWidth.js","sourceRoot":"","sources":["../src/getScrollBarWidth.ts"],"names":[],"mappings":"AAAA,IAAI,cAAsB,CAAC;AAE3B;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB;IAC7B,IAAI,cAAc,IAAI,IAAI,EAAE,CAAC;QACzB,OAAO,cAAc,CAAC;IAC1B,CAAC;IAED,OAAO,CAAC,cAAc,GAAG,qBAAqB,EAAE,CAAC,CAAC;AACtD,CAAC;AAED;;;;;GAKG;AACH,SAAS,qBAAqB;IAC1B,+BAA+B;IAC/B,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5C,KAAK,CAAC,KAAK,CAAC,UAAU,GAAG,QAAQ,CAAC;IAClC,KAAK,CAAC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC,CAAC,8BAA8B;IAC/D,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAE5B,yDAAyD;IACzD,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5C,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAEpB,4EAA4E;IAC5E,MAAM,cAAc,GAAG,KAAK,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW,CAAC;IAE7D,2CAA2C;IAC3C,KAAK,CAAC,MAAM,EAAE,CAAC;IAEf,OAAO,cAAc,CAAC;AAC1B,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,21 @@
1
- export * from './virtualHistory.js';
2
- export * from './requestIdleCallback.js';
3
- export * from './isBrowser.js';
4
- export * from './localStorageUtils.js';
5
- export * from './loadScript.js';
6
- export * from './loadStyles.js';
7
- export * from './getOuterHeight.js';
1
+ export * from './animateOnce.js';
2
+ export * from './clearFocus.js';
3
+ export * from './downloadFile.js';
4
+ export * from './getBrowserLanguage.js';
8
5
  export * from './getChildrenHeight.js';
9
- export * from './scrollToTopElement.js';
6
+ export * from './getCookie.js';
7
+ export * from './getOs.js';
8
+ export * from './getOuterHeight.js';
10
9
  export * from './getScrollBarWidth.js';
11
- export * from './clearFocus.js';
12
10
  export * from './isAncestorOf.js';
11
+ export * from './isBrowser.js';
12
+ export * from './isProbablyMobile.js';
13
+ export * from './loadScript.js';
14
+ export * from './loadStyles.js';
15
+ export * from './localStorageUtils.js';
13
16
  export * from './lockBodyScroll.js';
17
+ export * from './navigateToUrl.js';
18
+ export * from './requestIdleCallback.js';
19
+ export * from './scrollToTopElement.js';
20
+ export * from './setCookie.js';
21
+ export * from './virtualHistory.js';
package/dist/index.js CHANGED
@@ -1,14 +1,22 @@
1
- export * from './virtualHistory.js';
2
- export * from './requestIdleCallback.js';
3
- export * from './isBrowser.js';
4
- export * from './localStorageUtils.js';
5
- export * from './loadScript.js';
6
- export * from './loadStyles.js';
7
- export * from './getOuterHeight.js';
1
+ export * from './animateOnce.js';
2
+ export * from './clearFocus.js';
3
+ export * from './downloadFile.js';
4
+ export * from './getBrowserLanguage.js';
8
5
  export * from './getChildrenHeight.js';
9
- export * from './scrollToTopElement.js';
6
+ export * from './getCookie.js';
7
+ export * from './getOs.js';
8
+ export * from './getOuterHeight.js';
10
9
  export * from './getScrollBarWidth.js';
11
- export * from './clearFocus.js';
12
10
  export * from './isAncestorOf.js';
11
+ export * from './isBrowser.js';
12
+ export * from './isProbablyMobile.js';
13
+ export * from './loadScript.js';
14
+ export * from './loadStyles.js';
15
+ export * from './localStorageUtils.js';
13
16
  export * from './lockBodyScroll.js';
17
+ export * from './navigateToUrl.js';
18
+ export * from './requestIdleCallback.js';
19
+ export * from './scrollToTopElement.js';
20
+ export * from './setCookie.js';
21
+ export * from './virtualHistory.js';
14
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC"}
@@ -1,4 +1,9 @@
1
1
  /**
2
- * Check if one element is an ancestor of another.
2
+ * Checks if one element is an ancestor (parent, grandparent, etc.) of another element.
3
+ * @util
4
+ *
5
+ * @param ancestor The potential ancestor element to check
6
+ * @param element The descendant element to check against
7
+ * @returns True if the ancestor contains the element in its descendant tree, false otherwise
3
8
  */
4
9
  export declare function isAncestorOf(ancestor: Element, element: Element): boolean;
@@ -1,5 +1,10 @@
1
1
  /**
2
- * Check if one element is an ancestor of another.
2
+ * Checks if one element is an ancestor (parent, grandparent, etc.) of another element.
3
+ * @util
4
+ *
5
+ * @param ancestor The potential ancestor element to check
6
+ * @param element The descendant element to check against
7
+ * @returns True if the ancestor contains the element in its descendant tree, false otherwise
3
8
  */
4
9
  export function isAncestorOf(ancestor, element) {
5
10
  let parent = element.parentElement;
@@ -1 +1 @@
1
- {"version":3,"file":"isAncestorOf.js","sourceRoot":"","sources":["../src/isAncestorOf.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,QAAiB,EAAE,OAAgB;IAC5D,IAAI,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAEnC,OAAO,MAAM,EAAE,CAAC;QACZ,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC;IAClC,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
1
+ {"version":3,"file":"isAncestorOf.js","sourceRoot":"","sources":["../src/isAncestorOf.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,QAAiB,EAAE,OAAgB;IAC5D,IAAI,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAEnC,OAAO,MAAM,EAAE,CAAC;QACZ,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC;IAClC,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Returns if we are in a browser context.
3
- * Convenient for mixed SSR/CSR code.
2
+ * Determines if the code is running in a browser environment.
3
+ * Useful for writing code that works in both server-side and client-side contexts,
4
+ * particularly for isomorphic/universal applications with SSR/CSR.
5
+ * @util
6
+ *
7
+ * @returns True if running in a browser context, false otherwise (e.g., Node.js)
4
8
  */
5
9
  export declare function isBrowser(): boolean;
package/dist/isBrowser.js CHANGED
@@ -1,6 +1,10 @@
1
1
  /**
2
- * Returns if we are in a browser context.
3
- * Convenient for mixed SSR/CSR code.
2
+ * Determines if the code is running in a browser environment.
3
+ * Useful for writing code that works in both server-side and client-side contexts,
4
+ * particularly for isomorphic/universal applications with SSR/CSR.
5
+ * @util
6
+ *
7
+ * @returns True if running in a browser context, false otherwise (e.g., Node.js)
4
8
  */
5
9
  export function isBrowser() {
6
10
  return typeof window === 'object';
@@ -1 +1 @@
1
- {"version":3,"file":"isBrowser.js","sourceRoot":"","sources":["../src/isBrowser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,SAAS;IACrB,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC;AACtC,CAAC"}
1
+ {"version":3,"file":"isBrowser.js","sourceRoot":"","sources":["../src/isBrowser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS;IACrB,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC;AACtC,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Used to check if the device is probably a mobile device.
3
+ * @util
4
+ */
5
+ export declare function isProbablyMobile(): boolean;
@@ -0,0 +1,30 @@
1
+ let isMobile;
2
+ /**
3
+ * Used to check if the device is probably a mobile device.
4
+ * @util
5
+ */
6
+ export function isProbablyMobile() {
7
+ if (isMobile !== undefined) {
8
+ return isMobile;
9
+ }
10
+ return (isMobile = isProbablyMobileCore());
11
+ }
12
+ function isProbablyMobileCore() {
13
+ if (typeof navigator === 'undefined') {
14
+ return false;
15
+ }
16
+ // 1. User-Agent Client Hints (Chrome ≥ 89, Edge, Opera)
17
+ // https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/mobile
18
+ if (navigator.userAgentData?.mobile !== undefined) {
19
+ return navigator.userAgentData.mobile;
20
+ }
21
+ // 2. Pointer & hover capability (CSS Media Queries Level 4)
22
+ // Phones/tablets: coarse pointer, no hover
23
+ // https://www.smashingmagazine.com/2022/03/guide-hover-pointer-media-queries/
24
+ const noHover = matchMedia('(hover: none)').matches;
25
+ const coarse = matchMedia('(pointer: coarse)').matches;
26
+ // 3. Number of touch points
27
+ const touchPoints = navigator.maxTouchPoints || 0;
28
+ return noHover && coarse && touchPoints > 0;
29
+ }
30
+ //# sourceMappingURL=isProbablyMobile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isProbablyMobile.js","sourceRoot":"","sources":["../src/isProbablyMobile.ts"],"names":[],"mappings":"AAAA,IAAI,QAA6B,CAAC;AAElC;;;GAGG;AACH,MAAM,UAAU,gBAAgB;IAC5B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,OAAO,CAAC,QAAQ,GAAG,oBAAoB,EAAE,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,oBAAoB;IACzB,IAAI,OAAO,SAAS,KAAK,WAAW,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,yDAAyD;IACzD,0EAA0E;IAC1E,IAAI,SAAS,CAAC,aAAa,EAAE,MAAM,KAAK,SAAS,EAAE,CAAC;QAChD,OAAO,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC;IAC1C,CAAC;IAED,6DAA6D;IAC7D,+CAA+C;IAC/C,8EAA8E;IAC9E,MAAM,OAAO,GAAG,UAAU,CAAC,eAAe,CAAC,CAAC,OAAO,CAAC;IACpD,MAAM,MAAM,GAAG,UAAU,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC;IAEvD,6BAA6B;IAC7B,MAAM,WAAW,GAAG,SAAS,CAAC,cAAc,IAAI,CAAC,CAAC;IAElD,OAAO,OAAO,IAAI,MAAM,IAAI,WAAW,GAAG,CAAC,CAAC;AAChD,CAAC"}
@@ -8,7 +8,12 @@ export interface LoadScriptOptions {
8
8
  document?: Document;
9
9
  }
10
10
  /**
11
- * Loads a given script into the page.
12
- * If script was already loaded, it won't load it again
11
+ * Loads a JavaScript file into the page by creating a script tag.
12
+ * If the script was already loaded, returns the existing Promise without loading it again.
13
+ * @util
14
+ *
15
+ * @param url The URL of the script to load
16
+ * @param options Additional options for loading the script
17
+ * @returns A Promise that resolves when the script has been loaded, or rejects on error
13
18
  */
14
19
  export declare function loadScript(url: string, options?: LoadScriptOptions): Promise<void>;
@@ -1,7 +1,12 @@
1
1
  const CACHE_SYMBOL = Symbol('script-cache');
2
2
  /**
3
- * Loads a given script into the page.
4
- * If script was already loaded, it won't load it again
3
+ * Loads a JavaScript file into the page by creating a script tag.
4
+ * If the script was already loaded, returns the existing Promise without loading it again.
5
+ * @util
6
+ *
7
+ * @param url The URL of the script to load
8
+ * @param options Additional options for loading the script
9
+ * @returns A Promise that resolves when the script has been loaded, or rejects on error
5
10
  */
6
11
  export function loadScript(url, options) {
7
12
  const cache = getScriptCache((options?.document ?? document));
@@ -12,20 +17,27 @@ export function loadScript(url, options) {
12
17
  const doc = options?.document ?? document;
13
18
  const scriptTag = doc.createElement('script');
14
19
  scriptTag.src = url;
15
- scriptTag.onload = () => resolve();
16
- scriptTag.onerror = e => {
17
- doc.body.removeChild(scriptTag);
20
+ scriptTag.addEventListener('load', () => resolve());
21
+ scriptTag.addEventListener('error', e => {
22
+ scriptTag.remove();
18
23
  // Remove pending promise from cache
19
24
  if (cache[url] === promise) {
20
25
  delete cache[url];
21
26
  }
22
27
  reject(new Error(`Failed to load script: ${url}`, { cause: e }));
23
- };
24
- doc.body.appendChild(scriptTag);
28
+ });
29
+ doc.body.append(scriptTag);
25
30
  });
26
31
  cache[url] = promise;
27
32
  return promise;
28
33
  }
34
+ /**
35
+ * Gets the script cache object from a document or creates one if it doesn't exist.
36
+ * Uses a Symbol to avoid conflicts with other properties.
37
+ *
38
+ * @param cache The document or object to use as cache storage
39
+ * @returns The script cache object for the document
40
+ */
29
41
  function getScriptCache(cache) {
30
42
  if (!cache[CACHE_SYMBOL]) {
31
43
  cache[CACHE_SYMBOL] = {};
@@ -1 +1 @@
1
- {"version":3,"file":"loadScript.js","sourceRoot":"","sources":["../src/loadScript.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAiB5C;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,OAA2B;IAC/D,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,OAAO,EAAE,QAAQ,IAAI,QAAQ,CAAgB,CAAC,CAAC;IAC7E,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACb,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAClD,MAAM,GAAG,GAAG,OAAO,EAAE,QAAQ,IAAI,QAAQ,CAAC;QAC1C,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAE9C,SAAS,CAAC,GAAG,GAAG,GAAG,CAAC;QACpB,SAAS,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;QACnC,SAAS,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE;YACpB,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;YAChC,oCAAoC;YACpC,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAC;gBACzB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YACtB,CAAC;YAED,MAAM,CAAC,IAAI,KAAK,CAAC,0BAA0B,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACrE,CAAC,CAAC;QAEF,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC;IAErB,OAAO,OAAO,CAAC;AACnB,CAAC;AAED,SAAS,cAAc,CAAC,KAAkB;IACtC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACvB,KAAK,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;IAC7B,CAAC;IAED,OAAO,KAAK,CAAC,YAAY,CAAC,CAAC;AAC/B,CAAC"}
1
+ {"version":3,"file":"loadScript.js","sourceRoot":"","sources":["../src/loadScript.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAiB5C;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,OAA2B;IAC/D,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,OAAO,EAAE,QAAQ,IAAI,QAAQ,CAAgB,CAAC,CAAC;IAC7E,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACb,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAClD,MAAM,GAAG,GAAG,OAAO,EAAE,QAAQ,IAAI,QAAQ,CAAC;QAC1C,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAE9C,SAAS,CAAC,GAAG,GAAG,GAAG,CAAC;QACpB,SAAS,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;QACpD,SAAS,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE;YACpC,SAAS,CAAC,MAAM,EAAE,CAAC;YACnB,oCAAoC;YACpC,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAC;gBACzB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YACtB,CAAC;YAED,MAAM,CAAC,IAAI,KAAK,CAAC,0BAA0B,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACrE,CAAC,CAAC,CAAC;QAEH,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC;IAErB,OAAO,OAAO,CAAC;AACnB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,KAAkB;IACtC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACvB,KAAK,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;IAC7B,CAAC;IAED,OAAO,KAAK,CAAC,YAAY,CAAC,CAAC;AAC/B,CAAC"}