@contember/react-utils 1.2.0-alpha.0 → 1.2.0-alpha.10

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 (155) hide show
  1. package/dist/development/context/createNonNullableContextFactory.js +17 -0
  2. package/dist/development/context/createNonNullableContextFactory.js.map +1 -0
  3. package/dist/development/debug-context/Constants.js +25 -0
  4. package/dist/development/debug-context/Constants.js.map +1 -0
  5. package/dist/development/debug-context/Context.js +71 -0
  6. package/dist/development/debug-context/Context.js.map +1 -0
  7. package/dist/development/hooks/unwrapRefValue.js +11 -0
  8. package/dist/development/hooks/unwrapRefValue.js.map +1 -0
  9. package/dist/development/hooks/useAddClassNameDuringResize.js +34 -0
  10. package/dist/development/hooks/useAddClassNameDuringResize.js.map +1 -0
  11. package/dist/development/hooks/useArrayMapMemo.js +2 -0
  12. package/dist/development/hooks/useArrayMapMemo.js.map +1 -1
  13. package/dist/development/hooks/useComposeRef.js +31 -0
  14. package/dist/development/hooks/useComposeRef.js.map +1 -0
  15. package/dist/development/hooks/useConstantLengthInvariant.js +9 -0
  16. package/dist/development/hooks/useConstantLengthInvariant.js.map +1 -1
  17. package/dist/development/hooks/useConstantValueInvariant.js +9 -0
  18. package/dist/development/hooks/useConstantValueInvariant.js.map +1 -1
  19. package/dist/development/hooks/useElementSize.js +47 -0
  20. package/dist/development/hooks/useElementSize.js.map +1 -0
  21. package/dist/development/hooks/useEventHandler.js +19 -0
  22. package/dist/development/hooks/useEventHandler.js.map +1 -0
  23. package/dist/development/hooks/useExpectSameValueReference.js +21 -0
  24. package/dist/development/hooks/useExpectSameValueReference.js.map +1 -0
  25. package/dist/development/hooks/useOnElementMutation.js +35 -0
  26. package/dist/development/hooks/useOnElementMutation.js.map +1 -0
  27. package/dist/development/hooks/useOnElementResize.js +44 -0
  28. package/dist/development/hooks/useOnElementResize.js.map +1 -0
  29. package/dist/development/hooks/useOnScrollWithin.js +50 -0
  30. package/dist/development/hooks/useOnScrollWithin.js.map +1 -0
  31. package/dist/development/hooks/useOnWindowResize.js +36 -0
  32. package/dist/development/hooks/useOnWindowResize.js.map +1 -0
  33. package/dist/development/hooks/useScrollOffsets.js +68 -0
  34. package/dist/development/hooks/useScrollOffsets.js.map +1 -0
  35. package/dist/development/hooks/useStoredState.js.map +1 -1
  36. package/dist/development/hooks/useUpdatedRef.js +10 -0
  37. package/dist/development/hooks/useUpdatedRef.js.map +1 -0
  38. package/dist/development/hooks/useWindowSize.js +20 -0
  39. package/dist/development/hooks/useWindowSize.js.map +1 -0
  40. package/dist/development/index.js +36 -1
  41. package/dist/development/index.js.map +1 -1
  42. package/dist/production/context/createNonNullableContextFactory.js +17 -0
  43. package/dist/production/context/createNonNullableContextFactory.js.map +1 -0
  44. package/dist/production/debug-context/Constants.js +25 -0
  45. package/dist/production/debug-context/Constants.js.map +1 -0
  46. package/dist/production/debug-context/Context.js +67 -0
  47. package/dist/production/debug-context/Context.js.map +1 -0
  48. package/dist/production/hooks/unwrapRefValue.js +11 -0
  49. package/dist/production/hooks/unwrapRefValue.js.map +1 -0
  50. package/dist/production/hooks/useAddClassNameDuringResize.js +34 -0
  51. package/dist/production/hooks/useAddClassNameDuringResize.js.map +1 -0
  52. package/dist/production/hooks/useComposeRef.js +31 -0
  53. package/dist/production/hooks/useComposeRef.js.map +1 -0
  54. package/dist/production/hooks/useElementSize.js +47 -0
  55. package/dist/production/hooks/useElementSize.js.map +1 -0
  56. package/dist/production/hooks/useEventHandler.js +19 -0
  57. package/dist/production/hooks/useEventHandler.js.map +1 -0
  58. package/dist/production/hooks/useExpectSameValueReference.js +15 -0
  59. package/dist/production/hooks/useExpectSameValueReference.js.map +1 -0
  60. package/dist/production/hooks/useOnElementMutation.js +35 -0
  61. package/dist/production/hooks/useOnElementMutation.js.map +1 -0
  62. package/dist/production/hooks/useOnElementResize.js +44 -0
  63. package/dist/production/hooks/useOnElementResize.js.map +1 -0
  64. package/dist/production/hooks/useOnScrollWithin.js +50 -0
  65. package/dist/production/hooks/useOnScrollWithin.js.map +1 -0
  66. package/dist/production/hooks/useOnWindowResize.js +36 -0
  67. package/dist/production/hooks/useOnWindowResize.js.map +1 -0
  68. package/dist/production/hooks/useScrollOffsets.js +68 -0
  69. package/dist/production/hooks/useScrollOffsets.js.map +1 -0
  70. package/dist/production/hooks/useStoredState.js.map +1 -1
  71. package/dist/production/hooks/useUpdatedRef.js +10 -0
  72. package/dist/production/hooks/useUpdatedRef.js.map +1 -0
  73. package/dist/production/hooks/useWindowSize.js +20 -0
  74. package/dist/production/hooks/useWindowSize.js.map +1 -0
  75. package/dist/production/index.js +36 -1
  76. package/dist/production/index.js.map +1 -1
  77. package/dist/types/context/createNonNullableContextFactory.d.ts +3 -0
  78. package/dist/types/context/createNonNullableContextFactory.d.ts.map +1 -0
  79. package/dist/types/context/index.d.ts +2 -0
  80. package/dist/types/context/index.d.ts.map +1 -0
  81. package/dist/types/debug-context/Constants.d.ts +6 -0
  82. package/dist/types/debug-context/Constants.d.ts.map +1 -0
  83. package/dist/types/debug-context/Context.d.ts +15 -0
  84. package/dist/types/debug-context/Context.d.ts.map +1 -0
  85. package/dist/types/debug-context/Types.d.ts +13 -0
  86. package/dist/types/debug-context/Types.d.ts.map +1 -0
  87. package/dist/types/debug-context/index.d.ts +4 -0
  88. package/dist/types/debug-context/index.d.ts.map +1 -0
  89. package/dist/types/hooks/index.d.ts +12 -0
  90. package/dist/types/hooks/index.d.ts.map +1 -1
  91. package/dist/types/hooks/unwrapRefValue.d.ts +9 -0
  92. package/dist/types/hooks/unwrapRefValue.d.ts.map +1 -0
  93. package/dist/types/hooks/useAddClassNameDuringResize.d.ts +13 -0
  94. package/dist/types/hooks/useAddClassNameDuringResize.d.ts.map +1 -0
  95. package/dist/types/hooks/useComposeRef.d.ts +16 -0
  96. package/dist/types/hooks/useComposeRef.d.ts.map +1 -0
  97. package/dist/types/hooks/useElementSize.d.ts +12 -0
  98. package/dist/types/hooks/useElementSize.d.ts.map +1 -0
  99. package/dist/types/hooks/useEventHandler.d.ts +10 -0
  100. package/dist/types/hooks/useEventHandler.d.ts.map +1 -0
  101. package/dist/types/hooks/useExpectSameValueReference.d.ts +2 -0
  102. package/dist/types/hooks/useExpectSameValueReference.d.ts.map +1 -0
  103. package/dist/types/hooks/useOnElementMutation.d.ts +3 -0
  104. package/dist/types/hooks/useOnElementMutation.d.ts.map +1 -0
  105. package/dist/types/hooks/useOnElementResize.d.ts +3 -0
  106. package/dist/types/hooks/useOnElementResize.d.ts.map +1 -0
  107. package/dist/types/hooks/useOnScrollWithin.d.ts +10 -0
  108. package/dist/types/hooks/useOnScrollWithin.d.ts.map +1 -0
  109. package/dist/types/hooks/useOnWindowResize.d.ts +2 -0
  110. package/dist/types/hooks/useOnWindowResize.d.ts.map +1 -0
  111. package/dist/types/hooks/useScrollOffsets.d.ts +14 -0
  112. package/dist/types/hooks/useScrollOffsets.d.ts.map +1 -0
  113. package/dist/types/hooks/useStoredState.d.ts +2 -3
  114. package/dist/types/hooks/useStoredState.d.ts.map +1 -1
  115. package/dist/types/hooks/useUpdatedRef.d.ts +8 -0
  116. package/dist/types/hooks/useUpdatedRef.d.ts.map +1 -0
  117. package/dist/types/hooks/useWindowSize.d.ts +5 -0
  118. package/dist/types/hooks/useWindowSize.d.ts.map +1 -0
  119. package/dist/types/index.d.ts +2 -0
  120. package/dist/types/index.d.ts.map +1 -1
  121. package/dist/types/tsconfig.tsbuildinfo +1 -1
  122. package/dist/types/types/PropsWithRequiredChildren.d.ts +5 -0
  123. package/dist/types/types/PropsWithRequiredChildren.d.ts.map +1 -0
  124. package/dist/types/{types.d.ts → types/Serializable.d.ts} +1 -1
  125. package/dist/types/types/Serializable.d.ts.map +1 -0
  126. package/dist/types/types/index.d.ts +3 -0
  127. package/dist/types/types/index.d.ts.map +1 -0
  128. package/package.json +6 -2
  129. package/src/context/createNonNullableContextFactory.ts +19 -0
  130. package/src/context/index.ts +1 -0
  131. package/src/debug-context/Constants.ts +22 -0
  132. package/src/debug-context/Context.tsx +82 -0
  133. package/src/debug-context/Types.ts +13 -0
  134. package/src/debug-context/index.ts +3 -0
  135. package/src/hooks/index.ts +12 -0
  136. package/src/hooks/unwrapRefValue.ts +16 -0
  137. package/src/hooks/useAddClassNameDuringResize.ts +58 -0
  138. package/src/hooks/useComposeRef.ts +46 -0
  139. package/src/hooks/useElementSize.ts +73 -0
  140. package/src/hooks/useEventHandler.ts +24 -0
  141. package/src/hooks/useExpectSameValueReference.ts +26 -0
  142. package/src/hooks/useOnElementMutation.ts +42 -0
  143. package/src/hooks/useOnElementResize.ts +52 -0
  144. package/src/hooks/useOnScrollWithin.ts +67 -0
  145. package/src/hooks/useOnWindowResize.ts +42 -0
  146. package/src/hooks/useScrollOffsets.ts +88 -0
  147. package/src/hooks/useStoredState.ts +2 -2
  148. package/src/hooks/useUpdatedRef.ts +14 -0
  149. package/src/hooks/useWindowSize.ts +20 -0
  150. package/src/index.ts +2 -0
  151. package/src/tsconfig.json +4 -1
  152. package/src/types/PropsWithRequiredChildren.ts +3 -0
  153. package/src/types/index.ts +2 -0
  154. package/dist/types/types.d.ts.map +0 -1
  155. /package/src/{types.ts → types/Serializable.ts} +0 -0
@@ -0,0 +1,68 @@
1
+ import { assert } from "@contember/utilities";
2
+ import { useState, useLayoutEffect, useMemo } from "react";
3
+ import { unwrapRefValue } from "./unwrapRefValue.js";
4
+ import { useOnScrollWithin } from "./useOnScrollWithin.js";
5
+ import { useOnWindowResize } from "./useOnWindowResize.js";
6
+ import { useScopedConsoleRef } from "../debug-context/Context.js";
7
+ import { useReferentiallyStableCallback } from "../referentiallyStable/useReferentiallyStableCallback.js";
8
+ function useScrollOffsets(refOrElement, interval = 1e3 / 60) {
9
+ const { logged } = useScopedConsoleRef("useScrollOffsets").current;
10
+ const [top, setTop] = useState(0);
11
+ const [bottom, setBottom] = useState(0);
12
+ const [left, setLeft] = useState(0);
13
+ const [right, setRight] = useState(0);
14
+ const updateScrollOffsetsState = useReferentiallyStableCallback((offsets) => {
15
+ logged("Update the offsets:", {
16
+ previous: { bottom, left, right, top },
17
+ next: offsets
18
+ });
19
+ if (offsets.bottom !== bottom)
20
+ setBottom(offsets.bottom);
21
+ if (offsets.left !== left)
22
+ setLeft(offsets.left);
23
+ if (offsets.right !== right)
24
+ setRight(offsets.right);
25
+ if (offsets.top !== top)
26
+ setTop(offsets.top);
27
+ });
28
+ const handler = useReferentiallyStableCallback(() => {
29
+ const maybeScrollContent = unwrapRefValue(refOrElement) ?? document;
30
+ if (maybeScrollContent) {
31
+ updateScrollOffsetsState(
32
+ getElementScrollOffsets(maybeScrollContent)
33
+ );
34
+ }
35
+ });
36
+ useOnScrollWithin(refOrElement, handler, interval);
37
+ useOnWindowResize(handler);
38
+ useLayoutEffect(handler, [handler]);
39
+ return useMemo(() => ({ bottom, left, right, top }), [bottom, left, right, top]);
40
+ }
41
+ function isIntrinsicScrollElement(element) {
42
+ return element instanceof HTMLBodyElement || element instanceof HTMLHtmlElement || element instanceof DocumentType || element instanceof Document || element instanceof HTMLDocument;
43
+ }
44
+ function getIntrinsicScrollContainer(element) {
45
+ const html = document.body.parentElement;
46
+ assert("HTML element exists", html, (value) => value instanceof HTMLElement);
47
+ if (isIntrinsicScrollElement(element)) {
48
+ return html;
49
+ } else {
50
+ return element;
51
+ }
52
+ }
53
+ function getElementScrollOffsets(element) {
54
+ const isIntrinsicScrolling = isIntrinsicScrollElement(element);
55
+ const scrollContainer = getIntrinsicScrollContainer(element);
56
+ const { scrollLeft, scrollTop, scrollHeight, scrollWidth } = scrollContainer;
57
+ const height = isIntrinsicScrolling ? window.innerHeight : scrollContainer.offsetHeight;
58
+ const width = isIntrinsicScrolling ? window.innerWidth : scrollContainer.offsetWidth;
59
+ const left = Math.round(scrollLeft);
60
+ const top = Math.round(scrollTop);
61
+ const right = Math.round(scrollWidth - width - left);
62
+ const bottom = Math.round(scrollHeight - height - top);
63
+ return { bottom, left, right, top };
64
+ }
65
+ export {
66
+ useScrollOffsets
67
+ };
68
+ //# sourceMappingURL=useScrollOffsets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useScrollOffsets.js","sources":["../../../src/hooks/useScrollOffsets.ts"],"sourcesContent":["import { assert } from '@contember/utilities'\nimport { useLayoutEffect, useMemo, useState } from 'react'\nimport { useScopedConsoleRef } from '../debug-context'\nimport { useReferentiallyStableCallback } from '../referentiallyStable'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnScrollWithin } from './useOnScrollWithin'\nimport { useOnWindowResize } from './useOnWindowResize'\n\nexport type Offsets = {\n\tbottom: number;\n\tleft: number;\n\tright: number;\n\ttop: number;\n}\n\nexport function useScrollOffsets(refOrElement: RefObjectOrElement<HTMLElement | null> | null, interval: number = 1000 / 60) {\n\tconst { logged } = useScopedConsoleRef('useScrollOffsets').current\n\n\tconst [top, setTop] = useState(0)\n\tconst [bottom, setBottom] = useState(0)\n\tconst [left, setLeft] = useState(0)\n\tconst [right, setRight] = useState(0)\n\n\tconst updateScrollOffsetsState = useReferentiallyStableCallback((offsets: Offsets) => {\n\t\tlogged('Update the offsets:', {\n\t\t\tprevious: { bottom, left, right, top },\n\t\t\tnext: offsets,\n\t\t})\n\n\t\tif (offsets.bottom !== bottom) setBottom(offsets.bottom)\n\t\tif (offsets.left !== left) setLeft(offsets.left)\n\t\tif (offsets.right !== right) setRight(offsets.right)\n\t\tif (offsets.top !== top) setTop(offsets.top)\n\t})\n\n\tconst handler = useReferentiallyStableCallback(() => {\n\t\tconst maybeScrollContent = unwrapRefValue(refOrElement) ?? document\n\n\t\tif (maybeScrollContent) {\n\t\t\tupdateScrollOffsetsState(\n\t\t\t\tgetElementScrollOffsets(maybeScrollContent),\n\t\t\t)\n\t\t}\n\t})\n\n\tuseOnScrollWithin(refOrElement, handler, interval)\n\tuseOnWindowResize(handler)\n\tuseLayoutEffect(handler, [handler])\n\n\treturn useMemo(() => ({ bottom, left, right, top }), [bottom, left, right, top])\n}\n\nfunction isIntrinsicScrollElement(element: unknown): element is Document {\n\treturn element instanceof HTMLBodyElement\n\t\t|| element instanceof HTMLHtmlElement\n\t\t|| element instanceof DocumentType\n\t\t|| element instanceof Document\n\t\t|| element instanceof HTMLDocument\n}\n\nfunction getIntrinsicScrollContainer(element: HTMLElement | Document): HTMLElement {\n\tconst html = document.body.parentElement\n\tassert('HTML element exists', html, (value): value is HTMLElement => value instanceof HTMLElement)\n\n\tif (isIntrinsicScrollElement(element)) {\n\t\treturn html\n\t} else {\n\t\treturn element\n\t}\n}\n\nfunction getElementScrollOffsets(element: HTMLElement | Document): Offsets {\n\tconst isIntrinsicScrolling = isIntrinsicScrollElement(element)\n\tconst scrollContainer = getIntrinsicScrollContainer(element)\n\n\tconst { scrollLeft, scrollTop, scrollHeight, scrollWidth } = scrollContainer\n\n\tconst height = isIntrinsicScrolling ? window.innerHeight : scrollContainer.offsetHeight\n\tconst width = isIntrinsicScrolling ? window.innerWidth : scrollContainer.offsetWidth\n\n\tconst left = Math.round(scrollLeft)\n\tconst top = Math.round(scrollTop)\n\n\tconst right = Math.round(scrollWidth - width - left)\n\tconst bottom = Math.round(scrollHeight - height - top)\n\n\treturn { bottom, left, right, top }\n}\n"],"names":[],"mappings":";;;;;;;AAeO,SAAS,iBAAiB,cAA6D,WAAmB,MAAO,IAAI;AAC3H,QAAM,EAAE,OAAW,IAAA,oBAAoB,kBAAkB,EAAE;AAE3D,QAAM,CAAC,KAAK,MAAM,IAAI,SAAS,CAAC;AAChC,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAS,CAAC;AACtC,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,CAAC;AAClC,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,CAAC;AAE9B,QAAA,2BAA2B,+BAA+B,CAAC,YAAqB;AACrF,WAAO,uBAAuB;AAAA,MAC7B,UAAU,EAAE,QAAQ,MAAM,OAAO,IAAI;AAAA,MACrC,MAAM;AAAA,IAAA,CACN;AAED,QAAI,QAAQ,WAAW;AAAQ,gBAAU,QAAQ,MAAM;AACvD,QAAI,QAAQ,SAAS;AAAM,cAAQ,QAAQ,IAAI;AAC/C,QAAI,QAAQ,UAAU;AAAO,eAAS,QAAQ,KAAK;AACnD,QAAI,QAAQ,QAAQ;AAAK,aAAO,QAAQ,GAAG;AAAA,EAAA,CAC3C;AAEK,QAAA,UAAU,+BAA+B,MAAM;AAC9C,UAAA,qBAAqB,eAAe,YAAY,KAAK;AAE3D,QAAI,oBAAoB;AACvB;AAAA,QACC,wBAAwB,kBAAkB;AAAA,MAAA;AAAA,IAE5C;AAAA,EAAA,CACA;AAEiB,oBAAA,cAAc,SAAS,QAAQ;AACjD,oBAAkB,OAAO;AACT,kBAAA,SAAS,CAAC,OAAO,CAAC;AAElC,SAAO,QAAQ,OAAO,EAAE,QAAQ,MAAM,OAAO,QAAQ,CAAC,QAAQ,MAAM,OAAO,GAAG,CAAC;AAChF;AAEA,SAAS,yBAAyB,SAAuC;AACjE,SAAA,mBAAmB,mBACtB,mBAAmB,mBACnB,mBAAmB,gBACnB,mBAAmB,YACnB,mBAAmB;AACxB;AAEA,SAAS,4BAA4B,SAA8C;AAC5E,QAAA,OAAO,SAAS,KAAK;AAC3B,SAAO,uBAAuB,MAAM,CAAC,UAAgC,iBAAiB,WAAW;AAE7F,MAAA,yBAAyB,OAAO,GAAG;AAC/B,WAAA;AAAA,EAAA,OACD;AACC,WAAA;AAAA,EACR;AACD;AAEA,SAAS,wBAAwB,SAA0C;AACpE,QAAA,uBAAuB,yBAAyB,OAAO;AACvD,QAAA,kBAAkB,4BAA4B,OAAO;AAE3D,QAAM,EAAE,YAAY,WAAW,cAAc,gBAAgB;AAE7D,QAAM,SAAS,uBAAuB,OAAO,cAAc,gBAAgB;AAC3E,QAAM,QAAQ,uBAAuB,OAAO,aAAa,gBAAgB;AAEnE,QAAA,OAAO,KAAK,MAAM,UAAU;AAC5B,QAAA,MAAM,KAAK,MAAM,SAAS;AAEhC,QAAM,QAAQ,KAAK,MAAM,cAAc,QAAQ,IAAI;AACnD,QAAM,SAAS,KAAK,MAAM,eAAe,SAAS,GAAG;AAErD,SAAO,EAAE,QAAQ,MAAM,OAAO,IAAI;AACnC;"}
@@ -1 +1 @@
1
- {"version":3,"file":"useStoredState.js","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\nimport { Serializable } from '../types'\n\ntype SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\ntype ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nconst createStoredStateHook = (getStorage: () => Storage) => {\n\treturn <V extends Serializable>(key: string, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\t\tconst storage = getStorage()\n\t\tconst [value, setValue] = useState<V>(() => {\n\t\t\tconst value = storage.getItem(key)\n\t\t\tconst parsedValue: V | undefined = value !== null ? JSON.parse(value) : undefined\n\t\t\treturn initializeValue(parsedValue)\n\t\t})\n\t\treturn [value, useCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, JSON.stringify(newValue))\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage])]\n\t}\n\n}\n\nexport const useSessionStorageState = createStoredStateHook(() => sessionStorage)\n"],"names":["value"],"mappings":";AAMA,MAAM,wBAAwB,CAAC,eAA8B;AACrD,SAAA,CAAyB,KAAa,oBAA2D;AACvG,UAAM,UAAU;AAChB,UAAM,CAAC,OAAO,QAAQ,IAAI,SAAY,MAAM;AACrCA,YAAAA,SAAQ,QAAQ,QAAQ,GAAG;AACjC,YAAM,cAA6BA,WAAU,OAAO,KAAK,MAAMA,MAAK,IAAI;AACxE,aAAO,gBAAgB,WAAW;AAAA,IAAA,CAClC;AACD,WAAO,CAAC,OAAO,YAAY,CAAAA,WAAS;AACnC,eAAS,CAAW,YAAA;AACnB,cAAM,WAAW,OAAOA,WAAU,aAAcA,OAA4B,OAAO,IAAIA;AACvF,gBAAQ,QAAQ,KAAK,KAAK,UAAU,QAAQ,CAAC;AACtC,eAAA;AAAA,MAAA,CACP;AAAA,IACC,GAAA,CAAC,KAAK,OAAO,CAAC,CAAC;AAAA,EAAA;AAGpB;AAEa,MAAA,yBAAyB,sBAAsB,MAAM,cAAc;"}
1
+ {"version":3,"file":"useStoredState.js","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\nimport { Serializable } from '../types'\n\nexport type SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\nexport type ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nconst createStoredStateHook = (getStorage: () => Storage) => {\n\treturn <V extends Serializable>(key: string, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\t\tconst storage = getStorage()\n\t\tconst [value, setValue] = useState<V>(() => {\n\t\t\tconst value = storage.getItem(key)\n\t\t\tconst parsedValue: V | undefined = value !== null ? JSON.parse(value) : undefined\n\t\t\treturn initializeValue(parsedValue)\n\t\t})\n\t\treturn [value, useCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, JSON.stringify(newValue))\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage])]\n\t}\n\n}\n\nexport const useSessionStorageState = createStoredStateHook(() => sessionStorage)\n"],"names":["value"],"mappings":";AAMA,MAAM,wBAAwB,CAAC,eAA8B;AACrD,SAAA,CAAyB,KAAa,oBAA2D;AACvG,UAAM,UAAU;AAChB,UAAM,CAAC,OAAO,QAAQ,IAAI,SAAY,MAAM;AACrCA,YAAAA,SAAQ,QAAQ,QAAQ,GAAG;AACjC,YAAM,cAA6BA,WAAU,OAAO,KAAK,MAAMA,MAAK,IAAI;AACxE,aAAO,gBAAgB,WAAW;AAAA,IAAA,CAClC;AACD,WAAO,CAAC,OAAO,YAAY,CAAAA,WAAS;AACnC,eAAS,CAAW,YAAA;AACnB,cAAM,WAAW,OAAOA,WAAU,aAAcA,OAA4B,OAAO,IAAIA;AACvF,gBAAQ,QAAQ,KAAK,KAAK,UAAU,QAAQ,CAAC;AACtC,eAAA;AAAA,MAAA,CACP;AAAA,IACC,GAAA,CAAC,KAAK,OAAO,CAAC,CAAC;AAAA,EAAA;AAGpB;AAEa,MAAA,yBAAyB,sBAAsB,MAAM,cAAc;"}
@@ -0,0 +1,10 @@
1
+ import { useRef } from "react";
2
+ function useUpdatedRef(value) {
3
+ const ref = useRef(value);
4
+ ref.current = value;
5
+ return ref;
6
+ }
7
+ export {
8
+ useUpdatedRef
9
+ };
10
+ //# sourceMappingURL=useUpdatedRef.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUpdatedRef.js","sources":["../../../src/hooks/useUpdatedRef.ts"],"sourcesContent":["import { useRef } from 'react'\n\n/**\n * Returns a stable mutable object with value that is updated upon each call\n *\n * @param value - Value that could update after initial call\n * @returns Mutable ref object with updated value\n */\nexport function useUpdatedRef<T>(value: T) {\n\tconst ref = useRef(value)\n\tref.current = value\n\n\treturn ref\n}\n"],"names":[],"mappings":";AAQO,SAAS,cAAiB,OAAU;AACpC,QAAA,MAAM,OAAO,KAAK;AACxB,MAAI,UAAU;AAEP,SAAA;AACR;"}
@@ -0,0 +1,20 @@
1
+ import { useState, useMemo } from "react";
2
+ import { useOnWindowResize } from "./useOnWindowResize.js";
3
+ function useWindowSize() {
4
+ const [width, setWidth] = useState(window.innerWidth);
5
+ const [height, setHeight] = useState(window.innerHeight);
6
+ useOnWindowResize(() => {
7
+ const { innerHeight, innerWidth } = window;
8
+ if (innerHeight !== height) {
9
+ setHeight(innerHeight);
10
+ }
11
+ if (innerWidth !== width) {
12
+ setWidth(innerWidth);
13
+ }
14
+ });
15
+ return useMemo(() => ({ height, width }), [height, width]);
16
+ }
17
+ export {
18
+ useWindowSize
19
+ };
20
+ //# sourceMappingURL=useWindowSize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useWindowSize.js","sources":["../../../src/hooks/useWindowSize.ts"],"sourcesContent":["import { useMemo, useState } from 'react'\nimport { useOnWindowResize } from './useOnWindowResize'\n\nexport function useWindowSize() {\n const [width, setWidth] = useState(window.innerWidth)\n const [height, setHeight] = useState(window.innerHeight)\n\n useOnWindowResize(() => {\n const { innerHeight, innerWidth } = window\n if (innerHeight !== height) {\n setHeight(innerHeight)\n }\n\n if (innerWidth !== width) {\n setWidth(innerWidth)\n }\n })\n\n return useMemo(() => ({ height, width }), [height, width])\n}\n"],"names":[],"mappings":";;AAGO,SAAS,gBAAgB;AAC9B,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,OAAO,UAAU;AACpD,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAS,OAAO,WAAW;AAEvD,oBAAkB,MAAM;AAChB,UAAA,EAAE,aAAa,WAAe,IAAA;AACpC,QAAI,gBAAgB,QAAQ;AAC1B,gBAAU,WAAW;AAAA,IACvB;AAEA,QAAI,eAAe,OAAO;AACxB,eAAS,UAAU;AAAA,IACrB;AAAA,EAAA,CACD;AAEM,SAAA,QAAQ,OAAO,EAAE,QAAQ,MAAU,IAAA,CAAC,QAAQ,KAAK,CAAC;AAC3D;"}
@@ -1,14 +1,29 @@
1
+ import { createNonNullableContextFactory } from "./context/createNonNullableContextFactory.js";
2
+ import { isNoopScopedConsole, noopLog, noopLogged, noopScopedConsole } from "./debug-context/Constants.js";
3
+ import { DebugChildren, ScopedConsoleContext, useScopedConsoleRef } from "./debug-context/Context.js";
4
+ import { unwrapRefValue } from "./hooks/unwrapRefValue.js";
1
5
  import { useAbortController } from "./hooks/useAbortController.js";
6
+ import { useAddClassNameDuringResize } from "./hooks/useAddClassNameDuringResize.js";
2
7
  import { useArrayMapMemo } from "./hooks/useArrayMapMemo.js";
8
+ import { useComposeRef } from "./hooks/useComposeRef.js";
3
9
  import { useConstantLengthInvariant } from "./hooks/useConstantLengthInvariant.js";
4
10
  import { useConstantValueInvariant } from "./hooks/useConstantValueInvariant.js";
5
11
  import { useDebounce } from "./hooks/useDebounce.js";
6
12
  import { useDebounceCallback } from "./hooks/useDebounceCallback.js";
13
+ import { useElementSize } from "./hooks/useElementSize.js";
14
+ import { useEventHandler } from "./hooks/useEventHandler.js";
15
+ import { useExpectSameValueReference } from "./hooks/useExpectSameValueReference.js";
7
16
  import { useForceRender } from "./hooks/useForceRender.js";
8
17
  import { useIsMounted } from "./hooks/useIsMounted.js";
9
18
  import { useObjectMemo } from "./hooks/useObjectMemo.js";
19
+ import { useOnElementMutation } from "./hooks/useOnElementMutation.js";
20
+ import { useOnElementResize } from "./hooks/useOnElementResize.js";
21
+ import { useOnWindowResize } from "./hooks/useOnWindowResize.js";
10
22
  import { usePreviousValue } from "./hooks/usePreviousValue.js";
23
+ import { useScrollOffsets } from "./hooks/useScrollOffsets.js";
11
24
  import { useSessionStorageState } from "./hooks/useStoredState.js";
25
+ import { useUpdatedRef } from "./hooks/useUpdatedRef.js";
26
+ import { useWindowSize } from "./hooks/useWindowSize.js";
12
27
  import { emptyArray } from "./referentiallyStable/emptyArray.js";
13
28
  import { emptyObject } from "./referentiallyStable/emptyObject.js";
14
29
  import { identityFunction } from "./referentiallyStable/identityFunction.js";
@@ -17,23 +32,43 @@ import { returnFalse } from "./referentiallyStable/returnFalse.js";
17
32
  import { returnTrue } from "./referentiallyStable/returnTrue.js";
18
33
  import { useReferentiallyStableCallback } from "./referentiallyStable/useReferentiallyStableCallback.js";
19
34
  export {
35
+ DebugChildren,
36
+ ScopedConsoleContext,
37
+ createNonNullableContextFactory,
20
38
  emptyArray,
21
39
  emptyObject,
22
40
  identityFunction,
41
+ isNoopScopedConsole,
23
42
  noop,
43
+ noopLog,
44
+ noopLogged,
45
+ noopScopedConsole,
24
46
  returnFalse,
25
47
  returnTrue,
48
+ unwrapRefValue,
26
49
  useAbortController,
50
+ useAddClassNameDuringResize,
27
51
  useArrayMapMemo,
52
+ useComposeRef,
28
53
  useConstantLengthInvariant,
29
54
  useConstantValueInvariant,
30
55
  useDebounce,
31
56
  useDebounceCallback,
57
+ useElementSize,
58
+ useEventHandler,
59
+ useExpectSameValueReference,
32
60
  useForceRender,
33
61
  useIsMounted,
34
62
  useObjectMemo,
63
+ useOnElementMutation,
64
+ useOnElementResize,
65
+ useOnWindowResize,
35
66
  usePreviousValue,
36
67
  useReferentiallyStableCallback,
37
- useSessionStorageState
68
+ useScopedConsoleRef,
69
+ useScrollOffsets,
70
+ useSessionStorageState,
71
+ useUpdatedRef,
72
+ useWindowSize
38
73
  };
39
74
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,17 @@
1
+ import { createContext, useContext } from "react";
2
+ function createNonNullableContextFactory(displayName, initialValue) {
3
+ const RequiredContext = createContext(initialValue ?? null);
4
+ RequiredContext.displayName = displayName;
5
+ const useRequiredContext = () => {
6
+ const context = useContext(RequiredContext);
7
+ if (context == null) {
8
+ throw new Error(`use${displayName} must be used within a ${displayName} provider`);
9
+ }
10
+ return context;
11
+ };
12
+ return [RequiredContext, useRequiredContext];
13
+ }
14
+ export {
15
+ createNonNullableContextFactory
16
+ };
17
+ //# sourceMappingURL=createNonNullableContextFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createNonNullableContextFactory.js","sources":["../../../src/context/createNonNullableContextFactory.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nexport function createNonNullableContextFactory<T>(displayName: string, initialValue?: T): [Context<T>, () => NonNullable<T>] {\n const RequiredContext = createContext<T>((initialValue ?? null)!)\n RequiredContext.displayName = displayName\n\n const useRequiredContext = () => {\n const context = useContext(RequiredContext)\n\n if (context == null) {\n throw new Error(`use${displayName} must be used within a ${displayName} provider`)\n }\n\n return context\n }\n\n return [RequiredContext, useRequiredContext]\n}\n"],"names":[],"mappings":";AAGgB,SAAA,gCAAmC,aAAqB,cAAsD;AACtH,QAAA,kBAAkB,cAAkB,gBAAgB,IAAM;AAChE,kBAAgB,cAAc;AAE9B,QAAM,qBAAqB,MAAM;AACzB,UAAA,UAAU,WAAW,eAAe;AAE1C,QAAI,WAAW,MAAM;AACnB,YAAM,IAAI,MAAM,MAAM,qCAAqC,sBAAsB;AAAA,IACnF;AAEO,WAAA;AAAA,EAAA;AAGF,SAAA,CAAC,iBAAiB,kBAAkB;AAC7C;"}
@@ -0,0 +1,25 @@
1
+ function noopLog(...parameters) {
2
+ }
3
+ function noopLogged(message, value) {
4
+ return value;
5
+ }
6
+ const noopScopedConsole = Object.freeze({
7
+ log: noopLog,
8
+ logged: noopLogged,
9
+ warn: noopLog,
10
+ warned: noopLogged,
11
+ error: noopLog,
12
+ errored: noopLogged,
13
+ trace: noopLog,
14
+ traced: noopLogged
15
+ });
16
+ function isNoopScopedConsole(value) {
17
+ return value === noopScopedConsole;
18
+ }
19
+ export {
20
+ isNoopScopedConsole,
21
+ noopLog,
22
+ noopLogged,
23
+ noopScopedConsole
24
+ };
25
+ //# sourceMappingURL=Constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Constants.js","sources":["../../../src/debug-context/Constants.ts"],"sourcesContent":["import type { ScopedConsoleContextType } from './Types'\n\nexport function noopLog(...parameters: any[]) { }\n\nexport function noopLogged<T>(message: string, value: T): T {\n\treturn value\n}\n\nexport const noopScopedConsole: ScopedConsoleContextType = Object.freeze({\n\tlog: noopLog,\n\tlogged: noopLogged,\n\twarn: noopLog,\n\twarned: noopLogged,\n\terror: noopLog,\n\terrored: noopLogged,\n\ttrace: noopLog,\n\ttraced: noopLogged,\n})\n\nexport function isNoopScopedConsole(value: unknown): boolean {\n\treturn value === noopScopedConsole\n}\n"],"names":[],"mappings":"AAEO,SAAS,WAAW,YAAmB;AAAE;AAEhC,SAAA,WAAc,SAAiB,OAAa;AACpD,SAAA;AACR;AAEa,MAAA,oBAA8C,OAAO,OAAO;AAAA,EACxE,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,SAAS;AAAA,EACT,OAAO;AAAA,EACP,QAAQ;AACT,CAAC;AAEM,SAAS,oBAAoB,OAAyB;AAC5D,SAAO,UAAU;AAClB;"}
@@ -0,0 +1,67 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { assert, isNonEmptyString } from "@contember/utilities";
3
+ import { createContext, useContext, useMemo, useRef, memo } from "react";
4
+ import { noopScopedConsole, isNoopScopedConsole } from "./Constants.js";
5
+ function createPrefixedConsole(message, prefixedConsole) {
6
+ return {
7
+ error: function error(...parameters) {
8
+ (prefixedConsole ?? console).error(message, ...parameters);
9
+ },
10
+ errored: function errored(message2, value) {
11
+ (prefixedConsole ?? console).error(message2, value);
12
+ return value;
13
+ },
14
+ log: function log(...parameters) {
15
+ (prefixedConsole ?? console).log(message, ...parameters);
16
+ },
17
+ logged: function logged(message2, value) {
18
+ (prefixedConsole ?? console).log(message2, value);
19
+ return value;
20
+ },
21
+ trace: function trace(...parameters) {
22
+ (prefixedConsole ?? console).trace(message, ...parameters);
23
+ },
24
+ traced: function traced(message2, value) {
25
+ (prefixedConsole ?? console).trace(message2, value);
26
+ return value;
27
+ },
28
+ warn: function warn(...parameters) {
29
+ (prefixedConsole ?? console).warn(message, ...parameters);
30
+ },
31
+ warned: function warned(message2, value) {
32
+ (prefixedConsole ?? console).warn(message2, value);
33
+ return value;
34
+ }
35
+ };
36
+ }
37
+ const ScopedConsoleContext = createContext(null);
38
+ ScopedConsoleContext.displayName = "ScopedConsoleContext";
39
+ function parentScopedConsoleOrNone(parentConsole) {
40
+ return isNoopScopedConsole(parentConsole) ? void 0 : parentConsole;
41
+ }
42
+ const useScopedConsoleRef = (prefix) => {
43
+ assert("prefix is non-empty string", prefix, isNonEmptyString);
44
+ const parentConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext));
45
+ const scopedConsole = useMemo(() => {
46
+ if (parentConsole) {
47
+ return createPrefixedConsole(prefix, parentConsole);
48
+ } else {
49
+ return noopScopedConsole;
50
+ }
51
+ }, [parentConsole, prefix]);
52
+ const ref = useRef(scopedConsole);
53
+ ref.current = scopedConsole;
54
+ return ref;
55
+ };
56
+ const DebugChildren = memo(({ active = true, children, id }) => {
57
+ const parentScopedConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext));
58
+ const console2 = active && id ? createPrefixedConsole(id, parentScopedConsole) : noopScopedConsole;
59
+ return /* @__PURE__ */ jsx(ScopedConsoleContext.Provider, { value: active ? console2 : noopScopedConsole, children });
60
+ });
61
+ DebugChildren.displayName = "DebugChildren";
62
+ export {
63
+ DebugChildren,
64
+ ScopedConsoleContext,
65
+ useScopedConsoleRef
66
+ };
67
+ //# sourceMappingURL=Context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Context.js","sources":["../../../src/debug-context/Context.tsx"],"sourcesContent":["import { assert, isNonEmptyString } from '@contember/utilities'\nimport { ReactNode, createContext, memo, useContext, useMemo, useRef } from 'react'\nimport { isNoopScopedConsole, noopScopedConsole } from './Constants'\nimport { ScopedConsoleContextType } from './Types'\n\nfunction createPrefixedConsole(message: string, prefixedConsole?: ScopedConsoleContextType): ScopedConsoleContextType {\n\treturn {\n\t\terror: function error(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).error(message, ...parameters)\n\t\t},\n\t\terrored: function errored<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).error(message, value)\n\t\t\treturn value\n\t\t},\n\t\tlog: function log(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).log(message, ...parameters)\n\t\t},\n\t\tlogged: function logged<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).log(message, value)\n\t\t\treturn value\n\t\t},\n\t\ttrace: function trace(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).trace(message, ...parameters)\n\t\t},\n\t\ttraced: function traced<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).trace(message, value)\n\t\t\treturn value\n\t\t},\n\t\twarn: function warn(...parameters: any[]) {\n\t\t\t(prefixedConsole ?? console).warn(message, ...parameters)\n\t\t},\n\t\twarned: function warned<T>(message: string, value: T): T {\n\t\t\t(prefixedConsole ?? console).warn(message, value)\n\t\t\treturn value\n\t\t},\n\t}\n}\n\nexport const ScopedConsoleContext = createContext<ScopedConsoleContextType>(null!)\nScopedConsoleContext.displayName = 'ScopedConsoleContext'\n\nfunction parentScopedConsoleOrNone(parentConsole: ScopedConsoleContextType): ScopedConsoleContextType | undefined {\n\treturn isNoopScopedConsole(parentConsole) ? undefined : parentConsole\n}\n\nexport const useScopedConsoleRef = (prefix: string) => {\n\tassert('prefix is non-empty string', prefix, isNonEmptyString)\n\n\tconst parentConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst scopedConsole = useMemo(() => {\n\t\tif (parentConsole) {\n\t\t\treturn createPrefixedConsole(prefix, parentConsole)\n\t\t} else {\n\t\t\treturn noopScopedConsole\n\t\t}\n\t}, [parentConsole, prefix])\n\n\tconst ref = useRef(scopedConsole)\n\tref.current = scopedConsole\n\n\treturn ref\n}\n\nexport type DebugChildrenProps =\n\t| { active: true; children: ReactNode; id: string; }\n\t| { active?: false; children: ReactNode; id?: string; }\n\nexport const DebugChildren = memo<DebugChildrenProps>(({ active = true, children, id }) => {\n\tconst parentScopedConsole = parentScopedConsoleOrNone(useContext(ScopedConsoleContext))\n\n\tconst console = active && id\n\t\t? createPrefixedConsole(id, parentScopedConsole)\n\t\t: noopScopedConsole\n\n\treturn (\n\t\t<ScopedConsoleContext.Provider value={active ? console : noopScopedConsole}>\n\t\t\t{children}\n\t\t</ScopedConsoleContext.Provider>\n\t)\n})\nDebugChildren.displayName = 'DebugChildren'\n"],"names":["message","console"],"mappings":";;;;AAKA,SAAS,sBAAsB,SAAiB,iBAAsE;AAC9G,SAAA;AAAA,IACN,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,SAAS,SAAS,QAAWA,UAAiB,OAAa;AAC1D,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,KAAK,SAAS,OAAO,YAAmB;AACvC,OAAC,mBAAmB,SAAS,IAAI,SAAS,GAAG,UAAU;AAAA,IACxD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,IAAIA,UAAS,KAAK;AACxC,aAAA;AAAA,IACR;AAAA,IACA,OAAO,SAAS,SAAS,YAAmB;AAC3C,OAAC,mBAAmB,SAAS,MAAM,SAAS,GAAG,UAAU;AAAA,IAC1D;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,MAAMA,UAAS,KAAK;AAC1C,aAAA;AAAA,IACR;AAAA,IACA,MAAM,SAAS,QAAQ,YAAmB;AACzC,OAAC,mBAAmB,SAAS,KAAK,SAAS,GAAG,UAAU;AAAA,IACzD;AAAA,IACA,QAAQ,SAAS,OAAUA,UAAiB,OAAa;AACxD,OAAC,mBAAmB,SAAS,KAAKA,UAAS,KAAK;AACzC,aAAA;AAAA,IACR;AAAA,EAAA;AAEF;AAEa,MAAA,uBAAuB,cAAwC,IAAK;AACjF,qBAAqB,cAAc;AAEnC,SAAS,0BAA0B,eAA+E;AAC1G,SAAA,oBAAoB,aAAa,IAAI,SAAY;AACzD;AAEa,MAAA,sBAAsB,CAAC,WAAmB;AAC/C,SAAA,8BAA8B,QAAQ,gBAAgB;AAE7D,QAAM,gBAAgB,0BAA0B,WAAW,oBAAoB,CAAC;AAE1E,QAAA,gBAAgB,QAAQ,MAAM;AACnC,QAAI,eAAe;AACX,aAAA,sBAAsB,QAAQ,aAAa;AAAA,IAAA,OAC5C;AACC,aAAA;AAAA,IACR;AAAA,EAAA,GACE,CAAC,eAAe,MAAM,CAAC;AAEpB,QAAA,MAAM,OAAO,aAAa;AAChC,MAAI,UAAU;AAEP,SAAA;AACR;AAMa,MAAA,gBAAgB,KAAyB,CAAC,EAAE,SAAS,MAAM,UAAU,SAAS;AAC1F,QAAM,sBAAsB,0BAA0B,WAAW,oBAAoB,CAAC;AAEtF,QAAMC,WAAU,UAAU,KACvB,sBAAsB,IAAI,mBAAmB,IAC7C;AAGF,SAAA,oBAAC,qBAAqB,UAArB,EAA8B,OAAO,SAASA,WAAU,mBACvD,SACF,CAAA;AAEF,CAAC;AACD,cAAc,cAAc;"}
@@ -0,0 +1,11 @@
1
+ function unwrapRefValue(value) {
2
+ if (value && typeof value === "object" && "current" in value) {
3
+ return value.current;
4
+ } else {
5
+ return value;
6
+ }
7
+ }
8
+ export {
9
+ unwrapRefValue
10
+ };
11
+ //# sourceMappingURL=unwrapRefValue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unwrapRefValue.js","sources":["../../../src/hooks/unwrapRefValue.ts"],"sourcesContent":["import { RefObject } from 'react'\n\nexport type RefObjectOrElement<T> = T | RefObject<T>\n\n/**\n * Unwraps a ref object or returns the value if it is not a ref object.\n * @param value - Value to unwrap\n * @returns Unwrapped value\n */\nexport function unwrapRefValue<T>(value: RefObjectOrElement<T>): T | null {\n\tif (value && typeof value === 'object' && 'current' in value) {\n\t\treturn value.current\n\t} else {\n\t\treturn value\n\t}\n}\n"],"names":[],"mappings":"AASO,SAAS,eAAkB,OAAwC;AACzE,MAAI,SAAS,OAAO,UAAU,YAAY,aAAa,OAAO;AAC7D,WAAO,MAAM;AAAA,EAAA,OACP;AACC,WAAA;AAAA,EACR;AACD;"}
@@ -0,0 +1,34 @@
1
+ import { useRef, useLayoutEffect } from "react";
2
+ import { useOnWindowResize } from "./useOnWindowResize.js";
3
+ import { useReferentiallyStableCallback } from "../referentiallyStable/useReferentiallyStableCallback.js";
4
+ function intendedFlushOfCSSChangesToCauseImmediateReflow(element) {
5
+ element.offsetHeight;
6
+ }
7
+ function addClassName(className, element) {
8
+ if (element.classList.contains(className)) {
9
+ element.classList.remove(className);
10
+ intendedFlushOfCSSChangesToCauseImmediateReflow(element);
11
+ }
12
+ }
13
+ function removeClassName(className, element) {
14
+ if (!element.classList.contains(className)) {
15
+ element.classList.add(className);
16
+ intendedFlushOfCSSChangesToCauseImmediateReflow(element);
17
+ }
18
+ }
19
+ function useAddClassNameDuringResize(className, timeoutToRestore = 300, element) {
20
+ const timeoutID = useRef();
21
+ const addTemporaryClassName = useReferentiallyStableCallback(() => {
22
+ clearTimeout(timeoutID.current);
23
+ removeClassName(className, element ?? document.body);
24
+ timeoutID.current = setTimeout(() => {
25
+ addClassName(className, element ?? document.body);
26
+ }, timeoutToRestore);
27
+ });
28
+ useLayoutEffect(addTemporaryClassName, [addTemporaryClassName]);
29
+ useOnWindowResize(addTemporaryClassName);
30
+ }
31
+ export {
32
+ useAddClassNameDuringResize
33
+ };
34
+ //# sourceMappingURL=useAddClassNameDuringResize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAddClassNameDuringResize.js","sources":["../../../src/hooks/useAddClassNameDuringResize.ts"],"sourcesContent":["import { useLayoutEffect, useRef } from 'react'\nimport { useReferentiallyStableCallback } from '../referentiallyStable'\nimport { useOnWindowResize } from './useOnWindowResize'\n\nfunction intendedFlushOfCSSChangesToCauseImmediateReflow(element: HTMLElement) {\n\telement.offsetHeight\n}\n\nfunction addClassName(\n\tclassName: string,\n\telement: HTMLElement,\n) {\n\tif (element.classList.contains(className)) {\n\t\telement.classList.remove(className)\n\t\tintendedFlushOfCSSChangesToCauseImmediateReflow(element)\n\t}\n}\n\nfunction removeClassName(\n\tclassName: string,\n\telement: HTMLElement,\n) {\n\tif (!element.classList.contains(className)) {\n\t\telement.classList.add(className)\n\t\tintendedFlushOfCSSChangesToCauseImmediateReflow(element)\n\t}\n}\n\n/**\n * Adds CSS class during resize event to element\n *\n * - Falls back to document.body when no element is specified\n * - Adds CSS class to element also on the initial load\n * - Uses element.classList directly\n *\n * @param className - CSS class to apply during the resize event\n * @param timeoutToRestore - Timeout in milliseconds to wait before finally removing the CSS class\n * @param element - Optional HTML element to apply CSS class to\n */\nexport function useAddClassNameDuringResize(\n\tclassName: string,\n\ttimeoutToRestore: number = 300,\n\telement?: HTMLElement,\n) {\n\tconst timeoutID = useRef<number>()\n\n\tconst addTemporaryClassName = useReferentiallyStableCallback(() => {\n\t\tclearTimeout(timeoutID.current)\n\t\tremoveClassName(className, element ?? document.body)\n\n\t\ttimeoutID.current = setTimeout(() => {\n\t\t\taddClassName(className, element ?? document.body)\n\t\t}, timeoutToRestore)\n\t})\n\n\tuseLayoutEffect(addTemporaryClassName, [addTemporaryClassName])\n\tuseOnWindowResize(addTemporaryClassName)\n}\n"],"names":[],"mappings":";;;AAIA,SAAS,gDAAgD,SAAsB;AACtE,UAAA;AACT;AAEA,SAAS,aACR,WACA,SACC;AACD,MAAI,QAAQ,UAAU,SAAS,SAAS,GAAG;AAClC,YAAA,UAAU,OAAO,SAAS;AAClC,oDAAgD,OAAO;AAAA,EACxD;AACD;AAEA,SAAS,gBACR,WACA,SACC;AACD,MAAI,CAAC,QAAQ,UAAU,SAAS,SAAS,GAAG;AACnC,YAAA,UAAU,IAAI,SAAS;AAC/B,oDAAgD,OAAO;AAAA,EACxD;AACD;AAaO,SAAS,4BACf,WACA,mBAA2B,KAC3B,SACC;AACD,QAAM,YAAY;AAEZ,QAAA,wBAAwB,+BAA+B,MAAM;AAClE,iBAAa,UAAU,OAAO;AACd,oBAAA,WAAW,WAAW,SAAS,IAAI;AAEzC,cAAA,UAAU,WAAW,MAAM;AACvB,mBAAA,WAAW,WAAW,SAAS,IAAI;AAAA,OAC9C,gBAAgB;AAAA,EAAA,CACnB;AAEe,kBAAA,uBAAuB,CAAC,qBAAqB,CAAC;AAC9D,oBAAkB,qBAAqB;AACxC;"}
@@ -0,0 +1,31 @@
1
+ import { useRef, useMemo } from "react";
2
+ function setRef(ref, instance) {
3
+ if (ref) {
4
+ if (typeof ref === "function") {
5
+ ref(instance);
6
+ } else if (instance) {
7
+ ref.current = instance;
8
+ }
9
+ }
10
+ }
11
+ function useComposeRef(...refs) {
12
+ const initialRefs = useRef(refs);
13
+ const composeRef = useMemo(() => {
14
+ function composedRefCallback(instance) {
15
+ initialRefs.current.forEach((ref) => setRef(ref, instance));
16
+ setCurrent(instance);
17
+ }
18
+ function setCurrent(instance) {
19
+ if (instance) {
20
+ composedRefCallback.current = instance;
21
+ }
22
+ }
23
+ composedRefCallback.current = null;
24
+ return composedRefCallback;
25
+ }, []);
26
+ return composeRef;
27
+ }
28
+ export {
29
+ useComposeRef
30
+ };
31
+ //# sourceMappingURL=useComposeRef.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useComposeRef.js","sources":["../../../src/hooks/useComposeRef.ts"],"sourcesContent":["import { MutableRefObject, RefCallback, useMemo, useRef } from 'react'\n\nexport type MaybeRef<T> = ((instance: T | null) => void) | MutableRefObject<T | null> | null | undefined\nexport type ComposedRefCallback<T> = RefCallback<T> & { current: T | null };\n\nfunction setRef<T>(ref: MaybeRef<T>, instance: T | null): void {\n\tif (ref) {\n\t\tif (typeof ref === 'function') {\n\t\t\tref(instance)\n\t\t} else if (instance) {\n\t\t\tref.current = instance\n\t\t}\n\t}\n}\n\n/**\n * Fills any number of refs with an instance\n *\n * Useful when you need to use outer ref and inner in the same time, e.g. passing element\n * back to forwardedRef an also using the same value within the component locally.\n *\n * @param refs - Rest parameter of refs to fill with instance\n * @returns Returns a function similar to a RefCallback\n */\nexport function useComposeRef<T>(...refs: MaybeRef<T>[]): ComposedRefCallback<T> {\n\tconst initialRefs = useRef(refs)\n\n\tconst composeRef: ComposedRefCallback<T> = useMemo(() => {\n\t\tfunction composedRefCallback(instance: T | null) {\n\t\t\tinitialRefs.current.forEach(ref => setRef(ref, instance))\n\t\t\tsetCurrent(instance)\n\t\t}\n\n\t\tfunction setCurrent(instance: T | null) {\n\t\t\tif (instance) {\n\t\t\t\tcomposedRefCallback.current = instance\n\t\t\t}\n\t\t}\n\n\t\tcomposedRefCallback.current = null as T | null\n\n\t\treturn composedRefCallback\n\t}, [])\n\n\treturn composeRef\n}\n"],"names":[],"mappings":";AAKA,SAAS,OAAU,KAAkB,UAA0B;AAC9D,MAAI,KAAK;AACJ,QAAA,OAAO,QAAQ,YAAY;AAC9B,UAAI,QAAQ;AAAA,eACF,UAAU;AACpB,UAAI,UAAU;AAAA,IACf;AAAA,EACD;AACD;AAWO,SAAS,iBAAoB,MAA6C;AAC1E,QAAA,cAAc,OAAO,IAAI;AAEzB,QAAA,aAAqC,QAAQ,MAAM;AACxD,aAAS,oBAAoB,UAAoB;AAChD,kBAAY,QAAQ,QAAQ,CAAA,QAAO,OAAO,KAAK,QAAQ,CAAC;AACxD,iBAAW,QAAQ;AAAA,IACpB;AAEA,aAAS,WAAW,UAAoB;AACvC,UAAI,UAAU;AACb,4BAAoB,UAAU;AAAA,MAC/B;AAAA,IACD;AAEA,wBAAoB,UAAU;AAEvB,WAAA;AAAA,EACR,GAAG,CAAE,CAAA;AAEE,SAAA;AACR;"}
@@ -0,0 +1,47 @@
1
+ import { getSizeFromResizeObserverEntryFactory } from "@contember/utilities";
2
+ import { useMemo, useState, useRef, useLayoutEffect } from "react";
3
+ import { unwrapRefValue } from "./unwrapRefValue.js";
4
+ import { useOnElementResize } from "./useOnElementResize.js";
5
+ import { useScopedConsoleRef } from "../debug-context/Context.js";
6
+ function getElementWidth(element) {
7
+ return element.offsetWidth;
8
+ }
9
+ function getElementHeight(element) {
10
+ return element.offsetHeight;
11
+ }
12
+ function useElementSize(refOrElement, options = {}, timeout = 300) {
13
+ const { logged } = useScopedConsoleRef("useElementSize").current;
14
+ const { box = "border-box" } = options;
15
+ const getSizeFromResizeObserverEntry = useMemo(() => getSizeFromResizeObserverEntryFactory(box), [box]);
16
+ const refValue = unwrapRefValue(refOrElement);
17
+ const [width, setWidth] = useState(refValue?.offsetWidth);
18
+ const [height, setHeight] = useState(refValue?.offsetHeight);
19
+ logged("dimensions", { width, height });
20
+ useOnElementResize(refOrElement, (entry) => {
21
+ const dimensions = logged("useOnElementResize => entry:dimensions", getSizeFromResizeObserverEntry(entry));
22
+ if (width !== dimensions.width) {
23
+ setWidth(dimensions.width);
24
+ }
25
+ if (height !== dimensions.height) {
26
+ setHeight(dimensions.height);
27
+ }
28
+ }, { box }, timeout);
29
+ const measure = useRef((element) => {
30
+ logged("measure(element)", element);
31
+ if (element instanceof HTMLElement) {
32
+ setWidth(logged("getElementWidth(element):", getElementWidth(element)));
33
+ setHeight(logged("getElementHeight(element):", getElementHeight(element)));
34
+ } else if (element) {
35
+ throw new Error("Exhaustive error: Expecting element to be instance of HTMLElement or Window");
36
+ }
37
+ });
38
+ useLayoutEffect(() => {
39
+ const element = unwrapRefValue(refOrElement);
40
+ logged("useLayoutEffect => measure", measure.current(element));
41
+ });
42
+ return { height, width };
43
+ }
44
+ export {
45
+ useElementSize
46
+ };
47
+ //# sourceMappingURL=useElementSize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useElementSize.js","sources":["../../../src/hooks/useElementSize.ts"],"sourcesContent":["import { getSizeFromResizeObserverEntryFactory } from '@contember/utilities'\nimport { useLayoutEffect, useMemo, useRef, useState } from 'react'\nimport { useScopedConsoleRef } from '../debug-context'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnElementResize } from './useOnElementResize'\n\nfunction getElementWidth(element: HTMLElement) {\n\treturn element.offsetWidth\n}\n\nfunction getElementHeight(element: HTMLElement) {\n\treturn element.offsetHeight\n}\n\n/**\n * Measure HTMLElement\n *\n * @param refOrElement - HTMLElement passed directly or indirectly as RefObject\n * @returns Size vector consisting of width and height properties\n */\nexport function useElementSize(\n\trefOrElement: RefObjectOrElement<HTMLElement>,\n\toptions: ResizeObserverOptions = {},\n\ttimeout: number = 300,\n): { height: number | undefined, width: number | undefined } {\n\tconst { logged } = useScopedConsoleRef('useElementSize').current\n\n\tconst { box = 'border-box' } = options\n\tconst getSizeFromResizeObserverEntry = useMemo(() => getSizeFromResizeObserverEntryFactory(box), [box])\n\n\tconst refValue = unwrapRefValue(refOrElement)\n\tconst [width, setWidth] = useState<number | undefined>(refValue?.offsetWidth)\n\tconst [height, setHeight] = useState<number | undefined>(refValue?.offsetHeight)\n\n\tlogged('dimensions', { width, height })\n\n\tuseOnElementResize(refOrElement, entry => {\n\t\tconst dimensions = logged('useOnElementResize => entry:dimensions', getSizeFromResizeObserverEntry(entry))\n\n\t\tif (width !== dimensions.width) {\n\t\t\tsetWidth(dimensions.width)\n\t\t}\n\t\tif (height !== dimensions.height) {\n\t\t\tsetHeight(dimensions.height)\n\t\t}\n\t}, { box }, timeout)\n\n\t// INTENTIONAL AVOID OF ESLint ERROR: We need measure on every Layout side-effect\n\t// otherwise the browser will paint and cause layout shifts.\n\t//\n\t// React Hook useLayoutEffect contains a call to 'setWidth'. Without a list of dependencies,\n\t// this can lead to an infinite chain of updates. To fix this, pass [element] as a second argument\n\t// to the useLayoutEffect Hook.\n\t//\n\t// Seems like only pure function lets us measure before the browser has a chance to paint\n\tconst measure = useRef((element: HTMLElement | null) => {\n\t\tlogged('measure(element)', element)\n\n\t\tif (element instanceof HTMLElement) {\n\t\t\tsetWidth(logged('getElementWidth(element):', getElementWidth(element)))\n\t\t\tsetHeight(logged('getElementHeight(element):', getElementHeight(element)))\n\t\t} else if (element) {\n\t\t\tthrow new Error('Exhaustive error: Expecting element to be instance of HTMLElement or Window')\n\t\t}\n\t})\n\n\tuseLayoutEffect(() => {\n\t\tconst element = unwrapRefValue(refOrElement)\n\t\tlogged('useLayoutEffect => measure', measure.current(element))\n\t})\n\n\treturn { height, width }\n}\n"],"names":[],"mappings":";;;;;AAMA,SAAS,gBAAgB,SAAsB;AAC9C,SAAO,QAAQ;AAChB;AAEA,SAAS,iBAAiB,SAAsB;AAC/C,SAAO,QAAQ;AAChB;AAQO,SAAS,eACf,cACA,UAAiC,CAAA,GACjC,UAAkB,KAC0C;AAC5D,QAAM,EAAE,OAAW,IAAA,oBAAoB,gBAAgB,EAAE;AAEnD,QAAA,EAAE,MAAM,aAAiB,IAAA;AACzB,QAAA,iCAAiC,QAAQ,MAAM,sCAAsC,GAAG,GAAG,CAAC,GAAG,CAAC;AAEhG,QAAA,WAAW,eAAe,YAAY;AAC5C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA6B,UAAU,WAAW;AAC5E,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA6B,UAAU,YAAY;AAE/E,SAAO,cAAc,EAAE,OAAO,OAAQ,CAAA;AAEtC,qBAAmB,cAAc,CAAS,UAAA;AACzC,UAAM,aAAa,OAAO,0CAA0C,+BAA+B,KAAK,CAAC;AAErG,QAAA,UAAU,WAAW,OAAO;AAC/B,eAAS,WAAW,KAAK;AAAA,IAC1B;AACI,QAAA,WAAW,WAAW,QAAQ;AACjC,gBAAU,WAAW,MAAM;AAAA,IAC5B;AAAA,EAAA,GACE,EAAE,OAAO,OAAO;AAUb,QAAA,UAAU,OAAO,CAAC,YAAgC;AACvD,WAAO,oBAAoB,OAAO;AAElC,QAAI,mBAAmB,aAAa;AACnC,eAAS,OAAO,6BAA6B,gBAAgB,OAAO,CAAC,CAAC;AACtE,gBAAU,OAAO,8BAA8B,iBAAiB,OAAO,CAAC,CAAC;AAAA,eAC/D,SAAS;AACb,YAAA,IAAI,MAAM,6EAA6E;AAAA,IAC9F;AAAA,EAAA,CACA;AAED,kBAAgB,MAAM;AACf,UAAA,UAAU,eAAe,YAAY;AAC3C,WAAO,8BAA8B,QAAQ,QAAQ,OAAO,CAAC;AAAA,EAAA,CAC7D;AAEM,SAAA,EAAE,QAAQ;AAClB;"}
@@ -0,0 +1,19 @@
1
+ import { useRef, useEffect } from "react";
2
+ function useEventHandler(type, callback) {
3
+ const callbackRef = useRef(callback);
4
+ callbackRef.current = callback;
5
+ const stableHandler = useRef(function(event) {
6
+ return callbackRef.current(event);
7
+ }).current;
8
+ useEffect(() => {
9
+ return () => {
10
+ callbackRef.current = () => {
11
+ };
12
+ };
13
+ }, []);
14
+ return stableHandler;
15
+ }
16
+ export {
17
+ useEventHandler
18
+ };
19
+ //# sourceMappingURL=useEventHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEventHandler.js","sources":["../../../src/hooks/useEventHandler.ts"],"sourcesContent":["import { EventHandler, SyntheticEvent, useEffect, useRef } from 'react'\n\n/**\n * Similar to useEvent with inferred type for the event parameter according to event type\n *\n * @param type - String, keyof DocumentEventMap, e.g. 'click', 'keydown',...\n * @param callback - EventHandler\n * @returns Stable reference callback\n */\nexport function useEventHandler<E extends SyntheticEvent<any>>(type: E['type'], callback: EventHandler<E>): EventHandler<E> {\n\tconst callbackRef = useRef(callback); callbackRef.current = callback\n\n\tconst stableHandler = useRef(function (event: any) {\n\t\treturn callbackRef.current(event)\n\t}).current\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tcallbackRef.current = () => { }\n\t\t}\n\t}, [])\n\n\treturn stableHandler\n}\n"],"names":[],"mappings":";AASgB,SAAA,gBAA+C,MAAiB,UAA4C;AACrH,QAAA,cAAc,OAAO,QAAQ;AAAG,cAAY,UAAU;AAEtD,QAAA,gBAAgB,OAAO,SAAU,OAAY;AAC3C,WAAA,YAAY,QAAQ,KAAK;AAAA,EAChC,CAAA,EAAE;AAEH,YAAU,MAAM;AACf,WAAO,MAAM;AACZ,kBAAY,UAAU,MAAM;AAAA,MAAA;AAAA,IAAE;AAAA,EAEhC,GAAG,CAAE,CAAA;AAEE,SAAA;AACR;"}
@@ -0,0 +1,15 @@
1
+ import deepEqual from "deep-equal";
2
+ import { useRef } from "react";
3
+ function useExpectSameValueReference(next, shouldThrow = false) {
4
+ const previous = useRef(next);
5
+ if (next !== previous.current && typeof next === "object" && typeof previous.current === "object" && deepEqual(next, previous.current)) {
6
+ if (shouldThrow) {
7
+ throw new Error("Expecting same reference for same values");
8
+ }
9
+ }
10
+ previous.current = next;
11
+ }
12
+ export {
13
+ useExpectSameValueReference
14
+ };
15
+ //# sourceMappingURL=useExpectSameValueReference.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useExpectSameValueReference.js","sources":["../../../src/hooks/useExpectSameValueReference.ts"],"sourcesContent":["import deepEqual from 'deep-equal'\nimport { useRef } from 'react'\n\n// TODO: Unify error behavior with useConstantValueInvariant() and maybe merge into one hook.\nexport function useExpectSameValueReference<T>(next: T, shouldThrow: boolean = false) {\n\tconst previous = useRef(next)\n\n\tif (next !== previous.current\n\t\t&& typeof next === 'object'\n\t\t&& typeof previous.current === 'object'\n\t\t&& deepEqual(next, previous.current)\n\t) {\n\t\tif (import.meta.env.DEV) {\n\t\t\tconsole.error('Previous and next next do not match in reference although match in value', {\n\t\t\t\tprevious: previous.current,\n\t\t\t\tnext,\n\t\t\t})\n\t\t}\n\n\t\tif (shouldThrow) {\n\t\t\tthrow new Error('Expecting same reference for same values')\n\t\t}\n\t}\n\n\tprevious.current = next\n}\n"],"names":[],"mappings":";;AAIgB,SAAA,4BAA+B,MAAS,cAAuB,OAAO;AAC/E,QAAA,WAAW,OAAO,IAAI;AAE5B,MAAI,SAAS,SAAS,WAClB,OAAO,SAAS,YAChB,OAAO,SAAS,YAAY,YAC5B,UAAU,MAAM,SAAS,OAAO,GAClC;AAQD,QAAI,aAAa;AACV,YAAA,IAAI,MAAM,0CAA0C;AAAA,IAC3D;AAAA,EACD;AAEA,WAAS,UAAU;AACpB;"}
@@ -0,0 +1,35 @@
1
+ import deepEqual from "deep-equal";
2
+ import { useRef, useState, useEffect, useLayoutEffect } from "react";
3
+ import { unwrapRefValue } from "./unwrapRefValue.js";
4
+ import { useScopedConsoleRef } from "../debug-context/Context.js";
5
+ function useOnElementMutation(refOrElement, callback, options = { attributes: true, childList: true, subtree: true }) {
6
+ const scopedConsoleRef = useScopedConsoleRef("useOnElementMutation");
7
+ const callbackRef = useRef(callback);
8
+ callbackRef.current = callback;
9
+ const optionsRef = useRef(options);
10
+ optionsRef.current = options;
11
+ const [optionsState, setOptionsState] = useState(options);
12
+ useEffect(() => {
13
+ if (!deepEqual(optionsRef.current, options)) {
14
+ setOptionsState(options);
15
+ }
16
+ }, [options]);
17
+ useLayoutEffect(() => {
18
+ const element = unwrapRefValue(refOrElement);
19
+ if (element) {
20
+ let handler = function(mutations, observer2) {
21
+ scopedConsoleRef.current.logged("element.mutations", mutations);
22
+ callbackRef.current(mutations, observer2);
23
+ };
24
+ const observer = new MutationObserver(handler);
25
+ observer.observe(element, optionsState);
26
+ return () => {
27
+ observer.disconnect();
28
+ };
29
+ }
30
+ }, [optionsState, refOrElement, scopedConsoleRef]);
31
+ }
32
+ export {
33
+ useOnElementMutation
34
+ };
35
+ //# sourceMappingURL=useOnElementMutation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useOnElementMutation.js","sources":["../../../src/hooks/useOnElementMutation.ts"],"sourcesContent":["import deepEqual from 'deep-equal'\nimport { useEffect, useLayoutEffect, useRef, useState } from 'react'\nimport { useScopedConsoleRef } from '../debug-context'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\n\nexport function useOnElementMutation(\n\trefOrElement: RefObjectOrElement<HTMLElement | undefined>,\n\tcallback: MutationCallback,\n\toptions: MutationObserverInit = { attributes: true, childList: true, subtree: true },\n) {\n\tconst scopedConsoleRef = useScopedConsoleRef('useOnElementMutation')\n\n\tconst callbackRef = useRef(callback); callbackRef.current = callback\n\tconst optionsRef = useRef<MutationObserverInit>(options); optionsRef.current = options\n\n\tconst [optionsState, setOptionsState] = useState<MutationObserverInit>(options)\n\n\tuseEffect(() => {\n\t\tif (!deepEqual(optionsRef.current, options)) {\n\t\t\tsetOptionsState(options)\n\t\t}\n\t}, [options])\n\n\tuseLayoutEffect(() => {\n\t\tconst element = unwrapRefValue(refOrElement)\n\n\t\tif (element) {\n\t\t\tfunction handler(mutations: MutationRecord[], observer: MutationObserver) {\n\t\t\t\tscopedConsoleRef.current.logged('element.mutations', mutations)\n\t\t\t\tcallbackRef.current(mutations, observer)\n\t\t\t}\n\n\t\t\tconst observer = new MutationObserver(handler)\n\n\t\t\tobserver.observe(element, optionsState)\n\n\t\t\treturn () => {\n\t\t\t\tobserver.disconnect()\n\t\t\t}\n\t\t}\n\t}, [optionsState, refOrElement, scopedConsoleRef])\n}\n"],"names":["observer"],"mappings":";;;;AAKgB,SAAA,qBACf,cACA,UACA,UAAgC,EAAE,YAAY,MAAM,WAAW,MAAM,SAAS,KAAA,GAC7E;AACK,QAAA,mBAAmB,oBAAoB,sBAAsB;AAE7D,QAAA,cAAc,OAAO,QAAQ;AAAG,cAAY,UAAU;AACtD,QAAA,aAAa,OAA6B,OAAO;AAAG,aAAW,UAAU;AAE/E,QAAM,CAAC,cAAc,eAAe,IAAI,SAA+B,OAAO;AAE9E,YAAU,MAAM;AACf,QAAI,CAAC,UAAU,WAAW,SAAS,OAAO,GAAG;AAC5C,sBAAgB,OAAO;AAAA,IACxB;AAAA,EAAA,GACE,CAAC,OAAO,CAAC;AAEZ,kBAAgB,MAAM;AACf,UAAA,UAAU,eAAe,YAAY;AAE3C,QAAI,SAAS;AACH,UAAA,UAAT,SAAiB,WAA6BA,WAA4B;AACxD,yBAAA,QAAQ,OAAO,qBAAqB,SAAS;AAClD,oBAAA,QAAQ,WAAWA,SAAQ;AAAA,MAAA;AAGlC,YAAA,WAAW,IAAI,iBAAiB,OAAO;AAEpC,eAAA,QAAQ,SAAS,YAAY;AAEtC,aAAO,MAAM;AACZ,iBAAS,WAAW;AAAA,MAAA;AAAA,IAEtB;AAAA,EACE,GAAA,CAAC,cAAc,cAAc,gBAAgB,CAAC;AAClD;"}