tool-shack 0.0.9 → 0.0.13

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 (85) hide show
  1. package/dist/browser/index.d.ts +2 -0
  2. package/dist/browser/index.js +2 -0
  3. package/dist/browser/index.js.map +1 -1
  4. package/dist/browser/isPushNotificationSupported.d.ts +6 -0
  5. package/dist/browser/isPushNotificationSupported.js +7 -0
  6. package/dist/browser/isPushNotificationSupported.js.map +1 -0
  7. package/dist/browser/isShareSupported.d.ts +6 -0
  8. package/dist/browser/isShareSupported.js +7 -0
  9. package/dist/browser/isShareSupported.js.map +1 -0
  10. package/dist/browser/isTouchSupported.d.ts +1 -1
  11. package/dist/browser/isTouchSupported.js +4 -2
  12. package/dist/browser/isTouchSupported.js.map +1 -1
  13. package/dist/browser/preferColorScheme.js +2 -2
  14. package/dist/browser/preferColorScheme.js.map +1 -1
  15. package/dist/browser/tabFocusListener.js.map +1 -1
  16. package/dist/dateTime/dateAsIso.js.map +1 -1
  17. package/dist/dateTime/parseDuration.js.map +1 -1
  18. package/dist/dom/addAsyncEventListener.js.map +1 -1
  19. package/dist/dom/addEventListener.js +3 -3
  20. package/dist/dom/addEventListener.js.map +1 -1
  21. package/dist/dom/createElement.d.ts +1 -1
  22. package/dist/dom/createElement.js +5 -0
  23. package/dist/dom/createElement.js.map +1 -1
  24. package/dist/dom/fireEvent.js.map +1 -1
  25. package/dist/dom/interfaces/createElement.d.ts +2 -1
  26. package/dist/schedule/runAsync.js +1 -1
  27. package/dist/schedule/runAsync.js.map +1 -1
  28. package/dist/string/byteSize.d.ts +7 -0
  29. package/dist/string/byteSize.js +8 -0
  30. package/dist/string/byteSize.js.map +1 -0
  31. package/dist/string/escapeHTML.js.map +1 -1
  32. package/dist/string/index.d.ts +1 -0
  33. package/dist/string/index.js +1 -0
  34. package/dist/string/index.js.map +1 -1
  35. package/docs/assets/highlight.css +22 -22
  36. package/docs/assets/main.js +52 -52
  37. package/docs/assets/search.js +1 -1
  38. package/docs/assets/style.css +1413 -1388
  39. package/docs/interfaces/dateTime.IDuration.html +1 -1
  40. package/docs/interfaces/dom.IExtendingElementProps.html +1 -1
  41. package/docs/modules/browser.html +14 -8
  42. package/docs/modules/dateTime.html +2 -2
  43. package/docs/modules/dom.html +8 -8
  44. package/docs/modules/general.html +3 -3
  45. package/docs/modules/schedule.html +2 -2
  46. package/docs/modules/string.html +10 -5
  47. package/package.json +4 -4
  48. package/src/browser/index.ts +2 -0
  49. package/src/browser/interfaces/isTouchSupported.ts +3 -3
  50. package/src/browser/isPushNotificationSupported.ts +7 -0
  51. package/src/browser/isShareSupported.ts +7 -0
  52. package/src/browser/isTabFocused.ts +6 -6
  53. package/src/browser/isTouchSupported.ts +5 -6
  54. package/src/browser/preferColorScheme.ts +6 -3
  55. package/src/browser/tabFocusListener.ts +1 -1
  56. package/src/dateTime/dateAsIso.ts +9 -3
  57. package/src/dateTime/index.ts +4 -4
  58. package/src/dateTime/interfaces/index.ts +1 -1
  59. package/src/dateTime/interfaces/parseDuration.ts +7 -7
  60. package/src/dateTime/parseDuration.ts +1 -1
  61. package/src/dom/addAsyncEventListener.ts +15 -13
  62. package/src/dom/addClickOutsideListener.ts +16 -16
  63. package/src/dom/addEventListener.ts +12 -7
  64. package/src/dom/appendAfter.ts +9 -9
  65. package/src/dom/appendBefore.ts +9 -9
  66. package/src/dom/createElement.ts +55 -51
  67. package/src/dom/fireEvent.ts +2 -1
  68. package/src/dom/index.ts +9 -9
  69. package/src/dom/interfaces/createElement.ts +36 -35
  70. package/src/dom/interfaces/index.ts +1 -1
  71. package/src/general/index.ts +3 -3
  72. package/src/general/isEmpty.ts +21 -21
  73. package/src/general/isNil.ts +7 -7
  74. package/src/general/isValidJson.ts +15 -15
  75. package/src/index.ts +6 -6
  76. package/src/schedule/index.ts +2 -2
  77. package/src/schedule/runAnimation.ts +51 -51
  78. package/src/schedule/runAsync.ts +3 -5
  79. package/src/string/byteSize.ts +7 -0
  80. package/src/string/escapeHTML.ts +1 -1
  81. package/src/string/index.ts +1 -0
  82. package/src/string/removeDiacritics.ts +11 -11
  83. package/src/string/slugify.ts +17 -17
  84. package/src/string/truncate.ts +21 -21
  85. package/tsconfig.json +19 -19
@@ -8,9 +8,15 @@ export const dateAsIso = (date: Date) => {
8
8
  const timezoneOffset = date.getTimezoneOffset();
9
9
 
10
10
  let isoFormat = '';
11
- isoFormat += `${date.getFullYear()}-${padNumber(date.getMonth() + 1)}-${padNumber(date.getDate())}`;
12
- isoFormat += `T${padNumber(date.getHours())}:${padNumber(date.getMinutes())}:${padNumber(date.getSeconds())}`;
13
- isoFormat += `${timezoneOffset <= 0 ? '+' : '-'}${padNumber(Math.floor(Math.abs(timezoneOffset) / 60))}:${padNumber(Math.abs(timezoneOffset) % 60)}`;
11
+ isoFormat += `${date.getFullYear()}-${padNumber(date.getMonth() + 1)}-${padNumber(
12
+ date.getDate(),
13
+ )}`;
14
+ isoFormat += `T${padNumber(date.getHours())}:${padNumber(date.getMinutes())}:${padNumber(
15
+ date.getSeconds(),
16
+ )}`;
17
+ isoFormat += `${timezoneOffset <= 0 ? '+' : '-'}${padNumber(
18
+ Math.floor(Math.abs(timezoneOffset) / 60),
19
+ )}:${padNumber(Math.abs(timezoneOffset) % 60)}`;
14
20
 
15
21
  return isoFormat;
16
22
  };
@@ -1,4 +1,4 @@
1
- export * from './interfaces';
2
-
3
- export * from './dateAsIso';
4
- export * from './parseDuration';
1
+ export * from './interfaces';
2
+
3
+ export * from './dateAsIso';
4
+ export * from './parseDuration';
@@ -1 +1 @@
1
- export * from './parseDuration';
1
+ export * from './parseDuration';
@@ -1,7 +1,7 @@
1
- export interface IDuration {
2
- days: number;
3
- hours: number;
4
- minutes: number;
5
- seconds: number;
6
- milliseconds: number;
7
- }
1
+ export interface IDuration {
2
+ days: number;
3
+ hours: number;
4
+ minutes: number;
5
+ seconds: number;
6
+ milliseconds: number;
7
+ }
@@ -16,4 +16,4 @@ export const parseDuration = (durationInMs: number): IDuration => {
16
16
  seconds: Math.floor(durationInMs / 1000) % 60,
17
17
  milliseconds: Math.floor(durationInMs) % 1000,
18
18
  };
19
- }
19
+ };
@@ -9,24 +9,26 @@
9
9
  export const addAsyncEventListener = (
10
10
  elementSelector: string,
11
11
  listeners: Record<string, EventListener | EventListener[]>,
12
- acceptBubbling = true
12
+ acceptBubbling = true,
13
13
  ): void => {
14
14
  for (let eventName in listeners) {
15
15
  const functionList = (
16
16
  listeners[eventName] instanceof Array ? listeners[eventName] : [listeners[eventName]]
17
17
  ) as EventListener[];
18
18
 
19
- functionList.forEach((fn: EventListener) => document.addEventListener(eventName, (event: Event) => {
20
- const target = event.target as HTMLElement;
21
- if (!target) {
22
- return;
23
- }
19
+ functionList.forEach((fn: EventListener) =>
20
+ document.addEventListener(eventName, (event: Event) => {
21
+ const target = event.target as HTMLElement;
22
+ if (!target) {
23
+ return;
24
+ }
24
25
 
25
- if (!acceptBubbling && target.matches(elementSelector)) {
26
- fn.call(this, event);
27
- } else if (acceptBubbling && target.closest(elementSelector)) {
28
- fn.call(this, event);
29
- }
30
- }));
26
+ if (!acceptBubbling && target.matches(elementSelector)) {
27
+ fn.call(this, event);
28
+ } else if (acceptBubbling && target.closest(elementSelector)) {
29
+ fn.call(this, event);
30
+ }
31
+ }),
32
+ );
31
33
  }
32
- }
34
+ };
@@ -1,16 +1,16 @@
1
- /**
2
- * Add event listener to listen to clicks outside of given element
3
- *
4
- * @param element Node element outside which to listen to clicks
5
- * @param fn Function to trigger when clicked outside
6
- * @returns void
7
- */
8
- export const addClickOutsideListener = (element: HTMLElement, fn: () => any): void => {
9
- document.addEventListener('click', (event: Event) => {
10
- const target = event.target as HTMLElement;
11
-
12
- if (!!target && !element.contains(target)) {
13
- fn.call(this);
14
- }
15
- });
16
- };
1
+ /**
2
+ * Add event listener to listen to clicks outside of given element
3
+ *
4
+ * @param element Node element outside which to listen to clicks
5
+ * @param fn Function to trigger when clicked outside
6
+ * @returns void
7
+ */
8
+ export const addClickOutsideListener = (element: HTMLElement, fn: () => any): void => {
9
+ document.addEventListener('click', (event: Event) => {
10
+ const target = event.target as HTMLElement;
11
+
12
+ if (!!target && !element.contains(target)) {
13
+ fn.call(this);
14
+ }
15
+ });
16
+ };
@@ -7,13 +7,16 @@
7
7
  */
8
8
  export const addEventListener = (
9
9
  element: HTMLElement | HTMLElement[] | NodeListOf<HTMLElement> | HTMLCollectionOf<HTMLElement>,
10
- listeners: Record<string, EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]>
10
+ listeners: Record<
11
+ string,
12
+ EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]
13
+ >,
11
14
  ): void => {
12
15
  const elementList = (
13
- element instanceof Array || element instanceof HTMLCollection || element instanceof NodeList ?
14
- Array.from(element) :
15
- [element]
16
- ) as HTMLElement[];
16
+ element instanceof Array || element instanceof HTMLCollection || element instanceof NodeList
17
+ ? Array.from(element)
18
+ : [element]
19
+ ) as HTMLElement[];
17
20
 
18
21
  elementList.forEach((element: HTMLElement) => {
19
22
  for (let eventName in listeners) {
@@ -21,7 +24,9 @@ export const addEventListener = (
21
24
  listeners[eventName] instanceof Array ? listeners[eventName] : [listeners[eventName]]
22
25
  ) as EventListenerOrEventListenerObject[];
23
26
 
24
- functionList.forEach((fn: EventListenerOrEventListenerObject) => element.addEventListener(eventName, fn));
27
+ functionList.forEach((fn: EventListenerOrEventListenerObject) =>
28
+ element.addEventListener(eventName, fn),
29
+ );
25
30
  }
26
31
  });
27
- }
32
+ };
@@ -1,9 +1,9 @@
1
- /**
2
- * Append given node after reference node in the DOM
3
- *
4
- * @param newNode New node element to be appended
5
- * @param referenceNode Reference node element
6
- * @returns Result of the action
7
- */
8
- export const appendAfter = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
9
- !!referenceNode.parentElement?.insertBefore(newNode, referenceNode.nextSibling);
1
+ /**
2
+ * Append given node after reference node in the DOM
3
+ *
4
+ * @param newNode New node element to be appended
5
+ * @param referenceNode Reference node element
6
+ * @returns Result of the action
7
+ */
8
+ export const appendAfter = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
9
+ !!referenceNode.parentElement?.insertBefore(newNode, referenceNode.nextSibling);
@@ -1,9 +1,9 @@
1
- /**
2
- * Append given node before reference node in the DOM
3
- *
4
- * @param newNode New node element to be appended
5
- * @param referenceNode Reference node element
6
- * @returns Result of the action
7
- */
8
- export const appendBefore = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
9
- !!referenceNode.parentElement?.insertBefore(newNode, referenceNode);
1
+ /**
2
+ * Append given node before reference node in the DOM
3
+ *
4
+ * @param newNode New node element to be appended
5
+ * @param referenceNode Reference node element
6
+ * @returns Result of the action
7
+ */
8
+ export const appendBefore = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
9
+ !!referenceNode.parentElement?.insertBefore(newNode, referenceNode);
@@ -1,51 +1,55 @@
1
- import { IExtendingElementProps } from './interfaces/createElement';
2
-
3
- /**
4
- * Method for creating Node element and assigning multiple parameters, event listeners & children in one method call
5
- *
6
- * @param tagName Node tag name
7
- * @param props List of attributes, event listeners or/and children to assign to the newly created element
8
- * @returns Resulting Node element
9
- */
10
- export const createElement = <T = HTMLElement>(
11
- tagName: string,
12
- props?: Partial<Omit<T, 'children' | 'dataset'>> & IExtendingElementProps,
13
- ): T => {
14
- const element: HTMLElement = document.createElement(tagName);
15
-
16
- if (props) {
17
- for (let key in props) {
18
- const value = (props as any)[key];
19
-
20
- if (key === 'role') {
21
- element.setAttribute(key, value);
22
- } else if (key === 'aria') {
23
- for (let ariaKey in props.aria!) {
24
- element.setAttribute(`aria-${ariaKey}`, value[ariaKey]);
25
- }
26
- } else if (key === 'children') {
27
- props.children!.forEach((child: HTMLElement) => element.appendChild(child));
28
- } else if (key === 'listeners') {
29
- for (let eventName in props.listeners!) {
30
- const functionList = (
31
- props.listeners![eventName] instanceof Array
32
- ? props.listeners![eventName]
33
- : [props.listeners![eventName]]
34
- ) as EventListenerOrEventListenerObject[];
35
-
36
- functionList.forEach((fn: EventListenerOrEventListenerObject) =>
37
- element.addEventListener(eventName, fn),
38
- );
39
- }
40
- } else if (key === 'dataset') {
41
- for (let datasetKey in props.dataset) {
42
- element.dataset[datasetKey] = props.dataset[datasetKey];
43
- }
44
- } else {
45
- (element as any)[key] = value;
46
- }
47
- }
48
- }
49
-
50
- return element as unknown as T;
51
- };
1
+ import { IExtendingElementProps } from './interfaces/createElement';
2
+
3
+ /**
4
+ * Method for creating Node element and assigning multiple parameters, event listeners & children in one method call
5
+ *
6
+ * @param tagName Node tag name
7
+ * @param props List of attributes, event listeners or/and children to assign to the newly created element
8
+ * @returns Resulting Node element
9
+ */
10
+ export const createElement = <T = HTMLElement>(
11
+ tagName: string,
12
+ props?: Partial<Omit<T, 'children' | 'dataset' | 'style'>> & IExtendingElementProps,
13
+ ): T => {
14
+ const element: HTMLElement = document.createElement(tagName);
15
+
16
+ if (props) {
17
+ for (let key in props) {
18
+ const value = (props as any)[key];
19
+
20
+ if (key === 'role') {
21
+ element.setAttribute(key, value);
22
+ } else if (key === 'aria') {
23
+ for (let ariaKey in props.aria!) {
24
+ element.setAttribute(`aria-${ariaKey}`, value[ariaKey]);
25
+ }
26
+ } else if (key === 'style') {
27
+ for (let styleKey in props.style!) {
28
+ (element.style as any)[styleKey] = value[styleKey];
29
+ }
30
+ } else if (key === 'children') {
31
+ props.children!.forEach((child: HTMLElement) => element.appendChild(child));
32
+ } else if (key === 'listeners') {
33
+ for (let eventName in props.listeners!) {
34
+ const functionList = (
35
+ props.listeners![eventName] instanceof Array
36
+ ? props.listeners![eventName]
37
+ : [props.listeners![eventName]]
38
+ ) as EventListenerOrEventListenerObject[];
39
+
40
+ functionList.forEach((fn: EventListenerOrEventListenerObject) =>
41
+ element.addEventListener(eventName, fn),
42
+ );
43
+ }
44
+ } else if (key === 'dataset') {
45
+ for (let datasetKey in props.dataset) {
46
+ element.dataset[datasetKey] = props.dataset[datasetKey];
47
+ }
48
+ } else {
49
+ (element as any)[key] = value;
50
+ }
51
+ }
52
+ }
53
+
54
+ return element as unknown as T;
55
+ };
@@ -5,4 +5,5 @@
5
5
  * @param eventType Type name of event that should be fired
6
6
  * @returns Result of the action
7
7
  */
8
- export const fireEvent = (element: HTMLElement, eventType: string): boolean => element.dispatchEvent(new Event(eventType));
8
+ export const fireEvent = (element: HTMLElement, eventType: string): boolean =>
9
+ element.dispatchEvent(new Event(eventType));
package/src/dom/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- export * from './interfaces';
2
-
3
- export * from './addAsyncEventListener';
4
- export * from './addClickOutsideListener';
5
- export * from './addEventListener';
6
- export * from './appendAfter';
7
- export * from './appendBefore';
8
- export * from './createElement';
9
- export * from './fireEvent';
1
+ export * from './interfaces';
2
+
3
+ export * from './addAsyncEventListener';
4
+ export * from './addClickOutsideListener';
5
+ export * from './addEventListener';
6
+ export * from './appendAfter';
7
+ export * from './appendBefore';
8
+ export * from './createElement';
9
+ export * from './fireEvent';
@@ -1,35 +1,36 @@
1
- export type TAriaLabelKey =
2
- | 'atomic'
3
- | 'busy'
4
- | 'controls'
5
- | 'current'
6
- | 'describedby'
7
- | 'details'
8
- | 'disables'
9
- | 'dropeffect'
10
- | 'errormessage'
11
- | 'expanded'
12
- | 'flowto'
13
- | 'grabbed'
14
- | 'haspopup'
15
- | 'hidden'
16
- | 'invalid'
17
- | 'keyshortcuts'
18
- | 'label'
19
- | 'labelledby'
20
- | 'live'
21
- | 'modal'
22
- | 'owns'
23
- | 'relevant'
24
- | 'roledescription';
25
-
26
- export interface IExtendingElementProps {
27
- children?: HTMLElement[];
28
- listeners?: Record<
29
- string,
30
- EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]
31
- >;
32
- role?: string;
33
- aria?: Partial<Record<TAriaLabelKey, string>>;
34
- dataset: Record<string, string>;
35
- }
1
+ export type TAriaLabelKey =
2
+ | 'atomic'
3
+ | 'busy'
4
+ | 'controls'
5
+ | 'current'
6
+ | 'describedby'
7
+ | 'details'
8
+ | 'disables'
9
+ | 'dropeffect'
10
+ | 'errormessage'
11
+ | 'expanded'
12
+ | 'flowto'
13
+ | 'grabbed'
14
+ | 'haspopup'
15
+ | 'hidden'
16
+ | 'invalid'
17
+ | 'keyshortcuts'
18
+ | 'label'
19
+ | 'labelledby'
20
+ | 'live'
21
+ | 'modal'
22
+ | 'owns'
23
+ | 'relevant'
24
+ | 'roledescription';
25
+
26
+ export interface IExtendingElementProps {
27
+ children?: HTMLElement[];
28
+ listeners?: Record<
29
+ string,
30
+ EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]
31
+ >;
32
+ role?: string;
33
+ aria?: Partial<Record<TAriaLabelKey, string>>;
34
+ dataset?: Record<string, string>;
35
+ style?: Record<string, string | number>;
36
+ }
@@ -1 +1 @@
1
- export * from './createElement';
1
+ export * from './createElement';
@@ -1,3 +1,3 @@
1
- export * from './isEmpty';
2
- export * from './isNil';
3
- export * from './isValidJson';
1
+ export * from './isEmpty';
2
+ export * from './isNil';
3
+ export * from './isValidJson';
@@ -1,21 +1,21 @@
1
- import { isNil } from './isNil';
2
-
3
- /**
4
- * Check if given value is empty
5
- *
6
- * @param value Value to be checked
7
- * @returns Boolean indicating if given value is empty
8
- */
9
- export const isEmpty = (value: any): boolean => {
10
- if (value instanceof Function) {
11
- return false;
12
- }
13
-
14
- return (
15
- isNil(value) ||
16
- (value instanceof Object &&
17
- !Object.keys(value).length &&
18
- Object.getPrototypeOf(value) === Object.prototype) ||
19
- !value.valueOf().toString().length
20
- );
21
- };
1
+ import { isNil } from './isNil';
2
+
3
+ /**
4
+ * Check if given value is empty
5
+ *
6
+ * @param value Value to be checked
7
+ * @returns Boolean indicating if given value is empty
8
+ */
9
+ export const isEmpty = (value: any): boolean => {
10
+ if (value instanceof Function) {
11
+ return false;
12
+ }
13
+
14
+ return (
15
+ isNil(value) ||
16
+ (value instanceof Object &&
17
+ !Object.keys(value).length &&
18
+ Object.getPrototypeOf(value) === Object.prototype) ||
19
+ !value.valueOf().toString().length
20
+ );
21
+ };
@@ -1,7 +1,7 @@
1
- /**
2
- * Check if value is equal to null or undefined
3
- *
4
- * @param value Value to be checked
5
- * @returns Boolean indicating if value is equal to null or undefined
6
- */
7
- export const isNil = (value: any): boolean => value === undefined || value === null;
1
+ /**
2
+ * Check if value is equal to null or undefined
3
+ *
4
+ * @param value Value to be checked
5
+ * @returns Boolean indicating if value is equal to null or undefined
6
+ */
7
+ export const isNil = (value: any): boolean => value === undefined || value === null;
@@ -1,15 +1,15 @@
1
- /**
2
- * Check if string is valid JSON
3
- *
4
- * @param value String value to be checked
5
- * @returns Boolean indicating if string value is a valid JSON
6
- */
7
- export const isValidJson = (value: string): boolean => {
8
- try {
9
- JSON.parse(value);
10
- } catch (error: any) {
11
- return false;
12
- }
13
-
14
- return true;
15
- };
1
+ /**
2
+ * Check if string is valid JSON
3
+ *
4
+ * @param value String value to be checked
5
+ * @returns Boolean indicating if string value is a valid JSON
6
+ */
7
+ export const isValidJson = (value: string): boolean => {
8
+ try {
9
+ JSON.parse(value);
10
+ } catch (error: any) {
11
+ return false;
12
+ }
13
+
14
+ return true;
15
+ };
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from './browser';
2
- export * from './dateTime';
3
- export * from './dom';
4
- export * from './general';
5
- export * from './schedule';
6
- export * from './string';
1
+ export * from './browser';
2
+ export * from './dateTime';
3
+ export * from './dom';
4
+ export * from './general';
5
+ export * from './schedule';
6
+ export * from './string';
@@ -1,2 +1,2 @@
1
- export * from './runAnimation';
2
- export * from './runAsync';
1
+ export * from './runAnimation';
2
+ export * from './runAsync';
@@ -1,51 +1,51 @@
1
- /**
2
- * Run function as animation using requestAnimationFrame
3
- *
4
- * @param fn Function to be run as animation
5
- * @param autoStart If animation should start right away or just return callbacks
6
- * @returns Callbacks to start animation (startAnimation) and stop animation (stopAnimation)
7
- */
8
- export const runAnimation = (
9
- fn: () => any,
10
- autoStart = true,
11
- ): { startAnimation: () => void; stopAnimation: () => void } => {
12
- let isRunning = false;
13
- let requestedAnimationFrame: number | null = null;
14
-
15
- const runAnimation = (): void => {
16
- requestedAnimationFrame = requestAnimationFrame((): void => {
17
- fn.call(this);
18
-
19
- if (isRunning) {
20
- runAnimation();
21
- }
22
- });
23
- };
24
-
25
- const startAnimation = (): void => {
26
- if (isRunning) {
27
- return;
28
- }
29
-
30
- isRunning = true;
31
- runAnimation();
32
- };
33
-
34
- const stopAnimation = (): void => {
35
- if (!isRunning) {
36
- return;
37
- }
38
-
39
- isRunning = false;
40
-
41
- if (!!requestedAnimationFrame) {
42
- cancelAnimationFrame(requestedAnimationFrame);
43
- }
44
- };
45
-
46
- if (autoStart) {
47
- startAnimation();
48
- }
49
-
50
- return { startAnimation, stopAnimation };
51
- };
1
+ /**
2
+ * Run function as animation using requestAnimationFrame
3
+ *
4
+ * @param fn Function to be run as animation
5
+ * @param autoStart If animation should start right away or just return callbacks
6
+ * @returns Callbacks to start animation (startAnimation) and stop animation (stopAnimation)
7
+ */
8
+ export const runAnimation = (
9
+ fn: () => any,
10
+ autoStart = true,
11
+ ): { startAnimation: () => void; stopAnimation: () => void } => {
12
+ let isRunning = false;
13
+ let requestedAnimationFrame: number | null = null;
14
+
15
+ const runAnimation = (): void => {
16
+ requestedAnimationFrame = requestAnimationFrame((): void => {
17
+ fn.call(this);
18
+
19
+ if (isRunning) {
20
+ runAnimation();
21
+ }
22
+ });
23
+ };
24
+
25
+ const startAnimation = (): void => {
26
+ if (isRunning) {
27
+ return;
28
+ }
29
+
30
+ isRunning = true;
31
+ runAnimation();
32
+ };
33
+
34
+ const stopAnimation = (): void => {
35
+ if (!isRunning) {
36
+ return;
37
+ }
38
+
39
+ isRunning = false;
40
+
41
+ if (!!requestedAnimationFrame) {
42
+ cancelAnimationFrame(requestedAnimationFrame);
43
+ }
44
+ };
45
+
46
+ if (autoStart) {
47
+ startAnimation();
48
+ }
49
+
50
+ return { startAnimation, stopAnimation };
51
+ };