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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 - 2022 smapiot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ [![Piral Logo](https://github.com/smapiot/piral/raw/main/docs/assets/logo.png)](https://piral.io)
2
+
3
+ # [Piral Hooks Utils](https://piral.io) · [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/piral-hooks-utils.svg?style=flat)](https://www.npmjs.com/package/piral-hooks-utils) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://jestjs.io) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)
4
+
5
+ A set of hooks and HOCs for pilets and Piral instances.
6
+
7
+ ## Installation
8
+
9
+ This should only be installed as a dependency (`dependencies`) as it is only designed mostly for runtime / consumption by Piral or pilets. You could also share it explicitly or implicitly from your Piral instance if you'd like to optimize usage in your pilets. It's not required though as the code provided by it is minimal and tree-shakable.
10
+
11
+ If you'd love to use yarn:
12
+
13
+ ```sh
14
+ yarn add piral-hooks-utils
15
+ ```
16
+
17
+ Alternatively, npm works reliably, too:
18
+
19
+ ```sh
20
+ npm i --save piral-hooks-utils
21
+ ```
22
+
23
+ ## Included Hooks
24
+
25
+ The following hooks are included.
26
+
27
+ ### `useAfterVisible`
28
+
29
+ A hook to indicate something was suddenly visible.
30
+
31
+ ### `useAsyncReplace`
32
+
33
+ Gives a full async lifecycle in a hook.
34
+
35
+ ### `useLockBodyScroll`
36
+
37
+ Hook that locks scrolling on the main document.
38
+
39
+ ### `useOnClickOutside`
40
+
41
+ Hook that detects if a click outside the given reference has been performed.
42
+
43
+ ### `useOnScreenVisible`
44
+
45
+ Hook that detects if a reference element within the main document is visible.
46
+
47
+ ### `usePiletApi`
48
+
49
+ Retrieves the Pilet API stored in the provider.
50
+
51
+ ### `usePrompt`
52
+
53
+ Hook to notify the user in case of potential data loss when performing a page transition (internal or external).
54
+
55
+ ## License
56
+
57
+ Piral is released using the MIT license. For more information see the [license file](./LICENSE).
@@ -0,0 +1,2 @@
1
+ import * as React from 'react';
2
+ export declare const PiletApiProvider: React.Context<any>;
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ export const PiletApiProvider = React.createContext(undefined);
3
+ //# sourceMappingURL=PiletApiProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PiletApiProvider.js","sourceRoot":"","sources":["../../src/contexts/PiletApiProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,aAAa,CAAM,SAAS,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './PiletApiProvider';
@@ -0,0 +1,2 @@
1
+ export * from './PiletApiProvider';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/contexts/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './withProvider';
@@ -0,0 +1,2 @@
1
+ export * from './withProvider';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hocs/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+ interface BaseComponentProps {
3
+ /**
4
+ * The currently used pilet API.
5
+ */
6
+ piral: any;
7
+ }
8
+ /**
9
+ * Wraps the component in a Pilet API provider allowing to make use of
10
+ * the `usePilet` hook.
11
+ * @param Component The component that should be wrapped in a provider.
12
+ * @returns The wrapped component.
13
+ */
14
+ export declare function withPiletApi<T>(Component: React.FC<T>): React.FC<T & BaseComponentProps>;
15
+ export {};
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+ import { PiletApiProvider } from '../contexts';
3
+ /**
4
+ * Wraps the component in a Pilet API provider allowing to make use of
5
+ * the `usePilet` hook.
6
+ * @param Component The component that should be wrapped in a provider.
7
+ * @returns The wrapped component.
8
+ */
9
+ export function withPiletApi(Component) {
10
+ return (props) => (React.createElement(PiletApiProvider.Provider, { value: props.piral },
11
+ React.createElement(Component, Object.assign({}, props))));
12
+ }
13
+ //# sourceMappingURL=withProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withProvider.js","sourceRoot":"","sources":["../../src/hocs/withProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAS/C;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAI,SAAsB;IACpD,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAChB,oBAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,CAAC,KAAK;QAC3C,oBAAC,SAAS,oBAAK,KAAK,EAAI,CACE,CAC7B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ /**
3
+ * A hook to indicate something was suddenly visible.
4
+ * @param time The time after which it should be triggered.
5
+ * @param cb The callback to use when the part was visible.
6
+ * @returns The reference to the element which should be watched.
7
+ */
8
+ export declare function useAfterVisible(time: number, cb: () => void): import("react").MutableRefObject<HTMLDivElement>;
@@ -0,0 +1,26 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ /**
3
+ * A hook to indicate something was suddenly visible.
4
+ * @param time The time after which it should be triggered.
5
+ * @param cb The callback to use when the part was visible.
6
+ * @returns The reference to the element which should be watched.
7
+ */
8
+ export function useAfterVisible(time, cb) {
9
+ const ref = useRef(null);
10
+ const [intersecting, setIntersecting] = useState(false);
11
+ useEffect(() => {
12
+ const observer = new IntersectionObserver((ev) => {
13
+ setIntersecting(ev.some((m) => m.isIntersecting));
14
+ });
15
+ observer.observe(ref.current);
16
+ return () => observer.disconnect();
17
+ }, [ref.current]);
18
+ useEffect(() => {
19
+ if (intersecting) {
20
+ const tid = setTimeout(cb, time);
21
+ return () => clearTimeout(tid);
22
+ }
23
+ }, [intersecting, cb]);
24
+ return ref;
25
+ }
26
+ //# sourceMappingURL=afterVisible.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"afterVisible.js","sourceRoot":"","sources":["../../src/hooks/afterVisible.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,EAAc;IAC1D,MAAM,GAAG,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IACzC,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAExD,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,CAAC,EAAE,EAAE,EAAE;YAC/C,eAAe,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IACrC,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;IAElB,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,YAAY,EAAE;YAChB,MAAM,GAAG,GAAG,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;YACjC,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;SAChC;IACH,CAAC,EAAE,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC;IAEvB,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Gives a full async lifecycle in a hook.
3
+ * @param action The lazy loader.
4
+ * @param resolve The resolve function getting the result from the promise.
5
+ * @returns The current executing state, and the trigger to start the async operation.
6
+ */
7
+ export declare function useAsyncReplace<T = void>(action: () => Promise<T>, resolve?: (result: T) => void): [boolean, () => void, Error | undefined];
@@ -0,0 +1,21 @@
1
+ import { useState, useEffect } from 'react';
2
+ /**
3
+ * Gives a full async lifecycle in a hook.
4
+ * @param action The lazy loader.
5
+ * @param resolve The resolve function getting the result from the promise.
6
+ * @returns The current executing state, and the trigger to start the async operation.
7
+ */
8
+ export function useAsyncReplace(action, resolve = () => { }) {
9
+ const [executing, setExecuting] = useState(false);
10
+ const error = executing instanceof Error ? executing : undefined;
11
+ useEffect(() => {
12
+ if (executing === true) {
13
+ let active = true;
14
+ action().then((result) => active && resolve(result), (err) => active && setExecuting(err));
15
+ return () => (active = false);
16
+ }
17
+ return () => { };
18
+ }, [executing]);
19
+ return [executing === true, () => setExecuting(true), error];
20
+ }
21
+ //# sourceMappingURL=asyncReplace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asyncReplace.js","sourceRoot":"","sources":["../../src/hooks/asyncReplace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAwB,EACxB,UAA+B,GAAG,EAAE,GAAE,CAAC;IAEvC,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAkB,KAAK,CAAC,CAAC;IACnE,MAAM,KAAK,GAAG,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IAEjE,SAAS,CAAC,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"}
@@ -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,8 @@
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';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
@@ -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,15 @@
1
+ import { useLayoutEffect } from 'react';
2
+ /**
3
+ * Hook that locks scrolling on the main document.
4
+ * Useful for preventing the standard scrolling in context of
5
+ * a modal dialog.
6
+ */
7
+ export function useLockBodyScroll() {
8
+ useLayoutEffect(() => {
9
+ document.body.style.overflow = 'hidden';
10
+ return () => {
11
+ document.body.style.overflow = 'visible';
12
+ };
13
+ }, []);
14
+ }
15
+ //# sourceMappingURL=lockBodyScroll.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lockBodyScroll.js","sourceRoot":"","sources":["../../src/hooks/lockBodyScroll.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAExC;;;;GAIG;AACH,MAAM,UAAU,iBAAiB;IAC/B,eAAe,CAAC,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"}
@@ -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,23 @@
1
+ import { useEffect } 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 function useOnClickOutside(ref, handler) {
9
+ useEffect(() => {
10
+ const listener = (event) => {
11
+ if (ref.current && !ref.current.contains(event.target)) {
12
+ handler(event);
13
+ }
14
+ };
15
+ document.addEventListener('mousedown', listener);
16
+ document.addEventListener('touchstart', listener);
17
+ return () => {
18
+ document.removeEventListener('mousedown', listener);
19
+ document.removeEventListener('touchstart', listener);
20
+ };
21
+ }, [handler]);
22
+ }
23
+ //# sourceMappingURL=onClickOutside.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onClickOutside.js","sourceRoot":"","sources":["../../src/hooks/onClickOutside.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAa,MAAM,OAAO,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAwB,GAAiB,EAAE,OAAoC;IAC9G,SAAS,CAAC,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"}
@@ -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,24 @@
1
+ import { useState, useEffect } 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 function useOnScreenVisible(ref, rootMargin = '0px') {
12
+ const [isIntersecting, setIntersecting] = useState(false);
13
+ useEffect(() => {
14
+ const observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting), {
15
+ rootMargin,
16
+ });
17
+ if (ref.current) {
18
+ observer.observe(ref.current);
19
+ }
20
+ return () => observer.unobserve(ref.current);
21
+ }, []);
22
+ return isIntersecting;
23
+ }
24
+ //# sourceMappingURL=onScreenVisible.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onScreenVisible.js","sourceRoot":"","sources":["../../src/hooks/onScreenVisible.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAa,MAAM,OAAO,CAAC;AAEvD;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAwB,GAAiB,EAAE,UAAU,GAAG,KAAK;IAC7F,MAAM,CAAC,cAAc,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAE1D,SAAS,CAAC,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"}
@@ -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,12 @@
1
+ import { useContext } from 'react';
2
+ import { PiletApiProvider } from '../contexts';
3
+ /**
4
+ * Retrieves the Pilet API stored in the provider.
5
+ * The component must be somewhat wrapped in the
6
+ * PiletApiProvider, e.g., via withPiletApi wrapper.
7
+ * @returns The Pilet API for this component.
8
+ */
9
+ export function usePiletApi() {
10
+ return useContext(PiletApiProvider);
11
+ }
12
+ //# sourceMappingURL=piletApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"piletApi.js","sourceRoot":"","sources":["../../src/hooks/piletApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,UAAU,CAAC,gBAAgB,CAAC,CAAC;AACtC,CAAC"}
@@ -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,28 @@
1
+ import { useEffect } from 'react';
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 function usePrompt(active, history, message, onTransition) {
10
+ useEffect(() => {
11
+ if (active) {
12
+ const beforeUnload = (ev) => {
13
+ ev.returnValue = message;
14
+ return message;
15
+ };
16
+ const unlisten = onTransition && history.listen(onTransition);
17
+ const unblock = message && history.block(message);
18
+ window.addEventListener('beforeunload', beforeUnload);
19
+ return () => {
20
+ unlisten && unlisten();
21
+ unblock && unblock();
22
+ window.removeEventListener('beforeunload', beforeUnload);
23
+ };
24
+ }
25
+ return () => { };
26
+ }, [active, message]);
27
+ }
28
+ //# sourceMappingURL=prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/hooks/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC;;;;;;GAMG;AACF,MAAM,UAAU,SAAS,CACxB,MAAe,EACf,OAAgB,EAChB,OAAe,EACf,YAA2C;IAE3C,SAAS,CAAC,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"}
package/esm/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './contexts';
2
+ export * from './hooks';
3
+ export * from './hocs';
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './contexts';
2
+ export * from './hooks';
3
+ export * from './hocs';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ import * as React from 'react';
2
+ export declare const PiletApiProvider: React.Context<any>;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PiletApiProvider = void 0;
4
+ const React = require("react");
5
+ exports.PiletApiProvider = React.createContext(undefined);
6
+ //# sourceMappingURL=PiletApiProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PiletApiProvider.js","sourceRoot":"","sources":["../../src/contexts/PiletApiProvider.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAElB,QAAA,gBAAgB,GAAG,KAAK,CAAC,aAAa,CAAM,SAAS,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './PiletApiProvider';
@@ -0,0 +1,14 @@
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("./PiletApiProvider"), exports);
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/contexts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmC"}
@@ -0,0 +1 @@
1
+ export * from './withProvider';
@@ -0,0 +1,14 @@
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("./withProvider"), exports);
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hocs/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B"}
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+ interface BaseComponentProps {
3
+ /**
4
+ * The currently used pilet API.
5
+ */
6
+ piral: any;
7
+ }
8
+ /**
9
+ * Wraps the component in a Pilet API provider allowing to make use of
10
+ * the `usePilet` hook.
11
+ * @param Component The component that should be wrapped in a provider.
12
+ * @returns The wrapped component.
13
+ */
14
+ export declare function withPiletApi<T>(Component: React.FC<T>): React.FC<T & BaseComponentProps>;
15
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withPiletApi = void 0;
4
+ const React = require("react");
5
+ const contexts_1 = require("../contexts");
6
+ /**
7
+ * Wraps the component in a Pilet API provider allowing to make use of
8
+ * the `usePilet` hook.
9
+ * @param Component The component that should be wrapped in a provider.
10
+ * @returns The wrapped component.
11
+ */
12
+ function withPiletApi(Component) {
13
+ return (props) => (React.createElement(contexts_1.PiletApiProvider.Provider, { value: props.piral },
14
+ React.createElement(Component, Object.assign({}, props))));
15
+ }
16
+ exports.withPiletApi = withPiletApi;
17
+ //# sourceMappingURL=withProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withProvider.js","sourceRoot":"","sources":["../../src/hocs/withProvider.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,0CAA+C;AAS/C;;;;;GAKG;AACH,SAAgB,YAAY,CAAI,SAAsB;IACpD,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAChB,oBAAC,2BAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,CAAC,KAAK;QAC3C,oBAAC,SAAS,oBAAK,KAAK,EAAI,CACE,CAC7B,CAAC;AACJ,CAAC;AAND,oCAMC"}
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ /**
3
+ * A hook to indicate something was suddenly visible.
4
+ * @param time The time after which it should be triggered.
5
+ * @param cb The callback to use when the part was visible.
6
+ * @returns The reference to the element which should be watched.
7
+ */
8
+ export declare function useAfterVisible(time: number, cb: () => void): import("react").MutableRefObject<HTMLDivElement>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useAfterVisible = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * A hook to indicate something was suddenly visible.
7
+ * @param time The time after which it should be triggered.
8
+ * @param cb The callback to use when the part was visible.
9
+ * @returns The reference to the element which should be watched.
10
+ */
11
+ function useAfterVisible(time, cb) {
12
+ const ref = (0, react_1.useRef)(null);
13
+ const [intersecting, setIntersecting] = (0, react_1.useState)(false);
14
+ (0, react_1.useEffect)(() => {
15
+ const observer = new IntersectionObserver((ev) => {
16
+ setIntersecting(ev.some((m) => m.isIntersecting));
17
+ });
18
+ observer.observe(ref.current);
19
+ return () => observer.disconnect();
20
+ }, [ref.current]);
21
+ (0, react_1.useEffect)(() => {
22
+ if (intersecting) {
23
+ const tid = setTimeout(cb, time);
24
+ return () => clearTimeout(tid);
25
+ }
26
+ }, [intersecting, cb]);
27
+ return ref;
28
+ }
29
+ exports.useAfterVisible = useAfterVisible;
30
+ //# sourceMappingURL=afterVisible.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"afterVisible.js","sourceRoot":"","sources":["../../src/hooks/afterVisible.ts"],"names":[],"mappings":";;;AAAA,iCAAoD;AAEpD;;;;;GAKG;AACH,SAAgB,eAAe,CAAC,IAAY,EAAE,EAAc;IAC1D,MAAM,GAAG,GAAG,IAAA,cAAM,EAAiB,IAAI,CAAC,CAAC;IACzC,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAExD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,CAAC,EAAE,EAAE,EAAE;YAC/C,eAAe,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IACrC,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;IAElB,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,YAAY,EAAE;YAChB,MAAM,GAAG,GAAG,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;YACjC,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;SAChC;IACH,CAAC,EAAE,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC;IAEvB,OAAO,GAAG,CAAC;AACb,CAAC;AApBD,0CAoBC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Gives a full async lifecycle in a hook.
3
+ * @param action The lazy loader.
4
+ * @param resolve The resolve function getting the result from the promise.
5
+ * @returns The current executing state, and the trigger to start the async operation.
6
+ */
7
+ export declare function useAsyncReplace<T = void>(action: () => Promise<T>, resolve?: (result: T) => void): [boolean, () => void, Error | undefined];
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useAsyncReplace = void 0;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Gives a full async lifecycle in a hook.
7
+ * @param action The lazy loader.
8
+ * @param resolve The resolve function getting the result from the promise.
9
+ * @returns The current executing state, and the trigger to start the async operation.
10
+ */
11
+ function useAsyncReplace(action, resolve = () => { }) {
12
+ const [executing, setExecuting] = (0, react_1.useState)(false);
13
+ const error = executing instanceof Error ? executing : undefined;
14
+ (0, react_1.useEffect)(() => {
15
+ if (executing === true) {
16
+ let active = true;
17
+ action().then((result) => active && resolve(result), (err) => active && setExecuting(err));
18
+ return () => (active = false);
19
+ }
20
+ return () => { };
21
+ }, [executing]);
22
+ return [executing === true, () => setExecuting(true), error];
23
+ }
24
+ exports.useAsyncReplace = useAsyncReplace;
25
+ //# sourceMappingURL=asyncReplace.js.map