@tamagui/use-controllable-state 1.13.2 → 1.13.4
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.js +1 -18
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/useControllableState.js +1 -76
- package/dist/cjs/useControllableState.js.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/useControllableState.js +1 -52
- package/dist/esm/useControllableState.js.map +2 -2
- package/dist/esm/useControllableState.mjs +1 -52
- package/dist/esm/useControllableState.mjs.map +2 -2
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -1,19 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
var src_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(src_exports);
|
|
18
|
-
__reExport(src_exports, require("./useControllableState"), module.exports);
|
|
1
|
+
"use strict";var a=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(r,o,f,x)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of c(o))!d.call(r,e)&&e!==f&&a(r,e,{get:()=>o[e],enumerable:!(x=b(o,e))||x.enumerable});return r},t=(r,o,f)=>(p(r,o,"default"),f&&p(f,o,"default"));var g=r=>p(a({},"__esModule",{value:!0}),r);var m={};module.exports=g(m);t(m,require("./useControllableState"),module.exports);
|
|
19
2
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
4
|
"sourcesContent": ["export * from './useControllableState'\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "iaAAA,IAAAA,EAAA,kBAAAC,EAAAD,GAAAE,EAAAF,EAAc,kCAAd",
|
|
6
|
+
"names": ["src_exports", "__toCommonJS", "__reExport"]
|
|
7
7
|
}
|
|
@@ -1,77 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var useControllableState_exports = {};
|
|
20
|
-
__export(useControllableState_exports, {
|
|
21
|
-
useControllableState: () => useControllableState
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(useControllableState_exports);
|
|
24
|
-
var import_use_event = require("@tamagui/use-event");
|
|
25
|
-
var import_react = require("react");
|
|
26
|
-
const emptyCallbackFn = (_) => _();
|
|
27
|
-
function useControllableState({
|
|
28
|
-
prop,
|
|
29
|
-
defaultProp,
|
|
30
|
-
onChange,
|
|
31
|
-
strategy = "prop-wins",
|
|
32
|
-
preventUpdate,
|
|
33
|
-
transition
|
|
34
|
-
}) {
|
|
35
|
-
const [state, setState] = (0, import_react.useState)(prop ?? defaultProp);
|
|
36
|
-
const previous = (0, import_react.useRef)(state);
|
|
37
|
-
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
38
|
-
const value = propWins ? prop : state;
|
|
39
|
-
const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
40
|
-
const transitionFn = transition ? import_react.startTransition : emptyCallbackFn;
|
|
41
|
-
(0, import_react.useEffect)(() => {
|
|
42
|
-
if (prop === void 0)
|
|
43
|
-
return;
|
|
44
|
-
previous.current = prop;
|
|
45
|
-
transitionFn(() => {
|
|
46
|
-
setState(prop);
|
|
47
|
-
});
|
|
48
|
-
}, [prop]);
|
|
49
|
-
(0, import_react.useEffect)(() => {
|
|
50
|
-
if (propWins)
|
|
51
|
-
return;
|
|
52
|
-
if (state !== previous.current) {
|
|
53
|
-
previous.current = state;
|
|
54
|
-
onChangeCb(state);
|
|
55
|
-
}
|
|
56
|
-
}, [onChangeCb, state, propWins]);
|
|
57
|
-
const setter = (0, import_use_event.useEvent)((next) => {
|
|
58
|
-
if (preventUpdate)
|
|
59
|
-
return;
|
|
60
|
-
if (propWins) {
|
|
61
|
-
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
62
|
-
onChangeCb(nextValue);
|
|
63
|
-
} else {
|
|
64
|
-
transitionFn(() => {
|
|
65
|
-
setState(next);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
return [value, setter];
|
|
70
|
-
}
|
|
71
|
-
const idFn = () => {
|
|
72
|
-
};
|
|
73
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
-
0 && (module.exports = {
|
|
75
|
-
useControllableState
|
|
76
|
-
});
|
|
1
|
+
"use strict";var l=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var v=(t,e)=>{for(var s in e)l(t,s,{get:e[s],enumerable:!0})},y=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of R(e))!g.call(t,o)&&o!==s&&l(t,o,{get:()=>e[o],enumerable:!(r=m(e,o))||r.enumerable});return t};var w=t=>y(l({},"__esModule",{value:!0}),t);var E={};v(E,{useControllableState:()=>A});module.exports=w(E);var T=require("@tamagui/use-event"),n=require("react");const F=t=>t();function A({prop:t,defaultProp:e,onChange:s,strategy:r="prop-wins",preventUpdate:o,transition:C}){const[a,d]=(0,n.useState)(t??e),i=(0,n.useRef)(a),c=r==="prop-wins"&&t!==void 0,b=c?t:a,f=(0,T.useEvent)(s||D),p=C?n.startTransition:F;(0,n.useEffect)(()=>{t!==void 0&&(i.current=t,p(()=>{d(t)}))},[t]),(0,n.useEffect)(()=>{c||a!==i.current&&(i.current=a,f(a))},[f,a,c]);const S=(0,T.useEvent)(u=>{if(!o)if(c){const h=typeof u=="function"?u(i.current):u;f(h)}else p(()=>{d(u)})});return[b,S]}const D=()=>{};0&&(module.exports={useControllableState});
|
|
77
2
|
//# sourceMappingURL=useControllableState.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
4
|
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { startTransition, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nconst emptyCallbackFn = (_) => _()\n\nexport function useControllableState<T>({\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>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n transition?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n transitionFn(() => {\n setState(prop)\n })\n }, [prop])\n\n 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) => {\n if (preventUpdate) return\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]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,0BAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAyB,8BACzBC,EAAoE,iBAOpE,MAAMC,EAAmBC,GAAMA,EAAE,EAE1B,SAASL,EAAwB,CACtC,KAAAM,EACA,YAAAC,EACA,SAAAC,EACA,SAAAC,EAAW,YACX,cAAAC,EACA,WAAAC,CACF,EAOiD,CAC/C,KAAM,CAACC,EAAOC,CAAQ,KAAI,YAASP,GAAQC,CAAW,EAChDO,KAAW,UAAYF,CAAK,EAC5BG,EAAWN,IAAa,aAAeH,IAAS,OAChDU,EAAQD,EAAWT,EAAOM,EAC1BK,KAAa,YAAST,GAAYU,CAAI,EAEtCC,EAAeR,EAAa,kBAAkBP,KAEpD,aAAU,IAAM,CACVE,IAAS,SACbQ,EAAS,QAAUR,EACnBa,EAAa,IAAM,CACjBN,EAASP,CAAI,CACf,CAAC,EACH,EAAG,CAACA,CAAI,CAAC,KAET,aAAU,IAAM,CACVS,GACAH,IAAUE,EAAS,UACrBA,EAAS,QAAUF,EACnBK,EAAWL,CAAK,EAEpB,EAAG,CAACK,EAAYL,EAAOG,CAAQ,CAAC,EAEhC,MAAMK,KAAS,YAAUC,GAAS,CAChC,GAAI,CAAAX,EACJ,GAAIK,EAAU,CACZ,MAAMO,EAAY,OAAOD,GAAS,WAAaA,EAAKP,EAAS,OAAO,EAAIO,EACxEJ,EAAWK,CAAS,CACtB,MACEH,EAAa,IAAM,CACjBN,EAASQ,CAAI,CACf,CAAC,CAEL,CAAC,EAED,MAAO,CAACL,EAAYI,CAAM,CAC5B,CAEA,MAAMF,EAAO,IAAM,CAAC",
|
|
6
|
+
"names": ["useControllableState_exports", "__export", "useControllableState", "__toCommonJS", "import_use_event", "import_react", "emptyCallbackFn", "_", "prop", "defaultProp", "onChange", "strategy", "preventUpdate", "transition", "state", "setState", "previous", "propWins", "value", "onChangeCb", "idFn", "transitionFn", "setter", "next", "nextValue"]
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export*from"./useControllableState";
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export*from"./useControllableState";
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1,53 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { startTransition, useEffect, useRef, useState } from "react";
|
|
3
|
-
const emptyCallbackFn = (_) => _();
|
|
4
|
-
function useControllableState({
|
|
5
|
-
prop,
|
|
6
|
-
defaultProp,
|
|
7
|
-
onChange,
|
|
8
|
-
strategy = "prop-wins",
|
|
9
|
-
preventUpdate,
|
|
10
|
-
transition
|
|
11
|
-
}) {
|
|
12
|
-
const [state, setState] = useState(prop ?? defaultProp);
|
|
13
|
-
const previous = useRef(state);
|
|
14
|
-
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
15
|
-
const value = propWins ? prop : state;
|
|
16
|
-
const onChangeCb = useEvent(onChange || idFn);
|
|
17
|
-
const transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
if (prop === void 0)
|
|
20
|
-
return;
|
|
21
|
-
previous.current = prop;
|
|
22
|
-
transitionFn(() => {
|
|
23
|
-
setState(prop);
|
|
24
|
-
});
|
|
25
|
-
}, [prop]);
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (propWins)
|
|
28
|
-
return;
|
|
29
|
-
if (state !== previous.current) {
|
|
30
|
-
previous.current = state;
|
|
31
|
-
onChangeCb(state);
|
|
32
|
-
}
|
|
33
|
-
}, [onChangeCb, state, propWins]);
|
|
34
|
-
const setter = useEvent((next) => {
|
|
35
|
-
if (preventUpdate)
|
|
36
|
-
return;
|
|
37
|
-
if (propWins) {
|
|
38
|
-
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
39
|
-
onChangeCb(nextValue);
|
|
40
|
-
} else {
|
|
41
|
-
transitionFn(() => {
|
|
42
|
-
setState(next);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
return [value, setter];
|
|
47
|
-
}
|
|
48
|
-
const idFn = () => {
|
|
49
|
-
};
|
|
50
|
-
export {
|
|
51
|
-
useControllableState
|
|
52
|
-
};
|
|
1
|
+
import{useEvent as c}from"@tamagui/use-event";import{startTransition as h,useEffect as u,useRef as m,useState as R}from"react";const g=t=>t();function A({prop:t,defaultProp:f,onChange:l,strategy:T="prop-wins",preventUpdate:d,transition:p}){const[e,r]=R(t??f),n=m(e),o=T==="prop-wins"&&t!==void 0,C=o?t:e,a=c(l||v),i=p?h:g;u(()=>{t!==void 0&&(n.current=t,i(()=>{r(t)}))},[t]),u(()=>{o||e!==n.current&&(n.current=e,a(e))},[a,e,o]);const b=c(s=>{if(!d)if(o){const S=typeof s=="function"?s(n.current):s;a(S)}else i(()=>{r(s)})});return[C,b]}const v=()=>{};export{A as useControllableState};
|
|
53
2
|
//# sourceMappingURL=useControllableState.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
4
|
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { startTransition, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nconst emptyCallbackFn = (_) => _()\n\nexport function useControllableState<T>({\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>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n transition?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n transitionFn(() => {\n setState(prop)\n })\n }, [prop])\n\n 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) => {\n if (preventUpdate) return\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]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "AAAA,OAAS,YAAAA,MAAgB,qBACzB,OAAgB,mBAAAC,EAAiB,aAAAC,EAAW,UAAAC,EAAQ,YAAAC,MAAgB,QAOpE,MAAMC,EAAmBC,GAAMA,EAAE,EAE1B,SAASC,EAAwB,CACtC,KAAAC,EACA,YAAAC,EACA,SAAAC,EACA,SAAAC,EAAW,YACX,cAAAC,EACA,WAAAC,CACF,EAOiD,CAC/C,KAAM,CAACC,EAAOC,CAAQ,EAAIX,EAASI,GAAQC,CAAW,EAChDO,EAAWb,EAAYW,CAAK,EAC5BG,EAAWN,IAAa,aAAeH,IAAS,OAChDU,EAAQD,EAAWT,EAAOM,EAC1BK,EAAanB,EAASU,GAAYU,CAAI,EAEtCC,EAAeR,EAAaZ,EAAkBI,EAEpDH,EAAU,IAAM,CACVM,IAAS,SACbQ,EAAS,QAAUR,EACnBa,EAAa,IAAM,CACjBN,EAASP,CAAI,CACf,CAAC,EACH,EAAG,CAACA,CAAI,CAAC,EAETN,EAAU,IAAM,CACVe,GACAH,IAAUE,EAAS,UACrBA,EAAS,QAAUF,EACnBK,EAAWL,CAAK,EAEpB,EAAG,CAACK,EAAYL,EAAOG,CAAQ,CAAC,EAEhC,MAAMK,EAAStB,EAAUuB,GAAS,CAChC,GAAI,CAAAX,EACJ,GAAIK,EAAU,CACZ,MAAMO,EAAY,OAAOD,GAAS,WAAaA,EAAKP,EAAS,OAAO,EAAIO,EACxEJ,EAAWK,CAAS,CACtB,MACEH,EAAa,IAAM,CACjBN,EAASQ,CAAI,CACf,CAAC,CAEL,CAAC,EAED,MAAO,CAACL,EAAYI,CAAM,CAC5B,CAEA,MAAMF,EAAO,IAAM,CAAC",
|
|
6
|
+
"names": ["useEvent", "startTransition", "useEffect", "useRef", "useState", "emptyCallbackFn", "_", "useControllableState", "prop", "defaultProp", "onChange", "strategy", "preventUpdate", "transition", "state", "setState", "previous", "propWins", "value", "onChangeCb", "idFn", "transitionFn", "setter", "next", "nextValue"]
|
|
7
7
|
}
|
|
@@ -1,53 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { startTransition, useEffect, useRef, useState } from "react";
|
|
3
|
-
const emptyCallbackFn = (_) => _();
|
|
4
|
-
function useControllableState({
|
|
5
|
-
prop,
|
|
6
|
-
defaultProp,
|
|
7
|
-
onChange,
|
|
8
|
-
strategy = "prop-wins",
|
|
9
|
-
preventUpdate,
|
|
10
|
-
transition
|
|
11
|
-
}) {
|
|
12
|
-
const [state, setState] = useState(prop ?? defaultProp);
|
|
13
|
-
const previous = useRef(state);
|
|
14
|
-
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
15
|
-
const value = propWins ? prop : state;
|
|
16
|
-
const onChangeCb = useEvent(onChange || idFn);
|
|
17
|
-
const transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
if (prop === void 0)
|
|
20
|
-
return;
|
|
21
|
-
previous.current = prop;
|
|
22
|
-
transitionFn(() => {
|
|
23
|
-
setState(prop);
|
|
24
|
-
});
|
|
25
|
-
}, [prop]);
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (propWins)
|
|
28
|
-
return;
|
|
29
|
-
if (state !== previous.current) {
|
|
30
|
-
previous.current = state;
|
|
31
|
-
onChangeCb(state);
|
|
32
|
-
}
|
|
33
|
-
}, [onChangeCb, state, propWins]);
|
|
34
|
-
const setter = useEvent((next) => {
|
|
35
|
-
if (preventUpdate)
|
|
36
|
-
return;
|
|
37
|
-
if (propWins) {
|
|
38
|
-
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
39
|
-
onChangeCb(nextValue);
|
|
40
|
-
} else {
|
|
41
|
-
transitionFn(() => {
|
|
42
|
-
setState(next);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
return [value, setter];
|
|
47
|
-
}
|
|
48
|
-
const idFn = () => {
|
|
49
|
-
};
|
|
50
|
-
export {
|
|
51
|
-
useControllableState
|
|
52
|
-
};
|
|
1
|
+
import{useEvent as c}from"@tamagui/use-event";import{startTransition as h,useEffect as u,useRef as m,useState as R}from"react";const g=t=>t();function A({prop:t,defaultProp:f,onChange:l,strategy:T="prop-wins",preventUpdate:d,transition:p}){const[e,r]=R(t??f),n=m(e),o=T==="prop-wins"&&t!==void 0,C=o?t:e,a=c(l||v),i=p?h:g;u(()=>{t!==void 0&&(n.current=t,i(()=>{r(t)}))},[t]),u(()=>{o||e!==n.current&&(n.current=e,a(e))},[a,e,o]);const b=c(s=>{if(!d)if(o){const S=typeof s=="function"?s(n.current):s;a(S)}else i(()=>{r(s)})});return[C,b]}const v=()=>{};export{A as useControllableState};
|
|
53
2
|
//# sourceMappingURL=useControllableState.mjs.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
4
|
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { startTransition, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nconst emptyCallbackFn = (_) => _()\n\nexport function useControllableState<T>({\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>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n transition?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n transitionFn(() => {\n setState(prop)\n })\n }, [prop])\n\n 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) => {\n if (preventUpdate) return\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]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "AAAA,OAAS,YAAAA,MAAgB,qBACzB,OAAgB,mBAAAC,EAAiB,aAAAC,EAAW,UAAAC,EAAQ,YAAAC,MAAgB,QAOpE,MAAMC,EAAmBC,GAAMA,EAAE,EAE1B,SAASC,EAAwB,CACtC,KAAAC,EACA,YAAAC,EACA,SAAAC,EACA,SAAAC,EAAW,YACX,cAAAC,EACA,WAAAC,CACF,EAOiD,CAC/C,KAAM,CAACC,EAAOC,CAAQ,EAAIX,EAASI,GAAQC,CAAW,EAChDO,EAAWb,EAAYW,CAAK,EAC5BG,EAAWN,IAAa,aAAeH,IAAS,OAChDU,EAAQD,EAAWT,EAAOM,EAC1BK,EAAanB,EAASU,GAAYU,CAAI,EAEtCC,EAAeR,EAAaZ,EAAkBI,EAEpDH,EAAU,IAAM,CACVM,IAAS,SACbQ,EAAS,QAAUR,EACnBa,EAAa,IAAM,CACjBN,EAASP,CAAI,CACf,CAAC,EACH,EAAG,CAACA,CAAI,CAAC,EAETN,EAAU,IAAM,CACVe,GACAH,IAAUE,EAAS,UACrBA,EAAS,QAAUF,EACnBK,EAAWL,CAAK,EAEpB,EAAG,CAACK,EAAYL,EAAOG,CAAQ,CAAC,EAEhC,MAAMK,EAAStB,EAAUuB,GAAS,CAChC,GAAI,CAAAX,EACJ,GAAIK,EAAU,CACZ,MAAMO,EAAY,OAAOD,GAAS,WAAaA,EAAKP,EAAS,OAAO,EAAIO,EACxEJ,EAAWK,CAAS,CACtB,MACEH,EAAa,IAAM,CACjBN,EAASQ,CAAI,CACf,CAAC,CAEL,CAAC,EAED,MAAO,CAACL,EAAYI,CAAM,CAC5B,CAEA,MAAMF,EAAO,IAAM,CAAC",
|
|
6
|
+
"names": ["useEvent", "startTransition", "useEffect", "useRef", "useState", "emptyCallbackFn", "_", "useControllableState", "prop", "defaultProp", "onChange", "strategy", "preventUpdate", "transition", "state", "setState", "previous", "propWins", "value", "onChangeCb", "idFn", "transitionFn", "setter", "next", "nextValue"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-controllable-state",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.4",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@tamagui/use-event": "1.13.
|
|
31
|
+
"@tamagui/use-event": "1.13.4"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"react": "*"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@tamagui/build": "1.13.
|
|
37
|
+
"@tamagui/build": "1.13.4",
|
|
38
38
|
"react": "^18.2.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|