@tamagui/use-controllable-state 1.0.1-beta.98 → 1.0.1-rc.0

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,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -20,43 +21,47 @@ __export(useControllableState_exports, {
20
21
  useControllableState: () => useControllableState
21
22
  });
22
23
  module.exports = __toCommonJS(useControllableState_exports);
23
- var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
24
+ var import_use_event = require("@tamagui/use-event");
24
25
  var import_react = require("react");
25
26
  function useControllableState({
26
27
  prop,
27
28
  defaultProp,
28
29
  onChange,
29
- strategy = "prop-wins"
30
+ strategy = "prop-wins",
31
+ preventUpdate
30
32
  }) {
31
- const currentProp = (0, import_react.useRef)(prop);
32
- const handleChange = (0, import_react_use_callback_ref.useCallbackRef)(onChange);
33
- const [val, setVal] = (0, import_react.useState)(prop ?? defaultProp);
34
- 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);
35
38
  (0, import_react.useEffect)(() => {
36
- currentProp.current = prop;
37
- setVal((prev) => {
38
- return getNextStateWithCallback(prev, prop, handleChange);
39
- });
40
- }, [handleChange, prop]);
41
- return [
42
- val,
43
- (0, import_react.useCallback)((next) => {
44
- if (propWins && currentProp.current !== void 0) {
45
- handleChange(next);
46
- return;
47
- }
48
- setVal((prev) => {
49
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
50
- });
51
- }, [propWins, handleChange])
52
- ];
39
+ if (prop === void 0)
40
+ return;
41
+ previous.current = prop;
42
+ setState(prop);
43
+ }, [prop]);
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];
53
63
  }
54
- const getNextStateWithCallback = (prev, next, handleChange) => {
55
- if (prev === next || next === void 0) {
56
- return prev;
57
- }
58
- handleChange(next);
59
- return next;
64
+ const idFn = () => {
60
65
  };
61
66
  // Annotate the CommonJS export names for ESM import in node:
62
67
  0 && (module.exports = {
@@ -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) => {\n return getNextStateWithCallback(prev, prop, handleChange)\n })\n }, [handleChange, prop])\n\n return [\n val,\n useCallback(\n (next: unknown) => {\n if (propWins && currentProp.current !== undefined) {\n handleChange(next as T)\n return\n }\n setVal((prev) => {\n return getNextStateWithCallback(\n prev,\n typeof next === 'function' ? next(prev) : next,\n handleChange\n )\n })\n },\n [propWins, handleChange]\n ),\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;AACf,aAAO,yBAAyB,MAAM,MAAM,YAAY;AAAA,IAC1D,CAAC;AAAA,EACH,GAAG,CAAC,cAAc,IAAI,CAAC;AAEvB,SAAO;AAAA,IACL;AAAA,IACA,8BACE,CAAC,SAAkB;AACjB,UAAI,YAAY,YAAY,YAAY,QAAW;AACjD,qBAAa,IAAS;AACtB;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,UAAU,YAAY,CACzB;AAAA,EACF;AACF;AAEA,MAAM,2BAA2B,CAAC,MAAW,MAAW,iBAAsB;AAC5E,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO;AAAA,EACT;AACA,eAAa,IAAI;AACjB,SAAO;AACT;",
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,40 +1,44 @@
1
- import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
2
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useEvent } from "@tamagui/use-event";
2
+ import { useEffect, useRef, useState } from "react";
3
3
  function useControllableState({
4
4
  prop,
5
5
  defaultProp,
6
6
  onChange,
7
- strategy = "prop-wins"
7
+ strategy = "prop-wins",
8
+ preventUpdate
8
9
  }) {
9
- const currentProp = useRef(prop);
10
- const handleChange = useCallbackRef(onChange);
11
- const [val, setVal] = useState(prop ?? defaultProp);
12
- 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);
13
15
  useEffect(() => {
14
- currentProp.current = prop;
15
- setVal((prev) => {
16
- return getNextStateWithCallback(prev, prop, handleChange);
17
- });
18
- }, [handleChange, prop]);
19
- return [
20
- val,
21
- useCallback((next) => {
22
- if (propWins && currentProp.current !== void 0) {
23
- handleChange(next);
24
- return;
25
- }
26
- setVal((prev) => {
27
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
28
- });
29
- }, [propWins, handleChange])
30
- ];
16
+ if (prop === void 0)
17
+ return;
18
+ previous.current = prop;
19
+ setState(prop);
20
+ }, [prop]);
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];
31
40
  }
32
- const getNextStateWithCallback = (prev, next, handleChange) => {
33
- if (prev === next || next === void 0) {
34
- return prev;
35
- }
36
- handleChange(next);
37
- return next;
41
+ const idFn = () => {
38
42
  };
39
43
  export {
40
44
  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) => {\n return getNextStateWithCallback(prev, prop, handleChange)\n })\n }, [handleChange, prop])\n\n return [\n val,\n useCallback(\n (next: unknown) => {\n if (propWins && currentProp.current !== undefined) {\n handleChange(next as T)\n return\n }\n setVal((prev) => {\n return getNextStateWithCallback(\n prev,\n typeof next === 'function' ? next(prev) : next,\n handleChange\n )\n })\n },\n [propWins, handleChange]\n ),\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,QAAQ,WAAW;AAClD,QAAM,WAAW,aAAa;AAE9B,YAAU,MAAM;AACd,gBAAY,UAAU;AACtB,WAAO,CAAC,SAAS;AACf,aAAO,yBAAyB,MAAM,MAAM,YAAY;AAAA,IAC1D,CAAC;AAAA,EACH,GAAG,CAAC,cAAc,IAAI,CAAC;AAEvB,SAAO;AAAA,IACL;AAAA,IACA,YACE,CAAC,SAAkB;AACjB,UAAI,YAAY,YAAY,YAAY,QAAW;AACjD,qBAAa,IAAS;AACtB;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,UAAU,YAAY,CACzB;AAAA,EACF;AACF;AAEA,MAAM,2BAA2B,CAAC,MAAW,MAAW,iBAAsB;AAC5E,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO;AAAA,EACT;AACA,eAAa,IAAI;AACjB,SAAO;AACT;",
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
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,40 +1,44 @@
1
- import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
2
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useEvent } from "@tamagui/use-event";
2
+ import { useEffect, useRef, useState } from "react";
3
3
  function useControllableState({
4
4
  prop,
5
5
  defaultProp,
6
6
  onChange,
7
- strategy = "prop-wins"
7
+ strategy = "prop-wins",
8
+ preventUpdate
8
9
  }) {
9
- const currentProp = useRef(prop);
10
- const handleChange = useCallbackRef(onChange);
11
- const [val, setVal] = useState(prop != null ? prop : defaultProp);
12
- 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);
13
15
  useEffect(() => {
14
- currentProp.current = prop;
15
- setVal((prev) => {
16
- return getNextStateWithCallback(prev, prop, handleChange);
17
- });
18
- }, [handleChange, prop]);
19
- return [
20
- val,
21
- useCallback((next) => {
22
- if (propWins && currentProp.current !== void 0) {
23
- handleChange(next);
24
- return;
25
- }
26
- setVal((prev) => {
27
- return getNextStateWithCallback(prev, typeof next === "function" ? next(prev) : next, handleChange);
28
- });
29
- }, [propWins, handleChange])
30
- ];
16
+ if (prop === void 0)
17
+ return;
18
+ previous.current = prop;
19
+ setState(prop);
20
+ }, [prop]);
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];
31
40
  }
32
- const getNextStateWithCallback = (prev, next, handleChange) => {
33
- if (prev === next || next === void 0) {
34
- return prev;
35
- }
36
- handleChange(next);
37
- return next;
41
+ const idFn = () => {
38
42
  };
39
43
  export {
40
44
  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) => {\n return getNextStateWithCallback(prev, prop, handleChange)\n })\n }, [handleChange, prop])\n\n return [\n val,\n useCallback(\n (next: unknown) => {\n if (propWins && currentProp.current !== undefined) {\n handleChange(next as T)\n return\n }\n setVal((prev) => {\n return getNextStateWithCallback(\n prev,\n typeof next === 'function' ? next(prev) : next,\n handleChange\n )\n })\n },\n [propWins, handleChange]\n ),\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;AACf,aAAO,yBAAyB,MAAM,MAAM,YAAY;AAAA,IAC1D,CAAC;AAAA,EACH,GAAG,CAAC,cAAc,IAAI,CAAC;AAEvB,SAAO;AAAA,IACL;AAAA,IACA,YACE,CAAC,SAAkB;AACjB,UAAI,YAAY,YAAY,YAAY,QAAW;AACjD,qBAAa,IAAS;AACtB;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,UAAU,YAAY,CACzB;AAAA,EACF;AACF;AAEA,MAAM,2BAA2B,CAAC,MAAW,MAAW,iBAAsB;AAC5E,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO;AAAA,EACT;AACA,eAAa,IAAI;AACjB,SAAO;AACT;",
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/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@tamagui/use-controllable-state",
3
- "version": "1.0.1-beta.98",
3
+ "version": "1.0.1-rc.0",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
7
7
  "main": "dist/cjs",
8
8
  "module": "dist/esm",
9
- "module:jsx": "dist/jsx",
10
9
  "files": [
11
10
  "src",
12
11
  "types",
@@ -15,20 +14,22 @@
15
14
  "scripts": {
16
15
  "build": "tamagui-build",
17
16
  "watch": "tamagui-build --watch",
17
+ "lint": "eslint src",
18
+ "lint:fix": "yarn lint --fix",
18
19
  "clean": "tamagui-build clean",
19
20
  "clean:build": "tamagui-build clean:build"
20
21
  },
21
22
  "dependencies": {
22
- "@radix-ui/react-use-callback-ref": "^0.1.0"
23
+ "@tamagui/use-event": "^1.0.1-rc.0"
23
24
  },
24
25
  "peerDependencies": {
25
- "react": "*",
26
- "react-dom": "*"
26
+ "react": ">=18",
27
+ "react-dom": ">=18"
27
28
  },
28
29
  "devDependencies": {
29
- "@tamagui/build": "^1.0.1-beta.98",
30
- "react": "*",
31
- "react-dom": "*"
30
+ "@tamagui/build": "^1.0.1-rc.0",
31
+ "react": ">=18",
32
+ "react-dom": ">=18"
32
33
  },
33
34
  "publishConfig": {
34
35
  "access": "public"
@@ -1,57 +1,55 @@
1
- import { useCallbackRef } from '@radix-ui/react-use-callback-ref'
1
+ import { useEvent } from '@tamagui/use-event'
2
2
  import React, { useCallback, useEffect, useRef, useState } from 'react'
3
3
 
4
4
  // can configure to allow most-recent-wins or prop-wins
5
5
  // defaults to prop-wins
6
6
 
7
+ type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
8
+
7
9
  export function useControllableState<T>({
8
10
  prop,
9
11
  defaultProp,
10
12
  onChange,
11
13
  strategy = 'prop-wins',
14
+ preventUpdate,
12
15
  }: {
13
16
  prop?: T | undefined
14
17
  defaultProp: T
15
- onChange?: ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
18
+ onChange?: ChangeCb<T>
16
19
  strategy?: 'prop-wins' | 'most-recent-wins'
20
+ preventUpdate?: boolean
17
21
  }): [T, React.Dispatch<React.SetStateAction<T>>] {
18
- const currentProp = useRef(prop)
19
- const handleChange = useCallbackRef(onChange)
20
- const [val, setVal] = useState(prop ?? defaultProp)
21
- const propWins = strategy === 'prop-wins'
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)
22
27
 
23
28
  useEffect(() => {
24
- currentProp.current = prop
25
- setVal((prev) => {
26
- return getNextStateWithCallback(prev, prop, handleChange)
27
- })
28
- }, [handleChange, prop])
29
-
30
- return [
31
- val,
32
- useCallback(
33
- (next: unknown) => {
34
- if (propWins && currentProp.current !== undefined) {
35
- handleChange(next as T)
36
- return
37
- }
38
- setVal((prev) => {
39
- return getNextStateWithCallback(
40
- prev,
41
- typeof next === 'function' ? next(prev) : next,
42
- handleChange
43
- )
44
- })
45
- },
46
- [propWins, handleChange]
47
- ),
48
- ]
49
- }
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
+ })
50
51
 
51
- const getNextStateWithCallback = (prev: any, next: any, handleChange: any) => {
52
- if (prev === next || next === undefined) {
53
- return prev
54
- }
55
- handleChange(next)
56
- return next
52
+ return [value as T, setter]
57
53
  }
54
+
55
+ const idFn = () => {}
package/types/index.d.ts CHANGED
File without changes
@@ -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":"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,CAgC/C"}