@radix-ui/react-use-controllable-state 0.0.0-20250116175529
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/README.md +13 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +47 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# `react-use-controllable-state`
|
|
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
|
+
This is an internal utility, not intended for public usage.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type UseControllableStateParams<T> = {
|
|
4
|
+
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>>];
|
|
9
|
+
|
|
10
|
+
export { useControllableState };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type UseControllableStateParams<T> = {
|
|
4
|
+
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>>];
|
|
9
|
+
|
|
10
|
+
export { useControllableState };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// packages/react/use-controllable-state/src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
useControllableState: () => useControllableState
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// packages/react/use-controllable-state/src/useControllableState.tsx
|
|
38
|
+
var React = __toESM(require("react"));
|
|
39
|
+
var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
|
|
40
|
+
function useControllableState({
|
|
41
|
+
prop,
|
|
42
|
+
defaultProp,
|
|
43
|
+
onChange = () => {
|
|
44
|
+
}
|
|
45
|
+
}) {
|
|
46
|
+
const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });
|
|
47
|
+
const isControlled = prop !== void 0;
|
|
48
|
+
const value = isControlled ? prop : uncontrolledProp;
|
|
49
|
+
const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
|
|
50
|
+
const setValue = React.useCallback(
|
|
51
|
+
(nextValue) => {
|
|
52
|
+
if (isControlled) {
|
|
53
|
+
const setter = nextValue;
|
|
54
|
+
const value2 = typeof nextValue === "function" ? setter(prop) : nextValue;
|
|
55
|
+
if (value2 !== prop) handleChange(value2);
|
|
56
|
+
} else {
|
|
57
|
+
setUncontrolledProp(nextValue);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
[isControlled, prop, setUncontrolledProp, handleChange]
|
|
61
|
+
);
|
|
62
|
+
return [value, setValue];
|
|
63
|
+
}
|
|
64
|
+
function useUncontrolledState({
|
|
65
|
+
defaultProp,
|
|
66
|
+
onChange
|
|
67
|
+
}) {
|
|
68
|
+
const uncontrolledState = React.useState(defaultProp);
|
|
69
|
+
const [value] = uncontrolledState;
|
|
70
|
+
const prevValueRef = React.useRef(value);
|
|
71
|
+
const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
if (prevValueRef.current !== value) {
|
|
74
|
+
handleChange(value);
|
|
75
|
+
prevValueRef.current = value;
|
|
76
|
+
}
|
|
77
|
+
}, [value, prevValueRef, handleChange]);
|
|
78
|
+
return uncontrolledState;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/useControllableState.tsx"],
|
|
4
|
+
"sourcesContent": ["export { useControllableState } from './useControllableState';\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;",
|
|
6
|
+
"names": ["value"]
|
|
7
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// packages/react/use-controllable-state/src/useControllableState.tsx
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
4
|
+
function useControllableState({
|
|
5
|
+
prop,
|
|
6
|
+
defaultProp,
|
|
7
|
+
onChange = () => {
|
|
8
|
+
}
|
|
9
|
+
}) {
|
|
10
|
+
const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });
|
|
11
|
+
const isControlled = prop !== void 0;
|
|
12
|
+
const value = isControlled ? prop : uncontrolledProp;
|
|
13
|
+
const handleChange = useCallbackRef(onChange);
|
|
14
|
+
const setValue = React.useCallback(
|
|
15
|
+
(nextValue) => {
|
|
16
|
+
if (isControlled) {
|
|
17
|
+
const setter = nextValue;
|
|
18
|
+
const value2 = typeof nextValue === "function" ? setter(prop) : nextValue;
|
|
19
|
+
if (value2 !== prop) handleChange(value2);
|
|
20
|
+
} else {
|
|
21
|
+
setUncontrolledProp(nextValue);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
[isControlled, prop, setUncontrolledProp, handleChange]
|
|
25
|
+
);
|
|
26
|
+
return [value, setValue];
|
|
27
|
+
}
|
|
28
|
+
function useUncontrolledState({
|
|
29
|
+
defaultProp,
|
|
30
|
+
onChange
|
|
31
|
+
}) {
|
|
32
|
+
const uncontrolledState = React.useState(defaultProp);
|
|
33
|
+
const [value] = uncontrolledState;
|
|
34
|
+
const prevValueRef = React.useRef(value);
|
|
35
|
+
const handleChange = useCallbackRef(onChange);
|
|
36
|
+
React.useEffect(() => {
|
|
37
|
+
if (prevValueRef.current !== value) {
|
|
38
|
+
handleChange(value);
|
|
39
|
+
prevValueRef.current = value;
|
|
40
|
+
}
|
|
41
|
+
}, [value, prevValueRef, handleChange]);
|
|
42
|
+
return uncontrolledState;
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
useControllableState
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/useControllableState.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;",
|
|
6
|
+
"names": ["value"]
|
|
7
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@radix-ui/react-use-controllable-state",
|
|
3
|
+
"version": "0.0.0-20250116175529",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": {
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"default": "./dist/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"require": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"source": "./src/index.ts",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "rm -rf dist"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@radix-ui/react-use-callback-ref": "workspace:*"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@types/react": "*",
|
|
34
|
+
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@types/react": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://radix-ui.com/primitives",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/radix-ui/primitives.git"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/radix-ui/primitives/issues"
|
|
48
|
+
}
|
|
49
|
+
}
|