@tale-ui/utils 0.0.3

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 (165) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +21 -0
  3. package/README.md +3 -0
  4. package/detectBrowser.d.ts +8 -0
  5. package/detectBrowser.js +64 -0
  6. package/empty.d.ts +3 -0
  7. package/empty.js +10 -0
  8. package/error.d.ts +2 -0
  9. package/error.js +23 -0
  10. package/esm/detectBrowser.d.ts +8 -0
  11. package/esm/detectBrowser.js +58 -0
  12. package/esm/empty.d.ts +3 -0
  13. package/esm/empty.js +3 -0
  14. package/esm/error.d.ts +2 -0
  15. package/esm/error.js +16 -0
  16. package/esm/fastHooks.d.ts +14 -0
  17. package/esm/fastHooks.js +43 -0
  18. package/esm/fastObjectShallowCompare.d.ts +1 -0
  19. package/esm/fastObjectShallowCompare.js +29 -0
  20. package/esm/formatErrorMessage.d.ts +18 -0
  21. package/esm/formatErrorMessage.js +26 -0
  22. package/esm/generateId.d.ts +1 -0
  23. package/esm/generateId.js +5 -0
  24. package/esm/getReactElementRef.d.ts +5 -0
  25. package/esm/getReactElementRef.js +14 -0
  26. package/esm/inertValue.d.ts +1 -0
  27. package/esm/inertValue.js +8 -0
  28. package/esm/isElementDisabled.d.ts +1 -0
  29. package/esm/isElementDisabled.js +3 -0
  30. package/esm/isMouseWithinBounds.d.ts +1 -0
  31. package/esm/isMouseWithinBounds.js +10 -0
  32. package/esm/mergeObjects.d.ts +1 -0
  33. package/esm/mergeObjects.js +15 -0
  34. package/esm/owner.d.ts +2 -0
  35. package/esm/owner.js +4 -0
  36. package/esm/package.json +1 -0
  37. package/esm/reactVersion.d.ts +3 -0
  38. package/esm/reactVersion.js +5 -0
  39. package/esm/safeReact.d.ts +2 -0
  40. package/esm/safeReact.js +6 -0
  41. package/esm/store/ReactStore.d.ts +91 -0
  42. package/esm/store/ReactStore.js +199 -0
  43. package/esm/store/Store.d.ts +58 -0
  44. package/esm/store/Store.js +111 -0
  45. package/esm/store/StoreInspector.d.ts +41 -0
  46. package/esm/store/StoreInspector.js +537 -0
  47. package/esm/store/createSelector.d.ts +30 -0
  48. package/esm/store/createSelector.js +148 -0
  49. package/esm/store/index.d.ts +5 -0
  50. package/esm/store/index.js +5 -0
  51. package/esm/store/useStore.d.ts +22 -0
  52. package/esm/store/useStore.js +107 -0
  53. package/esm/testUtils.d.ts +17 -0
  54. package/esm/testUtils.js +19 -0
  55. package/esm/useAnimationFrame.d.ts +18 -0
  56. package/esm/useAnimationFrame.js +106 -0
  57. package/esm/useControlled.d.ts +24 -0
  58. package/esm/useControlled.js +40 -0
  59. package/esm/useEnhancedClickHandler.d.ts +13 -0
  60. package/esm/useEnhancedClickHandler.js +38 -0
  61. package/esm/useForcedRerendering.d.ts +4 -0
  62. package/esm/useForcedRerendering.js +13 -0
  63. package/esm/useId.d.ts +7 -0
  64. package/esm/useId.js +41 -0
  65. package/esm/useInterval.d.ts +13 -0
  66. package/esm/useInterval.js +36 -0
  67. package/esm/useIsoLayoutEffect.d.ts +2 -0
  68. package/esm/useIsoLayoutEffect.js +5 -0
  69. package/esm/useMergedRefs.d.ts +21 -0
  70. package/esm/useMergedRefs.js +108 -0
  71. package/esm/useOnFirstRender.d.ts +1 -0
  72. package/esm/useOnFirstRender.js +10 -0
  73. package/esm/useOnMount.d.ts +5 -0
  74. package/esm/useOnMount.js +14 -0
  75. package/esm/usePreviousValue.d.ts +6 -0
  76. package/esm/usePreviousValue.js +22 -0
  77. package/esm/useRefWithInit.d.ts +10 -0
  78. package/esm/useRefWithInit.js +20 -0
  79. package/esm/useScrollLock.d.ts +7 -0
  80. package/esm/useScrollLock.js +244 -0
  81. package/esm/useStableCallback.d.ts +13 -0
  82. package/esm/useStableCallback.js +44 -0
  83. package/esm/useTimeout.d.ts +17 -0
  84. package/esm/useTimeout.js +43 -0
  85. package/esm/useValueAsRef.d.ts +10 -0
  86. package/esm/useValueAsRef.js +28 -0
  87. package/esm/visuallyHidden.d.ts +3 -0
  88. package/esm/visuallyHidden.js +20 -0
  89. package/esm/warn.d.ts +1 -0
  90. package/esm/warn.js +13 -0
  91. package/fastHooks.d.ts +14 -0
  92. package/fastHooks.js +54 -0
  93. package/fastObjectShallowCompare.d.ts +1 -0
  94. package/fastObjectShallowCompare.js +35 -0
  95. package/formatErrorMessage.d.ts +18 -0
  96. package/formatErrorMessage.js +33 -0
  97. package/generateId.d.ts +1 -0
  98. package/generateId.js +11 -0
  99. package/getReactElementRef.d.ts +5 -0
  100. package/getReactElementRef.js +20 -0
  101. package/inertValue.d.ts +1 -0
  102. package/inertValue.js +14 -0
  103. package/isElementDisabled.d.ts +1 -0
  104. package/isElementDisabled.js +9 -0
  105. package/isMouseWithinBounds.d.ts +1 -0
  106. package/isMouseWithinBounds.js +16 -0
  107. package/mergeObjects.d.ts +1 -0
  108. package/mergeObjects.js +21 -0
  109. package/owner.d.ts +2 -0
  110. package/owner.js +16 -0
  111. package/package.json +64 -0
  112. package/reactVersion.d.ts +3 -0
  113. package/reactVersion.js +12 -0
  114. package/safeReact.d.ts +2 -0
  115. package/safeReact.js +12 -0
  116. package/store/ReactStore.d.ts +91 -0
  117. package/store/ReactStore.js +205 -0
  118. package/store/Store.d.ts +58 -0
  119. package/store/Store.js +118 -0
  120. package/store/StoreInspector.d.ts +41 -0
  121. package/store/StoreInspector.js +544 -0
  122. package/store/createSelector.d.ts +30 -0
  123. package/store/createSelector.js +154 -0
  124. package/store/index.d.ts +5 -0
  125. package/store/index.js +60 -0
  126. package/store/useStore.d.ts +22 -0
  127. package/store/useStore.js +115 -0
  128. package/testUtils.d.ts +17 -0
  129. package/testUtils.js +26 -0
  130. package/useAnimationFrame.d.ts +18 -0
  131. package/useAnimationFrame.js +113 -0
  132. package/useControlled.d.ts +24 -0
  133. package/useControlled.js +46 -0
  134. package/useEnhancedClickHandler.d.ts +13 -0
  135. package/useEnhancedClickHandler.js +44 -0
  136. package/useForcedRerendering.d.ts +4 -0
  137. package/useForcedRerendering.js +18 -0
  138. package/useId.d.ts +7 -0
  139. package/useId.js +47 -0
  140. package/useInterval.d.ts +13 -0
  141. package/useInterval.js +43 -0
  142. package/useIsoLayoutEffect.d.ts +2 -0
  143. package/useIsoLayoutEffect.js +11 -0
  144. package/useMergedRefs.d.ts +21 -0
  145. package/useMergedRefs.js +114 -0
  146. package/useOnFirstRender.d.ts +1 -0
  147. package/useOnFirstRender.js +16 -0
  148. package/useOnMount.d.ts +5 -0
  149. package/useOnMount.js +20 -0
  150. package/usePreviousValue.d.ts +6 -0
  151. package/usePreviousValue.js +27 -0
  152. package/useRefWithInit.d.ts +10 -0
  153. package/useRefWithInit.js +26 -0
  154. package/useScrollLock.d.ts +7 -0
  155. package/useScrollLock.js +249 -0
  156. package/useStableCallback.d.ts +13 -0
  157. package/useStableCallback.js +49 -0
  158. package/useTimeout.d.ts +17 -0
  159. package/useTimeout.js +50 -0
  160. package/useValueAsRef.d.ts +10 -0
  161. package/useValueAsRef.js +32 -0
  162. package/visuallyHidden.d.ts +3 -0
  163. package/visuallyHidden.js +26 -0
  164. package/warn.d.ts +1 -0
  165. package/warn.js +19 -0
package/fastHooks.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.fastComponent = fastComponent;
8
+ exports.fastComponentRef = fastComponentRef;
9
+ exports.getInstance = getInstance;
10
+ exports.register = register;
11
+ exports.setInstance = setInstance;
12
+ var React = _interopRequireWildcard(require("react"));
13
+ var _useRefWithInit = require("./useRefWithInit");
14
+ const hooks = [];
15
+ let currentInstance = undefined;
16
+ function getInstance() {
17
+ return currentInstance;
18
+ }
19
+ function setInstance(instance) {
20
+ currentInstance = instance;
21
+ }
22
+ function register(hook) {
23
+ hooks.push(hook);
24
+ }
25
+ function fastComponent(fn) {
26
+ const FastComponent = (props, forwardedRef) => {
27
+ const instance = (0, _useRefWithInit.useRefWithInit)(createInstance).current;
28
+ let result;
29
+ try {
30
+ currentInstance = instance;
31
+ for (const hook of hooks) {
32
+ hook.before(instance);
33
+ }
34
+ result = fn(props, forwardedRef);
35
+ for (const hook of hooks) {
36
+ hook.after(instance);
37
+ }
38
+ instance.didInitialize = true;
39
+ } finally {
40
+ currentInstance = undefined;
41
+ }
42
+ return result;
43
+ };
44
+ FastComponent.displayName = fn.displayName || fn.name;
45
+ return FastComponent;
46
+ }
47
+ function fastComponentRef(fn) {
48
+ return /*#__PURE__*/React.forwardRef(fastComponent(fn));
49
+ }
50
+ function createInstance() {
51
+ return {
52
+ didInitialize: false
53
+ };
54
+ }
@@ -0,0 +1 @@
1
+ export declare function fastObjectShallowCompare<T extends Record<string, any> | null>(a: T, b: T): boolean;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.fastObjectShallowCompare = fastObjectShallowCompare;
7
+ // https://github.com/mui/mui-x/blob/master/packages/x-internals/src/fastObjectShallowCompare/fastObjectShallowCompare.ts
8
+ const is = Object.is;
9
+ function fastObjectShallowCompare(a, b) {
10
+ if (a === b) {
11
+ return true;
12
+ }
13
+ if (!(a instanceof Object) || !(b instanceof Object)) {
14
+ return false;
15
+ }
16
+ let aLength = 0;
17
+ let bLength = 0;
18
+
19
+ /* eslint-disable guard-for-in */
20
+ for (const key in a) {
21
+ aLength += 1;
22
+ if (!is(a[key], b[key])) {
23
+ return false;
24
+ }
25
+ if (!(key in b)) {
26
+ return false;
27
+ }
28
+ }
29
+
30
+ /* eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars */
31
+ for (const _ in b) {
32
+ bLength += 1;
33
+ }
34
+ return aLength === bLength;
35
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Creates a formatErrorMessage function with a custom URL and prefix.
3
+ * @param baseUrl - The base URL for the error page (e.g., 'https://tale-ui.com/production-error')
4
+ * @param prefix - The prefix for the error message (e.g., 'Tale UI')
5
+ * @returns A function that formats error messages with the given URL and prefix
6
+ */
7
+ export declare function createFormatErrorMessage(baseUrl: string, prefix: string): (code: number, ...args: string[]) => string;
8
+ /**
9
+ * WARNING: Don't import this directly. It's imported by the code generated by
10
+ * `@mui/interal-babel-plugin-minify-errors`. Make sure to always use string literals in `Error`
11
+ * constructors to ensure the plugin works as expected. Supported patterns include:
12
+ * throw new Error('My message');
13
+ * throw new Error(`My message: ${foo}`);
14
+ * throw new Error(`My message: ${foo}` + 'another string');
15
+ * ...
16
+ */
17
+ declare const formatErrorMessage: (code: number, ...args: string[]) => string;
18
+ export default formatErrorMessage;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createFormatErrorMessage = createFormatErrorMessage;
7
+ exports.default = void 0;
8
+ /**
9
+ * Creates a formatErrorMessage function with a custom URL and prefix.
10
+ * @param baseUrl - The base URL for the error page (e.g., 'https://tale-ui.com/production-error')
11
+ * @param prefix - The prefix for the error message (e.g., 'Tale UI')
12
+ * @returns A function that formats error messages with the given URL and prefix
13
+ */
14
+ function createFormatErrorMessage(baseUrl, prefix) {
15
+ return function formatErrorMessage(code, ...args) {
16
+ const url = new URL(baseUrl);
17
+ url.searchParams.set('code', code.toString());
18
+ args.forEach(arg => url.searchParams.append('args[]', arg));
19
+ return `${prefix} error #${code}; visit ${url} for the full message.`;
20
+ };
21
+ }
22
+
23
+ /**
24
+ * WARNING: Don't import this directly. It's imported by the code generated by
25
+ * `@mui/interal-babel-plugin-minify-errors`. Make sure to always use string literals in `Error`
26
+ * constructors to ensure the plugin works as expected. Supported patterns include:
27
+ * throw new Error('My message');
28
+ * throw new Error(`My message: ${foo}`);
29
+ * throw new Error(`My message: ${foo}` + 'another string');
30
+ * ...
31
+ */
32
+ const formatErrorMessage = createFormatErrorMessage('https://base-ui.com/production-error', 'Tale UI');
33
+ var _default = exports.default = formatErrorMessage;
@@ -0,0 +1 @@
1
+ export declare function generateId(prefix: string): string;
package/generateId.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.generateId = generateId;
7
+ let counter = 0;
8
+ function generateId(prefix) {
9
+ counter += 1;
10
+ return `${prefix}-${Math.random().toString(36).slice(2, 6)}-${counter}`;
11
+ }
@@ -0,0 +1,5 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Extracts the `ref` from a React element, handling different React versions.
4
+ */
5
+ export declare function getReactElementRef(element: unknown): React.Ref<unknown> | null;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.getReactElementRef = getReactElementRef;
8
+ var React = _interopRequireWildcard(require("react"));
9
+ var _reactVersion = require("./reactVersion");
10
+ /**
11
+ * Extracts the `ref` from a React element, handling different React versions.
12
+ */
13
+ function getReactElementRef(element) {
14
+ if (! /*#__PURE__*/React.isValidElement(element)) {
15
+ return null;
16
+ }
17
+ const reactElement = element;
18
+ const propsWithRef = reactElement.props;
19
+ return ((0, _reactVersion.isReactVersionAtLeast)(19) ? propsWithRef?.ref : reactElement.ref) ?? null;
20
+ }
@@ -0,0 +1 @@
1
+ export declare function inertValue(value?: boolean): boolean | undefined;
package/inertValue.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.inertValue = inertValue;
7
+ var _reactVersion = require("./reactVersion");
8
+ function inertValue(value) {
9
+ if ((0, _reactVersion.isReactVersionAtLeast)(19)) {
10
+ return value;
11
+ }
12
+ // compatibility with React < 19
13
+ return value ? 'true' : undefined;
14
+ }
@@ -0,0 +1 @@
1
+ export declare function isElementDisabled(element: HTMLElement | null): boolean;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isElementDisabled = isElementDisabled;
7
+ function isElementDisabled(element) {
8
+ return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true';
9
+ }
@@ -0,0 +1 @@
1
+ export declare function isMouseWithinBounds(event: React.MouseEvent): boolean;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isMouseWithinBounds = isMouseWithinBounds;
7
+ function isMouseWithinBounds(event) {
8
+ const targetRect = event.currentTarget.getBoundingClientRect();
9
+
10
+ // Safari randomly fires `mouseleave` incorrectly when the item is
11
+ // aligned to the trigger. This is a workaround to prevent the highlight
12
+ // from being removed while the cursor is still within the bounds of the item.
13
+ // https://github.com/mui/base-ui/issues/869
14
+ const isWithinBounds = targetRect.top + 1 <= event.clientY && event.clientY <= targetRect.bottom - 1 && targetRect.left + 1 <= event.clientX && event.clientX <= targetRect.right - 1;
15
+ return isWithinBounds;
16
+ }
@@ -0,0 +1 @@
1
+ export declare function mergeObjects<A extends object | undefined, B extends object | undefined>(a: A, b: B): NonNullable<A> | NonNullable<B> | (A & B) | undefined;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.mergeObjects = mergeObjects;
7
+ function mergeObjects(a, b) {
8
+ if (a && !b) {
9
+ return a;
10
+ }
11
+ if (!a && b) {
12
+ return b;
13
+ }
14
+ if (a || b) {
15
+ return {
16
+ ...a,
17
+ ...b
18
+ };
19
+ }
20
+ return undefined;
21
+ }
package/owner.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { getWindow as ownerWindow } from '@floating-ui/utils/dom';
2
+ export declare function ownerDocument(node: Element | null): Document;
package/owner.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ownerDocument = ownerDocument;
7
+ Object.defineProperty(exports, "ownerWindow", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _dom.getWindow;
11
+ }
12
+ });
13
+ var _dom = require("@floating-ui/utils/dom");
14
+ function ownerDocument(node) {
15
+ return node?.ownerDocument || document;
16
+ }
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@tale-ui/utils",
3
+ "version": "0.0.3",
4
+ "description": "A collection of React utility functions for Tale UI.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Tale-UI/react.git",
8
+ "directory": "packages/utils"
9
+ },
10
+ "license": "MIT",
11
+ "type": "commonjs",
12
+ "dependencies": {
13
+ "@babel/runtime": "^7.28.6",
14
+ "@floating-ui/utils": "^0.2.10",
15
+ "reselect": "^5.1.1",
16
+ "use-sync-external-store": "^1.6.0"
17
+ },
18
+ "peerDependencies": {
19
+ "@types/react": "^17 || ^18 || ^19",
20
+ "react": "^17 || ^18 || ^19",
21
+ "react-dom": "^17 || ^18 || ^19"
22
+ },
23
+ "peerDependenciesMeta": {
24
+ "@types/react": {
25
+ "optional": true
26
+ }
27
+ },
28
+ "sideEffects": false,
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "exports": {
33
+ "./package.json": "./package.json",
34
+ "./store": {
35
+ "import": {
36
+ "types": "./esm/store/index.d.ts",
37
+ "default": "./esm/store/index.js"
38
+ },
39
+ "require": {
40
+ "types": "./store/index.d.ts",
41
+ "default": "./store/index.js"
42
+ },
43
+ "default": {
44
+ "types": "./esm/store/index.d.ts",
45
+ "default": "./esm/store/index.js"
46
+ }
47
+ },
48
+ "./*": {
49
+ "import": {
50
+ "types": "./esm/*.d.ts",
51
+ "default": "./esm/*.js"
52
+ },
53
+ "require": {
54
+ "types": "./*.d.ts",
55
+ "default": "./*.js"
56
+ },
57
+ "default": {
58
+ "types": "./esm/*.d.ts",
59
+ "default": "./esm/*.js"
60
+ }
61
+ },
62
+ "./esm": null
63
+ }
64
+ }
@@ -0,0 +1,3 @@
1
+ type SupportedVersions = 17 | 18 | 19;
2
+ export declare function isReactVersionAtLeast(reactVersionToCheck: SupportedVersions): boolean;
3
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.isReactVersionAtLeast = isReactVersionAtLeast;
8
+ var React = _interopRequireWildcard(require("react"));
9
+ const majorVersion = parseInt(React.version, 10);
10
+ function isReactVersionAtLeast(reactVersionToCheck) {
11
+ return majorVersion >= reactVersionToCheck;
12
+ }
package/safeReact.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import * as React from 'react';
2
+ export declare const SafeReact: typeof React;
package/safeReact.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.SafeReact = void 0;
8
+ var React = _interopRequireWildcard(require("react"));
9
+ // https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379
10
+ const SafeReact = exports.SafeReact = {
11
+ ...React
12
+ };
@@ -0,0 +1,91 @@
1
+ import { Store } from "./Store.js";
2
+ /**
3
+ * A Store that supports controlled state keys, non-reactive values and provides utility methods for React.
4
+ */
5
+ export declare class ReactStore<State extends object, Context = Record<string, never>, Selectors extends Record<string, SelectorFunction<State>> = Record<string, never>> extends Store<State> {
6
+ /**
7
+ * Creates a new ReactStore instance.
8
+ *
9
+ * @param state Initial state of the store.
10
+ * @param context Non-reactive context values.
11
+ * @param selectors Optional selectors for use with `useState`.
12
+ */
13
+ constructor(state: State, context?: Context, selectors?: Selectors);
14
+ /**
15
+ * Non-reactive values such as refs, callbacks, etc.
16
+ */
17
+ readonly context: Context;
18
+ private selectors;
19
+ /**
20
+ * Synchronizes a single external value into the store.
21
+ *
22
+ * Note that the while the value in `state` is updated immediately, the value returned
23
+ * by `useState` is updated before the next render (similarly to React's `useState`).
24
+ */
25
+ useSyncedValue<Key extends keyof State, Value extends State[Key]>(key: keyof State, value: Value): void;
26
+ /**
27
+ * Synchronizes a single external value into the store and
28
+ * cleans it up (sets to `undefined`) on unmount.
29
+ *
30
+ * Note that the while the value in `state` is updated immediately, the value returned
31
+ * by `useState` is updated before the next render (similarly to React's `useState`).
32
+ */
33
+ useSyncedValueWithCleanup<Key extends KeysAllowingUndefined<State>>(key: Key, value: State[Key]): void;
34
+ /**
35
+ * Synchronizes multiple external values into the store.
36
+ *
37
+ * Note that the while the values in `state` are updated immediately, the values returned
38
+ * by `useState` are updated before the next render (similarly to React's `useState`).
39
+ */
40
+ useSyncedValues(statePart: Partial<State>): void;
41
+ /**
42
+ * Registers a controllable prop pair (`controlled`, `defaultValue`) for a specific key. If `controlled`
43
+ * is non-undefined, the store's state at `key` is updated to match `controlled`.
44
+ */
45
+ useControlledProp<Key extends keyof State, Value extends State[Key]>(key: keyof State, controlled: Value | undefined): void;
46
+ /** Gets the current value from the store using a selector with the provided key.
47
+ *
48
+ * @param key Key of the selector to use.
49
+ */
50
+ select<Key extends keyof Selectors>(key: Key, ...args: SelectorArgs<Selectors[Key]>): ReturnType<Selectors[Key]>;
51
+ /**
52
+ * Returns a value from the store's state using a selector function.
53
+ * Used to subscribe to specific parts of the state.
54
+ * This methods causes a rerender whenever the selected state changes.
55
+ *
56
+ * @param key Key of the selector to use.
57
+ */
58
+ useState<Key extends keyof Selectors>(key: Key, ...args: SelectorArgs<Selectors[Key]>): ReturnType<Selectors[Key]>;
59
+ /**
60
+ * Wraps a function with `useStableCallback` to ensure it has a stable reference
61
+ * and assigns it to the context.
62
+ *
63
+ * @param key Key of the event callback. Must be a function in the context.
64
+ * @param fn Function to assign.
65
+ */
66
+ useContextCallback<Key extends ContextFunctionKeys<Context>>(key: Key, fn: ContextFunction<Context, Key> | undefined): void;
67
+ /**
68
+ * Returns a stable setter function for a specific key in the store's state.
69
+ * It's commonly used to pass as a ref callback to React elements.
70
+ *
71
+ * @param key Key of the state to set.
72
+ */
73
+ useStateSetter<const Key extends keyof State, Value extends State[Key]>(key: keyof State): (v: Value) => void;
74
+ /**
75
+ * Observes changes derived from the store's selectors and calls the listener when the selected value changes.
76
+ *
77
+ * @param key Key of the selector to observe.
78
+ * @param listener Listener function called when the selector result changes.
79
+ */
80
+ observe<Key extends keyof Selectors>(selector: Key, listener: (newValue: ReturnType<Selectors[Key]>, oldValue: ReturnType<Selectors[Key]>, store: this) => void): () => void;
81
+ observe<Selector extends ObserveSelector<State>>(selector: Selector, listener: (newValue: ReturnType<Selector>, oldValue: ReturnType<Selector>, store: this) => void): () => void;
82
+ }
83
+ type MaybeCallable = (...args: any[]) => any;
84
+ type ContextFunctionKeys<Context> = { [Key in keyof Context]-?: Extract<Context[Key], MaybeCallable> extends never ? never : Key }[keyof Context];
85
+ type ContextFunction<Context, Key extends keyof Context> = Extract<Context[Key], MaybeCallable>;
86
+ type KeysAllowingUndefined<State> = { [Key in keyof State]-?: undefined extends State[Key] ? Key : never }[keyof State];
87
+ type ObserveSelector<State> = (state: State) => any;
88
+ type SelectorFunction<State> = (state: State, ...args: any[]) => any;
89
+ type Tail<T extends readonly any[]> = T extends readonly [any, ...infer Rest] ? Rest : [];
90
+ type SelectorArgs<Selector> = Selector extends ((...params: infer Params) => any) ? Tail<Params> : never;
91
+ export {};