@fluentui/react-utilities 9.0.0-alpha.9 → 9.0.0-beta.4

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 (283) hide show
  1. package/CHANGELOG.json +1132 -1
  2. package/CHANGELOG.md +492 -2
  3. package/dist/react-utilities.d.ts +335 -67
  4. package/lib/compose/getSlots.d.ts +13 -4
  5. package/lib/compose/getSlots.js +44 -32
  6. package/lib/compose/getSlots.js.map +1 -1
  7. package/lib/compose/index.d.ts +1 -2
  8. package/lib/compose/index.js +2 -2
  9. package/lib/compose/index.js.map +1 -1
  10. package/lib/compose/nullRender.js +1 -1
  11. package/lib/compose/nullRender.js.map +1 -1
  12. package/lib/compose/resolveShorthand.d.ts +12 -0
  13. package/lib/compose/resolveShorthand.js +31 -0
  14. package/lib/compose/resolveShorthand.js.map +1 -0
  15. package/lib/compose/types.d.ts +95 -21
  16. package/lib/compose/types.js +5 -0
  17. package/lib/compose/types.js.map +1 -1
  18. package/lib/hooks/index.d.ts +9 -1
  19. package/lib/hooks/index.js +9 -1
  20. package/lib/hooks/index.js.map +1 -1
  21. package/lib/hooks/useBoolean.d.ts +2 -2
  22. package/lib/hooks/useBoolean.js +16 -11
  23. package/lib/hooks/useBoolean.js.map +1 -1
  24. package/lib/hooks/useConst.js +15 -12
  25. package/lib/hooks/useConst.js.map +1 -1
  26. package/lib/hooks/useControllableState.d.ts +21 -0
  27. package/lib/hooks/useControllableState.js +68 -0
  28. package/lib/hooks/useControllableState.js.map +1 -0
  29. package/lib/hooks/useControllableValue.js +47 -50
  30. package/lib/hooks/useControllableValue.js.map +1 -1
  31. package/lib/hooks/useEventCallback.d.ts +2 -2
  32. package/lib/hooks/useEventCallback.js +14 -17
  33. package/lib/hooks/useEventCallback.js.map +1 -1
  34. package/lib/hooks/useFirstMount.d.ts +11 -0
  35. package/lib/hooks/useFirstMount.js +23 -0
  36. package/lib/hooks/useFirstMount.js.map +1 -0
  37. package/lib/hooks/useForceUpdate.d.ts +4 -0
  38. package/lib/hooks/useForceUpdate.js +9 -0
  39. package/lib/hooks/useForceUpdate.js.map +1 -0
  40. package/lib/hooks/useId.d.ts +7 -3
  41. package/lib/hooks/useId.js +13 -17
  42. package/lib/hooks/useId.js.map +1 -1
  43. package/lib/hooks/useIsomorphicLayoutEffect.d.ts +9 -0
  44. package/lib/hooks/useIsomorphicLayoutEffect.js +13 -4
  45. package/lib/hooks/useIsomorphicLayoutEffect.js.map +1 -1
  46. package/lib/hooks/useMergedRefs.js +16 -20
  47. package/lib/hooks/useMergedRefs.js.map +1 -1
  48. package/lib/hooks/useMount.d.ts +6 -0
  49. package/lib/hooks/useMount.js +17 -0
  50. package/lib/hooks/useMount.js.map +1 -0
  51. package/lib/hooks/useOnClickOutside.d.ts +14 -4
  52. package/lib/hooks/useOnClickOutside.js +129 -18
  53. package/lib/hooks/useOnClickOutside.js.map +1 -1
  54. package/lib/hooks/useOnScrollOutside.d.ts +5 -0
  55. package/lib/hooks/useOnScrollOutside.js +36 -0
  56. package/lib/hooks/useOnScrollOutside.js.map +1 -0
  57. package/lib/hooks/usePrevious.d.ts +1 -0
  58. package/lib/hooks/usePrevious.js +9 -0
  59. package/lib/hooks/usePrevious.js.map +1 -0
  60. package/lib/hooks/useTimeout.d.ts +8 -0
  61. package/lib/hooks/useTimeout.js +29 -0
  62. package/lib/hooks/useTimeout.js.map +1 -0
  63. package/lib/hooks/useUnmount.d.ts +6 -0
  64. package/lib/hooks/useUnmount.js +17 -0
  65. package/lib/hooks/useUnmount.js.map +1 -0
  66. package/lib/index.d.ts +1 -0
  67. package/lib/index.js +1 -0
  68. package/lib/index.js.map +1 -1
  69. package/lib/ssr/SSRContext.d.ts +33 -0
  70. package/lib/ssr/SSRContext.js +65 -0
  71. package/lib/ssr/SSRContext.js.map +1 -0
  72. package/lib/ssr/canUseDOM.d.ts +4 -0
  73. package/lib/ssr/canUseDOM.js +8 -0
  74. package/lib/ssr/canUseDOM.js.map +1 -0
  75. package/lib/ssr/index.d.ts +2 -0
  76. package/lib/ssr/index.js +3 -0
  77. package/lib/ssr/index.js.map +1 -0
  78. package/lib/tsdoc-metadata.json +1 -1
  79. package/lib/utils/applyTriggerPropsToChildren.d.ts +5 -0
  80. package/lib/utils/applyTriggerPropsToChildren.js +16 -0
  81. package/lib/utils/applyTriggerPropsToChildren.js.map +1 -0
  82. package/lib/utils/clamp.d.ts +8 -0
  83. package/lib/utils/clamp.js +9 -0
  84. package/lib/utils/clamp.js.map +1 -0
  85. package/lib/utils/getNativeElementProps.d.ts +22 -0
  86. package/lib/utils/getNativeElementProps.js +48 -24
  87. package/lib/utils/getNativeElementProps.js.map +1 -1
  88. package/lib/utils/getRTLSafeKey.d.ts +4 -0
  89. package/lib/utils/getRTLSafeKey.js +21 -0
  90. package/lib/utils/getRTLSafeKey.js.map +1 -0
  91. package/lib/utils/index.d.ts +5 -0
  92. package/lib/utils/index.js +5 -0
  93. package/lib/utils/index.js.map +1 -1
  94. package/lib/utils/omit.d.ts +1 -1
  95. package/lib/utils/omit.js +9 -7
  96. package/lib/utils/omit.js.map +1 -1
  97. package/lib/utils/onlyChild.d.ts +5 -0
  98. package/lib/utils/onlyChild.js +13 -0
  99. package/lib/utils/onlyChild.js.map +1 -0
  100. package/lib/utils/properties.js +80 -282
  101. package/lib/utils/properties.js.map +1 -1
  102. package/lib/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
  103. package/lib/utils/shouldPreventDefaultOnKeyDown.js +20 -0
  104. package/lib/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
  105. package/lib-commonjs/compose/getSlots.d.ts +13 -4
  106. package/lib-commonjs/compose/getSlots.js +57 -35
  107. package/lib-commonjs/compose/getSlots.js.map +1 -1
  108. package/lib-commonjs/compose/index.d.ts +1 -2
  109. package/lib-commonjs/compose/index.js +12 -4
  110. package/lib-commonjs/compose/index.js.map +1 -1
  111. package/lib-commonjs/compose/nullRender.js +9 -2
  112. package/lib-commonjs/compose/nullRender.js.map +1 -1
  113. package/lib-commonjs/compose/resolveShorthand.d.ts +12 -0
  114. package/lib-commonjs/compose/resolveShorthand.js +41 -0
  115. package/lib-commonjs/compose/resolveShorthand.js.map +1 -0
  116. package/lib-commonjs/compose/types.d.ts +95 -21
  117. package/lib-commonjs/compose/types.js +8 -1
  118. package/lib-commonjs/compose/types.js.map +1 -1
  119. package/lib-commonjs/hooks/index.d.ts +9 -1
  120. package/lib-commonjs/hooks/index.js +31 -4
  121. package/lib-commonjs/hooks/index.js.map +1 -1
  122. package/lib-commonjs/hooks/useBoolean.d.ts +2 -2
  123. package/lib-commonjs/hooks/useBoolean.js +27 -14
  124. package/lib-commonjs/hooks/useBoolean.js.map +1 -1
  125. package/lib-commonjs/hooks/useConst.js +24 -14
  126. package/lib-commonjs/hooks/useConst.js.map +1 -1
  127. package/lib-commonjs/hooks/useControllableState.d.ts +21 -0
  128. package/lib-commonjs/hooks/useControllableState.js +78 -0
  129. package/lib-commonjs/hooks/useControllableState.js.map +1 -0
  130. package/lib-commonjs/hooks/useControllableValue.js +58 -53
  131. package/lib-commonjs/hooks/useControllableValue.js.map +1 -1
  132. package/lib-commonjs/hooks/useEventCallback.d.ts +2 -2
  133. package/lib-commonjs/hooks/useEventCallback.js +26 -20
  134. package/lib-commonjs/hooks/useEventCallback.js.map +1 -1
  135. package/lib-commonjs/hooks/useFirstMount.d.ts +11 -0
  136. package/lib-commonjs/hooks/useFirstMount.js +33 -0
  137. package/lib-commonjs/hooks/useFirstMount.js.map +1 -0
  138. package/lib-commonjs/hooks/useForceUpdate.d.ts +4 -0
  139. package/lib-commonjs/hooks/useForceUpdate.js +19 -0
  140. package/lib-commonjs/hooks/useForceUpdate.js.map +1 -0
  141. package/lib-commonjs/hooks/useId.d.ts +7 -3
  142. package/lib-commonjs/hooks/useId.js +25 -21
  143. package/lib-commonjs/hooks/useId.js.map +1 -1
  144. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.d.ts +9 -0
  145. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js +22 -6
  146. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js.map +1 -1
  147. package/lib-commonjs/hooks/useMergedRefs.js +25 -22
  148. package/lib-commonjs/hooks/useMergedRefs.js.map +1 -1
  149. package/lib-commonjs/hooks/useMount.d.ts +6 -0
  150. package/lib-commonjs/hooks/useMount.js +27 -0
  151. package/lib-commonjs/hooks/useMount.js.map +1 -0
  152. package/lib-commonjs/hooks/useOnClickOutside.d.ts +14 -4
  153. package/lib-commonjs/hooks/useOnClickOutside.js +141 -21
  154. package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -1
  155. package/lib-commonjs/hooks/useOnScrollOutside.d.ts +5 -0
  156. package/lib-commonjs/hooks/useOnScrollOutside.js +47 -0
  157. package/lib-commonjs/hooks/useOnScrollOutside.js.map +1 -0
  158. package/lib-commonjs/hooks/usePrevious.d.ts +1 -0
  159. package/lib-commonjs/hooks/usePrevious.js +19 -0
  160. package/lib-commonjs/hooks/usePrevious.js.map +1 -0
  161. package/lib-commonjs/hooks/useTimeout.d.ts +8 -0
  162. package/lib-commonjs/hooks/useTimeout.js +40 -0
  163. package/lib-commonjs/hooks/useTimeout.js.map +1 -0
  164. package/lib-commonjs/hooks/useUnmount.d.ts +6 -0
  165. package/lib-commonjs/hooks/useUnmount.js +27 -0
  166. package/lib-commonjs/hooks/useUnmount.js.map +1 -0
  167. package/lib-commonjs/index.d.ts +1 -0
  168. package/lib-commonjs/index.js +11 -2
  169. package/lib-commonjs/index.js.map +1 -1
  170. package/lib-commonjs/ssr/SSRContext.d.ts +33 -0
  171. package/lib-commonjs/ssr/SSRContext.js +80 -0
  172. package/lib-commonjs/ssr/SSRContext.js.map +1 -0
  173. package/lib-commonjs/ssr/canUseDOM.d.ts +4 -0
  174. package/lib-commonjs/ssr/canUseDOM.js +17 -0
  175. package/lib-commonjs/ssr/canUseDOM.js.map +1 -0
  176. package/lib-commonjs/ssr/index.d.ts +2 -0
  177. package/lib-commonjs/ssr/index.js +12 -0
  178. package/lib-commonjs/ssr/index.js.map +1 -0
  179. package/lib-commonjs/utils/applyTriggerPropsToChildren.d.ts +5 -0
  180. package/lib-commonjs/utils/applyTriggerPropsToChildren.js +27 -0
  181. package/lib-commonjs/utils/applyTriggerPropsToChildren.js.map +1 -0
  182. package/lib-commonjs/utils/clamp.d.ts +8 -0
  183. package/lib-commonjs/utils/clamp.js +18 -0
  184. package/lib-commonjs/utils/clamp.js.map +1 -0
  185. package/lib-commonjs/utils/getNativeElementProps.d.ts +22 -0
  186. package/lib-commonjs/utils/getNativeElementProps.js +58 -25
  187. package/lib-commonjs/utils/getNativeElementProps.js.map +1 -1
  188. package/lib-commonjs/utils/getRTLSafeKey.d.ts +4 -0
  189. package/lib-commonjs/utils/getRTLSafeKey.js +30 -0
  190. package/lib-commonjs/utils/getRTLSafeKey.js.map +1 -0
  191. package/lib-commonjs/utils/index.d.ts +5 -0
  192. package/lib-commonjs/utils/index.js +19 -2
  193. package/lib-commonjs/utils/index.js.map +1 -1
  194. package/lib-commonjs/utils/omit.d.ts +1 -1
  195. package/lib-commonjs/utils/omit.js +16 -8
  196. package/lib-commonjs/utils/omit.js.map +1 -1
  197. package/lib-commonjs/utils/onlyChild.d.ts +5 -0
  198. package/lib-commonjs/utils/onlyChild.js +23 -0
  199. package/lib-commonjs/utils/onlyChild.js.map +1 -0
  200. package/lib-commonjs/utils/properties.js +84 -280
  201. package/lib-commonjs/utils/properties.js.map +1 -1
  202. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
  203. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js +30 -0
  204. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
  205. package/package.json +11 -9
  206. package/NOTICE.txt +0 -0
  207. package/config/api-extractor.json +0 -3
  208. package/config/tests.js +0 -7
  209. package/etc/react-utilities.api.md +0 -207
  210. package/just.config.ts +0 -3
  211. package/lib/compose/makeMergeProps.d.ts +0 -15
  212. package/lib/compose/makeMergeProps.js +0 -96
  213. package/lib/compose/makeMergeProps.js.map +0 -1
  214. package/lib/compose/resolveShorthandProps.d.ts +0 -7
  215. package/lib/compose/resolveShorthandProps.js +0 -27
  216. package/lib/compose/resolveShorthandProps.js.map +0 -1
  217. package/lib-amd/compose/getSlots.d.ts +0 -20
  218. package/lib-amd/compose/getSlots.js +0 -51
  219. package/lib-amd/compose/getSlots.js.map +0 -1
  220. package/lib-amd/compose/index.d.ts +0 -5
  221. package/lib-amd/compose/index.js +0 -9
  222. package/lib-amd/compose/index.js.map +0 -1
  223. package/lib-amd/compose/makeMergeProps.d.ts +0 -15
  224. package/lib-amd/compose/makeMergeProps.js +0 -98
  225. package/lib-amd/compose/makeMergeProps.js.map +0 -1
  226. package/lib-amd/compose/nullRender.d.ts +0 -4
  227. package/lib-amd/compose/nullRender.js +0 -9
  228. package/lib-amd/compose/nullRender.js.map +0 -1
  229. package/lib-amd/compose/resolveShorthandProps.d.ts +0 -7
  230. package/lib-amd/compose/resolveShorthandProps.js +0 -29
  231. package/lib-amd/compose/resolveShorthandProps.js.map +0 -1
  232. package/lib-amd/compose/types.d.ts +0 -29
  233. package/lib-amd/compose/types.js +0 -5
  234. package/lib-amd/compose/types.js.map +0 -1
  235. package/lib-amd/hooks/index.d.ts +0 -8
  236. package/lib-amd/hooks/index.js +0 -13
  237. package/lib-amd/hooks/index.js.map +0 -1
  238. package/lib-amd/hooks/useBoolean.d.ts +0 -17
  239. package/lib-amd/hooks/useBoolean.js +0 -26
  240. package/lib-amd/hooks/useBoolean.js.map +0 -1
  241. package/lib-amd/hooks/useConst.d.ts +0 -12
  242. package/lib-amd/hooks/useConst.js +0 -31
  243. package/lib-amd/hooks/useConst.js.map +0 -1
  244. package/lib-amd/hooks/useControllableValue.d.ts +0 -20
  245. package/lib-amd/hooks/useControllableValue.js +0 -42
  246. package/lib-amd/hooks/useControllableValue.js.map +0 -1
  247. package/lib-amd/hooks/useEventCallback.d.ts +0 -13
  248. package/lib-amd/hooks/useEventCallback.js +0 -33
  249. package/lib-amd/hooks/useEventCallback.js.map +0 -1
  250. package/lib-amd/hooks/useId.d.ts +0 -11
  251. package/lib-amd/hooks/useId.js +0 -35
  252. package/lib-amd/hooks/useId.js.map +0 -1
  253. package/lib-amd/hooks/useIsomorphicLayoutEffect.d.ts +0 -2
  254. package/lib-amd/hooks/useIsomorphicLayoutEffect.js +0 -9
  255. package/lib-amd/hooks/useIsomorphicLayoutEffect.js.map +0 -1
  256. package/lib-amd/hooks/useMergedRefs.d.ts +0 -13
  257. package/lib-amd/hooks/useMergedRefs.js +0 -33
  258. package/lib-amd/hooks/useMergedRefs.js.map +0 -1
  259. package/lib-amd/hooks/useOnClickOutside.d.ts +0 -19
  260. package/lib-amd/hooks/useOnClickOutside.js +0 -27
  261. package/lib-amd/hooks/useOnClickOutside.js.map +0 -1
  262. package/lib-amd/index.d.ts +0 -3
  263. package/lib-amd/index.js +0 -8
  264. package/lib-amd/index.js.map +0 -1
  265. package/lib-amd/utils/getNativeElementProps.d.ts +0 -9
  266. package/lib-amd/utils/getNativeElementProps.js +0 -40
  267. package/lib-amd/utils/getNativeElementProps.js.map +0 -1
  268. package/lib-amd/utils/index.d.ts +0 -3
  269. package/lib-amd/utils/index.js +0 -8
  270. package/lib-amd/utils/index.js.map +0 -1
  271. package/lib-amd/utils/omit.d.ts +0 -14
  272. package/lib-amd/utils/omit.js +0 -30
  273. package/lib-amd/utils/omit.js.map +0 -1
  274. package/lib-amd/utils/properties.d.ts +0 -148
  275. package/lib-amd/utils/properties.js +0 -416
  276. package/lib-amd/utils/properties.js.map +0 -1
  277. package/lib-commonjs/compose/makeMergeProps.d.ts +0 -15
  278. package/lib-commonjs/compose/makeMergeProps.js +0 -98
  279. package/lib-commonjs/compose/makeMergeProps.js.map +0 -1
  280. package/lib-commonjs/compose/resolveShorthandProps.d.ts +0 -7
  281. package/lib-commonjs/compose/resolveShorthandProps.js +0 -29
  282. package/lib-commonjs/compose/resolveShorthandProps.js.map +0 -1
  283. package/src/compose/README.md +0 -279
@@ -0,0 +1,21 @@
1
+ import * as React from 'react';
2
+ export declare type UseControllableStateOptions<State> = {
3
+ /**
4
+ * User provided default state or factory initializer
5
+ */
6
+ defaultState?: State | (() => State);
7
+ /**
8
+ * User provided controllable state, undefined state means internal state will be used
9
+ */
10
+ state: State | undefined;
11
+ /**
12
+ * Used to initialize state if all user provided states are undefined
13
+ */
14
+ initialState: State;
15
+ };
16
+ /**
17
+ * A useState 'like' hook that allows optional user control
18
+ * Useful for components which allow uncontrolled and controlled behaviour for users
19
+ * @returns - https://reactjs.org/docs/hooks-state.html
20
+ */
21
+ export declare const useControllableState: <State>(options: UseControllableStateOptions<State>) => [State, React.Dispatch<React.SetStateAction<State>>];
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useControllableState = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+
10
+ const useConst_1 = /*#__PURE__*/require("./useConst");
11
+
12
+ function isUndefined(state) {
13
+ return typeof state === 'undefined';
14
+ }
15
+
16
+ function isFactoryDispatch(newState) {
17
+ return typeof newState === 'function';
18
+ }
19
+ /**
20
+ * A useState 'like' hook that allows optional user control
21
+ * Useful for components which allow uncontrolled and controlled behaviour for users
22
+ * @returns - https://reactjs.org/docs/hooks-state.html
23
+ */
24
+
25
+
26
+ const useControllableState = options => {
27
+ const isControlled = useIsControlled(options.state);
28
+ const initialState = isUndefined(options.defaultState) ? options.initialState : options.defaultState;
29
+ const [internalState, setInternalState] = React.useState(initialState);
30
+ const state = isControlled ? options.state : internalState;
31
+ const stateRef = React.useRef(state);
32
+ React.useEffect(() => {
33
+ stateRef.current = state;
34
+ }, [state]); // To match the behavior of the setter returned by React.useState, this callback's identity
35
+ // should never change. This means it MUST NOT directly reference variables that can change.
36
+
37
+ const setState = React.useCallback(newState => {
38
+ // React dispatch can use a factory
39
+ // https://reactjs.org/docs/hooks-reference.html#functional-updates
40
+ if (isFactoryDispatch(newState)) {
41
+ stateRef.current = newState(stateRef.current);
42
+ } else {
43
+ stateRef.current = newState;
44
+ }
45
+
46
+ setInternalState(stateRef.current);
47
+ }, []);
48
+ return [state, setState];
49
+ };
50
+
51
+ exports.useControllableState = useControllableState;
52
+ /**
53
+ * Helper hook to handle previous comparison of controlled/uncontrolled
54
+ * Prints an error when isControlled value switches between subsequent renders
55
+ * @returns - whether the value is controlled
56
+ */
57
+
58
+ const useIsControlled = controlledValue => {
59
+ const isControlled = useConst_1.useConst(controlledValue !== undefined);
60
+
61
+ if (process.env.NODE_ENV !== 'production') {
62
+ // We don't want these warnings in production even though it is against native behaviour
63
+ // eslint-disable-next-line react-hooks/rules-of-hooks
64
+ React.useEffect(() => {
65
+ if (isControlled !== (controlledValue !== undefined)) {
66
+ const error = new Error();
67
+ const controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
68
+ const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
69
+
70
+ console.error([// Default react error
71
+ 'A component is changing ' + controlWarning + '. This is likely caused by the value', 'changing from ' + undefinedWarning + ' value, which should not happen.', 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.', 'More info: https://reactjs.org/link/controlled-components', error.stack].join(' '));
72
+ }
73
+ }, [isControlled, controlledValue]);
74
+ }
75
+
76
+ return isControlled;
77
+ };
78
+ //# sourceMappingURL=useControllableState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,UAAA,gBAAA,OAAA,CAAA,YAAA,CAAA;;AAiBA,SAAS,WAAT,CAAqB,KAArB,EAAmC;AACjC,SAAO,OAAO,KAAP,KAAiB,WAAxB;AACD;;AAED,SAAS,iBAAT,CAAkC,QAAlC,EAAuE;AACrE,SAAO,OAA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Z,KAAyB,YAA7B,EAA2C;AACzC;AACA;AACA,IAAA,KAAK,CAAC,SAAN,CAAgB,MAAK;AACnB,UAAI,YAAY,MAAM,eAAe,KAAK,SAA1B,CAAhB,EAAsD;AACpD,cAAM,KAAK,GAAG,IAAI,KAAJ,EAAd;AAEA,cAAM,cAAc,GAAG,YAAY,GAC/B,uCAD+B,GAE/B,wCAFJ;AAIA,cAAM,gBAAgB,GAAG,YAAY,GAAG,yBAAH,GAA+B,wBAApE,CAPoD,CASpD;;AACA,QAAA,OAAO,CAAC,KAAR,CACE,CACE;AACA,qCAA6B,cAA7B,GAA8C,sCAFhD,EAGE,mBAAmB,gBAAnB,GAAsC,kCAHxC,EAIE,oGAJF,EAKE,2DALF,EAME,KAAK,CAAC,KANR,EAOE,IAPF,CAOO,GAPP,CADF;AAUD;AACF,KAtBD,EAsBG,CAAC,YAAD,EAAe,eAAf,CAtBH;AAuBD;;AAED,SAAO,YAAP;AACD,CAhCD","sourceRoot":""}
@@ -1,63 +1,68 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var React = require("react");
4
- var useConst_1 = require("./useConst");
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useControllableValue = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+
10
+ const useConst_1 = /*#__PURE__*/require("./useConst");
11
+
5
12
  function useControllableValue(controlledValue, defaultUncontrolledValue, onChange) {
6
- var _a = React.useState(defaultUncontrolledValue), value = _a[0], setValue = _a[1];
7
- var isControlled = useIsControlled(controlledValue);
8
- var currentValue = isControlled ? controlledValue : value;
9
- // Duplicate the current value and onChange in refs so they're accessible from
10
- // setValueOrCallOnChange without creating a new callback every time
11
- var valueRef = React.useRef(currentValue);
12
- var onChangeRef = React.useRef(onChange);
13
- React.useEffect(function () {
14
- valueRef.current = currentValue;
15
- onChangeRef.current = onChange;
16
- });
17
- // To match the behavior of the setter returned by React.useState, this callback's identity
18
- // should never change. This means it MUST NOT directly reference variables that can change.
19
- var setValueOrCallOnChange = useConst_1.useConst(function () { return function (update, ev) {
20
- // Assuming here that TValue is not a function, because a controllable value will typically
21
- // be something a user can enter as input
22
- var newValue = typeof update === 'function' ? update(valueRef.current) : update;
23
- if (onChangeRef.current) {
24
- onChangeRef.current(ev, newValue);
25
- }
26
- if (!isControlled) {
27
- setValue(newValue);
28
- }
29
- }; });
30
- return [currentValue, setValueOrCallOnChange];
13
+ const [value, setValue] = React.useState(defaultUncontrolledValue);
14
+ const isControlled = useIsControlled(controlledValue);
15
+ const currentValue = isControlled ? controlledValue : value; // Duplicate the current value and onChange in refs so they're accessible from
16
+ // setValueOrCallOnChange without creating a new callback every time
17
+
18
+ const valueRef = React.useRef(currentValue);
19
+ const onChangeRef = React.useRef(onChange);
20
+ React.useEffect(() => {
21
+ valueRef.current = currentValue;
22
+ onChangeRef.current = onChange;
23
+ }); // To match the behavior of the setter returned by React.useState, this callback's identity
24
+ // should never change. This means it MUST NOT directly reference variables that can change.
25
+
26
+ const setValueOrCallOnChange = useConst_1.useConst(() => (update, ev) => {
27
+ // Assuming here that TValue is not a function, because a controllable value will typically
28
+ // be something a user can enter as input
29
+ const newValue = typeof update === 'function' ? update(valueRef.current) : update;
30
+
31
+ if (onChangeRef.current) {
32
+ onChangeRef.current(ev, newValue);
33
+ }
34
+
35
+ if (!isControlled) {
36
+ setValue(newValue);
37
+ }
38
+ });
39
+ return [currentValue, setValueOrCallOnChange];
31
40
  }
41
+
32
42
  exports.useControllableValue = useControllableValue;
33
43
  /**
34
44
  * Helper hook to handle previous comparison of controlled/uncontrolled
35
45
  * Prints an error when isControlled value switches between subsequent renders
36
46
  */
37
- var useIsControlled = function (controlledValue) {
38
- var isControlled = useConst_1.useConst(controlledValue !== undefined);
39
- if (process.env.NODE_ENV !== 'production') {
40
- // We don't want these warnings in production even though it is against native behaviour
41
- // eslint-disable-next-line react-hooks/rules-of-hooks
42
- React.useEffect(function () {
43
- if (isControlled !== (controlledValue !== undefined)) {
44
- var error = new Error();
45
- var controlWarning = isControlled
46
- ? 'a controlled value to be uncontrolled'
47
- : 'an uncontrolled value to be controlled';
48
- var undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined';
49
- // eslint-disable-next-line no-console
50
- console.error([
51
- // Default react error
52
- 'A component is changing ' + controlWarning + '. This is likely caused by the value',
53
- 'changing from ' + undefinedWarning + ' value, which should not happen.',
54
- 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.',
55
- 'More info: https://reactjs.org/link/controlled-components',
56
- error.stack,
57
- ].join(' '));
58
- }
59
- }, [isControlled, controlledValue]);
60
- }
61
- return isControlled;
47
+
48
+ const useIsControlled = controlledValue => {
49
+ const isControlled = useConst_1.useConst(controlledValue !== undefined);
50
+
51
+ if (process.env.NODE_ENV !== 'production') {
52
+ // We don't want these warnings in production even though it is against native behaviour
53
+ // eslint-disable-next-line react-hooks/rules-of-hooks
54
+ React.useEffect(() => {
55
+ if (isControlled !== (controlledValue !== undefined)) {
56
+ const error = new Error();
57
+ const controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
58
+ const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
59
+
60
+ console.error([// Default react error
61
+ 'A component is changing ' + controlWarning + '. This is likely caused by the value', 'changing from ' + undefinedWarning + ' value, which should not happen.', 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.', 'More info: https://reactjs.org/link/controlled-components', error.stack].join(' '));
62
+ }
63
+ }, [isControlled, controlledValue]);
64
+ }
65
+
66
+ return isControlled;
62
67
  };
63
68
  //# sourceMappingURL=useControllableValue.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useControllableValue.js","sourceRoot":"../src/","sources":["hooks/useControllableValue.ts"],"names":[],"mappings":";;AAAA,6BAA+B;AAC/B,uCAAsC;AAqCtC,SAAgB,oBAAoB,CAKlC,eAAuB,EACvB,wBAA8C,EAC9C,QAAmD;IAE7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AAK,YAAY,EAAE;QACzC,wFAAwF;QACxF,sDAAsD;QACtD,KAAK,CAAC,SAAS,CAAC;YACd,IAAI,YAAY,KAAK,CAAC,eAAe,KAAK,SAAS,CAAC,EAAE;gBACpD,IAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC;gBAE1B,IAAM,cAAc,GAAG,YAAY;oBACjC,CAAC,CAAC,uCAAuC;oBACzC,CAAC,CAAC,wCAAwC,CAAC;gBAE7C,IAAM,gBAAgB,GAAG,YAAY,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,wBAAwB,CAAC;gBAE7F,sCAAsC;gBACtC,OAAO,CAAC,KAAK,CACX;oBACE,sBAAsB;oBACtB,0BAA0B,GAAG,cAAc,GAAG,sCAAsC;oBACpF,gBAAgB,GAAG,gBAAgB,GAAG,kCAAkC;oBACxE,oGAAoG;oBACpG,2DAA2D;oBAC3D,KAAK,CAAC,KAAK;iBACZ,CAAC,IAAI,CAAC,GAAG,CAAC,CACZ,CAAC;aACH;QACH,CAAC,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,CAAC;KACrC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC","sourcesContent":["import * as React from 'react';\nimport { useConst } from './useConst';\n\nexport type ChangeCallback<\n TElement extends HTMLElement,\n TValue,\n TEvent extends React.SyntheticEvent<TElement> | undefined\n> = (ev: TEvent, newValue: TValue | undefined) => void;\n\n/**\n * Default value can be a value or an initializer\n */\ntype DefaultValue<TValue> = TValue | (() => TValue);\n\n/**\n * Hook to manage a value that could be either controlled or uncontrolled, such as a checked state or\n * text box string.\n * @param controlledValue - The controlled value passed in the props. This value will always be used if provided,\n * and the internal state will be updated to reflect it.\n * @param defaultUncontrolledValue - Initial value for the internal state in the uncontrolled case.\n * @returns An array of the current value and an updater callback. Like `React.useState`, the updater\n * callback always has the same identity, and it can take either a new value, or a function which\n * is passed the previous value and returns the new value.\n * @see https://reactjs.org/docs/uncontrolled-components.html\n */\nexport function useControllableValue<TValue, TElement extends HTMLElement>(\n controlledValue: TValue,\n defaultUncontrolledValue: DefaultValue<TValue>,\n): Readonly<[TValue, (update: React.SetStateAction<TValue>) => void]>;\nexport function useControllableValue<\n TValue,\n TElement extends HTMLElement,\n TEvent extends React.SyntheticEvent<TElement> | undefined\n>(\n controlledValue: TValue,\n defaultUncontrolledValue: DefaultValue<TValue>,\n onChange: ChangeCallback<TElement, TValue, TEvent>,\n): Readonly<[TValue, (update: React.SetStateAction<TValue>, ev?: React.FormEvent<TElement>) => void]>;\nexport function useControllableValue<\n TValue,\n TElement extends HTMLElement,\n TEvent extends React.SyntheticEvent<TElement> | undefined\n>(\n controlledValue: TValue,\n defaultUncontrolledValue: DefaultValue<TValue>,\n onChange?: ChangeCallback<TElement, TValue, TEvent>,\n) {\n const [value, setValue] = React.useState<TValue | undefined>(defaultUncontrolledValue);\n const isControlled = useIsControlled(controlledValue);\n const currentValue = isControlled ? controlledValue : value;\n\n // Duplicate the current value and onChange in refs so they're accessible from\n // setValueOrCallOnChange without creating a new callback every time\n const valueRef = React.useRef(currentValue);\n const onChangeRef = React.useRef(onChange);\n React.useEffect(() => {\n valueRef.current = currentValue;\n onChangeRef.current = onChange;\n });\n\n // To match the behavior of the setter returned by React.useState, this callback's identity\n // should never change. This means it MUST NOT directly reference variables that can change.\n const setValueOrCallOnChange = useConst(() => (update: React.SetStateAction<TValue | undefined>, ev?: TEvent) => {\n // Assuming here that TValue is not a function, because a controllable value will typically\n // be something a user can enter as input\n const newValue = typeof update === 'function' ? (update as Function)(valueRef.current) : update;\n\n if (onChangeRef.current) {\n onChangeRef.current(ev!, newValue);\n }\n\n if (!isControlled) {\n setValue(newValue);\n }\n });\n\n return [currentValue, setValueOrCallOnChange] as const;\n}\n\n/**\n * Helper hook to handle previous comparison of controlled/uncontrolled\n * Prints an error when isControlled value switches between subsequent renders\n */\nconst useIsControlled = <TValue>(controlledValue: TValue) => {\n const isControlled = useConst<boolean>(controlledValue !== undefined);\n\n if (process.env.NODE_ENV !== 'production') {\n // We don't want these warnings in production even though it is against native behaviour\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (isControlled !== (controlledValue !== undefined)) {\n const error = new Error();\n\n const controlWarning = isControlled\n ? 'a controlled value to be uncontrolled'\n : 'an uncontrolled value to be controlled';\n\n const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined';\n\n // eslint-disable-next-line no-console\n console.error(\n [\n // Default react error\n 'A component is changing ' + controlWarning + '. This is likely caused by the value',\n 'changing from ' + undefinedWarning + ' value, which should not happen.',\n 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.',\n 'More info: https://reactjs.org/link/controlled-components',\n error.stack,\n ].join(' '),\n );\n }\n }, [isControlled, controlledValue]);\n }\n\n return isControlled;\n};\n"]}
1
+ {"version":3,"sources":["../../src/hooks/useControllableValue.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,UAAA,gBAAA,OAAA,CAAA,YAAA,CAAA;;AAqCA,SAAgB,oBAAhB,CAKE,eALF,EAME,wBANF,EAOE,QAPF,EAOqD;AAEnD,QAAM,CAAC,KAAD,EAAQ,QAAR,IAAoB,KAAK,CAAC,QAAN,CAAmC,wBAAnC,CAA1B;AACA,QAAM,YAAY,GAAG,eAAe,CAAC,eAAD,CAApC;AACA,QAAM,YAAY,GAAG,YAAY,GAAG,eAAH,GAAqB,KAAtD,CAJmD,CAMnD;AACA;;AACA,QAAM,QAAQ,GAAG,KAAK,CAAC,MAAN,CAAa,YAAb,CAAjB;AACA,QAAM,WAAW,GAAG,KAAK,CAAC,MAAN,CAAa,QAAb,CAApB;AACA,EAAA,KAAK,CAAC,SAAN,CAAgB,MAAK;AACnB,IAAA,QAAQ,CAAC,OAAT,GAAmB,YAAnB;AACA,IAAA,WAAW,CAAC,OAAZ,GAAsB,QAAtB;AACD,GAHD,EAVmD,CAenD;AACA;;AACA,QAAM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,SAAO,YAAP;AACD,CAhCD","sourceRoot":""}
@@ -2,11 +2,11 @@
2
2
  * https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
3
3
  *
4
4
  * Modified `useCallback` that can be used when dependencies change too frequently. Can occur when
5
- * e.g. user props are depedencies which could change on every render
5
+ * e.g. user props are dependencies which could change on every render
6
6
  * e.g. volatile values (i.e. useState/useDispatch) are dependencies which could change frequently
7
7
  *
8
8
  * This should not be used often, but can be a useful re-render optimization since the callback is a ref and
9
- * will not be invalidated between rerenders
9
+ * will not be invalidated between re-renders
10
10
  *
11
11
  * @param fn - The callback function that will be used
12
12
  */
@@ -1,33 +1,39 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var React = require("react");
4
- var useIsomorphicLayoutEffect_1 = require("./useIsomorphicLayoutEffect");
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useEventCallback = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+
10
+ const useIsomorphicLayoutEffect_1 = /*#__PURE__*/require("./useIsomorphicLayoutEffect");
5
11
  /**
6
12
  * https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
7
13
  *
8
14
  * Modified `useCallback` that can be used when dependencies change too frequently. Can occur when
9
- * e.g. user props are depedencies which could change on every render
15
+ * e.g. user props are dependencies which could change on every render
10
16
  * e.g. volatile values (i.e. useState/useDispatch) are dependencies which could change frequently
11
17
  *
12
18
  * This should not be used often, but can be a useful re-render optimization since the callback is a ref and
13
- * will not be invalidated between rerenders
19
+ * will not be invalidated between re-renders
14
20
  *
15
21
  * @param fn - The callback function that will be used
16
22
  */
17
- exports.useEventCallback = function (fn) {
18
- var callbackRef = React.useRef(function () {
19
- throw new Error('Cannot call an event handler while rendering');
20
- });
21
- useIsomorphicLayoutEffect_1.useIsomorphicLayoutEffect(function () {
22
- callbackRef.current = fn;
23
- }, [fn]);
24
- return React.useCallback(function () {
25
- var args = [];
26
- for (var _i = 0; _i < arguments.length; _i++) {
27
- args[_i] = arguments[_i];
28
- }
29
- var callback = callbackRef.current;
30
- return callback.apply(void 0, args);
31
- }, [callbackRef]);
23
+
24
+
25
+ const useEventCallback = fn => {
26
+ const callbackRef = React.useRef(() => {
27
+ throw new Error('Cannot call an event handler while rendering');
28
+ });
29
+ useIsomorphicLayoutEffect_1.useIsomorphicLayoutEffect(() => {
30
+ callbackRef.current = fn;
31
+ }, [fn]);
32
+ return React.useCallback((...args) => {
33
+ const callback = callbackRef.current;
34
+ return callback(...args);
35
+ }, [callbackRef]);
32
36
  };
37
+
38
+ exports.useEventCallback = useEventCallback;
33
39
  //# sourceMappingURL=useEventCallback.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useEventCallback.js","sourceRoot":"../src/","sources":["hooks/useEventCallback.ts"],"names":[],"mappings":";;AAAA,6BAA+B;AAC/B,yEAAwE;AAExE;;;;;;;;;;;GAWG;AACU,QAAA,gBAAgB,GAAG,UAAiC,EAA6B;IAC5F,IAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAY;QAC1C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;IAEH,qDAAyB,CAAC;QACxB,WAAW,CAAC,OAAO,GAAG,EAAE,CAAC;IAC3B,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAET,OAAO,KAAK,CAAC,WAAW,CACtB;QAAC,cAAa;aAAb,UAAa,EAAb,qBAAa,EAAb,IAAa;YAAb,yBAAa;;QACZ,IAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC;QACrC,OAAO,QAAQ,eAAI,IAAI,EAAE;IAC3B,CAAC,EACD,CAAC,WAAW,CAAC,CACd,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import * as React from 'react';\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';\n\n/**\n * https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback\n *\n * Modified `useCallback` that can be used when dependencies change too frequently. Can occur when\n * e.g. user props are depedencies which could change on every render\n * e.g. volatile values (i.e. useState/useDispatch) are dependencies which could change frequently\n *\n * This should not be used often, but can be a useful re-render optimization since the callback is a ref and\n * will not be invalidated between rerenders\n *\n * @param fn - The callback function that will be used\n */\nexport const useEventCallback = <Args extends unknown[], Return>(fn: (...args: Args) => Return) => {\n const callbackRef = React.useRef<typeof fn>(() => {\n throw new Error('Cannot call an event handler while rendering');\n });\n\n useIsomorphicLayoutEffect(() => {\n callbackRef.current = fn;\n }, [fn]);\n\n return React.useCallback(\n (...args: Args) => {\n const callback = callbackRef.current;\n return callback(...args);\n },\n [callbackRef],\n );\n};\n"]}
1
+ {"version":3,"sources":["../../src/hooks/useEventCallback.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,2BAAA,gBAAA,OAAA,CAAA,6BAAA,CAAA;AAEA;;;;;;;;;;;AAWG;;;AACI,MAAM,gBAAgB,GAAoC,EAAjC,IAAkE;AAChG,QAAM,WAAW,GAAG,KAAK,CAAC,MAAN,CAAwB,MAAK;AAC/C,UAAM,IAAI,KAAJ,CAAU,8CAAV,CAAN;AACD,GAFmB,CAApB;AAIA,EAAA,2BAAA,CAAA,yBAAA,CAA0B,MAAK;AAC7B,IAAA,WAAW,CAAC,OAAZ,GAAsB,EAAtB;AACD,GAFD,EAEG,CAAC,EAAD,CAFH;AAIA,SAAO,KAAK,CAAC,WAAN,CACL,CAAC,GAAG,IAAJ,KAAkB;AAChB,UAAM,QAAQ,GAAG,WAAW,CAAC,OAA7B;AACA,WAAO,QAAQ,CAAC,GAAG,IAAJ,CAAf;AACD,GAJI,EAKL,CAAC,WAAD,CALK,CAAP;AAOD,CAhBM;;AAAM,OAAA,CAAA,gBAAA,GAAgB,gBAAhB","sourceRoot":""}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Checks if components was mounted the first time.
3
+ * Since concurrent mode will be released in the future this needs to be verified
4
+ * Currently (React 17) will always render the initial mount once
5
+ * https://codesandbox.io/s/heuristic-brook-s4w0q?file=/src/App.jsx
6
+ * https://codesandbox.io/s/holy-grass-8nieu?file=/src/App.jsx
7
+ *
8
+ * @example
9
+ * const isFirstMount = useFirstMount();
10
+ */
11
+ export declare function useFirstMount(): boolean;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFirstMount = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+ /**
10
+ * Checks if components was mounted the first time.
11
+ * Since concurrent mode will be released in the future this needs to be verified
12
+ * Currently (React 17) will always render the initial mount once
13
+ * https://codesandbox.io/s/heuristic-brook-s4w0q?file=/src/App.jsx
14
+ * https://codesandbox.io/s/holy-grass-8nieu?file=/src/App.jsx
15
+ *
16
+ * @example
17
+ * const isFirstMount = useFirstMount();
18
+ */
19
+
20
+
21
+ function useFirstMount() {
22
+ const isFirst = React.useRef(true);
23
+
24
+ if (isFirst.current) {
25
+ isFirst.current = false;
26
+ return true;
27
+ }
28
+
29
+ return isFirst.current;
30
+ }
31
+
32
+ exports.useFirstMount = useFirstMount;
33
+ //# sourceMappingURL=useFirstMount.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useFirstMount.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;AAEA;;;;;;;;;AASG;;;AACH,SAAgB,aAAhB,GAA6B;AAC3B,QAAM,OAAO,GAAG,KAAK,CAAC,MAAN,CAAa,IAAb,CAAhB;;AAEA,MAAI,OAAO,CAAC,OAAZ,EAAqB;AACnB,IAAA,OAAO,CAAC,OAAR,GAAkB,KAAlB;AACA,WAAO,IAAP;AACD;;AAED,SAAO,OAAO,CAAC,OAAf;AACD;;AATD,OAAA,CAAA,aAAA,GAAA,aAAA","sourceRoot":""}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Forces a re-render, similar to `forceUpdate` in class components.
3
+ */
4
+ export declare function useForceUpdate(): import("react").DispatchWithoutAction;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useForceUpdate = void 0;
7
+
8
+ const react_1 = /*#__PURE__*/require("react");
9
+ /**
10
+ * Forces a re-render, similar to `forceUpdate` in class components.
11
+ */
12
+
13
+
14
+ function useForceUpdate() {
15
+ return react_1.useReducer(x => x + 1, 0)[1];
16
+ }
17
+
18
+ exports.useForceUpdate = useForceUpdate;
19
+ //# sourceMappingURL=useForceUpdate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,OAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;AAEA;;AAEG;;;AACH,SAAgB,cAAhB,GAA8B;AAC5B,SAAO,OAAA,CAAA,UAAA,CAAW,CAAC,IAAI,CAAC,GAAG,CAApB,EAAuB,CAAvB,EAA0B,CAA1B,CAAP;AACD;;AAFD,OAAA,CAAA,cAAA,GAAA,cAAA","sourceRoot":""}
@@ -1,7 +1,11 @@
1
- export declare function getId(prefix?: string): string;
2
- export declare function resetIds(): void;
3
1
  /**
4
- * Hook to generate a unique ID in the global scope (spanning across duplicate copies of the same library).
2
+ * Resets generated IDs, should be used only in tests.
3
+ *
4
+ * @private
5
+ */
6
+ export declare function resetIdsForTests(): void;
7
+ /**
8
+ * Hook to generate a unique ID.
5
9
  *
6
10
  * @param prefix - Optional prefix for the ID
7
11
  * @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,
@@ -1,34 +1,38 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var React = require("react");
4
- // TODO
5
- // getId() is a temporary approach, useId() should be reimplemented to properly support SSR & scenarios with
6
- // different globals (document, window)
7
- var id = 0;
8
- function getId(prefix) {
9
- return (prefix || '') + id++;
10
- }
11
- exports.getId = getId;
12
- function resetIds() {
13
- id = 0;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useId = exports.resetIdsForTests = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+
10
+ const index_1 = /*#__PURE__*/require("../ssr/index");
11
+ /**
12
+ * Resets generated IDs, should be used only in tests.
13
+ *
14
+ * @private
15
+ */
16
+
17
+
18
+ function resetIdsForTests() {
19
+ index_1.defaultSSRContextValue.current = 0;
14
20
  }
15
- exports.resetIds = resetIds;
21
+
22
+ exports.resetIdsForTests = resetIdsForTests;
16
23
  /**
17
- * Hook to generate a unique ID in the global scope (spanning across duplicate copies of the same library).
24
+ * Hook to generate a unique ID.
18
25
  *
19
26
  * @param prefix - Optional prefix for the ID
20
27
  * @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,
21
28
  * without conditioning the hook call
22
29
  * @returns The ID
23
30
  */
31
+
24
32
  function useId(prefix, providedId) {
25
- // getId should only be called once since it updates the global constant for the next ID value.
26
- // (While an extra update isn't likely to cause problems in practice, it's better to avoid it.)
27
- var ref = React.useRef(providedId);
28
- if (!ref.current) {
29
- ref.current = getId(prefix);
30
- }
31
- return ref.current;
33
+ const contextValue = index_1.useSSRContext();
34
+ return React.useMemo(() => providedId || `${prefix}${++contextValue.current}`, [prefix, providedId, contextValue]);
32
35
  }
36
+
33
37
  exports.useId = useId;
34
38
  //# sourceMappingURL=useId.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useId.js","sourceRoot":"../src/","sources":["hooks/useId.ts"],"names":[],"mappings":";;AAAA,6BAA+B;AAC/B,OAAO;AACP,4GAA4G;AAC5G,uCAAuC;AACvC,IAAI,EAAE,GAAG,CAAC,CAAC;AAEX,SAAgB,KAAK,CAAC,MAAe;IACnC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC;AAC/B,CAAC;AAFD,sBAEC;AAED,SAAgB,QAAQ;IACtB,EAAE,GAAG,CAAC,CAAC;AACT,CAAC;AAFD,4BAEC;AAED;;;;;;;GAOG;AACH,SAAgB,KAAK,CAAC,MAAe,EAAE,UAAmB;IACxD,+FAA+F;IAC/F,+FAA+F;IAC/F,IAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAqB,UAAU,CAAC,CAAC;IACzD,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE;QAChB,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;KAC7B;IACD,OAAO,GAAG,CAAC,OAAO,CAAC;AACrB,CAAC;AARD,sBAQC","sourcesContent":["import * as React from 'react';\n// TODO\n// getId() is a temporary approach, useId() should be reimplemented to properly support SSR & scenarios with\n// different globals (document, window)\nlet id = 0;\n\nexport function getId(prefix?: string): string {\n return (prefix || '') + id++;\n}\n\nexport function resetIds(): void {\n id = 0;\n}\n\n/**\n * Hook to generate a unique ID in the global scope (spanning across duplicate copies of the same library).\n *\n * @param prefix - Optional prefix for the ID\n * @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,\n * without conditioning the hook call\n * @returns The ID\n */\nexport function useId(prefix?: string, providedId?: string): string {\n // getId should only be called once since it updates the global constant for the next ID value.\n // (While an extra update isn't likely to cause problems in practice, it's better to avoid it.)\n const ref = React.useRef<string | undefined>(providedId);\n if (!ref.current) {\n ref.current = getId(prefix);\n }\n return ref.current;\n}\n"]}
1
+ {"version":3,"sources":["../../src/hooks/useId.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,OAAA,gBAAA,OAAA,CAAA,cAAA,CAAA;AAEA;;;;AAIG;;;AACH,SAAgB,gBAAhB,GAAgC;AAC9B,EAAA,OAAA,CAAA,sBAAA,CAAuB,OAAvB,GAAiC,CAAjC;AACD;;AAFD,OAAA,CAAA,gBAAA,GAAA,gBAAA;AAIA;;;;;;;AAOG;;AACH,SAAgB,KAAhB,CAAsB,MAAtB,EAAuC,UAAvC,EAA0D;AACxD,QAAM,YAAY,GAAG,OAAA,CAAA,aAAA,EAArB;AAEA,SAAO,KAAK,CAAC,OAAN,CAAc,MAAM,UAAU,IAAI,GAAG,MAAM,GAAG,EAAE,YAAY,CAAC,OAAO,EAApE,EAAwE,CAAC,MAAD,EAAS,UAAT,EAAqB,YAArB,CAAxE,CAAP;AACD;;AAJD,OAAA,CAAA,KAAA,GAAA,KAAA","sourceRoot":""}
@@ -1,2 +1,11 @@
1
1
  import * as React from 'react';
2
+ /**
3
+ * React currently throws a warning when using useLayoutEffect on the server. To get around it, we can conditionally
4
+ * useEffect on the server (no-op) and useLayoutEffect in the browser. We occasionally need useLayoutEffect to
5
+ * ensure we don't get a render flash for certain operations, but we may also need affected components to render on
6
+ * the server.
7
+ *
8
+ * https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
9
+ * https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.js
10
+ */
2
11
  export declare const useIsomorphicLayoutEffect: typeof React.useEffect;
@@ -1,8 +1,24 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var React = require("react");
4
- var isSSR = typeof window === 'undefined' || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent);
5
- // useLayoutEffect() produces a warning with SSR rendering
6
- // https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
7
- exports.useIsomorphicLayoutEffect = isSSR ? React.useEffect : React.useLayoutEffect;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useIsomorphicLayoutEffect = void 0;
7
+
8
+ const React = /*#__PURE__*/require("react");
9
+
10
+ const index_1 = /*#__PURE__*/require("../ssr/index");
11
+ /**
12
+ * React currently throws a warning when using useLayoutEffect on the server. To get around it, we can conditionally
13
+ * useEffect on the server (no-op) and useLayoutEffect in the browser. We occasionally need useLayoutEffect to
14
+ * ensure we don't get a render flash for certain operations, but we may also need affected components to render on
15
+ * the server.
16
+ *
17
+ * https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
18
+ * https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.js
19
+ */
20
+ // eslint-disable-next-line no-restricted-properties
21
+
22
+
23
+ exports.useIsomorphicLayoutEffect = /*#__PURE__*/index_1.canUseDOM() ? React.useLayoutEffect : React.useEffect;
8
24
  //# sourceMappingURL=useIsomorphicLayoutEffect.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useIsomorphicLayoutEffect.js","sourceRoot":"../src/","sources":["hooks/useIsomorphicLayoutEffect.ts"],"names":[],"mappings":";;AAAA,6BAA+B;AAE/B,IAAM,KAAK,GACT,OAAO,MAAM,KAAK,WAAW,IAAI,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AAE9G,0DAA0D;AAC1D,2EAA2E;AAC9D,QAAA,yBAAyB,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,eAAe,CAAC","sourcesContent":["import * as React from 'react';\n\nconst isSSR =\n typeof window === 'undefined' || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent);\n\n// useLayoutEffect() produces a warning with SSR rendering\n// https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a\nexport const useIsomorphicLayoutEffect = isSSR ? React.useEffect : React.useLayoutEffect;\n"]}
1
+ {"version":3,"sources":["../../src/hooks/useIsomorphicLayoutEffect.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;;AACA,MAAA,OAAA,gBAAA,OAAA,CAAA,cAAA,CAAA;AAEA;;;;;;;;AAQG;AACH;;;AACa,OAAA,CAAA,yBAAA,GAAoD,aAAA,OAAA,CAAA,SAAA,KAAc,KAAK,CAAC,eAApB,GAAsC,KAAK,CAAC,SAAhG","sourceRoot":""}