@tamagui/use-controllable-state 1.0.1-rc.5.1 → 1.0.1-rc.6

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.
@@ -23,23 +23,28 @@ __export(useControllableState_exports, {
23
23
  module.exports = __toCommonJS(useControllableState_exports);
24
24
  var import_use_event = require("@tamagui/use-event");
25
25
  var import_react = require("react");
26
+ const emptyCallbackFn = (_) => _();
26
27
  function useControllableState({
27
28
  prop,
28
29
  defaultProp,
29
30
  onChange,
30
31
  strategy = "prop-wins",
31
- preventUpdate
32
+ preventUpdate,
33
+ transition
32
34
  }) {
33
35
  const [state, setState] = (0, import_react.useState)(prop ?? defaultProp);
34
36
  const previous = (0, import_react.useRef)(state);
35
37
  const propWins = strategy === "prop-wins" && prop !== void 0;
36
38
  const value = propWins ? prop : state;
37
39
  const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
40
+ const transitionFn = transition ? import_react.startTransition : emptyCallbackFn;
38
41
  (0, import_react.useEffect)(() => {
39
42
  if (prop === void 0)
40
43
  return;
41
44
  previous.current = prop;
42
- setState(prop);
45
+ transitionFn(() => {
46
+ setState(prop);
47
+ });
43
48
  }, [prop]);
44
49
  (0, import_react.useEffect)(() => {
45
50
  if (propWins)
@@ -56,7 +61,9 @@ function useControllableState({
56
61
  const nextValue = typeof next === "function" ? next(previous.current) : next;
57
62
  onChangeCb(nextValue);
58
63
  } else {
59
- setState(next);
64
+ transitionFn(() => {
65
+ setState(next);
66
+ });
60
67
  }
61
68
  });
62
69
  return [value, setter];
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;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;",
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, {\n startTransition,\n useCallback,\n useEffect,\n useRef,\n useState,\n} 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;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAyB;AACzB,mBAMO;AAOP,MAAM,kBAAkB,CAAC,MAAM,EAAE;AAE1B,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA;AACF,GAOiD;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,QAAM,eAAe,aAAa,+BAAkB;AAEpD,8BAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,iBAAa,MAAM;AACjB,eAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH,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,mBAAa,MAAM;AACjB,iBAAS,IAAI;AAAA,MACf,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
6
6
  "names": []
7
7
  }
@@ -1,22 +1,32 @@
1
1
  import { useEvent } from "@tamagui/use-event";
2
- import { useEffect, useRef, useState } from "react";
2
+ import {
3
+ startTransition,
4
+ useEffect,
5
+ useRef,
6
+ useState
7
+ } from "react";
8
+ const emptyCallbackFn = (_) => _();
3
9
  function useControllableState({
4
10
  prop,
5
11
  defaultProp,
6
12
  onChange,
7
13
  strategy = "prop-wins",
8
- preventUpdate
14
+ preventUpdate,
15
+ transition
9
16
  }) {
10
17
  const [state, setState] = useState(prop ?? defaultProp);
11
18
  const previous = useRef(state);
12
19
  const propWins = strategy === "prop-wins" && prop !== void 0;
13
20
  const value = propWins ? prop : state;
14
21
  const onChangeCb = useEvent(onChange || idFn);
22
+ const transitionFn = transition ? startTransition : emptyCallbackFn;
15
23
  useEffect(() => {
16
24
  if (prop === void 0)
17
25
  return;
18
26
  previous.current = prop;
19
- setState(prop);
27
+ transitionFn(() => {
28
+ setState(prop);
29
+ });
20
30
  }, [prop]);
21
31
  useEffect(() => {
22
32
  if (propWins)
@@ -33,7 +43,9 @@ function useControllableState({
33
43
  const nextValue = typeof next === "function" ? next(previous.current) : next;
34
44
  onChangeCb(nextValue);
35
45
  } else {
36
- setState(next);
46
+ transitionFn(() => {
47
+ setState(next);
48
+ });
37
49
  }
38
50
  });
39
51
  return [value, setter];
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;",
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, {\n startTransition,\n useCallback,\n useEffect,\n useRef,\n useState,\n} 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,SAAS,gBAAgB;AACzB;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAOP,MAAM,kBAAkB,CAAC,MAAM,EAAE;AAE1B,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA;AACF,GAOiD;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,QAAM,eAAe,aAAa,kBAAkB;AAEpD,YAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,iBAAa,MAAM;AACjB,eAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH,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,mBAAa,MAAM;AACjB,iBAAS,IAAI;AAAA,MACf,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/use-controllable-state",
3
- "version": "1.0.1-rc.5.1",
3
+ "version": "1.0.1-rc.6",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -14,20 +14,20 @@
14
14
  "scripts": {
15
15
  "build": "tamagui-build",
16
16
  "watch": "tamagui-build --watch",
17
- "lint": "eslint src",
18
- "lint:fix": "yarn lint --fix",
17
+ "lint": "../../node_modules/.bin/rome check src",
18
+ "lint:fix": "../../node_modules/.bin/rome check --apply-suggested src",
19
19
  "clean": "tamagui-build clean",
20
20
  "clean:build": "tamagui-build clean:build"
21
21
  },
22
22
  "dependencies": {
23
- "@tamagui/use-event": "^1.0.1-rc.5.1"
23
+ "@tamagui/use-event": "^1.0.1-rc.6"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "react": "^18.2.0",
27
27
  "react-dom": "^18.2.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@tamagui/build": "^1.0.1-rc.5.1",
30
+ "@tamagui/build": "^1.0.1-rc.6",
31
31
  "react": "^18.2.0",
32
32
  "react-dom": "^18.2.0"
33
33
  },
@@ -1,23 +1,33 @@
1
1
  import { useEvent } from '@tamagui/use-event'
2
- import React, { useCallback, useEffect, useRef, useState } from 'react'
2
+ import React, {
3
+ startTransition,
4
+ useCallback,
5
+ useEffect,
6
+ useRef,
7
+ useState,
8
+ } from 'react'
3
9
 
4
10
  // can configure to allow most-recent-wins or prop-wins
5
11
  // defaults to prop-wins
6
12
 
7
13
  type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
8
14
 
15
+ const emptyCallbackFn = (_) => _()
16
+
9
17
  export function useControllableState<T>({
10
18
  prop,
11
19
  defaultProp,
12
20
  onChange,
13
21
  strategy = 'prop-wins',
14
22
  preventUpdate,
23
+ transition,
15
24
  }: {
16
25
  prop?: T | undefined
17
26
  defaultProp: T
18
27
  onChange?: ChangeCb<T>
19
28
  strategy?: 'prop-wins' | 'most-recent-wins'
20
29
  preventUpdate?: boolean
30
+ transition?: boolean
21
31
  }): [T, React.Dispatch<React.SetStateAction<T>>] {
22
32
  const [state, setState] = useState(prop ?? defaultProp)
23
33
  const previous = useRef<any>(state)
@@ -25,10 +35,14 @@ export function useControllableState<T>({
25
35
  const value = propWins ? prop : state
26
36
  const onChangeCb = useEvent(onChange || idFn)
27
37
 
38
+ const transitionFn = transition ? startTransition : emptyCallbackFn
39
+
28
40
  useEffect(() => {
29
41
  if (prop === undefined) return
30
42
  previous.current = prop
31
- setState(prop)
43
+ transitionFn(() => {
44
+ setState(prop)
45
+ })
32
46
  }, [prop])
33
47
 
34
48
  useEffect(() => {
@@ -45,7 +59,9 @@ export function useControllableState<T>({
45
59
  const nextValue = typeof next === 'function' ? next(previous.current) : next
46
60
  onChangeCb(nextValue)
47
61
  } else {
48
- setState(next)
62
+ transitionFn(() => {
63
+ setState(next)
64
+ })
49
65
  }
50
66
  })
51
67
 
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
2
  type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>;
3
- export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, preventUpdate, }: {
3
+ export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, preventUpdate, transition, }: {
4
4
  prop?: T | undefined;
5
5
  defaultProp: T;
6
6
  onChange?: ChangeCb<T>;
7
7
  strategy?: 'prop-wins' | 'most-recent-wins';
8
8
  preventUpdate?: boolean;
9
+ transition?: boolean;
9
10
  }): [T, React.Dispatch<React.SetStateAction<T>>];
10
11
  export {};
11
12
  //# sourceMappingURL=useControllableState.d.ts.map