@jobber/hooks 2.0.3-dar.45 → 2.0.3-dar.47

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 (44) hide show
  1. package/package.json +4 -3
  2. package/src/index.ts +10 -0
  3. package/src/useAssert/index.ts +1 -0
  4. package/src/useAssert/useAssert.stories.mdx +32 -0
  5. package/src/useAssert/useAssert.tsx +19 -0
  6. package/src/useCollectionQuery/index.ts +1 -0
  7. package/src/useCollectionQuery/mdxUtils.ts +190 -0
  8. package/src/useCollectionQuery/test-utilities/index.ts +3 -0
  9. package/src/useCollectionQuery/test-utilities/mocks.tsx +147 -0
  10. package/src/useCollectionQuery/test-utilities/queries.ts +95 -0
  11. package/src/useCollectionQuery/test-utilities/utils.ts +3 -0
  12. package/src/useCollectionQuery/uniqueEdges.tsx +26 -0
  13. package/src/useCollectionQuery/uniqueNodes.tsx +12 -0
  14. package/src/useCollectionQuery/useCollectionQuery.stories.mdx +129 -0
  15. package/src/useCollectionQuery/useCollectionQuery.test.tsx +419 -0
  16. package/src/useCollectionQuery/useCollectionQuery.ts +359 -0
  17. package/src/useFocusTrap/index.ts +1 -0
  18. package/src/useFocusTrap/useFocusTrap.stories.mdx +49 -0
  19. package/src/useFocusTrap/useFocusTrap.test.tsx +66 -0
  20. package/src/useFocusTrap/useFocusTrap.ts +64 -0
  21. package/src/useFormState/index.ts +1 -0
  22. package/src/useFormState/useFormState.stories.mdx +70 -0
  23. package/src/useFormState/useFormState.ts +10 -0
  24. package/src/useIsMounted/index.ts +1 -0
  25. package/src/useIsMounted/useIsMounted.stories.mdx +59 -0
  26. package/src/useIsMounted/useIsMounted.test.tsx +18 -0
  27. package/src/useIsMounted/useIsMounted.ts +30 -0
  28. package/src/useLiveAnnounce/index.ts +1 -0
  29. package/src/useLiveAnnounce/useLiveAnnounce.stories.mdx +38 -0
  30. package/src/useLiveAnnounce/useLiveAnnounce.test.tsx +55 -0
  31. package/src/useLiveAnnounce/useLiveAnnounce.tsx +47 -0
  32. package/src/useOnKeyDown/index.ts +1 -0
  33. package/src/useOnKeyDown/useOnKeyDown.stories.mdx +67 -0
  34. package/src/useOnKeyDown/useOnKeyDown.test.tsx +31 -0
  35. package/src/useOnKeyDown/useOnKeyDown.ts +52 -0
  36. package/src/usePasswordStrength/index.ts +1 -0
  37. package/src/usePasswordStrength/usePasswordStrength.stories.mdx +51 -0
  38. package/src/usePasswordStrength/usePasswordStrength.ts +21 -0
  39. package/src/useRefocusOnActivator/index.ts +1 -0
  40. package/src/useRefocusOnActivator/useRefocusOnActivator.stories.mdx +39 -0
  41. package/src/useRefocusOnActivator/useRefocusOnActivator.ts +26 -0
  42. package/src/useResizeObserver/index.ts +1 -0
  43. package/src/useResizeObserver/useResizeObserver.stories.mdx +134 -0
  44. package/src/useResizeObserver/useResizeObserver.ts +78 -0
@@ -0,0 +1,78 @@
1
+ import { useMemo, useState } from "react";
2
+ // Importing the polyfilled version of ResizeObserver
3
+ // eslint-disable-next-line import/no-internal-modules
4
+ import useResizeObserverPackage from "use-resize-observer/polyfilled";
5
+ import { throttle } from "lodash";
6
+
7
+ export const Breakpoints = {
8
+ base: 640,
9
+ small: 500,
10
+ smaller: 265,
11
+ large: 750,
12
+ larger: 1024,
13
+ };
14
+
15
+ interface ObservedSize {
16
+ width: number | undefined;
17
+ height: number | undefined;
18
+ }
19
+
20
+ interface ResizeObserverProps {
21
+ widths?: object;
22
+ heights?: object;
23
+ }
24
+
25
+ const wait = 100;
26
+
27
+ export function useResizeObserver<T extends HTMLElement>({
28
+ widths = Breakpoints,
29
+ heights = Breakpoints,
30
+ }: ResizeObserverProps = {}) {
31
+ const [exactSize, setSize] = useState<ObservedSize>({
32
+ width: undefined,
33
+ height: undefined,
34
+ });
35
+ const onResize = useMemo(() => throttle(setSize, wait), [wait]);
36
+ const { ref } = useResizeObserverPackage<T>({
37
+ onResize,
38
+ });
39
+
40
+ const exactWidth = exactSize.width;
41
+ const exactHeight = exactSize.height;
42
+
43
+ const sizes = {
44
+ width: getSize(widths, exactSize.width),
45
+ height: getSize(heights, exactSize.height),
46
+ exactWidth,
47
+ exactHeight,
48
+ };
49
+
50
+ return [ref, sizes] as const;
51
+ }
52
+
53
+ /**
54
+ * To get the proper size we want to make sure that our value is greater
55
+ * then the comparable, but less then the next largest number in our
56
+ * object.
57
+ */
58
+ function getSize(
59
+ sizes: object,
60
+ comparable: number | undefined,
61
+ ): number | undefined {
62
+ if (!comparable || !sizes) {
63
+ return undefined;
64
+ }
65
+
66
+ /**
67
+ * Sort the values of our object so that we know they are in proper
68
+ * order to be compared by
69
+ */
70
+ const sortedSizes = Object.values(sizes)
71
+ .sort((a, b) => a - b)
72
+ .reverse();
73
+
74
+ return (
75
+ sortedSizes.find(value => value <= comparable) ||
76
+ sortedSizes[sortedSizes.length - 1]
77
+ );
78
+ }