@radix-ui/react-use-controllable-state 1.1.1 → 1.2.0-rc.1744259481941

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 WorkOS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,13 +1,3 @@
1
1
  # `react-use-controllable-state`
2
2
 
3
- ## Installation
4
-
5
- ```sh
6
- $ yarn add @radix-ui/react-use-controllable-state
7
- # or
8
- $ npm install @radix-ui/react-use-controllable-state
9
- ```
10
-
11
- ## Usage
12
-
13
3
  This is an internal utility, not intended for public usage.
package/dist/index.d.mts CHANGED
@@ -1,10 +1,13 @@
1
1
  import * as React from 'react';
2
2
 
3
- type UseControllableStateParams<T> = {
3
+ type ChangeHandler<T> = (state: T) => void;
4
+ type SetStateFn<T> = React.Dispatch<React.SetStateAction<T>>;
5
+ interface UseControllableStateParams<T> {
4
6
  prop?: T | undefined;
5
- defaultProp?: T | undefined;
6
- onChange?: (state: T) => void;
7
- };
8
- declare function useControllableState<T>({ prop, defaultProp, onChange, }: UseControllableStateParams<T>): readonly [T | undefined, React.Dispatch<React.SetStateAction<T | undefined>>];
7
+ defaultProp: T;
8
+ onChange?: ChangeHandler<T>;
9
+ caller?: string;
10
+ }
11
+ declare function useControllableState<T>({ prop, defaultProp, onChange, caller, }: UseControllableStateParams<T>): [T, SetStateFn<T>];
9
12
 
10
13
  export { useControllableState };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import * as React from 'react';
2
2
 
3
- type UseControllableStateParams<T> = {
3
+ type ChangeHandler<T> = (state: T) => void;
4
+ type SetStateFn<T> = React.Dispatch<React.SetStateAction<T>>;
5
+ interface UseControllableStateParams<T> {
4
6
  prop?: T | undefined;
5
- defaultProp?: T | undefined;
6
- onChange?: (state: T) => void;
7
- };
8
- declare function useControllableState<T>({ prop, defaultProp, onChange, }: UseControllableStateParams<T>): readonly [T | undefined, React.Dispatch<React.SetStateAction<T | undefined>>];
7
+ defaultProp: T;
8
+ onChange?: ChangeHandler<T>;
9
+ caller?: string;
10
+ }
11
+ declare function useControllableState<T>({ prop, defaultProp, onChange, caller, }: UseControllableStateParams<T>): [T, SetStateFn<T>];
9
12
 
10
13
  export { useControllableState };
package/dist/index.js CHANGED
@@ -27,37 +27,56 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
- // packages/react/use-controllable-state/src/index.ts
30
+ // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  useControllableState: () => useControllableState
34
34
  });
35
35
  module.exports = __toCommonJS(index_exports);
36
36
 
37
- // packages/react/use-controllable-state/src/use-controllable-state.tsx
37
+ // src/use-controllable-state.tsx
38
38
  var React = __toESM(require("react"));
39
- var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
39
+ var import_react_use_layout_effect = require("@radix-ui/react-use-layout-effect");
40
+ var useInsertionEffect = React[" useInsertionEffect ".trim().toString()] || import_react_use_layout_effect.useLayoutEffect;
40
41
  function useControllableState({
41
42
  prop,
42
43
  defaultProp,
43
44
  onChange = () => {
44
- }
45
+ },
46
+ caller
45
47
  }) {
46
- const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });
48
+ const [uncontrolledProp, setUncontrolledProp, onChangeRef] = useUncontrolledState({
49
+ defaultProp,
50
+ onChange
51
+ });
47
52
  const isControlled = prop !== void 0;
48
53
  const value = isControlled ? prop : uncontrolledProp;
49
- const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
54
+ if (true) {
55
+ const isControlledRef = React.useRef(prop !== void 0);
56
+ React.useEffect(() => {
57
+ const wasControlled = isControlledRef.current;
58
+ if (wasControlled !== isControlled) {
59
+ const from = wasControlled ? "controlled" : "uncontrolled";
60
+ const to = isControlled ? "controlled" : "uncontrolled";
61
+ console.warn(
62
+ `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`
63
+ );
64
+ }
65
+ isControlledRef.current = isControlled;
66
+ }, [isControlled, caller]);
67
+ }
50
68
  const setValue = React.useCallback(
51
69
  (nextValue) => {
52
70
  if (isControlled) {
53
- const setter = nextValue;
54
- const value2 = typeof nextValue === "function" ? setter(prop) : nextValue;
55
- if (value2 !== prop) handleChange(value2);
71
+ const value2 = isFunction(nextValue) ? nextValue(prop) : nextValue;
72
+ if (value2 !== prop) {
73
+ onChangeRef.current?.(value2);
74
+ }
56
75
  } else {
57
76
  setUncontrolledProp(nextValue);
58
77
  }
59
78
  },
60
- [isControlled, prop, setUncontrolledProp, handleChange]
79
+ [isControlled, prop, setUncontrolledProp, onChangeRef]
61
80
  );
62
81
  return [value, setValue];
63
82
  }
@@ -65,16 +84,21 @@ function useUncontrolledState({
65
84
  defaultProp,
66
85
  onChange
67
86
  }) {
68
- const uncontrolledState = React.useState(defaultProp);
69
- const [value] = uncontrolledState;
87
+ const [value, setValue] = React.useState(defaultProp);
70
88
  const prevValueRef = React.useRef(value);
71
- const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
89
+ const onChangeRef = React.useRef(onChange);
90
+ useInsertionEffect(() => {
91
+ onChangeRef.current = onChange;
92
+ }, [onChange]);
72
93
  React.useEffect(() => {
73
94
  if (prevValueRef.current !== value) {
74
- handleChange(value);
95
+ onChangeRef.current?.(value);
75
96
  prevValueRef.current = value;
76
97
  }
77
- }, [value, prevValueRef, handleChange]);
78
- return uncontrolledState;
98
+ }, [value, prevValueRef]);
99
+ return [value, setValue, onChangeRef];
100
+ }
101
+ function isFunction(value) {
102
+ return typeof value === "function";
79
103
  }
80
104
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/use-controllable-state.tsx"],
4
- "sourcesContent": ["export { useControllableState } from './use-controllable-state';\n", "import * as React from 'react';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\ntype UseControllableStateParams<T> = {\n prop?: T | undefined;\n defaultProp?: T | undefined;\n onChange?: (state: T) => void;\n};\n\ntype SetStateFn<T> = (prevState?: T) => T;\n\nfunction useControllableState<T>({\n prop,\n defaultProp,\n onChange = () => {},\n}: UseControllableStateParams<T>) {\n const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });\n const isControlled = prop !== undefined;\n const value = isControlled ? prop : uncontrolledProp;\n const handleChange = useCallbackRef(onChange);\n\n const setValue: React.Dispatch<React.SetStateAction<T | undefined>> = React.useCallback(\n (nextValue) => {\n if (isControlled) {\n const setter = nextValue as SetStateFn<T>;\n const value = typeof nextValue === 'function' ? setter(prop) : nextValue;\n if (value !== prop) handleChange(value as T);\n } else {\n setUncontrolledProp(nextValue);\n }\n },\n [isControlled, prop, setUncontrolledProp, handleChange]\n );\n\n return [value, setValue] as const;\n}\n\nfunction useUncontrolledState<T>({\n defaultProp,\n onChange,\n}: Omit<UseControllableStateParams<T>, 'prop'>) {\n const uncontrolledState = React.useState<T | undefined>(defaultProp);\n const [value] = uncontrolledState;\n const prevValueRef = React.useRef(value);\n const handleChange = useCallbackRef(onChange);\n\n React.useEffect(() => {\n if (prevValueRef.current !== value) {\n handleChange(value as T);\n prevValueRef.current = value;\n }\n }, [value, prevValueRef, handleChange]);\n\n return uncontrolledState;\n}\n\nexport { useControllableState };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,oCAA+B;AAU/B,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA,WAAW,MAAM;AAAA,EAAC;AACpB,GAAkC;AAChC,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,qBAAqB,EAAE,aAAa,SAAS,CAAC;AAC9F,QAAM,eAAe,SAAS;AAC9B,QAAM,QAAQ,eAAe,OAAO;AACpC,QAAM,mBAAe,8CAAe,QAAQ;AAE5C,QAAM,WAAsE;AAAA,IAC1E,CAAC,cAAc;AACb,UAAI,cAAc;AAChB,cAAM,SAAS;AACf,cAAMA,SAAQ,OAAO,cAAc,aAAa,OAAO,IAAI,IAAI;AAC/D,YAAIA,WAAU,KAAM,cAAaA,MAAU;AAAA,MAC7C,OAAO;AACL,4BAAoB,SAAS;AAAA,MAC/B;AAAA,IACF;AAAA,IACA,CAAC,cAAc,MAAM,qBAAqB,YAAY;AAAA,EACxD;AAEA,SAAO,CAAC,OAAO,QAAQ;AACzB;AAEA,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AACF,GAAgD;AAC9C,QAAM,oBAA0B,eAAwB,WAAW;AACnE,QAAM,CAAC,KAAK,IAAI;AAChB,QAAM,eAAqB,aAAO,KAAK;AACvC,QAAM,mBAAe,8CAAe,QAAQ;AAE5C,EAAM,gBAAU,MAAM;AACpB,QAAI,aAAa,YAAY,OAAO;AAClC,mBAAa,KAAU;AACvB,mBAAa,UAAU;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,OAAO,cAAc,YAAY,CAAC;AAEtC,SAAO;AACT;",
4
+ "sourcesContent": ["export { useControllableState } from './use-controllable-state';\n", "import * as React from 'react';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\n\n// Prevent bundlers from trying to optimize the import\nconst useInsertionEffect: typeof useLayoutEffect =\n (React as any)[' useInsertionEffect '.trim().toString()] || useLayoutEffect;\n\ntype ChangeHandler<T> = (state: T) => void;\ntype SetStateFn<T> = React.Dispatch<React.SetStateAction<T>>;\n\ninterface UseControllableStateParams<T> {\n prop?: T | undefined;\n defaultProp: T;\n onChange?: ChangeHandler<T>;\n caller?: string;\n}\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange = () => {},\n caller,\n}: UseControllableStateParams<T>): [T, SetStateFn<T>] {\n const [uncontrolledProp, setUncontrolledProp, onChangeRef] = useUncontrolledState({\n defaultProp,\n onChange,\n });\n const isControlled = prop !== undefined;\n const value = isControlled ? prop : uncontrolledProp;\n\n // OK to disable conditionally calling hooks here because they will always run\n // consistently in the same environment. Bundlers should be able to remove the\n // code block entirely in production.\n /* eslint-disable react-hooks/rules-of-hooks */\n if (process.env.NODE_ENV !== 'production') {\n const isControlledRef = React.useRef(prop !== undefined);\n React.useEffect(() => {\n const wasControlled = isControlledRef.current;\n if (wasControlled !== isControlled) {\n const from = wasControlled ? 'controlled' : 'uncontrolled';\n const to = isControlled ? 'controlled' : 'uncontrolled';\n console.warn(\n `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`\n );\n }\n isControlledRef.current = isControlled;\n }, [isControlled, caller]);\n }\n /* eslint-enable react-hooks/rules-of-hooks */\n\n const setValue = React.useCallback<SetStateFn<T>>(\n (nextValue) => {\n if (isControlled) {\n const value = isFunction(nextValue) ? nextValue(prop) : nextValue;\n if (value !== prop) {\n onChangeRef.current?.(value);\n }\n } else {\n setUncontrolledProp(nextValue);\n }\n },\n [isControlled, prop, setUncontrolledProp, onChangeRef]\n );\n\n return [value, setValue];\n}\n\nfunction useUncontrolledState<T>({\n defaultProp,\n onChange,\n}: Omit<UseControllableStateParams<T>, 'prop'>): [\n Value: T,\n setValue: React.Dispatch<React.SetStateAction<T>>,\n OnChangeRef: React.RefObject<ChangeHandler<T> | undefined>,\n] {\n const [value, setValue] = React.useState(defaultProp);\n const prevValueRef = React.useRef(value);\n\n const onChangeRef = React.useRef(onChange);\n useInsertionEffect(() => {\n onChangeRef.current = onChange;\n }, [onChange]);\n\n React.useEffect(() => {\n if (prevValueRef.current !== value) {\n onChangeRef.current?.(value);\n prevValueRef.current = value;\n }\n }, [value, prevValueRef]);\n\n return [value, setValue, onChangeRef];\n}\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,qCAAgC;AAGhC,IAAM,qBACH,MAAc,uBAAuB,KAAK,EAAE,SAAS,CAAC,KAAK;AAYvD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB;AACF,GAAsD;AACpD,QAAM,CAAC,kBAAkB,qBAAqB,WAAW,IAAI,qBAAqB;AAAA,IAChF;AAAA,IACA;AAAA,EACF,CAAC;AACD,QAAM,eAAe,SAAS;AAC9B,QAAM,QAAQ,eAAe,OAAO;AAMpC,MAAI,MAAuC;AACzC,UAAM,kBAAwB,aAAO,SAAS,MAAS;AACvD,IAAM,gBAAU,MAAM;AACpB,YAAM,gBAAgB,gBAAgB;AACtC,UAAI,kBAAkB,cAAc;AAClC,cAAM,OAAO,gBAAgB,eAAe;AAC5C,cAAM,KAAK,eAAe,eAAe;AACzC,gBAAQ;AAAA,UACN,GAAG,MAAM,qBAAqB,IAAI,OAAO,EAAE;AAAA,QAC7C;AAAA,MACF;AACA,sBAAgB,UAAU;AAAA,IAC5B,GAAG,CAAC,cAAc,MAAM,CAAC;AAAA,EAC3B;AAGA,QAAM,WAAiB;AAAA,IACrB,CAAC,cAAc;AACb,UAAI,cAAc;AAChB,cAAMA,SAAQ,WAAW,SAAS,IAAI,UAAU,IAAI,IAAI;AACxD,YAAIA,WAAU,MAAM;AAClB,sBAAY,UAAUA,MAAK;AAAA,QAC7B;AAAA,MACF,OAAO;AACL,4BAAoB,SAAS;AAAA,MAC/B;AAAA,IACF;AAAA,IACA,CAAC,cAAc,MAAM,qBAAqB,WAAW;AAAA,EACvD;AAEA,SAAO,CAAC,OAAO,QAAQ;AACzB;AAEA,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AACF,GAIE;AACA,QAAM,CAAC,OAAO,QAAQ,IAAU,eAAS,WAAW;AACpD,QAAM,eAAqB,aAAO,KAAK;AAEvC,QAAM,cAAoB,aAAO,QAAQ;AACzC,qBAAmB,MAAM;AACvB,gBAAY,UAAU;AAAA,EACxB,GAAG,CAAC,QAAQ,CAAC;AAEb,EAAM,gBAAU,MAAM;AACpB,QAAI,aAAa,YAAY,OAAO;AAClC,kBAAY,UAAU,KAAK;AAC3B,mBAAa,UAAU;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,OAAO,YAAY,CAAC;AAExB,SAAO,CAAC,OAAO,UAAU,WAAW;AACtC;AAEA,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;",
6
6
  "names": ["value"]
7
7
  }
package/dist/index.mjs CHANGED
@@ -1,27 +1,46 @@
1
- // packages/react/use-controllable-state/src/use-controllable-state.tsx
1
+ // src/use-controllable-state.tsx
2
2
  import * as React from "react";
3
- import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
3
+ import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
4
+ var useInsertionEffect = React[" useInsertionEffect ".trim().toString()] || useLayoutEffect;
4
5
  function useControllableState({
5
6
  prop,
6
7
  defaultProp,
7
8
  onChange = () => {
8
- }
9
+ },
10
+ caller
9
11
  }) {
10
- const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });
12
+ const [uncontrolledProp, setUncontrolledProp, onChangeRef] = useUncontrolledState({
13
+ defaultProp,
14
+ onChange
15
+ });
11
16
  const isControlled = prop !== void 0;
12
17
  const value = isControlled ? prop : uncontrolledProp;
13
- const handleChange = useCallbackRef(onChange);
18
+ if (true) {
19
+ const isControlledRef = React.useRef(prop !== void 0);
20
+ React.useEffect(() => {
21
+ const wasControlled = isControlledRef.current;
22
+ if (wasControlled !== isControlled) {
23
+ const from = wasControlled ? "controlled" : "uncontrolled";
24
+ const to = isControlled ? "controlled" : "uncontrolled";
25
+ console.warn(
26
+ `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`
27
+ );
28
+ }
29
+ isControlledRef.current = isControlled;
30
+ }, [isControlled, caller]);
31
+ }
14
32
  const setValue = React.useCallback(
15
33
  (nextValue) => {
16
34
  if (isControlled) {
17
- const setter = nextValue;
18
- const value2 = typeof nextValue === "function" ? setter(prop) : nextValue;
19
- if (value2 !== prop) handleChange(value2);
35
+ const value2 = isFunction(nextValue) ? nextValue(prop) : nextValue;
36
+ if (value2 !== prop) {
37
+ onChangeRef.current?.(value2);
38
+ }
20
39
  } else {
21
40
  setUncontrolledProp(nextValue);
22
41
  }
23
42
  },
24
- [isControlled, prop, setUncontrolledProp, handleChange]
43
+ [isControlled, prop, setUncontrolledProp, onChangeRef]
25
44
  );
26
45
  return [value, setValue];
27
46
  }
@@ -29,17 +48,22 @@ function useUncontrolledState({
29
48
  defaultProp,
30
49
  onChange
31
50
  }) {
32
- const uncontrolledState = React.useState(defaultProp);
33
- const [value] = uncontrolledState;
51
+ const [value, setValue] = React.useState(defaultProp);
34
52
  const prevValueRef = React.useRef(value);
35
- const handleChange = useCallbackRef(onChange);
53
+ const onChangeRef = React.useRef(onChange);
54
+ useInsertionEffect(() => {
55
+ onChangeRef.current = onChange;
56
+ }, [onChange]);
36
57
  React.useEffect(() => {
37
58
  if (prevValueRef.current !== value) {
38
- handleChange(value);
59
+ onChangeRef.current?.(value);
39
60
  prevValueRef.current = value;
40
61
  }
41
- }, [value, prevValueRef, handleChange]);
42
- return uncontrolledState;
62
+ }, [value, prevValueRef]);
63
+ return [value, setValue, onChangeRef];
64
+ }
65
+ function isFunction(value) {
66
+ return typeof value === "function";
43
67
  }
44
68
  export {
45
69
  useControllableState
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/use-controllable-state.tsx"],
4
- "sourcesContent": ["import * as React from 'react';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\ntype UseControllableStateParams<T> = {\n prop?: T | undefined;\n defaultProp?: T | undefined;\n onChange?: (state: T) => void;\n};\n\ntype SetStateFn<T> = (prevState?: T) => T;\n\nfunction useControllableState<T>({\n prop,\n defaultProp,\n onChange = () => {},\n}: UseControllableStateParams<T>) {\n const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });\n const isControlled = prop !== undefined;\n const value = isControlled ? prop : uncontrolledProp;\n const handleChange = useCallbackRef(onChange);\n\n const setValue: React.Dispatch<React.SetStateAction<T | undefined>> = React.useCallback(\n (nextValue) => {\n if (isControlled) {\n const setter = nextValue as SetStateFn<T>;\n const value = typeof nextValue === 'function' ? setter(prop) : nextValue;\n if (value !== prop) handleChange(value as T);\n } else {\n setUncontrolledProp(nextValue);\n }\n },\n [isControlled, prop, setUncontrolledProp, handleChange]\n );\n\n return [value, setValue] as const;\n}\n\nfunction useUncontrolledState<T>({\n defaultProp,\n onChange,\n}: Omit<UseControllableStateParams<T>, 'prop'>) {\n const uncontrolledState = React.useState<T | undefined>(defaultProp);\n const [value] = uncontrolledState;\n const prevValueRef = React.useRef(value);\n const handleChange = useCallbackRef(onChange);\n\n React.useEffect(() => {\n if (prevValueRef.current !== value) {\n handleChange(value as T);\n prevValueRef.current = value;\n }\n }, [value, prevValueRef, handleChange]);\n\n return uncontrolledState;\n}\n\nexport { useControllableState };\n"],
5
- "mappings": ";AAAA,YAAY,WAAW;AACvB,SAAS,sBAAsB;AAU/B,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA,WAAW,MAAM;AAAA,EAAC;AACpB,GAAkC;AAChC,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,qBAAqB,EAAE,aAAa,SAAS,CAAC;AAC9F,QAAM,eAAe,SAAS;AAC9B,QAAM,QAAQ,eAAe,OAAO;AACpC,QAAM,eAAe,eAAe,QAAQ;AAE5C,QAAM,WAAsE;AAAA,IAC1E,CAAC,cAAc;AACb,UAAI,cAAc;AAChB,cAAM,SAAS;AACf,cAAMA,SAAQ,OAAO,cAAc,aAAa,OAAO,IAAI,IAAI;AAC/D,YAAIA,WAAU,KAAM,cAAaA,MAAU;AAAA,MAC7C,OAAO;AACL,4BAAoB,SAAS;AAAA,MAC/B;AAAA,IACF;AAAA,IACA,CAAC,cAAc,MAAM,qBAAqB,YAAY;AAAA,EACxD;AAEA,SAAO,CAAC,OAAO,QAAQ;AACzB;AAEA,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AACF,GAAgD;AAC9C,QAAM,oBAA0B,eAAwB,WAAW;AACnE,QAAM,CAAC,KAAK,IAAI;AAChB,QAAM,eAAqB,aAAO,KAAK;AACvC,QAAM,eAAe,eAAe,QAAQ;AAE5C,EAAM,gBAAU,MAAM;AACpB,QAAI,aAAa,YAAY,OAAO;AAClC,mBAAa,KAAU;AACvB,mBAAa,UAAU;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,OAAO,cAAc,YAAY,CAAC;AAEtC,SAAO;AACT;",
4
+ "sourcesContent": ["import * as React from 'react';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\n\n// Prevent bundlers from trying to optimize the import\nconst useInsertionEffect: typeof useLayoutEffect =\n (React as any)[' useInsertionEffect '.trim().toString()] || useLayoutEffect;\n\ntype ChangeHandler<T> = (state: T) => void;\ntype SetStateFn<T> = React.Dispatch<React.SetStateAction<T>>;\n\ninterface UseControllableStateParams<T> {\n prop?: T | undefined;\n defaultProp: T;\n onChange?: ChangeHandler<T>;\n caller?: string;\n}\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange = () => {},\n caller,\n}: UseControllableStateParams<T>): [T, SetStateFn<T>] {\n const [uncontrolledProp, setUncontrolledProp, onChangeRef] = useUncontrolledState({\n defaultProp,\n onChange,\n });\n const isControlled = prop !== undefined;\n const value = isControlled ? prop : uncontrolledProp;\n\n // OK to disable conditionally calling hooks here because they will always run\n // consistently in the same environment. Bundlers should be able to remove the\n // code block entirely in production.\n /* eslint-disable react-hooks/rules-of-hooks */\n if (process.env.NODE_ENV !== 'production') {\n const isControlledRef = React.useRef(prop !== undefined);\n React.useEffect(() => {\n const wasControlled = isControlledRef.current;\n if (wasControlled !== isControlled) {\n const from = wasControlled ? 'controlled' : 'uncontrolled';\n const to = isControlled ? 'controlled' : 'uncontrolled';\n console.warn(\n `${caller} is changing from ${from} to ${to}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`\n );\n }\n isControlledRef.current = isControlled;\n }, [isControlled, caller]);\n }\n /* eslint-enable react-hooks/rules-of-hooks */\n\n const setValue = React.useCallback<SetStateFn<T>>(\n (nextValue) => {\n if (isControlled) {\n const value = isFunction(nextValue) ? nextValue(prop) : nextValue;\n if (value !== prop) {\n onChangeRef.current?.(value);\n }\n } else {\n setUncontrolledProp(nextValue);\n }\n },\n [isControlled, prop, setUncontrolledProp, onChangeRef]\n );\n\n return [value, setValue];\n}\n\nfunction useUncontrolledState<T>({\n defaultProp,\n onChange,\n}: Omit<UseControllableStateParams<T>, 'prop'>): [\n Value: T,\n setValue: React.Dispatch<React.SetStateAction<T>>,\n OnChangeRef: React.RefObject<ChangeHandler<T> | undefined>,\n] {\n const [value, setValue] = React.useState(defaultProp);\n const prevValueRef = React.useRef(value);\n\n const onChangeRef = React.useRef(onChange);\n useInsertionEffect(() => {\n onChangeRef.current = onChange;\n }, [onChange]);\n\n React.useEffect(() => {\n if (prevValueRef.current !== value) {\n onChangeRef.current?.(value);\n prevValueRef.current = value;\n }\n }, [value, prevValueRef]);\n\n return [value, setValue, onChangeRef];\n}\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n"],
5
+ "mappings": ";AAAA,YAAY,WAAW;AACvB,SAAS,uBAAuB;AAGhC,IAAM,qBACH,MAAc,uBAAuB,KAAK,EAAE,SAAS,CAAC,KAAK;AAYvD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB;AACF,GAAsD;AACpD,QAAM,CAAC,kBAAkB,qBAAqB,WAAW,IAAI,qBAAqB;AAAA,IAChF;AAAA,IACA;AAAA,EACF,CAAC;AACD,QAAM,eAAe,SAAS;AAC9B,QAAM,QAAQ,eAAe,OAAO;AAMpC,MAAI,MAAuC;AACzC,UAAM,kBAAwB,aAAO,SAAS,MAAS;AACvD,IAAM,gBAAU,MAAM;AACpB,YAAM,gBAAgB,gBAAgB;AACtC,UAAI,kBAAkB,cAAc;AAClC,cAAM,OAAO,gBAAgB,eAAe;AAC5C,cAAM,KAAK,eAAe,eAAe;AACzC,gBAAQ;AAAA,UACN,GAAG,MAAM,qBAAqB,IAAI,OAAO,EAAE;AAAA,QAC7C;AAAA,MACF;AACA,sBAAgB,UAAU;AAAA,IAC5B,GAAG,CAAC,cAAc,MAAM,CAAC;AAAA,EAC3B;AAGA,QAAM,WAAiB;AAAA,IACrB,CAAC,cAAc;AACb,UAAI,cAAc;AAChB,cAAMA,SAAQ,WAAW,SAAS,IAAI,UAAU,IAAI,IAAI;AACxD,YAAIA,WAAU,MAAM;AAClB,sBAAY,UAAUA,MAAK;AAAA,QAC7B;AAAA,MACF,OAAO;AACL,4BAAoB,SAAS;AAAA,MAC/B;AAAA,IACF;AAAA,IACA,CAAC,cAAc,MAAM,qBAAqB,WAAW;AAAA,EACvD;AAEA,SAAO,CAAC,OAAO,QAAQ;AACzB;AAEA,SAAS,qBAAwB;AAAA,EAC/B;AAAA,EACA;AACF,GAIE;AACA,QAAM,CAAC,OAAO,QAAQ,IAAU,eAAS,WAAW;AACpD,QAAM,eAAqB,aAAO,KAAK;AAEvC,QAAM,cAAoB,aAAO,QAAQ;AACzC,qBAAmB,MAAM;AACvB,gBAAY,UAAU;AAAA,EACxB,GAAG,CAAC,QAAQ,CAAC;AAEb,EAAM,gBAAU,MAAM;AACpB,QAAI,aAAa,YAAY,OAAO;AAClC,kBAAY,UAAU,KAAK;AAC3B,mBAAa,UAAU;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,OAAO,YAAY,CAAC;AAExB,SAAO,CAAC,OAAO,UAAU,WAAW;AACtC;AAEA,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;",
6
6
  "names": ["value"]
7
7
  }
package/package.json CHANGED
@@ -1,49 +1,28 @@
1
1
  {
2
2
  "name": "@radix-ui/react-use-controllable-state",
3
- "version": "1.1.1",
3
+ "version": "1.2.0-rc.1744259481941",
4
4
  "license": "MIT",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
8
- "publishConfig": {
9
- "main": "./dist/index.js",
10
- "module": "./dist/index.mjs",
11
- "types": "./dist/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "import": {
15
- "types": "./dist/index.d.mts",
16
- "default": "./dist/index.mjs"
17
- },
18
- "require": {
19
- "types": "./dist/index.d.ts",
20
- "default": "./dist/index.js"
21
- }
22
- }
23
- }
24
- },
25
8
  "files": [
26
9
  "dist",
27
10
  "README.md"
28
11
  ],
29
12
  "sideEffects": false,
30
- "scripts": {
31
- "lint": "eslint --max-warnings 0 src",
32
- "clean": "rm -rf dist",
33
- "version": "yarn version"
34
- },
35
13
  "dependencies": {
36
- "@radix-ui/react-use-callback-ref": "1.1.1"
14
+ "@radix-ui/react-use-layout-effect": "1.1.1"
37
15
  },
38
16
  "devDependencies": {
39
- "@repo/eslint-config": "0.0.0",
40
- "@repo/typescript-config": "0.0.0",
41
17
  "@types/react": "^19.0.7",
42
18
  "@types/react-dom": "^19.0.3",
43
19
  "eslint": "^9.18.0",
44
20
  "react": "^19.0.0",
45
21
  "react-dom": "^19.0.0",
46
- "typescript": "^5.7.3"
22
+ "typescript": "^5.7.3",
23
+ "@repo/builder": "0.0.0",
24
+ "@repo/eslint-config": "0.0.0",
25
+ "@repo/typescript-config": "0.0.0"
47
26
  },
48
27
  "peerDependencies": {
49
28
  "@types/react": "*",
@@ -62,6 +41,13 @@
62
41
  "bugs": {
63
42
  "url": "https://github.com/radix-ui/primitives/issues"
64
43
  },
44
+ "scripts": {
45
+ "lint": "eslint --max-warnings 0 src",
46
+ "clean": "rm -rf dist",
47
+ "typecheck": "tsc --noEmit",
48
+ "build": "radix-build"
49
+ },
50
+ "types": "./dist/index.d.ts",
65
51
  "exports": {
66
52
  ".": {
67
53
  "import": {
@@ -73,6 +59,5 @@
73
59
  "default": "./dist/index.js"
74
60
  }
75
61
  }
76
- },
77
- "types": "./dist/index.d.ts"
62
+ }
78
63
  }