@fluentui/react-utilities 0.0.0-nightly050f89bf0020211102.1

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 (217) hide show
  1. package/CHANGELOG.json +1637 -0
  2. package/CHANGELOG.md +677 -0
  3. package/LICENSE +15 -0
  4. package/README.md +5 -0
  5. package/dist/react-utilities.d.ts +648 -0
  6. package/lib/compose/getSlots.d.ts +29 -0
  7. package/lib/compose/getSlots.js +72 -0
  8. package/lib/compose/getSlots.js.map +1 -0
  9. package/lib/compose/index.d.ts +4 -0
  10. package/lib/compose/index.js +5 -0
  11. package/lib/compose/index.js.map +1 -0
  12. package/lib/compose/nullRender.d.ts +4 -0
  13. package/lib/compose/nullRender.js +7 -0
  14. package/lib/compose/nullRender.js.map +1 -0
  15. package/lib/compose/resolveShorthand.d.ts +12 -0
  16. package/lib/compose/resolveShorthand.js +30 -0
  17. package/lib/compose/resolveShorthand.js.map +1 -0
  18. package/lib/compose/types.d.ts +104 -0
  19. package/lib/compose/types.js +6 -0
  20. package/lib/compose/types.js.map +1 -0
  21. package/lib/hooks/index.d.ts +16 -0
  22. package/lib/hooks/index.js +17 -0
  23. package/lib/hooks/index.js.map +1 -0
  24. package/lib/hooks/useBoolean.d.ts +17 -0
  25. package/lib/hooks/useBoolean.js +39 -0
  26. package/lib/hooks/useBoolean.js.map +1 -0
  27. package/lib/hooks/useConst.d.ts +12 -0
  28. package/lib/hooks/useConst.js +30 -0
  29. package/lib/hooks/useConst.js.map +1 -0
  30. package/lib/hooks/useControllableState.d.ts +21 -0
  31. package/lib/hooks/useControllableState.js +72 -0
  32. package/lib/hooks/useControllableState.js.map +1 -0
  33. package/lib/hooks/useControllableValue.d.ts +20 -0
  34. package/lib/hooks/useControllableValue.js +62 -0
  35. package/lib/hooks/useControllableValue.js.map +1 -0
  36. package/lib/hooks/useEventCallback.d.ts +13 -0
  37. package/lib/hooks/useEventCallback.js +34 -0
  38. package/lib/hooks/useEventCallback.js.map +1 -0
  39. package/lib/hooks/useFirstMount.d.ts +11 -0
  40. package/lib/hooks/useFirstMount.js +23 -0
  41. package/lib/hooks/useFirstMount.js.map +1 -0
  42. package/lib/hooks/useForceUpdate.d.ts +4 -0
  43. package/lib/hooks/useForceUpdate.js +11 -0
  44. package/lib/hooks/useForceUpdate.js.map +1 -0
  45. package/lib/hooks/useId.d.ts +15 -0
  46. package/lib/hooks/useId.js +27 -0
  47. package/lib/hooks/useId.js.map +1 -0
  48. package/lib/hooks/useIsomorphicLayoutEffect.d.ts +11 -0
  49. package/lib/hooks/useIsomorphicLayoutEffect.js +15 -0
  50. package/lib/hooks/useIsomorphicLayoutEffect.js.map +1 -0
  51. package/lib/hooks/useMergedRefs.d.ts +13 -0
  52. package/lib/hooks/useMergedRefs.js +34 -0
  53. package/lib/hooks/useMergedRefs.js.map +1 -0
  54. package/lib/hooks/useMount.d.ts +6 -0
  55. package/lib/hooks/useMount.js +17 -0
  56. package/lib/hooks/useMount.js.map +1 -0
  57. package/lib/hooks/useOnClickOutside.d.ts +29 -0
  58. package/lib/hooks/useOnClickOutside.js +142 -0
  59. package/lib/hooks/useOnClickOutside.js.map +1 -0
  60. package/lib/hooks/useOnScrollOutside.d.ts +5 -0
  61. package/lib/hooks/useOnScrollOutside.js +38 -0
  62. package/lib/hooks/useOnScrollOutside.js.map +1 -0
  63. package/lib/hooks/usePrevious.d.ts +1 -0
  64. package/lib/hooks/usePrevious.js +9 -0
  65. package/lib/hooks/usePrevious.js.map +1 -0
  66. package/lib/hooks/useTimeout.d.ts +8 -0
  67. package/lib/hooks/useTimeout.js +33 -0
  68. package/lib/hooks/useTimeout.js.map +1 -0
  69. package/lib/hooks/useUnmount.d.ts +6 -0
  70. package/lib/hooks/useUnmount.js +19 -0
  71. package/lib/hooks/useUnmount.js.map +1 -0
  72. package/lib/index.d.ts +4 -0
  73. package/lib/index.js +5 -0
  74. package/lib/index.js.map +1 -0
  75. package/lib/ssr/SSRContext.d.ts +33 -0
  76. package/lib/ssr/SSRContext.js +71 -0
  77. package/lib/ssr/SSRContext.js.map +1 -0
  78. package/lib/ssr/canUseDOM.d.ts +4 -0
  79. package/lib/ssr/canUseDOM.js +8 -0
  80. package/lib/ssr/canUseDOM.js.map +1 -0
  81. package/lib/ssr/index.d.ts +2 -0
  82. package/lib/ssr/index.js +3 -0
  83. package/lib/ssr/index.js.map +1 -0
  84. package/lib/tsdoc-metadata.json +11 -0
  85. package/lib/utils/applyTriggerPropsToChildren.d.ts +5 -0
  86. package/lib/utils/applyTriggerPropsToChildren.js +16 -0
  87. package/lib/utils/applyTriggerPropsToChildren.js.map +1 -0
  88. package/lib/utils/clamp.d.ts +8 -0
  89. package/lib/utils/clamp.js +11 -0
  90. package/lib/utils/clamp.js.map +1 -0
  91. package/lib/utils/getNativeElementProps.d.ts +9 -0
  92. package/lib/utils/getNativeElementProps.js +38 -0
  93. package/lib/utils/getNativeElementProps.js.map +1 -0
  94. package/lib/utils/getRTLSafeKey.d.ts +4 -0
  95. package/lib/utils/getRTLSafeKey.js +21 -0
  96. package/lib/utils/getRTLSafeKey.js.map +1 -0
  97. package/lib/utils/index.d.ts +8 -0
  98. package/lib/utils/index.js +9 -0
  99. package/lib/utils/index.js.map +1 -0
  100. package/lib/utils/omit.d.ts +14 -0
  101. package/lib/utils/omit.js +27 -0
  102. package/lib/utils/omit.js.map +1 -0
  103. package/lib/utils/onlyChild.d.ts +5 -0
  104. package/lib/utils/onlyChild.js +13 -0
  105. package/lib/utils/onlyChild.js.map +1 -0
  106. package/lib/utils/properties.d.ts +148 -0
  107. package/lib/utils/properties.js +218 -0
  108. package/lib/utils/properties.js.map +1 -0
  109. package/lib/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
  110. package/lib/utils/shouldPreventDefaultOnKeyDown.js +20 -0
  111. package/lib/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
  112. package/lib-commonjs/compose/getSlots.d.ts +29 -0
  113. package/lib-commonjs/compose/getSlots.js +85 -0
  114. package/lib-commonjs/compose/getSlots.js.map +1 -0
  115. package/lib-commonjs/compose/index.d.ts +4 -0
  116. package/lib-commonjs/compose/index.js +16 -0
  117. package/lib-commonjs/compose/index.js.map +1 -0
  118. package/lib-commonjs/compose/nullRender.d.ts +4 -0
  119. package/lib-commonjs/compose/nullRender.js +16 -0
  120. package/lib-commonjs/compose/nullRender.js.map +1 -0
  121. package/lib-commonjs/compose/resolveShorthand.d.ts +12 -0
  122. package/lib-commonjs/compose/resolveShorthand.js +41 -0
  123. package/lib-commonjs/compose/resolveShorthand.js.map +1 -0
  124. package/lib-commonjs/compose/types.d.ts +104 -0
  125. package/lib-commonjs/compose/types.js +10 -0
  126. package/lib-commonjs/compose/types.js.map +1 -0
  127. package/lib-commonjs/hooks/index.d.ts +16 -0
  128. package/lib-commonjs/hooks/index.js +40 -0
  129. package/lib-commonjs/hooks/index.js.map +1 -0
  130. package/lib-commonjs/hooks/useBoolean.d.ts +17 -0
  131. package/lib-commonjs/hooks/useBoolean.js +50 -0
  132. package/lib-commonjs/hooks/useBoolean.js.map +1 -0
  133. package/lib-commonjs/hooks/useConst.d.ts +12 -0
  134. package/lib-commonjs/hooks/useConst.js +40 -0
  135. package/lib-commonjs/hooks/useConst.js.map +1 -0
  136. package/lib-commonjs/hooks/useControllableState.d.ts +21 -0
  137. package/lib-commonjs/hooks/useControllableState.js +82 -0
  138. package/lib-commonjs/hooks/useControllableState.js.map +1 -0
  139. package/lib-commonjs/hooks/useControllableValue.d.ts +20 -0
  140. package/lib-commonjs/hooks/useControllableValue.js +73 -0
  141. package/lib-commonjs/hooks/useControllableValue.js.map +1 -0
  142. package/lib-commonjs/hooks/useEventCallback.d.ts +13 -0
  143. package/lib-commonjs/hooks/useEventCallback.js +45 -0
  144. package/lib-commonjs/hooks/useEventCallback.js.map +1 -0
  145. package/lib-commonjs/hooks/useFirstMount.d.ts +11 -0
  146. package/lib-commonjs/hooks/useFirstMount.js +33 -0
  147. package/lib-commonjs/hooks/useFirstMount.js.map +1 -0
  148. package/lib-commonjs/hooks/useForceUpdate.d.ts +4 -0
  149. package/lib-commonjs/hooks/useForceUpdate.js +21 -0
  150. package/lib-commonjs/hooks/useForceUpdate.js.map +1 -0
  151. package/lib-commonjs/hooks/useId.d.ts +15 -0
  152. package/lib-commonjs/hooks/useId.js +40 -0
  153. package/lib-commonjs/hooks/useId.js.map +1 -0
  154. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.d.ts +11 -0
  155. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js +24 -0
  156. package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js.map +1 -0
  157. package/lib-commonjs/hooks/useMergedRefs.d.ts +13 -0
  158. package/lib-commonjs/hooks/useMergedRefs.js +45 -0
  159. package/lib-commonjs/hooks/useMergedRefs.js.map +1 -0
  160. package/lib-commonjs/hooks/useMount.d.ts +6 -0
  161. package/lib-commonjs/hooks/useMount.js +27 -0
  162. package/lib-commonjs/hooks/useMount.js.map +1 -0
  163. package/lib-commonjs/hooks/useOnClickOutside.d.ts +29 -0
  164. package/lib-commonjs/hooks/useOnClickOutside.js +153 -0
  165. package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -0
  166. package/lib-commonjs/hooks/useOnScrollOutside.d.ts +5 -0
  167. package/lib-commonjs/hooks/useOnScrollOutside.js +49 -0
  168. package/lib-commonjs/hooks/useOnScrollOutside.js.map +1 -0
  169. package/lib-commonjs/hooks/usePrevious.d.ts +1 -0
  170. package/lib-commonjs/hooks/usePrevious.js +19 -0
  171. package/lib-commonjs/hooks/usePrevious.js.map +1 -0
  172. package/lib-commonjs/hooks/useTimeout.d.ts +8 -0
  173. package/lib-commonjs/hooks/useTimeout.js +44 -0
  174. package/lib-commonjs/hooks/useTimeout.js.map +1 -0
  175. package/lib-commonjs/hooks/useUnmount.d.ts +6 -0
  176. package/lib-commonjs/hooks/useUnmount.js +29 -0
  177. package/lib-commonjs/hooks/useUnmount.js.map +1 -0
  178. package/lib-commonjs/index.d.ts +4 -0
  179. package/lib-commonjs/index.js +16 -0
  180. package/lib-commonjs/index.js.map +1 -0
  181. package/lib-commonjs/ssr/SSRContext.d.ts +33 -0
  182. package/lib-commonjs/ssr/SSRContext.js +86 -0
  183. package/lib-commonjs/ssr/SSRContext.js.map +1 -0
  184. package/lib-commonjs/ssr/canUseDOM.d.ts +4 -0
  185. package/lib-commonjs/ssr/canUseDOM.js +17 -0
  186. package/lib-commonjs/ssr/canUseDOM.js.map +1 -0
  187. package/lib-commonjs/ssr/index.d.ts +2 -0
  188. package/lib-commonjs/ssr/index.js +12 -0
  189. package/lib-commonjs/ssr/index.js.map +1 -0
  190. package/lib-commonjs/utils/applyTriggerPropsToChildren.d.ts +5 -0
  191. package/lib-commonjs/utils/applyTriggerPropsToChildren.js +27 -0
  192. package/lib-commonjs/utils/applyTriggerPropsToChildren.js.map +1 -0
  193. package/lib-commonjs/utils/clamp.d.ts +8 -0
  194. package/lib-commonjs/utils/clamp.js +20 -0
  195. package/lib-commonjs/utils/clamp.js.map +1 -0
  196. package/lib-commonjs/utils/getNativeElementProps.d.ts +9 -0
  197. package/lib-commonjs/utils/getNativeElementProps.js +48 -0
  198. package/lib-commonjs/utils/getNativeElementProps.js.map +1 -0
  199. package/lib-commonjs/utils/getRTLSafeKey.d.ts +4 -0
  200. package/lib-commonjs/utils/getRTLSafeKey.js +30 -0
  201. package/lib-commonjs/utils/getRTLSafeKey.js.map +1 -0
  202. package/lib-commonjs/utils/index.d.ts +8 -0
  203. package/lib-commonjs/utils/index.js +24 -0
  204. package/lib-commonjs/utils/index.js.map +1 -0
  205. package/lib-commonjs/utils/omit.d.ts +14 -0
  206. package/lib-commonjs/utils/omit.js +36 -0
  207. package/lib-commonjs/utils/omit.js.map +1 -0
  208. package/lib-commonjs/utils/onlyChild.d.ts +5 -0
  209. package/lib-commonjs/utils/onlyChild.js +23 -0
  210. package/lib-commonjs/utils/onlyChild.js.map +1 -0
  211. package/lib-commonjs/utils/properties.d.ts +148 -0
  212. package/lib-commonjs/utils/properties.js +227 -0
  213. package/lib-commonjs/utils/properties.js.map +1 -0
  214. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.d.ts +7 -0
  215. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js +30 -0
  216. package/lib-commonjs/utils/shouldPreventDefaultOnKeyDown.js.map +1 -0
  217. package/package.json +55 -0
@@ -0,0 +1,29 @@
1
+ import * as React from 'react';
2
+ import { AsIntrinsicElement, ComponentState, ObjectShorthandPropsRecord, ObjectShorthandProps, UnionToIntersection } from './types';
3
+ export declare type Slots<S extends ObjectShorthandPropsRecord> = {
4
+ [K in keyof S]-?: NonNullable<S[K]> extends AsIntrinsicElement<infer As> ? As : S[K] extends ObjectShorthandProps<infer P> ? React.ElementType<NonNullable<P>> : React.ElementType<NonNullable<S[K]>>;
5
+ };
6
+ declare type SlotProps<S extends ObjectShorthandPropsRecord> = {
7
+ [K in keyof S]-?: NonNullable<S[K]> extends AsIntrinsicElement<infer As> ? UnionToIntersection<JSX.IntrinsicElements[As]> : NonNullable<S[K]> extends ObjectShorthandProps<infer P> ? P : never;
8
+ };
9
+ /**
10
+ * Given the state and an array of slot names, will break out `slots` and `slotProps`
11
+ * collections.
12
+ *
13
+ * The root is derived from a mix of `components` props and `as` prop.
14
+ *
15
+ * Slots will render as null if they are rendered as primitives with undefined children.
16
+ *
17
+ * The slotProps will always omit the `as` prop within them, and for slots that are string
18
+ * primitives, the props will be filtered according the the slot type. For example, if the
19
+ * slot is rendered `as: 'a'`, the props will be filtered for acceptable anchor props.
20
+ *
21
+ * @param state - State including slot definitions
22
+ * @param slotNames - Name of which props are slots
23
+ * @returns An object containing the `slots` map and `slotProps` map.
24
+ */
25
+ export declare function getSlots<R extends ObjectShorthandPropsRecord>(state: ComponentState<R>, slotNames?: (keyof R)[]): {
26
+ slots: Slots<R>;
27
+ slotProps: SlotProps<R>;
28
+ };
29
+ export {};
@@ -0,0 +1,72 @@
1
+ import { __rest } from "tslib";
2
+ import * as React from 'react';
3
+ import { nullRender } from './nullRender';
4
+ import { omit } from '../utils/omit';
5
+ /**
6
+ * Given the state and an array of slot names, will break out `slots` and `slotProps`
7
+ * collections.
8
+ *
9
+ * The root is derived from a mix of `components` props and `as` prop.
10
+ *
11
+ * Slots will render as null if they are rendered as primitives with undefined children.
12
+ *
13
+ * The slotProps will always omit the `as` prop within them, and for slots that are string
14
+ * primitives, the props will be filtered according the the slot type. For example, if the
15
+ * slot is rendered `as: 'a'`, the props will be filtered for acceptable anchor props.
16
+ *
17
+ * @param state - State including slot definitions
18
+ * @param slotNames - Name of which props are slots
19
+ * @returns An object containing the `slots` map and `slotProps` map.
20
+ */
21
+
22
+ export function getSlots(state, slotNames) {
23
+ if (slotNames === void 0) {
24
+ slotNames = ['root'];
25
+ }
26
+
27
+ var slots = {};
28
+ var slotProps = {};
29
+
30
+ for (var _i = 0, slotNames_1 = slotNames; _i < slotNames_1.length; _i++) {
31
+ var slotName = slotNames_1[_i];
32
+
33
+ var _a = getSlot(state, slotName),
34
+ slot = _a[0],
35
+ props = _a[1];
36
+
37
+ slots[slotName] = slot;
38
+ slotProps[slotName] = props;
39
+ }
40
+
41
+ return {
42
+ slots: slots,
43
+ slotProps: slotProps
44
+ };
45
+ }
46
+
47
+ function getSlot(state, slotName) {
48
+ var _a, _b, _c;
49
+
50
+ if (state[slotName] === undefined) {
51
+ return [nullRender, undefined];
52
+ }
53
+
54
+ var _d = state[slotName],
55
+ children = _d.children,
56
+ asProp = _d.as,
57
+ rest = __rest(_d, ["children", "as"]);
58
+
59
+ var slot = ((_a = state.components) === null || _a === void 0 ? void 0 : _a[slotName]) === undefined || typeof state.components[slotName] === 'string' ? asProp || ((_b = state.components) === null || _b === void 0 ? void 0 : _b[slotName]) || 'div' : state.components[slotName];
60
+
61
+ if (typeof children === 'function') {
62
+ var render = children;
63
+ return [React.Fragment, {
64
+ children: render(slot, rest)
65
+ }];
66
+ }
67
+
68
+ var shouldOmitAsProp = typeof slot === 'string' && ((_c = state[slotName]) === null || _c === void 0 ? void 0 : _c.as);
69
+ var slotProps = shouldOmitAsProp ? omit(state[slotName], ['as']) : state[slotName];
70
+ return [slot, slotProps];
71
+ }
72
+ //# sourceMappingURL=getSlots.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/compose/getSlots.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAUA,SAAS,UAAT,QAA2B,cAA3B;AACA,SAAS,IAAT,QAAqB,eAArB;AAqBA;;;;;;;;;;;;;;;AAeG;;AACH,OAAM,SAAU,QAAV,CACJ,KADI,EAEJ,SAFI,EAE6B;AAAjC,MAAA,SAAA,KAAA,KAAA,CAAA,EAAA;AAAA,IAAA,SAAA,GAAA,CAA0B,MAA1B,CAAA;AAAiC;;AAKjC,MAAM,KAAK,GAAG,EAAd;AACA,MAAM,SAAS,GAAG,EAAlB;;AAEA,OAAuB,IAAA,EAAA,GAAA,CAAA,EAAA,WAAA,GAAA,SAAvB,EAAuB,EAAA,GAAA,WAAA,CAAA,MAAvB,EAAuB,EAAA,EAAvB,EAAkC;AAA7B,QAAM,QAAQ,GAAA,WAAA,CAAA,EAAA,CAAd;;AACG,QAAA,EAAA,GAAgB,OAAO,CAAC,KAAD,EAAQ,QAAR,CAAvB;AAAA,QAAC,IAAI,GAAA,EAAA,CAAA,CAAA,CAAL;AAAA,QAAO,KAAK,GAAA,EAAA,CAAA,CAAA,CAAZ;;AACN,IAAA,KAAK,CAAC,QAAD,CAAL,GAAkB,IAAlB;AACA,IAAA,SAAS,CAAC,QAAD,CAAT,GAAsB,KAAtB;AACD;;AACD,SAAO;AAAE,IAAA,KAAK,EAAA,KAAP;AAAS,IAAA,SAAS,EAAG;AAArB,GAAP;AACD;;AAED,SAAS,OAAT,CACE,KADF,EAEE,QAFF,EAEa;;;AAEX,MAAI,KAAK,CAAC,QAAD,CAAL,KAAoB,SAAxB,EAAmC;AACjC,WAAO,CAAC,UAAD,EAAa,SAAb,CAAP;AACD;;AACD,MAAM,EAAA,GAAoC,KAAK,CAAC,QAAD,CAA/C;AAAA,MAAQ,QAAQ,GAAA,EAAA,CAAA,QAAhB;AAAA,MAAsB,MAAM,GAAA,EAAA,CAAA,EAA5B;AAAA,MAAiC,IAAI,GAAA,MAAA,CAAA,EAAA,EAA/B,CAAA,UAAA,EAAA,IAAA,CAA+B,CAArC;;AAEA,MAAM,IAAI,GAAI,CAAA,CAAA,EAAA,GAAA,KAAK,CAAC,UAAN,MAAgB,IAAhB,IAAgB,EAAA,KAAA,KAAA,CAAhB,GAAgB,KAAA,CAAhB,GAAgB,EAAA,CAAG,QAAH,CAAhB,MAAiC,SAAjC,IAA8C,OAAO,KAAK,CAAC,UAAN,CAAiB,QAAjB,CAAP,KAAsC,QAApF,GACV,MAAM,KAAA,CAAA,EAAA,GAAI,KAAK,CAAC,UAAV,MAAoB,IAApB,IAAoB,EAAA,KAAA,KAAA,CAApB,GAAoB,KAAA,CAApB,GAAoB,EAAA,CAAG,QAAH,CAApB,CAAN,IAA0C,KADhC,GAEV,KAAK,CAAC,UAAN,CAAiB,QAAjB,CAFJ;;AAIA,MAAI,OAAO,QAAP,KAAoB,UAAxB,EAAoC;AAClC,QAAM,MAAM,GAAG,QAAf;AACA,WAAO,CACL,KAAK,CAAC,QADD,EAEJ;AACC,MAAA,QAAQ,EAAE,MAAM,CAAC,IAAD,EAAO,IAAP;AADjB,KAFI,CAAP;AAMD;;AAED,MAAM,gBAAgB,GAAG,OAAO,IAAP,KAAgB,QAAhB,KAAwB,CAAA,EAAA,GAAI,KAAK,CAAC,QAAD,CAAT,MAAmB,IAAnB,IAAmB,EAAA,KAAA,KAAA,CAAnB,GAAmB,KAAA,CAAnB,GAAmB,EAAA,CAAE,EAA7C,CAAzB;AACA,MAAM,SAAS,GAAI,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,QAAD,CAAN,EAAmB,CAAC,IAAD,CAAnB,CAAP,GAAoC,KAAK,CAAC,QAAD,CAA5E;AAEA,SAAO,CAAC,IAAD,EAAO,SAAP,CAAP;AACD","sourceRoot":""}
@@ -0,0 +1,4 @@
1
+ export * from './getSlots';
2
+ export * from './nullRender';
3
+ export * from './resolveShorthand';
4
+ export * from './types';
@@ -0,0 +1,5 @@
1
+ export * from './getSlots';
2
+ export * from './nullRender';
3
+ export * from './resolveShorthand';
4
+ export * from './types';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/compose/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC"}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Simple constant function for returning null, used to render empty templates in JSX.
3
+ */
4
+ export declare const nullRender: () => null;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Simple constant function for returning null, used to render empty templates in JSX.
3
+ */
4
+ export var nullRender = function () {
5
+ return null;
6
+ };
7
+ //# sourceMappingURL=nullRender.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/compose/nullRender.tsx"],"names":[],"mappings":"AAAA;;AAEG;AACH,OAAO,IAAM,UAAU,GAAG,YAAA;AAAM,SAAA,IAAA;AAAI,CAA7B","sourceRoot":""}
@@ -0,0 +1,12 @@
1
+ import type { DefaultObjectShorthandProps, ShorthandProps } from './types';
2
+ export declare type ResolveShorthandOptions<Props extends Record<string, any>, Required extends boolean = false> = {
3
+ required?: Required;
4
+ defaultProps?: Props;
5
+ };
6
+ /**
7
+ * Resolves ShorthandProps into ObjectShorthandProps, to ensure normalization of the signature
8
+ * being passed down to getSlots method
9
+ * @param value - the base ShorthandProps
10
+ * @param options - options to resolve ShorthandProps
11
+ */
12
+ export declare function resolveShorthand<Props extends DefaultObjectShorthandProps, Required extends boolean = false>(value: ShorthandProps<Props>, options?: ResolveShorthandOptions<Props, Required>): Required extends false ? Props | undefined : Props;
@@ -0,0 +1,30 @@
1
+ import { __assign } from "tslib";
2
+ import { isValidElement } from 'react';
3
+ /**
4
+ * Resolves ShorthandProps into ObjectShorthandProps, to ensure normalization of the signature
5
+ * being passed down to getSlots method
6
+ * @param value - the base ShorthandProps
7
+ * @param options - options to resolve ShorthandProps
8
+ */
9
+
10
+ export function resolveShorthand(value, options) {
11
+ var _a = options || {},
12
+ _b = _a.required,
13
+ required = _b === void 0 ? false : _b,
14
+ defaultProps = _a.defaultProps;
15
+
16
+ if (value === null || value === undefined && !required) {
17
+ return undefined;
18
+ }
19
+
20
+ var resolvedShorthand = {};
21
+
22
+ if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value) || /*#__PURE__*/isValidElement(value)) {
23
+ resolvedShorthand.children = value;
24
+ } else if (typeof value === 'object') {
25
+ resolvedShorthand = value;
26
+ }
27
+
28
+ return defaultProps ? __assign(__assign({}, defaultProps), resolvedShorthand) : resolvedShorthand;
29
+ }
30
+ //# sourceMappingURL=resolveShorthand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/compose/resolveShorthand.ts"],"names":[],"mappings":";AAAA,SAAS,cAAT,QAA+B,OAA/B;AASA;;;;;AAKG;;AACH,OAAM,SAAU,gBAAV,CACJ,KADI,EAEJ,OAFI,EAE8C;AAE5C,MAAA,EAAA,GAAqC,OAAO,IAAI,EAAhD;AAAA,MAAE,EAAA,GAAA,EAAA,CAAA,QAAF;AAAA,MAAE,QAAQ,GAAA,EAAA,KAAA,KAAA,CAAA,GAAG,KAAH,GAAQ,EAAlB;AAAA,MAAoB,YAAY,GAAA,EAAA,CAAA,YAAhC;;AACN,MAAI,KAAK,KAAK,IAAV,IAAmB,KAAK,KAAK,SAAV,IAAuB,CAAC,QAA/C,EAA0D;AACxD,WAAO,SAAP;AACD;;AAED,MAAI,iBAAiB,GAAG,EAAxB;;AAEA,MAAI,OAAO,KAAP,KAAiB,QAAjB,IAA6B,OAAO,KAAP,KAAiB,QAA9C,IAA0D,KAAK,CAAC,OAAN,CAAc,KAAd,CAA1D,iBAAkF,cAAc,CAAC,KAAD,CAApG,EAA6G;AAC3G,IAAA,iBAAiB,CAAC,QAAlB,GAA6B,KAA7B;AACD,GAFD,MAEO,IAAI,OAAO,KAAP,KAAiB,QAArB,EAA+B;AACpC,IAAA,iBAAiB,GAAG,KAApB;AACD;;AAED,SAAQ,YAAY,GAAE,QAAA,CAAA,QAAA,CAAA,EAAA,EAAM,YAAN,CAAA,EAAuB,iBAAvB,CAAF,GAA+C,iBAAnE;AAGD","sourceRoot":""}
@@ -0,0 +1,104 @@
1
+ import * as React from 'react';
2
+ export declare type ShorthandRenderFunction<Props> = (Component: React.ElementType<Props>, props: Omit<Props, 'children' | 'as'>) => React.ReactNode;
3
+ export declare type ObjectShorthandPropsRecord = Record<string, DefaultObjectShorthandProps | undefined>;
4
+ export declare type ShorthandProps<Props extends DefaultObjectShorthandProps> = React.ReactChild | React.ReactNodeArray | React.ReactPortal | number | null | undefined | Props;
5
+ /**
6
+ * Matches any shorthand props type.
7
+ *
8
+ * This should ONLY be used in type templates as in `extends DefaultObjectShorthandProps`;
9
+ * it shouldn't be used as the type of a slot.
10
+ */
11
+ export declare type DefaultObjectShorthandProps = ObjectShorthandProps<{
12
+ children?: React.ReactNode;
13
+ as?: keyof JSX.IntrinsicElements;
14
+ }>;
15
+ /**
16
+ * Defines the slot props for a slot that supports a Component type.
17
+ *
18
+ * For intrinsic elements like 'div', use {@link IntrinsicShorthandProps} instead.
19
+ */
20
+ export declare type ObjectShorthandProps<Props extends {
21
+ children?: React.ReactNode;
22
+ } = {}> = Props & {
23
+ children?: Props['children'] | ShorthandRenderFunction<Props>;
24
+ };
25
+ /**
26
+ * Define the slot arguments for a slot that supports one or more intrinsic element types, such as 'div'.
27
+ * For slots that support custom components, use {@link ObjectShorthandProps} instead.
28
+ *
29
+ * The first param is the slot's default type if no `as` prop is specified.
30
+ * The second param is an optional union of alternative types that can be specified for the `as` prop.
31
+ *
32
+ * ```
33
+ * IntrinsicShorthandProps<'div'> // Slot is always div
34
+ * IntrinsicShorthandProps<'button', 'a'> // Defaults to button, but allows as="a" with anchor-specific props
35
+ * IntrinsicShorthandProps<'label', 'span' | 'div'>; // Defaults to label, but allows as="span" or as="div"
36
+ * ```
37
+ */
38
+ export declare type IntrinsicShorthandProps<DefaultAs extends keyof JSX.IntrinsicElements, AlternateAs extends keyof JSX.IntrinsicElements = never> = IsSingleton<DefaultAs> extends false ? 'Error: first parameter to IntrinsicShorthandProps must be a single element type, not a union of types' : ({
39
+ as?: DefaultAs;
40
+ } & ObjectShorthandProps<React.PropsWithRef<JSX.IntrinsicElements[DefaultAs]>>) | {
41
+ [As in AlternateAs]: {
42
+ as: As;
43
+ } & ObjectShorthandProps<React.PropsWithRef<JSX.IntrinsicElements[As]>>;
44
+ }[AlternateAs];
45
+ /**
46
+ * Evaluates to true if the given type contains exactly one string, or false if it is a union of strings.
47
+ *
48
+ * ```
49
+ * IsSingleton<'a'> // true
50
+ * IsSingleton<'a' | 'b' | 'c'> // false
51
+ * ```
52
+ */
53
+ export declare type IsSingleton<T extends string> = {
54
+ [K in T]: Exclude<T, K> extends never ? true : false;
55
+ }[T];
56
+ /**
57
+ * Helper type for inferring the type of the as prop from a Props type.
58
+ *
59
+ * For example:
60
+ * ```
61
+ * type Example<T> = T extends AsIntrinsicElement<infer As> ? As : never;
62
+ * ```
63
+ */
64
+ export declare type AsIntrinsicElement<As extends keyof JSX.IntrinsicElements> = {
65
+ as?: As;
66
+ };
67
+ /**
68
+ * Converts a union type (`A | B | C`) to an intersection type (`A & B & C`)
69
+ */
70
+ export declare type UnionToIntersection<U> = (U extends unknown ? (x: U) => U : never) extends (x: infer I) => U ? I : never;
71
+ /**
72
+ * Removes the 'ref' prop from the given Props type, leaving unions intact (such as the discriminated union created by
73
+ * IntrinsicShorthandProps). This allows IntrinsicShorthandProps to be used with React.forwardRef.
74
+ *
75
+ * The conditional "extends unknown" (always true) exploits a quirk in the way TypeScript handles conditional
76
+ * types, to prevent unions from being expanded.
77
+ */
78
+ export declare type PropsWithoutRef<P> = 'ref' extends keyof P ? (P extends unknown ? Omit<P, 'ref'> : P) : P;
79
+ export declare type ComponentProps<Shorthands extends ObjectShorthandPropsRecord, Primary extends keyof Shorthands = 'root'> = Omit<{
80
+ [Key in keyof Shorthands]?: ShorthandProps<NonNullable<Shorthands[Key]>>;
81
+ }, Primary> & PropsWithoutRef<Shorthands[Primary]>;
82
+ export declare type ComponentState<Shorthands extends ObjectShorthandPropsRecord> = {
83
+ components?: {
84
+ [Key in keyof Shorthands]-?: React.ComponentType<NonNullable<Shorthands[Key]> extends ObjectShorthandProps<infer P> ? P : NonNullable<Shorthands[Key]>> | (NonNullable<Shorthands[Key]> extends AsIntrinsicElement<infer As> ? As : keyof JSX.IntrinsicElements);
85
+ };
86
+ } & Shorthands;
87
+ /**
88
+ * This is part of a hack to infer the element type from a native element *props* type.
89
+ * The only place the original element is found in a native props type (at least that's workable
90
+ * for inference) is in the event handlers, so some of the helper types use this event handler
91
+ * name to infer the original element type.
92
+ *
93
+ * Notes:
94
+ * - Using an extremely obscure event handler reduces the likelihood that its signature will be
95
+ * modified in any component's props.
96
+ * - Inferring based on a single prop name instead of a larger type like `DOMAttributes<T>` should be
97
+ * less expensive for typescript to evaluate and is less likely to result in type expansion in .d.ts.
98
+ */
99
+ declare type ObscureEventName = 'onLostPointerCaptureCapture';
100
+ /**
101
+ * Return type for `React.forwardRef`, including inference of the proper typing for the ref.
102
+ */
103
+ export declare type ForwardRefComponent<Props> = ObscureEventName extends keyof Props ? Required<Props>[ObscureEventName] extends React.PointerEventHandler<infer Element> ? React.ForwardRefExoticComponent<Props & React.RefAttributes<Element>> : never : never;
104
+ export {};
@@ -0,0 +1,6 @@
1
+ export {}; // A definition like this would also work, but typescript is more likely to unnecessarily expand
2
+ // the props type with this version (and it's likely much more expensive to evaluate)
3
+ // export type ForwardRefComponent<Props> = Props extends React.DOMAttributes<infer Element>
4
+ // ? React.ForwardRefExoticComponent<Props> & React.RefAttributes<Element>
5
+ // : never;
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/compose/types.ts"],"names":[],"mappings":"WA2IA;AACA;AACA;AACA;AACA","sourceRoot":""}
@@ -0,0 +1,16 @@
1
+ export * from './useControllableState';
2
+ export * from './useBoolean';
3
+ export * from './useConst';
4
+ export * from './useControllableValue';
5
+ export * from './useEventCallback';
6
+ export * from './useFirstMount';
7
+ export * from './useId';
8
+ export * from './useIsomorphicLayoutEffect';
9
+ export * from './useMergedRefs';
10
+ export * from './useMount';
11
+ export * from './useOnClickOutside';
12
+ export * from './useOnScrollOutside';
13
+ export * from './usePrevious';
14
+ export * from './useTimeout';
15
+ export * from './useUnmount';
16
+ export * from './useForceUpdate';
@@ -0,0 +1,17 @@
1
+ export * from './useControllableState';
2
+ export * from './useBoolean';
3
+ export * from './useConst';
4
+ export * from './useControllableValue';
5
+ export * from './useEventCallback';
6
+ export * from './useFirstMount';
7
+ export * from './useId';
8
+ export * from './useIsomorphicLayoutEffect';
9
+ export * from './useMergedRefs';
10
+ export * from './useMount';
11
+ export * from './useOnClickOutside';
12
+ export * from './useOnScrollOutside';
13
+ export * from './usePrevious';
14
+ export * from './useTimeout';
15
+ export * from './useUnmount';
16
+ export * from './useForceUpdate';
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,17 @@
1
+ /** Updater callbacks returned by `useBoolean`. */
2
+ export declare type UseBooleanCallbacks = {
3
+ /** Set the value to true. Always has the same identity. */
4
+ setTrue: () => void;
5
+ /** Set the value to false. Always has the same identity. */
6
+ setFalse: () => void;
7
+ /** Toggle the value. Always has the same identity. */
8
+ toggle: () => void;
9
+ };
10
+ /**
11
+ * Hook to store a value and generate callbacks for setting the value to true or false.
12
+ * The identity of the callbacks will always stay the same.
13
+ *
14
+ * @param initialState - Initial value
15
+ * @returns Array with the current value and an object containing the updater callbacks.
16
+ */
17
+ export declare function useBoolean(initialState: boolean): [boolean, UseBooleanCallbacks];
@@ -0,0 +1,39 @@
1
+ import * as React from 'react';
2
+ import { useConst } from './useConst';
3
+ /**
4
+ * Hook to store a value and generate callbacks for setting the value to true or false.
5
+ * The identity of the callbacks will always stay the same.
6
+ *
7
+ * @param initialState - Initial value
8
+ * @returns Array with the current value and an object containing the updater callbacks.
9
+ */
10
+
11
+ export function useBoolean(initialState) {
12
+ var _a = React.useState(initialState),
13
+ value = _a[0],
14
+ setValue = _a[1];
15
+
16
+ var setTrue = useConst(function () {
17
+ return function () {
18
+ setValue(true);
19
+ };
20
+ });
21
+ var setFalse = useConst(function () {
22
+ return function () {
23
+ setValue(false);
24
+ };
25
+ });
26
+ var toggle = useConst(function () {
27
+ return function () {
28
+ setValue(function (currentValue) {
29
+ return !currentValue;
30
+ });
31
+ };
32
+ });
33
+ return [value, {
34
+ setTrue: setTrue,
35
+ setFalse: setFalse,
36
+ toggle: toggle
37
+ }];
38
+ }
39
+ //# sourceMappingURL=useBoolean.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useBoolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,QAAT,QAAyB,YAAzB;AAYA;;;;;;AAMG;;AACH,OAAM,SAAU,UAAV,CAAqB,YAArB,EAA0C;AACxC,MAAA,EAAA,GAAoB,KAAK,CAAC,QAAN,CAAe,YAAf,CAApB;AAAA,MAAC,KAAK,GAAA,EAAA,CAAA,CAAA,CAAN;AAAA,MAAQ,QAAQ,GAAA,EAAA,CAAA,CAAA,CAAhB;;AAEN,MAAM,OAAO,GAAG,QAAQ,CAAC,YAAA;AAAM,WAAA,YAAA;AAC7B,MAAA,QAAQ,CAAC,IAAD,CAAR;AAD6B,KAAA;AAE9B,GAFuB,CAAxB;AAGA,MAAM,QAAQ,GAAG,QAAQ,CAAC,YAAA;AAAM,WAAA,YAAA;AAC9B,MAAA,QAAQ,CAAC,KAAD,CAAR;AAD8B,KAAA;AAE/B,GAFwB,CAAzB;AAGA,MAAM,MAAM,GAAG,QAAQ,CAAC,YAAA;AAAM,WAAA,YAAA;AAC5B,MAAA,QAAQ,CAAC,UAAA,YAAA,EAAY;AAAI,eAAA,CAAA,YAAA;AAAa,OAA9B,CAAR;AAD4B,KAAA;AAE7B,GAFsB,CAAvB;AAIA,SAAO,CAAC,KAAD,EAAQ;AAAE,IAAA,OAAO,EAAA,OAAT;AAAW,IAAA,QAAQ,EAAA,QAAnB;AAAqB,IAAA,MAAM,EAAA;AAA3B,GAAR,CAAP;AACD","sourceRoot":""}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Hook to initialize and return a constant value. Unlike `React.useMemo`, this is guaranteed to
3
+ * always return the same value (and if the initializer is a function, only call it once).
4
+ * This is similar to setting a private member in a class constructor.
5
+ *
6
+ * If the value should ever change based on dependencies, use `React.useMemo` instead.
7
+ *
8
+ * @param initialValue - Initial value, or function to get the initial value. Similar to `useState`,
9
+ * only the value/function passed in the first time this is called is respected.
10
+ * @returns The value. The identity of this value will always be the same.
11
+ */
12
+ export declare function useConst<T>(initialValue: T | (() => T)): T;
@@ -0,0 +1,30 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Hook to initialize and return a constant value. Unlike `React.useMemo`, this is guaranteed to
4
+ * always return the same value (and if the initializer is a function, only call it once).
5
+ * This is similar to setting a private member in a class constructor.
6
+ *
7
+ * If the value should ever change based on dependencies, use `React.useMemo` instead.
8
+ *
9
+ * @param initialValue - Initial value, or function to get the initial value. Similar to `useState`,
10
+ * only the value/function passed in the first time this is called is respected.
11
+ * @returns The value. The identity of this value will always be the same.
12
+ */
13
+
14
+ export function useConst(initialValue) {
15
+ // Use useRef to store the value because it's the least expensive built-in hook that works here
16
+ // (we could also use `const [value] = React.useState(initialValue)` but that's more expensive
17
+ // internally due to reducer handling which we don't need)
18
+ var ref = React.useRef();
19
+
20
+ if (ref.current === undefined) {
21
+ // Box the value in an object so we can tell if it's initialized even if the initializer
22
+ // returns/is undefined
23
+ ref.current = {
24
+ value: typeof initialValue === 'function' ? initialValue() : initialValue
25
+ };
26
+ }
27
+
28
+ return ref.current.value;
29
+ }
30
+ //# sourceMappingURL=useConst.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useConst.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAEA;;;;;;;;;;AAUG;;AACH,OAAM,SAAU,QAAV,CAAsB,YAAtB,EAAiD;AACrD;AACA;AACA;AACA,MAAM,GAAG,GAAG,KAAK,CAAC,MAAN,EAAZ;;AACA,MAAI,GAAG,CAAC,OAAJ,KAAgB,SAApB,EAA+B;AAC7B;AACA;AACA,IAAA,GAAG,CAAC,OAAJ,GAAc;AACZ,MAAA,KAAK,EAAE,OAAO,YAAP,KAAwB,UAAxB,GAAsC,YAAyB,EAA/D,GAAoE;AAD/D,KAAd;AAGD;;AACD,SAAO,GAAG,CAAC,OAAJ,CAAY,KAAnB;AACD","sourceRoot":""}
@@ -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,72 @@
1
+ import * as React from 'react';
2
+ import { useConst } from './useConst';
3
+
4
+ function isUndefined(state) {
5
+ return typeof state === 'undefined';
6
+ }
7
+
8
+ function isFactoryDispatch(newState) {
9
+ return typeof newState === 'function';
10
+ }
11
+ /**
12
+ * A useState 'like' hook that allows optional user control
13
+ * Useful for components which allow uncontrolled and controlled behaviour for users
14
+ * @returns - https://reactjs.org/docs/hooks-state.html
15
+ */
16
+
17
+
18
+ export var useControllableState = function (options) {
19
+ var isControlled = useIsControlled(options.state);
20
+ var initialState = isUndefined(options.defaultState) ? options.initialState : options.defaultState;
21
+
22
+ var _a = React.useState(initialState),
23
+ internalState = _a[0],
24
+ setInternalState = _a[1];
25
+
26
+ var state = isControlled ? options.state : internalState;
27
+ var stateRef = React.useRef(state);
28
+ React.useEffect(function () {
29
+ stateRef.current = state;
30
+ }, [state]); // To match the behavior of the setter returned by React.useState, this callback's identity
31
+ // should never change. This means it MUST NOT directly reference variables that can change.
32
+
33
+ var setState = React.useCallback(function (newState) {
34
+ // React dispatch can use a factory
35
+ // https://reactjs.org/docs/hooks-reference.html#functional-updates
36
+ if (isFactoryDispatch(newState)) {
37
+ stateRef.current = newState(stateRef.current);
38
+ } else {
39
+ stateRef.current = newState;
40
+ }
41
+
42
+ setInternalState(stateRef.current);
43
+ }, []);
44
+ return [state, setState];
45
+ };
46
+ /**
47
+ * Helper hook to handle previous comparison of controlled/uncontrolled
48
+ * Prints an error when isControlled value switches between subsequent renders
49
+ * @returns - whether the value is controlled
50
+ */
51
+
52
+ var useIsControlled = function (controlledValue) {
53
+ var isControlled = useConst(controlledValue !== undefined);
54
+
55
+ if (process.env.NODE_ENV !== 'production') {
56
+ // We don't want these warnings in production even though it is against native behaviour
57
+ // eslint-disable-next-line react-hooks/rules-of-hooks
58
+ React.useEffect(function () {
59
+ if (isControlled !== (controlledValue !== undefined)) {
60
+ var error = new Error();
61
+ var controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
62
+ var undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
63
+
64
+ console.error([// Default react error
65
+ '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(' '));
66
+ }
67
+ }, [isControlled, controlledValue]);
68
+ }
69
+
70
+ return isControlled;
71
+ };
72
+ //# sourceMappingURL=useControllableState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,QAAT,QAAyB,YAAzB;;AAiBA,SAAS,WAAT,CAAqB,KAArB,EAAmC;AACjC,SAAO,OAAO,KAAP,KAAiB,WAAxB;AACD;;AAED,SAAS,iBAAT,CAAkC,QAAlC,EAAuE;AACrE,SAAO,OAAO,QAAP,KAAoB,UAA3B;AACD;AAED;;;;AAIG;;;AACH,OAAO,IAAM,oBAAoB,GAAG,UAClC,OADkC,EACS;AAE3C,MAAM,YAAY,GAAG,eAAe,CAAC,OAAO,CAAC,KAAT,CAApC;AACA,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,CAAC,YAAT,CAAX,GAAoC,OAAO,CAAC,YAA5C,GAA2D,OAAO,CAAC,YAAxF;;AACM,MAAA,EAAA,GAAoC,KAAK,CAAC,QAAN,CAAsB,YAAtB,CAApC;AAAA,MAAC,aAAa,GAAA,EAAA,CAAA,CAAA,CAAd;AAAA,MAAgB,gBAAgB,GAAA,EAAA,CAAA,CAAA,CAAhC;;AAEN,MAAM,KAAK,GAAG,YAAY,GAAI,OAAO,CAAC,KAAZ,GAA8B,aAAxD;AAEA,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAN,CAAa,KAAb,CAAjB;AACA,EAAA,KAAK,CAAC,SAAN,CAAgB,YAAA;AACd,IAAA,QAAQ,CAAC,OAAT,GAAmB,KAAnB;AACD,GAFD,EAEG,CAAC,KAAD,CAFH,EAT2C,CAa3C;AACA;;AACA,MAAM,QAAQ,GAAG,KAAK,CAAC,WAAN,CAAkB,UAAC,QAAD,EAAsC;AACvE;AACA;AACA,QAAI,iBAAiB,CAAC,QAAD,CAArB,EAAiC;AAC/B,MAAA,QAAQ,CAAC,OAAT,GAAmB,QAAQ,CAAC,QAAQ,CAAC,OAAV,CAA3B;AACD,KAFD,MAEO;AACL,MAAA,QAAQ,CAAC,OAAT,GAAmB,QAAnB;AACD;;AAED,IAAA,gBAAgB,CAAC,QAAQ,CAAC,OAAV,CAAhB;AACD,GAVgB,EAUd,EAVc,CAAjB;AAYA,SAAO,CAAC,KAAD,EAAQ,QAAR,CAAP;AACD,CA7BM;AA+BP;;;;AAIG;;AACH,IAAM,eAAe,GAAG,UAAC,eAAD,EAAyB;AAC/C,MAAM,YAAY,GAAG,QAAQ,CAAU,eAAe,KAAK,SAA9B,CAA7B;;AAEA,MAAI,OAAO,CAAC,GAAR,CAAY,QAAZ,KAAyB,YAA7B,EAA2C;AACzC;AACA;AACA,IAAA,KAAK,CAAC,SAAN,CAAgB,YAAA;AACd,UAAI,YAAY,MAAM,eAAe,KAAK,SAA1B,CAAhB,EAAsD;AACpD,YAAM,KAAK,GAAG,IAAI,KAAJ,EAAd;AAEA,YAAM,cAAc,GAAG,YAAY,GAC/B,uCAD+B,GAE/B,wCAFJ;AAIA,YAAM,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":""}
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ export declare type ChangeCallback<TElement extends HTMLElement, TValue, TEvent extends React.SyntheticEvent<TElement> | undefined> = (ev: TEvent, newValue: TValue | undefined) => void;
3
+ /**
4
+ * Default value can be a value or an initializer
5
+ */
6
+ declare type DefaultValue<TValue> = TValue | (() => TValue);
7
+ /**
8
+ * Hook to manage a value that could be either controlled or uncontrolled, such as a checked state or
9
+ * text box string.
10
+ * @param controlledValue - The controlled value passed in the props. This value will always be used if provided,
11
+ * and the internal state will be updated to reflect it.
12
+ * @param defaultUncontrolledValue - Initial value for the internal state in the uncontrolled case.
13
+ * @returns An array of the current value and an updater callback. Like `React.useState`, the updater
14
+ * callback always has the same identity, and it can take either a new value, or a function which
15
+ * is passed the previous value and returns the new value.
16
+ * @see https://reactjs.org/docs/uncontrolled-components.html
17
+ */
18
+ export declare function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>): Readonly<[TValue, (update: React.SetStateAction<TValue>) => void]>;
19
+ export declare function useControllableValue<TValue, TElement extends HTMLElement, TEvent extends React.SyntheticEvent<TElement> | undefined>(controlledValue: TValue, defaultUncontrolledValue: DefaultValue<TValue>, onChange: ChangeCallback<TElement, TValue, TEvent>): Readonly<[TValue, (update: React.SetStateAction<TValue>, ev?: React.FormEvent<TElement>) => void]>;
20
+ export {};
@@ -0,0 +1,62 @@
1
+ import * as React from 'react';
2
+ import { useConst } from './useConst';
3
+ export function useControllableValue(controlledValue, defaultUncontrolledValue, onChange) {
4
+ var _a = React.useState(defaultUncontrolledValue),
5
+ value = _a[0],
6
+ setValue = _a[1];
7
+
8
+ var isControlled = useIsControlled(controlledValue);
9
+ var currentValue = isControlled ? controlledValue : value; // Duplicate the current value and onChange in refs so they're accessible from
10
+ // setValueOrCallOnChange without creating a new callback every time
11
+
12
+ var valueRef = React.useRef(currentValue);
13
+ var onChangeRef = React.useRef(onChange);
14
+ React.useEffect(function () {
15
+ valueRef.current = currentValue;
16
+ onChangeRef.current = onChange;
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
+
20
+ var setValueOrCallOnChange = useConst(function () {
21
+ return function (update, ev) {
22
+ // Assuming here that TValue is not a function, because a controllable value will typically
23
+ // be something a user can enter as input
24
+ var newValue = typeof update === 'function' ? update(valueRef.current) : update;
25
+
26
+ if (onChangeRef.current) {
27
+ onChangeRef.current(ev, newValue);
28
+ }
29
+
30
+ if (!isControlled) {
31
+ setValue(newValue);
32
+ }
33
+ };
34
+ });
35
+ return [currentValue, setValueOrCallOnChange];
36
+ }
37
+ /**
38
+ * Helper hook to handle previous comparison of controlled/uncontrolled
39
+ * Prints an error when isControlled value switches between subsequent renders
40
+ */
41
+
42
+ var useIsControlled = function (controlledValue) {
43
+ var isControlled = useConst(controlledValue !== undefined);
44
+
45
+ if (process.env.NODE_ENV !== 'production') {
46
+ // We don't want these warnings in production even though it is against native behaviour
47
+ // eslint-disable-next-line react-hooks/rules-of-hooks
48
+ React.useEffect(function () {
49
+ if (isControlled !== (controlledValue !== undefined)) {
50
+ var error = new Error();
51
+ var controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
52
+ var undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined'; // eslint-disable-next-line no-console
53
+
54
+ console.error([// Default react error
55
+ '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(' '));
56
+ }
57
+ }, [isControlled, controlledValue]);
58
+ }
59
+
60
+ return isControlled;
61
+ };
62
+ //# sourceMappingURL=useControllableValue.js.map