piral-hooks-utils 0.15.0-alpha.3537

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 (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -0
  3. package/esm/contexts/PiletApiProvider.d.ts +2 -0
  4. package/esm/contexts/PiletApiProvider.js +3 -0
  5. package/esm/contexts/PiletApiProvider.js.map +1 -0
  6. package/esm/contexts/index.d.ts +1 -0
  7. package/esm/contexts/index.js +2 -0
  8. package/esm/contexts/index.js.map +1 -0
  9. package/esm/hocs/index.d.ts +1 -0
  10. package/esm/hocs/index.js +2 -0
  11. package/esm/hocs/index.js.map +1 -0
  12. package/esm/hocs/withProvider.d.ts +15 -0
  13. package/esm/hocs/withProvider.js +13 -0
  14. package/esm/hocs/withProvider.js.map +1 -0
  15. package/esm/hooks/afterVisible.d.ts +8 -0
  16. package/esm/hooks/afterVisible.js +26 -0
  17. package/esm/hooks/afterVisible.js.map +1 -0
  18. package/esm/hooks/asyncReplace.d.ts +7 -0
  19. package/esm/hooks/asyncReplace.js +21 -0
  20. package/esm/hooks/asyncReplace.js.map +1 -0
  21. package/esm/hooks/index.d.ts +7 -0
  22. package/esm/hooks/index.js +8 -0
  23. package/esm/hooks/index.js.map +1 -0
  24. package/esm/hooks/lockBodyScroll.d.ts +6 -0
  25. package/esm/hooks/lockBodyScroll.js +15 -0
  26. package/esm/hooks/lockBodyScroll.js.map +1 -0
  27. package/esm/hooks/onClickOutside.d.ts +8 -0
  28. package/esm/hooks/onClickOutside.js +23 -0
  29. package/esm/hooks/onClickOutside.js.map +1 -0
  30. package/esm/hooks/onScreenVisible.d.ts +11 -0
  31. package/esm/hooks/onScreenVisible.js +24 -0
  32. package/esm/hooks/onScreenVisible.js.map +1 -0
  33. package/esm/hooks/piletApi.d.ts +7 -0
  34. package/esm/hooks/piletApi.js +12 -0
  35. package/esm/hooks/piletApi.js.map +1 -0
  36. package/esm/hooks/prompt.d.ts +9 -0
  37. package/esm/hooks/prompt.js +28 -0
  38. package/esm/hooks/prompt.js.map +1 -0
  39. package/esm/index.d.ts +3 -0
  40. package/esm/index.js +4 -0
  41. package/esm/index.js.map +1 -0
  42. package/lib/contexts/PiletApiProvider.d.ts +2 -0
  43. package/lib/contexts/PiletApiProvider.js +6 -0
  44. package/lib/contexts/PiletApiProvider.js.map +1 -0
  45. package/lib/contexts/index.d.ts +1 -0
  46. package/lib/contexts/index.js +14 -0
  47. package/lib/contexts/index.js.map +1 -0
  48. package/lib/hocs/index.d.ts +1 -0
  49. package/lib/hocs/index.js +14 -0
  50. package/lib/hocs/index.js.map +1 -0
  51. package/lib/hocs/withProvider.d.ts +15 -0
  52. package/lib/hocs/withProvider.js +17 -0
  53. package/lib/hocs/withProvider.js.map +1 -0
  54. package/lib/hooks/afterVisible.d.ts +8 -0
  55. package/lib/hooks/afterVisible.js +30 -0
  56. package/lib/hooks/afterVisible.js.map +1 -0
  57. package/lib/hooks/asyncReplace.d.ts +7 -0
  58. package/lib/hooks/asyncReplace.js +25 -0
  59. package/lib/hooks/asyncReplace.js.map +1 -0
  60. package/lib/hooks/index.d.ts +7 -0
  61. package/lib/hooks/index.js +20 -0
  62. package/lib/hooks/index.js.map +1 -0
  63. package/lib/hooks/lockBodyScroll.d.ts +6 -0
  64. package/lib/hooks/lockBodyScroll.js +19 -0
  65. package/lib/hooks/lockBodyScroll.js.map +1 -0
  66. package/lib/hooks/onClickOutside.d.ts +8 -0
  67. package/lib/hooks/onClickOutside.js +27 -0
  68. package/lib/hooks/onClickOutside.js.map +1 -0
  69. package/lib/hooks/onScreenVisible.d.ts +11 -0
  70. package/lib/hooks/onScreenVisible.js +28 -0
  71. package/lib/hooks/onScreenVisible.js.map +1 -0
  72. package/lib/hooks/piletApi.d.ts +7 -0
  73. package/lib/hooks/piletApi.js +16 -0
  74. package/lib/hooks/piletApi.js.map +1 -0
  75. package/lib/hooks/prompt.d.ts +9 -0
  76. package/lib/hooks/prompt.js +32 -0
  77. package/lib/hooks/prompt.js.map +1 -0
  78. package/lib/index.d.ts +3 -0
  79. package/lib/index.js +16 -0
  80. package/lib/index.js.map +1 -0
  81. package/package.json +43 -0
  82. package/src/contexts/PiletApiProvider.ts +3 -0
  83. package/src/contexts/index.ts +1 -0
  84. package/src/hocs/index.ts +1 -0
  85. package/src/hocs/withProvider.tsx +23 -0
  86. package/src/hooks/afterVisible.ts +29 -0
  87. package/src/hooks/asyncReplace.ts +32 -0
  88. package/src/hooks/index.ts +7 -0
  89. package/src/hooks/lockBodyScroll.test.ts +24 -0
  90. package/src/hooks/lockBodyScroll.ts +15 -0
  91. package/src/hooks/onClickOutside.test.ts +113 -0
  92. package/src/hooks/onClickOutside.ts +25 -0
  93. package/src/hooks/onScreenVisible.test.ts +68 -0
  94. package/src/hooks/onScreenVisible.ts +28 -0
  95. package/src/hooks/piletApi.ts +12 -0
  96. package/src/hooks/prompt.ts +35 -0
  97. package/src/index.ts +3 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asyncReplace.js","sourceRoot":"","sources":["../../src/hooks/asyncReplace.ts"],"names":[],"mappings":";;;AAAA,iCAA4C;AAE5C;;;;;GAKG;AACH,SAAgB,eAAe,CAC7B,MAAwB,EACxB,UAA+B,GAAG,EAAE,GAAE,CAAC;IAEvC,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,IAAA,gBAAQ,EAAkB,KAAK,CAAC,CAAC;IACnE,MAAM,KAAK,GAAG,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IAEjE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,SAAS,KAAK,IAAI,EAAE;YACtB,IAAI,MAAM,GAAG,IAAI,CAAC;YAElB,MAAM,EAAE,CAAC,IAAI,CACX,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,EACrC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,IAAI,YAAY,CAAC,GAAG,CAAC,CACrC,CAAC;YAEF,OAAO,GAAG,EAAE,CAAC,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC;SAC/B;QAED,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IAClB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;AAC/D,CAAC;AAvBD,0CAuBC"}
@@ -0,0 +1,7 @@
1
+ export * from './afterVisible';
2
+ export * from './asyncReplace';
3
+ export * from './lockBodyScroll';
4
+ export * from './onClickOutside';
5
+ export * from './onScreenVisible';
6
+ export * from './piletApi';
7
+ export * from './prompt';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./afterVisible"), exports);
14
+ __exportStar(require("./asyncReplace"), exports);
15
+ __exportStar(require("./lockBodyScroll"), exports);
16
+ __exportStar(require("./onClickOutside"), exports);
17
+ __exportStar(require("./onScreenVisible"), exports);
18
+ __exportStar(require("./piletApi"), exports);
19
+ __exportStar(require("./prompt"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B;AAC/B,iDAA+B;AAC/B,mDAAiC;AACjC,mDAAiC;AACjC,oDAAkC;AAClC,6CAA2B;AAC3B,2CAAyB"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Hook that locks scrolling on the main document.
3
+ * Useful for preventing the standard scrolling in context of
4
+ * a modal dialog.
5
+ */
6
+ export declare function useLockBodyScroll(): void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLockBodyScroll = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Hook that locks scrolling on the main document.
7
+ * Useful for preventing the standard scrolling in context of
8
+ * a modal dialog.
9
+ */
10
+ function useLockBodyScroll() {
11
+ (0, react_1.useLayoutEffect)(() => {
12
+ document.body.style.overflow = 'hidden';
13
+ return () => {
14
+ document.body.style.overflow = 'visible';
15
+ };
16
+ }, []);
17
+ }
18
+ exports.useLockBodyScroll = useLockBodyScroll;
19
+ //# sourceMappingURL=lockBodyScroll.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lockBodyScroll.js","sourceRoot":"","sources":["../../src/hooks/lockBodyScroll.ts"],"names":[],"mappings":";;;AAAA,iCAAwC;AAExC;;;;GAIG;AACH,SAAgB,iBAAiB;IAC/B,IAAA,uBAAe,EAAC,GAAG,EAAE;QACnB,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACxC,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC3C,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC;AAPD,8CAOC"}
@@ -0,0 +1,8 @@
1
+ import { RefObject } from 'react';
2
+ /**
3
+ * Hook that detects if a click outside the given reference
4
+ * has been performed.
5
+ * @param ref The reference to the element.
6
+ * @param handler The callback to invoke when an outside click happened.
7
+ */
8
+ export declare function useOnClickOutside<T extends HTMLElement>(ref: RefObject<T>, handler: (event: MouseEvent) => void): void;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useOnClickOutside = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Hook that detects if a click outside the given reference
7
+ * has been performed.
8
+ * @param ref The reference to the element.
9
+ * @param handler The callback to invoke when an outside click happened.
10
+ */
11
+ function useOnClickOutside(ref, handler) {
12
+ (0, react_1.useEffect)(() => {
13
+ const listener = (event) => {
14
+ if (ref.current && !ref.current.contains(event.target)) {
15
+ handler(event);
16
+ }
17
+ };
18
+ document.addEventListener('mousedown', listener);
19
+ document.addEventListener('touchstart', listener);
20
+ return () => {
21
+ document.removeEventListener('mousedown', listener);
22
+ document.removeEventListener('touchstart', listener);
23
+ };
24
+ }, [handler]);
25
+ }
26
+ exports.useOnClickOutside = useOnClickOutside;
27
+ //# sourceMappingURL=onClickOutside.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onClickOutside.js","sourceRoot":"","sources":["../../src/hooks/onClickOutside.ts"],"names":[],"mappings":";;;AAAA,iCAA6C;AAE7C;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAwB,GAAiB,EAAE,OAAoC;IAC9G,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,CAAC,KAAiB,EAAE,EAAE;YACrC,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAc,CAAC,EAAE;gBAC9D,OAAO,CAAC,KAAK,CAAC,CAAC;aAChB;QACH,CAAC,CAAC;QAEF,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QACjD,QAAQ,CAAC,gBAAgB,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QAElD,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;YACpD,QAAQ,CAAC,mBAAmB,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAChB,CAAC;AAhBD,8CAgBC"}
@@ -0,0 +1,11 @@
1
+ import { RefObject } from 'react';
2
+ /**
3
+ * Hook that detects if a reference element within the main document is
4
+ * visible.
5
+ * Useful for performing some animation or triggering certain actions (e.g.,
6
+ * loading data for infinity scrolling) when an element appears or is close
7
+ * to appear on screen.
8
+ * @param ref The reference element to be visible.
9
+ * @param rootMargin The tolerance level to the reference element.
10
+ */
11
+ export declare function useOnScreenVisible<T extends HTMLElement>(ref: RefObject<T>, rootMargin?: string): boolean;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useOnScreenVisible = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Hook that detects if a reference element within the main document is
7
+ * visible.
8
+ * Useful for performing some animation or triggering certain actions (e.g.,
9
+ * loading data for infinity scrolling) when an element appears or is close
10
+ * to appear on screen.
11
+ * @param ref The reference element to be visible.
12
+ * @param rootMargin The tolerance level to the reference element.
13
+ */
14
+ function useOnScreenVisible(ref, rootMargin = '0px') {
15
+ const [isIntersecting, setIntersecting] = (0, react_1.useState)(false);
16
+ (0, react_1.useEffect)(() => {
17
+ const observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting), {
18
+ rootMargin,
19
+ });
20
+ if (ref.current) {
21
+ observer.observe(ref.current);
22
+ }
23
+ return () => observer.unobserve(ref.current);
24
+ }, []);
25
+ return isIntersecting;
26
+ }
27
+ exports.useOnScreenVisible = useOnScreenVisible;
28
+ //# sourceMappingURL=onScreenVisible.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onScreenVisible.js","sourceRoot":"","sources":["../../src/hooks/onScreenVisible.ts"],"names":[],"mappings":";;;AAAA,iCAAuD;AAEvD;;;;;;;;GAQG;AACH,SAAgB,kBAAkB,CAAwB,GAAiB,EAAE,UAAU,GAAG,KAAK;IAC7F,MAAM,CAAC,cAAc,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAE1D,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,KAAK,CAAC,cAAc,CAAC,EAAE;YAC5F,UAAU;SACX,CAAC,CAAC;QAEH,IAAI,GAAG,CAAC,OAAO,EAAE;YACf,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;SAC/B;QAED,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,cAAc,CAAC;AACxB,CAAC;AAhBD,gDAgBC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Retrieves the Pilet API stored in the provider.
3
+ * The component must be somewhat wrapped in the
4
+ * PiletApiProvider, e.g., via withPiletApi wrapper.
5
+ * @returns The Pilet API for this component.
6
+ */
7
+ export declare function usePiletApi<TPiletApi = any>(): TPiletApi;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePiletApi = void 0;
4
+ const react_1 = require("react");
5
+ const contexts_1 = require("../contexts");
6
+ /**
7
+ * Retrieves the Pilet API stored in the provider.
8
+ * The component must be somewhat wrapped in the
9
+ * PiletApiProvider, e.g., via withPiletApi wrapper.
10
+ * @returns The Pilet API for this component.
11
+ */
12
+ function usePiletApi() {
13
+ return (0, react_1.useContext)(contexts_1.PiletApiProvider);
14
+ }
15
+ exports.usePiletApi = usePiletApi;
16
+ //# sourceMappingURL=piletApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"piletApi.js","sourceRoot":"","sources":["../../src/hooks/piletApi.ts"],"names":[],"mappings":";;;AAAA,iCAAmC;AACnC,0CAA+C;AAE/C;;;;;GAKG;AACH,SAAgB,WAAW;IACzB,OAAO,IAAA,kBAAU,EAAC,2BAAgB,CAAC,CAAC;AACtC,CAAC;AAFD,kCAEC"}
@@ -0,0 +1,9 @@
1
+ import type { History, Location } from 'history';
2
+ /**
3
+ * Hook to notify the user in case of potential data loss when
4
+ * performing a page transition (internal or external).
5
+ * @param active True if the prompt should be shown, otherwise false.
6
+ * @param history The history of the currently used router.
7
+ * @param message The message to display when the prompt is shown.
8
+ */
9
+ export declare function usePrompt(active: boolean, history: History, message: string, onTransition?: (location: Location) => void): void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePrompt = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Hook to notify the user in case of potential data loss when
7
+ * performing a page transition (internal or external).
8
+ * @param active True if the prompt should be shown, otherwise false.
9
+ * @param history The history of the currently used router.
10
+ * @param message The message to display when the prompt is shown.
11
+ */
12
+ function usePrompt(active, history, message, onTransition) {
13
+ (0, react_1.useEffect)(() => {
14
+ if (active) {
15
+ const beforeUnload = (ev) => {
16
+ ev.returnValue = message;
17
+ return message;
18
+ };
19
+ const unlisten = onTransition && history.listen(onTransition);
20
+ const unblock = message && history.block(message);
21
+ window.addEventListener('beforeunload', beforeUnload);
22
+ return () => {
23
+ unlisten && unlisten();
24
+ unblock && unblock();
25
+ window.removeEventListener('beforeunload', beforeUnload);
26
+ };
27
+ }
28
+ return () => { };
29
+ }, [active, message]);
30
+ }
31
+ exports.usePrompt = usePrompt;
32
+ //# sourceMappingURL=prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/hooks/prompt.ts"],"names":[],"mappings":";;;AAAA,iCAAkC;AAGlC;;;;;;GAMG;AACF,SAAgB,SAAS,CACxB,MAAe,EACf,OAAgB,EAChB,OAAe,EACf,YAA2C;IAE3C,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,MAAM,EAAE;YACV,MAAM,YAAY,GAAG,CAAC,EAAqB,EAAE,EAAE;gBAC7C,EAAE,CAAC,WAAW,GAAG,OAAO,CAAC;gBACzB,OAAO,OAAO,CAAC;YACjB,CAAC,CAAC;YACF,MAAM,QAAQ,GAAG,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAC9D,MAAM,OAAO,GAAG,OAAO,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAClD,MAAM,CAAC,gBAAgB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;YACtD,OAAO,GAAG,EAAE;gBACV,QAAQ,IAAI,QAAQ,EAAE,CAAC;gBACvB,OAAO,IAAI,OAAO,EAAE,CAAC;gBACrB,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;YAC3D,CAAC,CAAC;SACH;QAED,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IAClB,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AACxB,CAAC;AAxBA,8BAwBA"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './contexts';
2
+ export * from './hooks';
3
+ export * from './hocs';
package/lib/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./contexts"), exports);
14
+ __exportStar(require("./hooks"), exports);
15
+ __exportStar(require("./hocs"), exports);
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB;AACxB,yCAAuB"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "piral-hooks-utils",
3
+ "version": "0.15.0-alpha.3537",
4
+ "description": "Hooks and HOC for pilets and Piral instances.",
5
+ "keywords": [
6
+ "piral",
7
+ "smapiot",
8
+ "pilet",
9
+ "modules",
10
+ "hoc",
11
+ "react",
12
+ "microfrontends",
13
+ "hooks",
14
+ "providers"
15
+ ],
16
+ "author": "smapiot",
17
+ "homepage": "https://piral.io",
18
+ "license": "MIT",
19
+ "module": "esm/index.js",
20
+ "main": "lib/index.js",
21
+ "typings": "lib/index.d.ts",
22
+ "sideEffects": false,
23
+ "files": [
24
+ "esm",
25
+ "lib",
26
+ "src"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/smapiot/piral.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/smapiot/piral/issues"
34
+ },
35
+ "scripts": {
36
+ "build": "yarn build:commonjs && yarn build:esnext",
37
+ "build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
38
+ "build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
39
+ "typedoc": "typedoc --json ../../../docs/types/piral-hooks-utils.json src --exclude \"src/**/*.test.*\"",
40
+ "test": "echo \"Error: run tests from root\" && exit 1"
41
+ },
42
+ "gitHead": "5da9d7fe4793668e956e96470aef95b41f2805d7"
43
+ }
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+
3
+ export const PiletApiProvider = React.createContext<any>(undefined);
@@ -0,0 +1 @@
1
+ export * from './PiletApiProvider';
@@ -0,0 +1 @@
1
+ export * from './withProvider';
@@ -0,0 +1,23 @@
1
+ import * as React from 'react';
2
+ import { PiletApiProvider } from '../contexts';
3
+
4
+ interface BaseComponentProps {
5
+ /**
6
+ * The currently used pilet API.
7
+ */
8
+ piral: any;
9
+ }
10
+
11
+ /**
12
+ * Wraps the component in a Pilet API provider allowing to make use of
13
+ * the `usePilet` hook.
14
+ * @param Component The component that should be wrapped in a provider.
15
+ * @returns The wrapped component.
16
+ */
17
+ export function withPiletApi<T>(Component: React.FC<T>): React.FC<T & BaseComponentProps> {
18
+ return (props) => (
19
+ <PiletApiProvider.Provider value={props.piral}>
20
+ <Component {...props} />
21
+ </PiletApiProvider.Provider>
22
+ );
23
+ }
@@ -0,0 +1,29 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+
3
+ /**
4
+ * A hook to indicate something was suddenly visible.
5
+ * @param time The time after which it should be triggered.
6
+ * @param cb The callback to use when the part was visible.
7
+ * @returns The reference to the element which should be watched.
8
+ */
9
+ export function useAfterVisible(time: number, cb: () => void) {
10
+ const ref = useRef<HTMLDivElement>(null);
11
+ const [intersecting, setIntersecting] = useState(false);
12
+
13
+ useEffect(() => {
14
+ const observer = new IntersectionObserver((ev) => {
15
+ setIntersecting(ev.some((m) => m.isIntersecting));
16
+ });
17
+ observer.observe(ref.current);
18
+ return () => observer.disconnect();
19
+ }, [ref.current]);
20
+
21
+ useEffect(() => {
22
+ if (intersecting) {
23
+ const tid = setTimeout(cb, time);
24
+ return () => clearTimeout(tid);
25
+ }
26
+ }, [intersecting, cb]);
27
+
28
+ return ref;
29
+ }
@@ -0,0 +1,32 @@
1
+ import { useState, useEffect } from 'react';
2
+
3
+ /**
4
+ * Gives a full async lifecycle in a hook.
5
+ * @param action The lazy loader.
6
+ * @param resolve The resolve function getting the result from the promise.
7
+ * @returns The current executing state, and the trigger to start the async operation.
8
+ */
9
+ export function useAsyncReplace<T = void>(
10
+ action: () => Promise<T>,
11
+ resolve: (result: T) => void = () => {},
12
+ ): [boolean, () => void, Error | undefined] {
13
+ const [executing, setExecuting] = useState<boolean | Error>(false);
14
+ const error = executing instanceof Error ? executing : undefined;
15
+
16
+ useEffect(() => {
17
+ if (executing === true) {
18
+ let active = true;
19
+
20
+ action().then(
21
+ (result) => active && resolve(result),
22
+ (err) => active && setExecuting(err),
23
+ );
24
+
25
+ return () => (active = false);
26
+ }
27
+
28
+ return () => {};
29
+ }, [executing]);
30
+
31
+ return [executing === true, () => setExecuting(true), error];
32
+ }
@@ -0,0 +1,7 @@
1
+ export * from './afterVisible';
2
+ export * from './asyncReplace';
3
+ export * from './lockBodyScroll';
4
+ export * from './onClickOutside';
5
+ export * from './onScreenVisible';
6
+ export * from './piletApi';
7
+ export * from './prompt';
@@ -0,0 +1,24 @@
1
+ import * as React from 'react';
2
+ import { useLockBodyScroll } from './lockBodyScroll';
3
+
4
+ jest.mock('react');
5
+
6
+ describe('LockBodyScroll Hook Module', () => {
7
+ it('sets the body overflow to hidden on being initiated', () => {
8
+ const usedEffect = jest.fn();
9
+ (React as any).useLayoutEffect = usedEffect;
10
+ useLockBodyScroll();
11
+ expect(usedEffect).toHaveBeenCalled();
12
+ usedEffect.mock.calls[0][0]();
13
+ expect(document.body.style.overflow).toBe('hidden');
14
+ });
15
+
16
+ it('sets the body overflow to visible on cleanup', () => {
17
+ const usedEffect = jest.fn();
18
+ (React as any).useLayoutEffect = usedEffect;
19
+ useLockBodyScroll();
20
+ const cleanup = usedEffect.mock.calls[0][0]();
21
+ cleanup();
22
+ expect(document.body.style.overflow).toBe('visible');
23
+ });
24
+ });
@@ -0,0 +1,15 @@
1
+ import { useLayoutEffect } from 'react';
2
+
3
+ /**
4
+ * Hook that locks scrolling on the main document.
5
+ * Useful for preventing the standard scrolling in context of
6
+ * a modal dialog.
7
+ */
8
+ export function useLockBodyScroll() {
9
+ useLayoutEffect(() => {
10
+ document.body.style.overflow = 'hidden';
11
+ return () => {
12
+ document.body.style.overflow = 'visible';
13
+ };
14
+ }, []);
15
+ }
@@ -0,0 +1,113 @@
1
+ import * as React from 'react';
2
+ import { useOnClickOutside } from './onClickOutside';
3
+
4
+ jest.mock('react');
5
+
6
+ describe('OnClickOutside Module', () => {
7
+ it('is properly hooked on when initiating the effect', () => {
8
+ const usedEffect = jest.fn();
9
+ const originalAdd = document.addEventListener;
10
+ const originalRemove = document.addEventListener;
11
+ document.addEventListener = jest.fn();
12
+ document.removeEventListener = jest.fn();
13
+ (React as any).useEffect = usedEffect;
14
+ useOnClickOutside({ current: {} as any }, jest.fn());
15
+ expect(usedEffect).toHaveBeenCalled();
16
+ const cleanup = usedEffect.mock.calls[0][0]();
17
+ cleanup();
18
+ expect(document.addEventListener).toHaveBeenCalledTimes(2);
19
+ document.addEventListener = originalAdd;
20
+ document.removeEventListener = originalRemove;
21
+ });
22
+
23
+ it('is properly cleaned up when leaving the effect', () => {
24
+ const usedEffect = jest.fn();
25
+ const originalAdd = document.addEventListener;
26
+ const originalRemove = document.addEventListener;
27
+ document.addEventListener = jest.fn();
28
+ document.removeEventListener = jest.fn();
29
+ (React as any).useEffect = usedEffect;
30
+ useOnClickOutside({ current: {} as any }, jest.fn());
31
+ expect(usedEffect).toHaveBeenCalled();
32
+ const cleanup = usedEffect.mock.calls[0][0]();
33
+ cleanup();
34
+ expect(document.removeEventListener).toHaveBeenCalledTimes(2);
35
+ document.addEventListener = originalAdd;
36
+ document.removeEventListener = originalRemove;
37
+ });
38
+
39
+ it('not calling the handler on mousedown if no ref is available', () => {
40
+ const usedEffect = jest.fn();
41
+ const handler = jest.fn();
42
+ (React as any).useEffect = usedEffect;
43
+ useOnClickOutside({ current: undefined }, handler);
44
+ const cleanup = usedEffect.mock.calls[0][0]();
45
+ const event = new Event('mousedown', { bubbles: true });
46
+ document.body.dispatchEvent(event);
47
+ cleanup();
48
+ expect(handler).not.toHaveBeenCalled();
49
+ });
50
+
51
+ it('not calling the handler on mousedown if ref is available, but contains the node', () => {
52
+ const usedEffect = jest.fn();
53
+ const handler = jest.fn();
54
+ (React as any).useEffect = usedEffect;
55
+ useOnClickOutside(
56
+ {
57
+ current: {
58
+ contains() {
59
+ return true;
60
+ },
61
+ } as any,
62
+ },
63
+ handler,
64
+ );
65
+ const cleanup = usedEffect.mock.calls[0][0]();
66
+ const event = new Event('mousedown', { bubbles: true });
67
+ document.body.dispatchEvent(event);
68
+ cleanup();
69
+ expect(handler).not.toHaveBeenCalled();
70
+ });
71
+
72
+ it('called the handler on mousedown if ref is available and does not contain node', () => {
73
+ const usedEffect = jest.fn();
74
+ const handler = jest.fn();
75
+ (React as any).useEffect = usedEffect;
76
+ useOnClickOutside(
77
+ {
78
+ current: {
79
+ contains() {
80
+ return false;
81
+ },
82
+ } as any,
83
+ },
84
+ handler,
85
+ );
86
+ const cleanup = usedEffect.mock.calls[0][0]();
87
+ const event = new Event('mousedown', { bubbles: true });
88
+ document.body.dispatchEvent(event);
89
+ cleanup();
90
+ expect(handler).toHaveBeenCalledWith(event);
91
+ });
92
+
93
+ it('called the handler on touchstart if ref is available and does not contain node', () => {
94
+ const usedEffect = jest.fn();
95
+ const handler = jest.fn();
96
+ (React as any).useEffect = usedEffect;
97
+ useOnClickOutside(
98
+ {
99
+ current: {
100
+ contains() {
101
+ return false;
102
+ },
103
+ } as any,
104
+ },
105
+ handler,
106
+ );
107
+ const cleanup = usedEffect.mock.calls[0][0]();
108
+ const event = new Event('touchstart', { bubbles: true });
109
+ document.body.dispatchEvent(event);
110
+ cleanup();
111
+ expect(handler).toHaveBeenCalledWith(event);
112
+ });
113
+ });
@@ -0,0 +1,25 @@
1
+ import { useEffect, RefObject } from 'react';
2
+
3
+ /**
4
+ * Hook that detects if a click outside the given reference
5
+ * has been performed.
6
+ * @param ref The reference to the element.
7
+ * @param handler The callback to invoke when an outside click happened.
8
+ */
9
+ export function useOnClickOutside<T extends HTMLElement>(ref: RefObject<T>, handler: (event: MouseEvent) => void) {
10
+ useEffect(() => {
11
+ const listener = (event: MouseEvent) => {
12
+ if (ref.current && !ref.current.contains(event.target as Node)) {
13
+ handler(event);
14
+ }
15
+ };
16
+
17
+ document.addEventListener('mousedown', listener);
18
+ document.addEventListener('touchstart', listener);
19
+
20
+ return () => {
21
+ document.removeEventListener('mousedown', listener);
22
+ document.removeEventListener('touchstart', listener);
23
+ };
24
+ }, [handler]);
25
+ }