@tamagui/use-controllable-state 2.7.7 → 3.0.0-beta.643.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.
- package/dist/cjs/index.cjs +10 -11
- package/dist/cjs/index.native.cjs +21 -0
- package/dist/cjs/index.native.js +10 -10
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/useControllableState.cjs +68 -67
- package/dist/cjs/useControllableState.native.cjs +90 -0
- package/dist/cjs/useControllableState.native.js +70 -68
- package/dist/cjs/useControllableState.native.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/index.native.js +1 -2
- package/dist/esm/useControllableState.mjs +52 -41
- package/dist/esm/useControllableState.mjs.map +1 -1
- package/dist/esm/useControllableState.native.js +54 -43
- package/dist/esm/useControllableState.native.js.map +1 -1
- package/package.json +5 -4
- package/src/useControllableState.ts +37 -6
- package/types/index.d.ts +2 -1
- package/types/index.d.ts.map +11 -1
- package/types/useControllableState.d.ts +25 -10
- package/types/useControllableState.d.ts.map +11 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js.map +0 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
+
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
6
|
var __copyProps = (to, from, except, desc) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
9
|
+
get: () => from[key],
|
|
10
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return to;
|
|
13
14
|
};
|
|
14
15
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
16
|
-
value: true
|
|
17
|
-
}), mod);
|
|
16
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
17
|
var index_exports = {};
|
|
19
18
|
module.exports = __toCommonJS(index_exports);
|
|
20
|
-
__reExport(index_exports, require("./useControllableState.cjs"), module.exports);
|
|
19
|
+
__reExport(index_exports, require("./useControllableState.cjs"), module.exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
11
|
+
get: () => from[key],
|
|
12
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
module.exports = __toCommonJS(index_exports);
|
|
21
|
+
__reExport(index_exports, require("./useControllableState.native.js"), module.exports);
|
package/dist/cjs/index.native.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
8
|
var __copyProps = (to, from, except, desc) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
11
|
+
get: () => from[key],
|
|
12
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
15
16
|
};
|
|
16
17
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
17
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
18
|
-
value: true
|
|
19
|
-
}), mod);
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
19
|
var index_exports = {};
|
|
21
20
|
module.exports = __toCommonJS(index_exports);
|
|
22
21
|
__reExport(index_exports, require("./useControllableState.native.js"), module.exports);
|
|
22
|
+
|
|
23
23
|
//# sourceMappingURL=index.native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.native.js","names":[],"sources":["cjs/index.native.js"],"sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, \"default\"), secondTarget && __copyProps(secondTarget, mod, \"default\"));\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar index_exports = {};\nmodule.exports = __toCommonJS(index_exports);\n__reExport(index_exports, require(\"./useControllableState\"), module.exports);\n//# sourceMappingURL=index.js.map\n"],"mappings":";;;;AACA,IAAI,YAAY,OAAO;AACvB,IAAI,mBAAmB,OAAO;AAC9B,IAAI,oBAAoB,OAAO;AAC/B,IAAI,eAAe,OAAO,UAAU;AACpC,IAAI,eAAe,IAAI,MAAM,QAAQ,SAAS;CAC5C,IAAI,QAAQ,OAAO,SAAS,YAAY,OAAO,SAAS,YAAY;EAClE,KAAK,IAAI,OAAO,kBAAkB,IAAI,GACpC,IAAI,CAAC,aAAa,KAAK,IAAI,GAAG,KAAK,QAAQ,QACzC,UAAU,IAAI,KAAK;GAAE,WAAW,KAAK;GAAM,YAAY,EAAE,OAAO,iBAAiB,MAAM,GAAG,MAAM,KAAK;EAAW,CAAC;CACvH;CACA,OAAO;AACT;AACA,IAAI,cAAc,QAAQ,KAAK,kBAAkB,YAAY,QAAQ,KAAK,SAAS,GAAG,gBAAgB,YAAY,cAAc,KAAK,SAAS;AAC9I,IAAI,gBAAgB,QAAQ,YAAY,UAAU,CAAC,GAAG,cAAc,EAAE,OAAO,KAAK,CAAC,GAAG,GAAG;AACzF,IAAI,gBAAgB,CAAC;AACrB,OAAO,UAAU,aAAa,aAAa;AAC3C,WAAW,eAAe,QAAQ,kCAAwB,GAAG,OAAO,OAAO"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -5,80 +6,80 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
5
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
8
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
for (var name in all) __defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
12
13
|
};
|
|
13
14
|
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
17
|
+
get: () => from[key],
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
21
22
|
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
-
value: mod,
|
|
29
|
-
enumerable: true
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
30
26
|
}) : target, mod));
|
|
31
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
-
value: true
|
|
33
|
-
}), mod);
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
34
28
|
var useControllableState_exports = {};
|
|
35
|
-
__export(useControllableState_exports, {
|
|
36
|
-
useControllableState: () => useControllableState
|
|
37
|
-
});
|
|
29
|
+
__export(useControllableState_exports, { useControllableState: () => useControllableState });
|
|
38
30
|
module.exports = __toCommonJS(useControllableState_exports);
|
|
39
31
|
var import_use_event = require("@tamagui/use-event");
|
|
40
32
|
var React = __toESM(require("react"), 1);
|
|
41
33
|
var import_start_transition = require("@tamagui/start-transition");
|
|
42
|
-
const emptyCallbackFn = _ => _();
|
|
43
|
-
function useControllableState({
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
34
|
+
const emptyCallbackFn = (_) => _();
|
|
35
|
+
function useControllableState({ prop, defaultProp, onChange, strategy = "prop-wins", preventUpdate, transition }) {
|
|
36
|
+
const [state, setState] = React.useState(prop ?? defaultProp);
|
|
37
|
+
const previous = React.useRef(state);
|
|
38
|
+
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
39
|
+
const value = propWins ? prop : state;
|
|
40
|
+
const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
41
|
+
const transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;
|
|
42
|
+
React.useEffect(() => {
|
|
43
|
+
if (prop === void 0) return;
|
|
44
|
+
previous.current = prop;
|
|
45
|
+
transitionFn(() => {
|
|
46
|
+
setState(prop);
|
|
47
|
+
});
|
|
48
|
+
}, [prop]);
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
if (propWins) return;
|
|
51
|
+
if (state !== previous.current) {
|
|
52
|
+
previous.current = state;
|
|
53
|
+
onChangeCb(state);
|
|
54
|
+
}
|
|
55
|
+
}, [
|
|
56
|
+
onChangeCb,
|
|
57
|
+
state,
|
|
58
|
+
propWins
|
|
59
|
+
]);
|
|
60
|
+
const setter = (0, import_use_event.useEvent)((next, details) => {
|
|
61
|
+
if (preventUpdate) return;
|
|
62
|
+
if (details?.isCanceled) return;
|
|
63
|
+
if (details) {
|
|
64
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
65
|
+
onChangeCb(nextValue, details);
|
|
66
|
+
if (details.isCanceled) return;
|
|
67
|
+
if (propWins) return;
|
|
68
|
+
previous.current = nextValue;
|
|
69
|
+
transitionFn(() => {
|
|
70
|
+
setState(nextValue);
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (propWins) {
|
|
75
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
76
|
+
onChangeCb(nextValue);
|
|
77
|
+
} else {
|
|
78
|
+
transitionFn(() => {
|
|
79
|
+
setState(next);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return [value, setter];
|
|
83
84
|
}
|
|
84
|
-
const idFn = () => {};
|
|
85
|
+
const idFn = () => {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
+
get: () => from[key],
|
|
20
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: true
|
|
28
|
+
}) : target, mod));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var useControllableState_exports = {};
|
|
31
|
+
__export(useControllableState_exports, { useControllableState: () => useControllableState });
|
|
32
|
+
module.exports = __toCommonJS(useControllableState_exports);
|
|
33
|
+
var import_use_event = require("@tamagui/use-event");
|
|
34
|
+
var React = __toESM(require("react"), 1);
|
|
35
|
+
var import_start_transition = require("@tamagui/start-transition");
|
|
36
|
+
var emptyCallbackFn = function(_) {
|
|
37
|
+
return _();
|
|
38
|
+
};
|
|
39
|
+
function useControllableState(param) {
|
|
40
|
+
var { prop, defaultProp, onChange, strategy = "prop-wins", preventUpdate, transition } = param;
|
|
41
|
+
var [state, setState] = React.useState(prop !== null && prop !== void 0 ? prop : defaultProp);
|
|
42
|
+
var previous = React.useRef(state);
|
|
43
|
+
var propWins = strategy === "prop-wins" && prop !== void 0;
|
|
44
|
+
var value = propWins ? prop : state;
|
|
45
|
+
var onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
46
|
+
var transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;
|
|
47
|
+
React.useEffect(function() {
|
|
48
|
+
if (prop === void 0) return;
|
|
49
|
+
previous.current = prop;
|
|
50
|
+
transitionFn(function() {
|
|
51
|
+
setState(prop);
|
|
52
|
+
});
|
|
53
|
+
}, [prop]);
|
|
54
|
+
React.useEffect(function() {
|
|
55
|
+
if (propWins) return;
|
|
56
|
+
if (state !== previous.current) {
|
|
57
|
+
previous.current = state;
|
|
58
|
+
onChangeCb(state);
|
|
59
|
+
}
|
|
60
|
+
}, [
|
|
61
|
+
onChangeCb,
|
|
62
|
+
state,
|
|
63
|
+
propWins
|
|
64
|
+
]);
|
|
65
|
+
var setter = (0, import_use_event.useEvent)(function(next, details) {
|
|
66
|
+
if (preventUpdate) return;
|
|
67
|
+
if (details === null || details === void 0 ? void 0 : details.isCanceled) return;
|
|
68
|
+
if (details) {
|
|
69
|
+
var nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
70
|
+
onChangeCb(nextValue, details);
|
|
71
|
+
if (details.isCanceled) return;
|
|
72
|
+
if (propWins) return;
|
|
73
|
+
previous.current = nextValue;
|
|
74
|
+
transitionFn(function() {
|
|
75
|
+
setState(nextValue);
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (propWins) {
|
|
80
|
+
var nextValue1 = typeof next === "function" ? next(previous.current) : next;
|
|
81
|
+
onChangeCb(nextValue1);
|
|
82
|
+
} else {
|
|
83
|
+
transitionFn(function() {
|
|
84
|
+
setState(next);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return [value, setter];
|
|
89
|
+
}
|
|
90
|
+
var idFn = function() {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
|
|
3
4
|
var __create = Object.create;
|
|
4
5
|
var __defProp = Object.defineProperty;
|
|
5
6
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -7,84 +8,85 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
7
8
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
9
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
10
|
var __export = (target, all) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
14
15
|
};
|
|
15
16
|
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
+
get: () => from[key],
|
|
20
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
23
24
|
};
|
|
24
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
30
|
-
value: mod,
|
|
31
|
-
enumerable: true
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: true
|
|
32
28
|
}) : target, mod));
|
|
33
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
34
|
-
value: true
|
|
35
|
-
}), mod);
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
30
|
var useControllableState_exports = {};
|
|
37
|
-
__export(useControllableState_exports, {
|
|
38
|
-
useControllableState: () => useControllableState
|
|
39
|
-
});
|
|
31
|
+
__export(useControllableState_exports, { useControllableState: () => useControllableState });
|
|
40
32
|
module.exports = __toCommonJS(useControllableState_exports);
|
|
41
33
|
var import_use_event = require("@tamagui/use-event");
|
|
42
34
|
var React = __toESM(require("react"), 1);
|
|
43
35
|
var import_start_transition = require("@tamagui/start-transition");
|
|
44
|
-
var emptyCallbackFn = function
|
|
45
|
-
|
|
36
|
+
var emptyCallbackFn = function(_) {
|
|
37
|
+
return _();
|
|
46
38
|
};
|
|
47
39
|
function useControllableState(param) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
40
|
+
var { prop, defaultProp, onChange, strategy = "prop-wins", preventUpdate, transition } = param;
|
|
41
|
+
var [state, setState] = React.useState(prop !== null && prop !== void 0 ? prop : defaultProp);
|
|
42
|
+
var previous = React.useRef(state);
|
|
43
|
+
var propWins = strategy === "prop-wins" && prop !== void 0;
|
|
44
|
+
var value = propWins ? prop : state;
|
|
45
|
+
var onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
46
|
+
var transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;
|
|
47
|
+
React.useEffect(function() {
|
|
48
|
+
if (prop === void 0) return;
|
|
49
|
+
previous.current = prop;
|
|
50
|
+
transitionFn(function() {
|
|
51
|
+
setState(prop);
|
|
52
|
+
});
|
|
53
|
+
}, [prop]);
|
|
54
|
+
React.useEffect(function() {
|
|
55
|
+
if (propWins) return;
|
|
56
|
+
if (state !== previous.current) {
|
|
57
|
+
previous.current = state;
|
|
58
|
+
onChangeCb(state);
|
|
59
|
+
}
|
|
60
|
+
}, [
|
|
61
|
+
onChangeCb,
|
|
62
|
+
state,
|
|
63
|
+
propWins
|
|
64
|
+
]);
|
|
65
|
+
var setter = (0, import_use_event.useEvent)(function(next, details) {
|
|
66
|
+
if (preventUpdate) return;
|
|
67
|
+
if (details === null || details === void 0 ? void 0 : details.isCanceled) return;
|
|
68
|
+
if (details) {
|
|
69
|
+
var nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
70
|
+
onChangeCb(nextValue, details);
|
|
71
|
+
if (details.isCanceled) return;
|
|
72
|
+
if (propWins) return;
|
|
73
|
+
previous.current = nextValue;
|
|
74
|
+
transitionFn(function() {
|
|
75
|
+
setState(nextValue);
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (propWins) {
|
|
80
|
+
var nextValue1 = typeof next === "function" ? next(previous.current) : next;
|
|
81
|
+
onChangeCb(nextValue1);
|
|
82
|
+
} else {
|
|
83
|
+
transitionFn(function() {
|
|
84
|
+
setState(next);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return [value, setter];
|
|
88
89
|
}
|
|
89
|
-
var idFn = function
|
|
90
|
+
var idFn = function() {};
|
|
91
|
+
|
|
90
92
|
//# sourceMappingURL=useControllableState.native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"useControllableState.native.js","names":[],"sources":["cjs/useControllableState.native.js"],"sourcesContent":["\"use strict\";\nvar __create = Object.create;\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __getProtoOf = Object.getPrototypeOf;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n // If the importer is in node compatibility mode or this is not an ESM\n // file that has been converted to a CommonJS file using a Babel-\n // compatible transform (i.e. \"__esModule\" has not been set), then set\n // \"default\" to the CommonJS \"module.exports\" for node compatibility.\n isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n mod\n));\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar useControllableState_exports = {};\n__export(useControllableState_exports, {\n useControllableState: () => useControllableState\n});\nmodule.exports = __toCommonJS(useControllableState_exports);\nvar import_use_event = require(\"@tamagui/use-event\");\nvar React = __toESM(require(\"react\"), 1);\nvar import_start_transition = require(\"@tamagui/start-transition\");\nvar emptyCallbackFn = function(_) {\n return _();\n};\nfunction useControllableState(param) {\n var { prop, defaultProp, onChange, strategy = \"prop-wins\", preventUpdate, transition } = param;\n var [state, setState] = React.useState(prop !== null && prop !== void 0 ? prop : defaultProp);\n var previous = React.useRef(state);\n var propWins = strategy === \"prop-wins\" && prop !== void 0;\n var value = propWins ? prop : state;\n var onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);\n var transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;\n React.useEffect(function() {\n if (prop === void 0) return;\n previous.current = prop;\n transitionFn(function() {\n setState(prop);\n });\n }, [\n prop\n ]);\n React.useEffect(function() {\n if (propWins) return;\n if (state !== previous.current) {\n previous.current = state;\n onChangeCb(state);\n }\n }, [\n onChangeCb,\n state,\n propWins\n ]);\n var setter = (0, import_use_event.useEvent)(function(next, details) {\n if (preventUpdate) return;\n if (details === null || details === void 0 ? void 0 : details.isCanceled) return;\n if (details) {\n var nextValue = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue, details);\n if (details.isCanceled) return;\n if (propWins) return;\n previous.current = nextValue;\n transitionFn(function() {\n setState(nextValue);\n });\n return;\n }\n if (propWins) {\n var nextValue1 = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue1);\n } else {\n transitionFn(function() {\n setState(next);\n });\n }\n });\n return [\n value,\n setter\n ];\n}\nvar idFn = function() {\n};\n//# sourceMappingURL=useControllableState.js.map\n"],"mappings":"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"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./useControllableState.mjs"
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export * from "./useControllableState.mjs"
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./useControllableState.mjs"
|
|
2
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
export * from "./useControllableState.mjs"
|
package/dist/esm/index.native.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./useControllableState.native.js"
|
|
2
|
-
//# sourceMappingURL=index.native.js.map
|
|
1
|
+
export * from "./useControllableState.native.js"
|
|
@@ -1,48 +1,59 @@
|
|
|
1
1
|
import { useEvent } from "@tamagui/use-event";
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import { startTransition } from "@tamagui/start-transition";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
4
|
+
|
|
5
|
+
const emptyCallbackFn = (_) => _();
|
|
6
|
+
function useControllableState({ prop, defaultProp, onChange, strategy = "prop-wins", preventUpdate, transition }) {
|
|
7
|
+
const [state, setState] = React.useState(prop ?? defaultProp);
|
|
8
|
+
const previous = React.useRef(state);
|
|
9
|
+
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
10
|
+
const value = propWins ? prop : state;
|
|
11
|
+
const onChangeCb = useEvent(onChange || idFn);
|
|
12
|
+
const transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
if (prop === void 0) return;
|
|
15
|
+
previous.current = prop;
|
|
16
|
+
transitionFn(() => {
|
|
17
|
+
setState(prop);
|
|
18
|
+
});
|
|
19
|
+
}, [prop]);
|
|
20
|
+
React.useEffect(() => {
|
|
21
|
+
if (propWins) return;
|
|
22
|
+
if (state !== previous.current) {
|
|
23
|
+
previous.current = state;
|
|
24
|
+
onChangeCb(state);
|
|
25
|
+
}
|
|
26
|
+
}, [
|
|
27
|
+
onChangeCb,
|
|
28
|
+
state,
|
|
29
|
+
propWins
|
|
30
|
+
]);
|
|
31
|
+
const setter = useEvent((next, details) => {
|
|
32
|
+
if (preventUpdate) return;
|
|
33
|
+
if (details?.isCanceled) return;
|
|
34
|
+
if (details) {
|
|
35
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
36
|
+
onChangeCb(nextValue, details);
|
|
37
|
+
if (details.isCanceled) return;
|
|
38
|
+
if (propWins) return;
|
|
39
|
+
previous.current = nextValue;
|
|
40
|
+
transitionFn(() => {
|
|
41
|
+
setState(nextValue);
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (propWins) {
|
|
46
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
47
|
+
onChangeCb(nextValue);
|
|
48
|
+
} else {
|
|
49
|
+
transitionFn(() => {
|
|
50
|
+
setState(next);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return [value, setter];
|
|
45
55
|
}
|
|
46
56
|
const idFn = () => {};
|
|
57
|
+
|
|
47
58
|
export { useControllableState };
|
|
48
59
|
//# sourceMappingURL=useControllableState.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"useControllableState.js","names":[],"sources":["esm/useControllableState.js"],"sourcesContent":["import { useEvent } from \"@tamagui/use-event\";\nimport * as React from \"react\";\nimport { startTransition } from \"@tamagui/start-transition\";\nconst emptyCallbackFn = (_) => _();\nfunction useControllableState({\n prop,\n defaultProp,\n onChange,\n strategy = \"prop-wins\",\n preventUpdate,\n transition\n}) {\n const [state, setState] = React.useState(prop ?? defaultProp);\n const previous = React.useRef(state);\n const propWins = strategy === \"prop-wins\" && prop !== void 0;\n const value = propWins ? prop : state;\n const onChangeCb = useEvent(onChange || idFn);\n const transitionFn = transition ? startTransition : emptyCallbackFn;\n React.useEffect(() => {\n if (prop === void 0) return;\n previous.current = prop;\n transitionFn(() => {\n setState(prop);\n });\n }, [prop]);\n React.useEffect(() => {\n if (propWins) return;\n if (state !== previous.current) {\n previous.current = state;\n onChangeCb(state);\n }\n }, [onChangeCb, state, propWins]);\n const setter = useEvent((next, details) => {\n if (preventUpdate) return;\n if (details?.isCanceled) return;\n if (details) {\n const nextValue = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue, details);\n if (details.isCanceled) return;\n if (propWins) return;\n previous.current = nextValue;\n transitionFn(() => {\n setState(nextValue);\n });\n return;\n }\n if (propWins) {\n const nextValue = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue);\n } else {\n transitionFn(() => {\n setState(next);\n });\n }\n });\n return [value, setter];\n}\nconst idFn = () => {\n};\nexport {\n useControllableState\n};\n//# sourceMappingURL=useControllableState.js.map\n"],"mappings":";;;;;AAGA,MAAM,mBAAmB,MAAM,EAAE;AACjC,SAAS,qBAAqB,EAC5B,MACA,aACA,UACA,WAAW,aACX,eACA,cACC;CACD,MAAM,CAAC,OAAO,YAAY,MAAM,SAAS,QAAQ,WAAW;CAC5D,MAAM,WAAW,MAAM,OAAO,KAAK;CACnC,MAAM,WAAW,aAAa,eAAe,SAAS,KAAK;CAC3D,MAAM,QAAQ,WAAW,OAAO;CAChC,MAAM,aAAa,SAAS,YAAY,IAAI;CAC5C,MAAM,eAAe,aAAa,kBAAkB;CACpD,MAAM,gBAAgB;EACpB,IAAI,SAAS,KAAK,GAAG;EACrB,SAAS,UAAU;EACnB,mBAAmB;GACjB,SAAS,IAAI;EACf,CAAC;CACH,GAAG,CAAC,IAAI,CAAC;CACT,MAAM,gBAAgB;EACpB,IAAI,UAAU;EACd,IAAI,UAAU,SAAS,SAAS;GAC9B,SAAS,UAAU;GACnB,WAAW,KAAK;EAClB;CACF,GAAG;EAAC;EAAY;EAAO;CAAQ,CAAC;CAChC,MAAM,SAAS,UAAU,MAAM,YAAY;EACzC,IAAI,eAAe;EACnB,IAAI,SAAS,YAAY;EACzB,IAAI,SAAS;GACX,MAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;GACxE,WAAW,WAAW,OAAO;GAC7B,IAAI,QAAQ,YAAY;GACxB,IAAI,UAAU;GACd,SAAS,UAAU;GACnB,mBAAmB;IACjB,SAAS,SAAS;GACpB,CAAC;GACD;EACF;EACA,IAAI,UAAU;GACZ,MAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;GACxE,WAAW,SAAS;EACtB,OAAO;GACL,mBAAmB;IACjB,SAAS,IAAI;GACf,CAAC;EACH;CACF,CAAC;CACD,OAAO,CAAC,OAAO,MAAM;AACvB;AACA,MAAM,aAAa,CACnB"}
|
|
@@ -1,51 +1,62 @@
|
|
|
1
1
|
import { useEvent } from "@tamagui/use-event";
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import { startTransition } from "@tamagui/start-transition";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
var emptyCallbackFn = function(_) {
|
|
6
|
+
return _();
|
|
6
7
|
};
|
|
7
8
|
function useControllableState(param) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
9
|
+
var { prop, defaultProp, onChange, strategy = "prop-wins", preventUpdate, transition } = param;
|
|
10
|
+
var [state, setState] = React.useState(prop !== null && prop !== void 0 ? prop : defaultProp);
|
|
11
|
+
var previous = React.useRef(state);
|
|
12
|
+
var propWins = strategy === "prop-wins" && prop !== void 0;
|
|
13
|
+
var value = propWins ? prop : state;
|
|
14
|
+
var onChangeCb = useEvent(onChange || idFn);
|
|
15
|
+
var transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
16
|
+
React.useEffect(function() {
|
|
17
|
+
if (prop === void 0) return;
|
|
18
|
+
previous.current = prop;
|
|
19
|
+
transitionFn(function() {
|
|
20
|
+
setState(prop);
|
|
21
|
+
});
|
|
22
|
+
}, [prop]);
|
|
23
|
+
React.useEffect(function() {
|
|
24
|
+
if (propWins) return;
|
|
25
|
+
if (state !== previous.current) {
|
|
26
|
+
previous.current = state;
|
|
27
|
+
onChangeCb(state);
|
|
28
|
+
}
|
|
29
|
+
}, [
|
|
30
|
+
onChangeCb,
|
|
31
|
+
state,
|
|
32
|
+
propWins
|
|
33
|
+
]);
|
|
34
|
+
var setter = useEvent(function(next, details) {
|
|
35
|
+
if (preventUpdate) return;
|
|
36
|
+
if (details === null || details === void 0 ? void 0 : details.isCanceled) return;
|
|
37
|
+
if (details) {
|
|
38
|
+
var nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
39
|
+
onChangeCb(nextValue, details);
|
|
40
|
+
if (details.isCanceled) return;
|
|
41
|
+
if (propWins) return;
|
|
42
|
+
previous.current = nextValue;
|
|
43
|
+
transitionFn(function() {
|
|
44
|
+
setState(nextValue);
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (propWins) {
|
|
49
|
+
var nextValue1 = typeof next === "function" ? next(previous.current) : next;
|
|
50
|
+
onChangeCb(nextValue1);
|
|
51
|
+
} else {
|
|
52
|
+
transitionFn(function() {
|
|
53
|
+
setState(next);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return [value, setter];
|
|
48
58
|
}
|
|
49
|
-
var idFn = function
|
|
59
|
+
var idFn = function() {};
|
|
60
|
+
|
|
50
61
|
export { useControllableState };
|
|
51
62
|
//# sourceMappingURL=useControllableState.native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"useControllableState.native.js","names":[],"sources":["esm/useControllableState.native.js"],"sourcesContent":["import { useEvent } from \"@tamagui/use-event\";\nimport * as React from \"react\";\nimport { startTransition } from \"@tamagui/start-transition\";\nvar emptyCallbackFn = function(_) {\n return _();\n};\nfunction useControllableState(param) {\n var { prop, defaultProp, onChange, strategy = \"prop-wins\", preventUpdate, transition } = param;\n var [state, setState] = React.useState(prop !== null && prop !== void 0 ? prop : defaultProp);\n var previous = React.useRef(state);\n var propWins = strategy === \"prop-wins\" && prop !== void 0;\n var value = propWins ? prop : state;\n var onChangeCb = useEvent(onChange || idFn);\n var transitionFn = transition ? startTransition : emptyCallbackFn;\n React.useEffect(function() {\n if (prop === void 0) return;\n previous.current = prop;\n transitionFn(function() {\n setState(prop);\n });\n }, [\n prop\n ]);\n React.useEffect(function() {\n if (propWins) return;\n if (state !== previous.current) {\n previous.current = state;\n onChangeCb(state);\n }\n }, [\n onChangeCb,\n state,\n propWins\n ]);\n var setter = useEvent(function(next, details) {\n if (preventUpdate) return;\n if (details === null || details === void 0 ? void 0 : details.isCanceled) return;\n if (details) {\n var nextValue = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue, details);\n if (details.isCanceled) return;\n if (propWins) return;\n previous.current = nextValue;\n transitionFn(function() {\n setState(nextValue);\n });\n return;\n }\n if (propWins) {\n var nextValue1 = typeof next === \"function\" ? next(previous.current) : next;\n onChangeCb(nextValue1);\n } else {\n transitionFn(function() {\n setState(next);\n });\n }\n });\n return [\n value,\n setter\n ];\n}\nvar idFn = function() {\n};\nexport {\n useControllableState\n};\n//# sourceMappingURL=useControllableState.js.map\n"],"mappings":";;;;;AAGA,IAAI,kBAAkB,SAAS,GAAG;CAChC,OAAO,EAAE;AACX;AACA,SAAS,qBAAqB,OAAO;CACnC,IAAI,EAAE,MAAM,aAAa,UAAU,WAAW,aAAa,eAAe,eAAe;CACzF,IAAI,CAAC,OAAO,YAAY,MAAM,SAAS,SAAS,QAAQ,SAAS,KAAK,IAAI,OAAO,WAAW;CAC5F,IAAI,WAAW,MAAM,OAAO,KAAK;CACjC,IAAI,WAAW,aAAa,eAAe,SAAS,KAAK;CACzD,IAAI,QAAQ,WAAW,OAAO;CAC9B,IAAI,aAAa,SAAS,YAAY,IAAI;CAC1C,IAAI,eAAe,aAAa,kBAAkB;CAClD,MAAM,UAAU,WAAW;EACzB,IAAI,SAAS,KAAK,GAAG;EACrB,SAAS,UAAU;EACnB,aAAa,WAAW;GACtB,SAAS,IAAI;EACf,CAAC;CACH,GAAG,CACD,IACF,CAAC;CACD,MAAM,UAAU,WAAW;EACzB,IAAI,UAAU;EACd,IAAI,UAAU,SAAS,SAAS;GAC9B,SAAS,UAAU;GACnB,WAAW,KAAK;EAClB;CACF,GAAG;EACD;EACA;EACA;CACF,CAAC;CACD,IAAI,SAAS,SAAS,SAAS,MAAM,SAAS;EAC5C,IAAI,eAAe;EACnB,IAAI,YAAY,QAAQ,YAAY,KAAK,IAAI,KAAK,IAAI,QAAQ,YAAY;EAC1E,IAAI,SAAS;GACX,IAAI,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;GACtE,WAAW,WAAW,OAAO;GAC7B,IAAI,QAAQ,YAAY;GACxB,IAAI,UAAU;GACd,SAAS,UAAU;GACnB,aAAa,WAAW;IACtB,SAAS,SAAS;GACpB,CAAC;GACD;EACF;EACA,IAAI,UAAU;GACZ,IAAI,aAAa,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;GACvE,WAAW,UAAU;EACvB,OAAO;GACL,aAAa,WAAW;IACtB,SAAS,IAAI;GACf,CAAC;EACH;CACF,CAAC;CACD,OAAO,CACL,OACA,MACF;AACF;AACA,IAAI,OAAO,WAAW,CACtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-controllable-state",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.643.1",
|
|
4
4
|
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"files": [
|
|
@@ -35,11 +35,12 @@
|
|
|
35
35
|
"clean:build": "tamagui-build clean:build"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@tamagui/
|
|
39
|
-
"@tamagui/
|
|
38
|
+
"@tamagui/core": "3.0.0-beta.643.1",
|
|
39
|
+
"@tamagui/start-transition": "3.0.0-beta.643.1",
|
|
40
|
+
"@tamagui/use-event": "3.0.0-beta.643.1"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@tamagui/build": "
|
|
43
|
+
"@tamagui/build": "3.0.0-beta.643.1",
|
|
43
44
|
"react": ">=19"
|
|
44
45
|
},
|
|
45
46
|
"peerDependencies": {
|
|
@@ -1,15 +1,30 @@
|
|
|
1
1
|
import { useEvent } from '@tamagui/use-event'
|
|
2
2
|
import * as React from 'react'
|
|
3
3
|
import { startTransition } from '@tamagui/start-transition'
|
|
4
|
+
import type { TamaguiChangeEventDetails } from '@tamagui/core'
|
|
4
5
|
|
|
5
6
|
// can configure to allow most-recent-wins or prop-wins
|
|
6
7
|
// defaults to prop-wins
|
|
7
8
|
|
|
8
|
-
type ChangeCb<T> = (
|
|
9
|
+
type ChangeCb<T, Details extends TamaguiChangeEventDetails> = (
|
|
10
|
+
next: T,
|
|
11
|
+
details?: Details
|
|
12
|
+
) => void
|
|
13
|
+
|
|
14
|
+
export type ControllableStateSetter<
|
|
15
|
+
T,
|
|
16
|
+
Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,
|
|
17
|
+
> = {
|
|
18
|
+
(next: React.SetStateAction<T>): void
|
|
19
|
+
(next: React.SetStateAction<T>, details: Details): void
|
|
20
|
+
}
|
|
9
21
|
|
|
10
22
|
const emptyCallbackFn = (_) => _()
|
|
11
23
|
|
|
12
|
-
export function useControllableState<
|
|
24
|
+
export function useControllableState<
|
|
25
|
+
T,
|
|
26
|
+
Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,
|
|
27
|
+
>({
|
|
13
28
|
prop,
|
|
14
29
|
defaultProp,
|
|
15
30
|
onChange,
|
|
@@ -19,11 +34,11 @@ export function useControllableState<T>({
|
|
|
19
34
|
}: {
|
|
20
35
|
prop?: T | undefined
|
|
21
36
|
defaultProp: T
|
|
22
|
-
onChange?: ChangeCb<T>
|
|
37
|
+
onChange?: ChangeCb<T, Details>
|
|
23
38
|
strategy?: 'prop-wins' | 'most-recent-wins'
|
|
24
39
|
preventUpdate?: boolean
|
|
25
40
|
transition?: boolean
|
|
26
|
-
}): [T,
|
|
41
|
+
}): [T, ControllableStateSetter<T, Details>] {
|
|
27
42
|
const [state, setState] = React.useState(prop ?? defaultProp)
|
|
28
43
|
const previous = React.useRef<any>(state)
|
|
29
44
|
const propWins = strategy === 'prop-wins' && prop !== undefined
|
|
@@ -48,8 +63,24 @@ export function useControllableState<T>({
|
|
|
48
63
|
}
|
|
49
64
|
}, [onChangeCb, state, propWins])
|
|
50
65
|
|
|
51
|
-
const setter = useEvent((next) => {
|
|
66
|
+
const setter = useEvent((next, details?: Details) => {
|
|
52
67
|
if (preventUpdate) return
|
|
68
|
+
if (details?.isCanceled) return
|
|
69
|
+
|
|
70
|
+
if (details) {
|
|
71
|
+
const nextValue = typeof next === 'function' ? next(previous.current) : next
|
|
72
|
+
onChangeCb(nextValue, details)
|
|
73
|
+
if (details.isCanceled) return
|
|
74
|
+
|
|
75
|
+
if (propWins) return
|
|
76
|
+
|
|
77
|
+
previous.current = nextValue
|
|
78
|
+
transitionFn(() => {
|
|
79
|
+
setState(nextValue)
|
|
80
|
+
})
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
53
84
|
if (propWins) {
|
|
54
85
|
const nextValue = typeof next === 'function' ? next(previous.current) : next
|
|
55
86
|
onChangeCb(nextValue)
|
|
@@ -60,7 +91,7 @@ export function useControllableState<T>({
|
|
|
60
91
|
}
|
|
61
92
|
})
|
|
62
93
|
|
|
63
|
-
return [value as T, setter]
|
|
94
|
+
return [value as T, setter as ControllableStateSetter<T, Details>]
|
|
64
95
|
}
|
|
65
96
|
|
|
66
97
|
const idFn = () => {}
|
package/types/index.d.ts
CHANGED
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAAA,cAAc",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/index.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"export * from './useControllableState'\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -1,12 +1,27 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { TamaguiChangeEventDetails } from "@tamagui/core";
|
|
3
|
+
type ChangeCb<
|
|
4
|
+
T,
|
|
5
|
+
Details extends TamaguiChangeEventDetails
|
|
6
|
+
> = (next: T, details?: Details) => void;
|
|
7
|
+
export type ControllableStateSetter<
|
|
8
|
+
T,
|
|
9
|
+
Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails
|
|
10
|
+
> = {
|
|
11
|
+
(next: React.SetStateAction<T>): void;
|
|
12
|
+
(next: React.SetStateAction<T>, details: Details): void;
|
|
13
|
+
};
|
|
14
|
+
export declare function useControllableState<
|
|
15
|
+
T,
|
|
16
|
+
Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails
|
|
17
|
+
>({ prop, defaultProp, onChange, strategy, preventUpdate, transition }: {
|
|
18
|
+
prop?: T | undefined;
|
|
19
|
+
defaultProp: T;
|
|
20
|
+
onChange?: ChangeCb<T, Details>;
|
|
21
|
+
strategy?: "prop-wins" | "most-recent-wins";
|
|
22
|
+
preventUpdate?: boolean;
|
|
23
|
+
transition?: boolean;
|
|
24
|
+
}): [T, ControllableStateSetter<T, Details>];
|
|
11
25
|
export {};
|
|
26
|
+
|
|
12
27
|
//# sourceMappingURL=useControllableState.d.ts.map
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AACA,YAAY,WAAW;AAEvB,cAAc,iCAAiC;KAK1C;CAAS;CAAG,gBAAgB;KAC/B,MAAM,GACN,UAAU;AAGZ,YAAY;CACV;CACA,gBAAgB,4BAA4B;IAC1C;EACD,MAAM,MAAM,eAAe;EAC3B,MAAM,MAAM,eAAe,IAAI,SAAS;;AAK3C,OAAO,iBAAS;CACd;CACA,gBAAgB,4BAA4B;EAC5C,EACA,MACA,aACA,UACA,UACA,eACA,cACC;CACD,OAAO;CACP,aAAa;CACb,WAAW,SAAS,GAAG;CACvB,WAAW,cAAc;CACzB;CACA;KACG,GAAG,wBAAwB,GAAG",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/useControllableState.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"import { useEvent } from '@tamagui/use-event'\nimport * as React from 'react'\nimport { startTransition } from '@tamagui/start-transition'\nimport type { TamaguiChangeEventDetails } from '@tamagui/core'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T, Details extends TamaguiChangeEventDetails> = (\n next: T,\n details?: Details\n) => void\n\nexport type ControllableStateSetter<\n T,\n Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,\n> = {\n (next: React.SetStateAction<T>): void\n (next: React.SetStateAction<T>, details: Details): void\n}\n\nconst emptyCallbackFn = (_) => _()\n\nexport function useControllableState<\n T,\n Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,\n>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n transition,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T, Details>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n transition?: boolean\n}): [T, ControllableStateSetter<T, Details>] {\n const [state, setState] = React.useState(prop ?? defaultProp)\n const previous = React.useRef<any>(state)\n const propWins = strategy === 'prop-wins' && prop !== undefined\n const value = propWins ? prop : state\n const onChangeCb = useEvent(onChange || idFn)\n\n const transitionFn = transition ? startTransition : emptyCallbackFn\n\n React.useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n transitionFn(() => {\n setState(prop)\n })\n }, [prop])\n\n React.useEffect(() => {\n if (propWins) return\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state, propWins])\n\n const setter = useEvent((next, details?: Details) => {\n if (preventUpdate) return\n if (details?.isCanceled) return\n\n if (details) {\n const nextValue = typeof next === 'function' ? next(previous.current) : next\n onChangeCb(nextValue, details)\n if (details.isCanceled) return\n\n if (propWins) return\n\n previous.current = nextValue\n transitionFn(() => {\n setState(nextValue)\n })\n return\n }\n\n if (propWins) {\n const nextValue = typeof next === 'function' ? next(previous.current) : next\n onChangeCb(nextValue)\n } else {\n transitionFn(() => {\n setState(next)\n })\n }\n })\n\n return [value as T, setter as ControllableStateSetter<T, Details>]\n}\n\nconst idFn = () => {}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|
package/dist/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|
package/dist/esm/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|