@tamagui/use-controllable-state 1.0.1-beta.18 → 1.0.1-beta.180

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 CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './useControllableState'\n"],
5
- "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,mCAAd;",
5
+ "mappings": ";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,mCAAd;",
6
6
  "names": []
7
7
  }
@@ -1,8 +1,8 @@
1
+ "use strict";
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
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
6
  var __export = (target, all) => {
7
7
  for (var name in all)
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -21,42 +21,48 @@ __export(useControllableState_exports, {
21
21
  useControllableState: () => useControllableState
22
22
  });
23
23
  module.exports = __toCommonJS(useControllableState_exports);
24
- var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
24
+ var import_use_event = require("@tamagui/use-event");
25
25
  var import_react = require("react");
26
26
  function useControllableState({
27
27
  prop,
28
28
  defaultProp,
29
29
  onChange,
30
- strategy = "prop-wins"
30
+ strategy = "prop-wins",
31
+ preventUpdate
31
32
  }) {
32
- const currentProp = (0, import_react.useRef)(prop);
33
- const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
34
- const [val, setVal] = (0, import_react.useState)(prop ?? defaultProp);
35
- const propWins = strategy === "prop-wins";
33
+ const [state, setState] = (0, import_react.useState)(prop ?? defaultProp);
34
+ const previous = (0, import_react.useRef)(state);
35
+ const propWins = strategy === "prop-wins" && prop !== void 0;
36
+ const value = propWins ? prop : state;
37
+ const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
36
38
  (0, import_react.useEffect)(() => {
37
- currentProp.current = prop;
38
- setVal((prev) => getNextStateWithCallback(prev, prop, handleChange));
39
+ if (prop === void 0)
40
+ return;
41
+ previous.current = prop;
42
+ setState(prop);
39
43
  }, [prop]);
40
- return [
41
- val,
42
- (0, import_react.useCallback)((next) => {
43
- if (propWins && currentProp.current !== void 0) {
44
- return;
45
- }
46
- setVal((prev) => {
47
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
48
- });
49
- }, [setVal, propWins])
50
- ];
44
+ (0, import_react.useEffect)(() => {
45
+ if (propWins)
46
+ return;
47
+ if (state !== previous.current) {
48
+ previous.current = state;
49
+ onChangeCb(state);
50
+ }
51
+ }, [onChangeCb, state, propWins]);
52
+ const setter = (0, import_use_event.useEvent)((next) => {
53
+ if (preventUpdate)
54
+ return;
55
+ if (propWins) {
56
+ const nextValue = typeof next === "function" ? next(previous.current) : next;
57
+ onChangeCb(nextValue);
58
+ } else {
59
+ setState(next);
60
+ }
61
+ });
62
+ return [value, setter];
51
63
  }
52
- __name(useControllableState, "useControllableState");
53
- const getNextStateWithCallback = /* @__PURE__ */ __name((prev, next, handleChange) => {
54
- if (prev === next || next === void 0) {
55
- return prev;
56
- }
57
- handleChange(next);
58
- return next;
59
- }, "getNextStateWithCallback");
64
+ const idFn = () => {
65
+ };
60
66
  // Annotate the CommonJS export names for ESM import in node:
61
67
  0 && (module.exports = {
62
68
  useControllableState
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/useControllableState.ts"],
4
- "sourcesContent": ["import { useCallbackRef } from '@radix-ui/react-use-callback-ref'\nimport React, { useCallback, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n strategy?: 'prop-wins' | 'most-recent-wins'\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const currentProp = useRef(prop)\n const handleChange = useCallbackRef(onChange)\n const [val, setVal] = useState(prop ?? defaultProp)\n const propWins = strategy === 'prop-wins'\n\n useEffect(() => {\n currentProp.current = prop\n setVal((prev) => getNextStateWithCallback(prev, prop, handleChange))\n }, [prop])\n\n return [\n val,\n useCallback(\n (next: any) => {\n if (propWins && currentProp.current !== undefined) {\n return\n }\n setVal((prev) => {\n return getNextStateWithCallback(\n prev,\n typeof next === 'function' ? next(prev) : next,\n handleChange\n )\n })\n },\n [setVal, propWins]\n ) as any,\n ]\n}\n\nconst getNextStateWithCallback = (prev: any, next: any, handleChange: any) => {\n if (prev === next || next === undefined) {\n return prev\n }\n handleChange(next)\n return next\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oCAA+B;AAC/B,mBAAgE;AAKzD,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,GAMoC;AAC/C,QAAM,cAAc,yBAAO,IAAI;AAC/B,QAAM,eAAe,kDAAe,QAAQ;AAC5C,QAAM,CAAC,KAAK,UAAU,2BAAS,QAAQ,WAAW;AAClD,QAAM,WAAW,aAAa;AAE9B,8BAAU,MAAM;AACd,gBAAY,UAAU;AACtB,WAAO,CAAC,SAAS,yBAAyB,MAAM,MAAM,YAAY,CAAC;AAAA,EACrE,GAAG,CAAC,IAAI,CAAC;AAET,SAAO;AAAA,IACL;AAAA,IACA,8BACE,CAAC,SAAc;AACb,UAAI,YAAY,YAAY,YAAY,QAAW;AACjD;AAAA,MACF;AACA,aAAO,CAAC,SAAS;AACf,eAAO,yBACL,MACA,OAAO,SAAS,aAAa,KAAK,IAAI,IAAI,MAC1C,YACF;AAAA,MACF,CAAC;AAAA,IACH,GACA,CAAC,QAAQ,QAAQ,CACnB;AAAA,EACF;AACF;AAvCgB;AAyChB,MAAM,2BAA2B,wBAAC,MAAW,MAAW,iBAAsB;AAC5E,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO;AAAA,EACT;AACA,eAAa,IAAI;AACjB,SAAO;AACT,GANiC;",
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, 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\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n setState(prop)\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 setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAyB;AACzB,mBAAgE;AAOzD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAS,QAAQ,WAAW;AACtD,QAAM,eAAW,qBAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,iBAAa,2BAAS,YAAY,IAAI;AAE5C,8BAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,8BAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,aAAS,2BAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
6
6
  "names": []
7
7
  }
package/dist/esm/index.js CHANGED
File without changes
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": ["export * from './useControllableState'\n"],
5
- "mappings": "AAAA;",
5
+ "mappings": "AAAA,cAAc;",
6
6
  "names": []
7
7
  }
@@ -1,41 +1,45 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
4
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useEvent } from "@tamagui/use-event";
2
+ import { useEffect, useRef, useState } from "react";
5
3
  function useControllableState({
6
4
  prop,
7
5
  defaultProp,
8
6
  onChange,
9
- strategy = "prop-wins"
7
+ strategy = "prop-wins",
8
+ preventUpdate
10
9
  }) {
11
- const currentProp = useRef(prop);
12
- const handleChange = useCallbackRef(onChange);
13
- const [val, setVal] = useState(prop != null ? prop : defaultProp);
14
- const propWins = strategy === "prop-wins";
10
+ const [state, setState] = useState(prop ?? defaultProp);
11
+ const previous = useRef(state);
12
+ const propWins = strategy === "prop-wins" && prop !== void 0;
13
+ const value = propWins ? prop : state;
14
+ const onChangeCb = useEvent(onChange || idFn);
15
15
  useEffect(() => {
16
- currentProp.current = prop;
17
- setVal((prev) => getNextStateWithCallback(prev, prop, handleChange));
16
+ if (prop === void 0)
17
+ return;
18
+ previous.current = prop;
19
+ setState(prop);
18
20
  }, [prop]);
19
- return [
20
- val,
21
- useCallback((next) => {
22
- if (propWins && currentProp.current !== void 0) {
23
- return;
24
- }
25
- setVal((prev) => {
26
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
27
- });
28
- }, [setVal, propWins])
29
- ];
21
+ useEffect(() => {
22
+ if (propWins)
23
+ return;
24
+ if (state !== previous.current) {
25
+ previous.current = state;
26
+ onChangeCb(state);
27
+ }
28
+ }, [onChangeCb, state, propWins]);
29
+ const setter = useEvent((next) => {
30
+ if (preventUpdate)
31
+ return;
32
+ if (propWins) {
33
+ const nextValue = typeof next === "function" ? next(previous.current) : next;
34
+ onChangeCb(nextValue);
35
+ } else {
36
+ setState(next);
37
+ }
38
+ });
39
+ return [value, setter];
30
40
  }
31
- __name(useControllableState, "useControllableState");
32
- const getNextStateWithCallback = /* @__PURE__ */ __name((prev, next, handleChange) => {
33
- if (prev === next || next === void 0) {
34
- return prev;
35
- }
36
- handleChange(next);
37
- return next;
38
- }, "getNextStateWithCallback");
41
+ const idFn = () => {
42
+ };
39
43
  export {
40
44
  useControllableState
41
45
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/useControllableState.ts"],
4
- "sourcesContent": ["import { useCallbackRef } from '@radix-ui/react-use-callback-ref'\nimport React, { useCallback, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n strategy?: 'prop-wins' | 'most-recent-wins'\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const currentProp = useRef(prop)\n const handleChange = useCallbackRef(onChange)\n const [val, setVal] = useState(prop ?? defaultProp)\n const propWins = strategy === 'prop-wins'\n\n useEffect(() => {\n currentProp.current = prop\n setVal((prev) => getNextStateWithCallback(prev, prop, handleChange))\n }, [prop])\n\n return [\n val,\n useCallback(\n (next: any) => {\n if (propWins && currentProp.current !== undefined) {\n return\n }\n setVal((prev) => {\n return getNextStateWithCallback(\n prev,\n typeof next === 'function' ? next(prev) : next,\n handleChange\n )\n })\n },\n [setVal, propWins]\n ) as any,\n ]\n}\n\nconst getNextStateWithCallback = (prev: any, next: any, handleChange: any) => {\n if (prev === next || next === undefined) {\n return prev\n }\n handleChange(next)\n return next\n}\n"],
5
- "mappings": ";;AAAA;AACA;AAKO,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,GAMoC;AAC/C,QAAM,cAAc,OAAO,IAAI;AAC/B,QAAM,eAAe,eAAe,QAAQ;AAC5C,QAAM,CAAC,KAAK,UAAU,SAAS,sBAAQ,WAAW;AAClD,QAAM,WAAW,aAAa;AAE9B,YAAU,MAAM;AACd,gBAAY,UAAU;AACtB,WAAO,CAAC,SAAS,yBAAyB,MAAM,MAAM,YAAY,CAAC;AAAA,EACrE,GAAG,CAAC,IAAI,CAAC;AAET,SAAO;AAAA,IACL;AAAA,IACA,YACE,CAAC,SAAc;AACb,UAAI,YAAY,YAAY,YAAY,QAAW;AACjD;AAAA,MACF;AACA,aAAO,CAAC,SAAS;AACf,eAAO,yBACL,MACA,OAAO,SAAS,aAAa,KAAK,IAAI,IAAI,MAC1C,YACF;AAAA,MACF,CAAC;AAAA,IACH,GACA,CAAC,QAAQ,QAAQ,CACnB;AAAA,EACF;AACF;AAvCgB;AAyChB,MAAM,2BAA2B,wBAAC,MAAW,MAAW,iBAAsB;AAC5E,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO;AAAA,EACT;AACA,eAAa,IAAI;AACjB,SAAO;AACT,GANiC;",
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, 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\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n setState(prop)\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 setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
5
+ "mappings": "AAAA,SAAS,gBAAgB;AACzB,SAA6B,WAAW,QAAQ,gBAAgB;AAOzD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,QAAQ,WAAW;AACtD,QAAM,WAAW,OAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,aAAa,SAAS,YAAY,IAAI;AAE5C,YAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
6
6
  "names": []
7
7
  }
package/dist/jsx/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./useControllableState";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './useControllableState'\n"],
5
+ "mappings": "AAAA,cAAc;",
6
+ "names": []
7
+ }
@@ -1,41 +1,46 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
4
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useEvent } from "@tamagui/use-event";
2
+ import { useEffect, useRef, useState } from "react";
5
3
  function useControllableState({
6
4
  prop,
7
5
  defaultProp,
8
6
  onChange,
9
- strategy = "prop-wins"
7
+ strategy = "prop-wins",
8
+ preventUpdate
10
9
  }) {
11
- const currentProp = useRef(prop);
12
- const handleChange = useCallbackRef(onChange);
13
- const [val, setVal] = useState(prop != null ? prop : defaultProp);
14
- const propWins = strategy === "prop-wins";
10
+ const [state, setState] = useState(prop ?? defaultProp);
11
+ const previous = useRef(state);
12
+ const propWins = strategy === "prop-wins" && prop !== void 0;
13
+ const value = propWins ? prop : state;
14
+ const onChangeCb = useEvent(onChange || idFn);
15
15
  useEffect(() => {
16
- currentProp.current = prop;
17
- setVal((prev) => getNextStateWithCallback(prev, prop, handleChange));
16
+ if (prop === void 0)
17
+ return;
18
+ previous.current = prop;
19
+ setState(prop);
18
20
  }, [prop]);
19
- return [
20
- val,
21
- useCallback((next) => {
22
- if (propWins && currentProp.current !== void 0) {
23
- return;
24
- }
25
- setVal((prev) => {
26
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
27
- });
28
- }, [setVal, propWins])
29
- ];
21
+ useEffect(() => {
22
+ if (propWins)
23
+ return;
24
+ if (state !== previous.current) {
25
+ previous.current = state;
26
+ onChangeCb(state);
27
+ }
28
+ }, [onChangeCb, state, propWins]);
29
+ const setter = useEvent((next) => {
30
+ if (preventUpdate)
31
+ return;
32
+ if (propWins) {
33
+ const nextValue = typeof next === "function" ? next(previous.current) : next;
34
+ onChangeCb(nextValue);
35
+ } else {
36
+ setState(next);
37
+ }
38
+ });
39
+ return [value, setter];
30
40
  }
31
- __name(useControllableState, "useControllableState");
32
- const getNextStateWithCallback = /* @__PURE__ */ __name((prev, next, handleChange) => {
33
- if (prev === next || next === void 0) {
34
- return prev;
35
- }
36
- handleChange(next);
37
- return next;
38
- }, "getNextStateWithCallback");
41
+ const idFn = () => {
42
+ };
39
43
  export {
40
44
  useControllableState
41
45
  };
46
+ //# sourceMappingURL=useControllableState.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useControllableState.ts"],
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, 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\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: 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 useEffect(() => {\n if (prop === undefined) return\n previous.current = prop\n setState(prop)\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 setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
5
+ "mappings": "AAAA,SAAS,gBAAgB;AACzB,SAA6B,WAAW,QAAQ,gBAAgB;AAOzD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,QAAQ,WAAW;AACtD,QAAM,WAAW,OAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,aAAa,SAAS,YAAY,IAAI;AAE5C,YAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,31 +1,34 @@
1
1
  {
2
2
  "name": "@tamagui/use-controllable-state",
3
- "version": "1.0.1-beta.18",
3
+ "version": "1.0.1-beta.180",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
- "typings": "types",
6
+ "types": "./types/index.d.ts",
7
7
  "main": "dist/cjs",
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
14
15
  "scripts": {
15
16
  "build": "tamagui-build",
16
17
  "watch": "tamagui-build --watch",
18
+ "lint": "eslint",
19
+ "lint:fix": "yarn lint --fix",
17
20
  "clean": "tamagui-build clean",
18
21
  "clean:build": "tamagui-build clean:build"
19
22
  },
20
23
  "dependencies": {
21
- "@radix-ui/react-use-callback-ref": "^0.1.0"
24
+ "@tamagui/use-event": "^1.0.1-beta.180"
22
25
  },
23
26
  "peerDependencies": {
24
27
  "react": "*",
25
28
  "react-dom": "*"
26
29
  },
27
30
  "devDependencies": {
28
- "@tamagui/build": "^1.0.1-beta.18",
31
+ "@tamagui/build": "^1.0.1-beta.180",
29
32
  "react": "*",
30
33
  "react-dom": "*"
31
34
  },
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './useControllableState'
@@ -0,0 +1,55 @@
1
+ import { useEvent } from '@tamagui/use-event'
2
+ import React, { useCallback, useEffect, useRef, useState } from 'react'
3
+
4
+ // can configure to allow most-recent-wins or prop-wins
5
+ // defaults to prop-wins
6
+
7
+ type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
8
+
9
+ export function useControllableState<T>({
10
+ prop,
11
+ defaultProp,
12
+ onChange,
13
+ strategy = 'prop-wins',
14
+ preventUpdate,
15
+ }: {
16
+ prop?: T | undefined
17
+ defaultProp: T
18
+ onChange?: ChangeCb<T>
19
+ strategy?: 'prop-wins' | 'most-recent-wins'
20
+ preventUpdate?: boolean
21
+ }): [T, React.Dispatch<React.SetStateAction<T>>] {
22
+ const [state, setState] = useState(prop ?? defaultProp)
23
+ const previous = useRef<any>(state)
24
+ const propWins = strategy === 'prop-wins' && prop !== undefined
25
+ const value = propWins ? prop : state
26
+ const onChangeCb = useEvent(onChange || idFn)
27
+
28
+ useEffect(() => {
29
+ if (prop === undefined) return
30
+ previous.current = prop
31
+ setState(prop)
32
+ }, [prop])
33
+
34
+ useEffect(() => {
35
+ if (propWins) return
36
+ if (state !== previous.current) {
37
+ previous.current = state
38
+ onChangeCb(state)
39
+ }
40
+ }, [onChangeCb, state, propWins])
41
+
42
+ const setter = useEvent((next) => {
43
+ if (preventUpdate) return
44
+ if (propWins) {
45
+ const nextValue = typeof next === 'function' ? next(previous.current) : next
46
+ onChangeCb(nextValue)
47
+ } else {
48
+ setState(next)
49
+ }
50
+ })
51
+
52
+ return [value as T, setter]
53
+ }
54
+
55
+ const idFn = () => {}
@@ -1,8 +1,11 @@
1
1
  import React from 'react';
2
- export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, }: {
2
+ declare type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>;
3
+ export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, preventUpdate, }: {
3
4
  prop?: T | undefined;
4
5
  defaultProp: T;
5
- onChange?: ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>;
6
+ onChange?: ChangeCb<T>;
6
7
  strategy?: 'prop-wins' | 'most-recent-wins';
8
+ preventUpdate?: boolean;
7
9
  }): [T, React.Dispatch<React.SetStateAction<T>>];
10
+ export {};
8
11
  //# sourceMappingURL=useControllableState.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../src/useControllableState.ts"],"names":[],"mappings":"AACA,OAAO,KAAmD,MAAM,OAAO,CAAA;AAKvE,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,EACtC,IAAI,EACJ,WAAW,EACX,QAAQ,EACR,QAAsB,GACvB,EAAE;IACD,IAAI,CAAC,EAAE,CAAC,GAAG,SAAS,CAAA;IACpB,WAAW,EAAE,CAAC,CAAA;IACd,QAAQ,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAA;IACxE,QAAQ,CAAC,EAAE,WAAW,GAAG,kBAAkB,CAAA;CAC5C,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CA6B/C"}