@seed-design/lynx-react 0.6.1 → 0.6.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,yBAAyB,CAAC,CAAC,CAAC,GAClC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAkBzB"}
1
+ {"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../../src/hooks/useControllableState.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,KAAK,EAAE,yBAAyB,CAAC,CAAC,CAAC,GAClC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAuBzB"}
@@ -16,7 +16,11 @@ function useControllableState(props) {
16
16
  const currentValue = isControlled ? value : uncontrolledValue;
17
17
  const prevValueRef = useRef(currentValue);
18
18
  return [currentValue, useMemoizedFn((nextValue) => {
19
- if (!isControlled) setUncontrolledValue(nextValue);
19
+ if (isControlled) {
20
+ if (nextValue !== value) onChange?.(nextValue);
21
+ return;
22
+ }
23
+ setUncontrolledValue(nextValue);
20
24
  if (nextValue !== prevValueRef.current) {
21
25
  onChange?.(nextValue);
22
26
  prevValueRef.current = nextValue;
@@ -1 +1 @@
1
- {"version":3,"file":"useIconColor.d.ts","sourceRoot":"","sources":["../../src/hooks/useIconColor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAOhE,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1C,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AA0DD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,cAAc,EACpB,OAAO,CAAC,EAAE,mBAAmB,GAC5B;IACD,GAAG,EAAE,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACnC,0BAA0B,EAAE,MAAM,IAAI,CAAC;CACxC,CAuBA"}
1
+ {"version":3,"file":"useIconColor.d.ts","sourceRoot":"","sources":["../../src/hooks/useIconColor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAOhE,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1C,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AA0DD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,cAAc,EACpB,OAAO,CAAC,EAAE,mBAAmB,GAC5B;IACD,GAAG,EAAE,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACnC,0BAA0B,EAAE,MAAM,IAAI,CAAC;CACxC,CA6BA"}
@@ -1,4 +1,4 @@
1
- import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef } from "@lynx-js/react";
1
+ import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef, useRef } from "@lynx-js/react";
2
2
  //#region src/hooks/useIconColor.ts
3
3
  function syncTintColorOnce(targetRef, sourceRef) {
4
4
  "main thread";
@@ -45,12 +45,17 @@ function useIconColor(deps, options) {
45
45
  const enabled = options?.enabled ?? true;
46
46
  const frameRef = useMainThreadRef(0);
47
47
  const theme = useGlobalProps()?.theme;
48
+ const hasMountedRef = useRef(false);
48
49
  function syncOnUiAppear() {
49
50
  "main thread";
50
51
  if (!enabled) return;
51
- syncTintColorOnce(ref, sourceRef);
52
+ scheduleTintColorSync(ref, sourceRef, frameRef);
52
53
  }
53
54
  useEffect(() => {
55
+ if (!hasMountedRef.current) {
56
+ hasMountedRef.current = true;
57
+ return;
58
+ }
54
59
  if (!enabled) return;
55
60
  runOnMainThread(scheduleTintColorSync)(ref, sourceRef, frameRef);
56
61
  return () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seed-design/lynx-react",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/daangn/seed-design.git",
@@ -39,7 +39,7 @@
39
39
  "devDependencies": {
40
40
  "@lynx-js/react": "^0.117.0",
41
41
  "@lynx-js/types": "^3.7.0",
42
- "@seed-design/lynx-css": "0.10.1",
42
+ "@seed-design/lynx-css": "0.10.2",
43
43
  "@testing-library/jest-dom": "^6.9.1",
44
44
  "jsdom": "^29.0.2",
45
45
  "preact": "npm:@lynx-js/internal-preact@10.28.4-ee7bb26",
@@ -80,7 +80,7 @@ function installMainThreadStyleMocks(getComputedColor: () => string) {
80
80
  }
81
81
 
82
82
  describe("InternalIcon", () => {
83
- it("reads the updated computed color in the next frame", async () => {
83
+ it("reads the updated computed color after dependency changes", async () => {
84
84
  let computedColor = "rgb(134, 139, 148)";
85
85
  const { frames, runNextFrame } = installMainThreadStyleMocks(() => computedColor);
86
86
 
@@ -94,16 +94,14 @@ describe("InternalIcon", () => {
94
94
  await waitSchedule();
95
95
 
96
96
  const { image } = getSourceAndImage();
97
- expect(frames.size).toBe(1);
98
- runNextFrame();
99
-
100
- expect(image.getAttribute("tint-color")).toBe("rgb(134, 139, 148)");
97
+ expect(frames.size).toBe(0);
98
+ expect(image.getAttribute("tint-color")).toBeNull();
101
99
 
102
100
  rerender(renderIcon("checked"));
103
101
  await waitSchedule();
104
102
 
105
103
  expect(frames.size).toBe(1);
106
- expect(image.getAttribute("tint-color")).toBe("rgb(134, 139, 148)");
104
+ expect(image.getAttribute("tint-color")).toBeNull();
107
105
 
108
106
  computedColor = "rgb(255, 102, 0)";
109
107
  runNextFrame();
@@ -64,6 +64,56 @@ describe("useControllableState", () => {
64
64
  });
65
65
 
66
66
  describe("controlled mode", () => {
67
+ it.each([false, true])("notifies after an external change from %s", (initialValue) => {
68
+ const onChange = vi.fn();
69
+ const { result, rerender } = renderHook(
70
+ (props: { value: boolean }) =>
71
+ useControllableState({ value: props.value, defaultValue: false, onChange }),
72
+ { initialProps: { value: initialValue } },
73
+ );
74
+
75
+ rerender({ value: !initialValue });
76
+ expect(result.current[0]).toBe(!initialValue);
77
+ expect(onChange).not.toHaveBeenCalled();
78
+
79
+ act(() => result.current[1](!result.current[0]));
80
+
81
+ expect(onChange).toHaveBeenCalledExactlyOnceWith(initialValue);
82
+ rerender({ value: initialValue });
83
+ act(() => result.current[1](!result.current[0]));
84
+ expect(onChange).toHaveBeenNthCalledWith(2, !initialValue);
85
+ expect(onChange).toHaveBeenCalledTimes(2);
86
+ });
87
+
88
+ it("notifies again when the parent does not accept a change", () => {
89
+ const onChange = vi.fn();
90
+ const { result } = renderHook(() =>
91
+ useControllableState({ value: false, defaultValue: false, onChange }),
92
+ );
93
+
94
+ act(() => result.current[1](true));
95
+ act(() => result.current[1](true));
96
+
97
+ expect(result.current[0]).toBe(false);
98
+ expect(onChange).toHaveBeenCalledTimes(2);
99
+ expect(onChange).toHaveBeenNthCalledWith(1, true);
100
+ expect(onChange).toHaveBeenNthCalledWith(2, true);
101
+ });
102
+
103
+ it("does not notify for the current prop after an unaccepted change", () => {
104
+ const onChange = vi.fn();
105
+ const { result } = renderHook(() =>
106
+ useControllableState({ value: false, defaultValue: false, onChange }),
107
+ );
108
+
109
+ act(() => result.current[1](false));
110
+ expect(onChange).not.toHaveBeenCalled();
111
+ act(() => result.current[1](true));
112
+ act(() => result.current[1](false));
113
+
114
+ expect(onChange).toHaveBeenCalledExactlyOnceWith(true);
115
+ });
116
+
67
117
  it("uses the provided value", () => {
68
118
  const { result } = renderHook(() =>
69
119
  useControllableState({ value: "external", defaultValue: "default" }),
@@ -25,9 +25,14 @@ export function useControllableState<T>(
25
25
  const prevValueRef = useRef(currentValue);
26
26
 
27
27
  const setValue = useMemoizedFn((nextValue: T) => {
28
- if (!isControlled) {
29
- setUncontrolledValue(nextValue);
28
+ if (isControlled) {
29
+ if (nextValue !== value) {
30
+ onChange?.(nextValue);
31
+ }
32
+ return;
30
33
  }
34
+
35
+ setUncontrolledValue(nextValue);
31
36
  if (nextValue !== prevValueRef.current) {
32
37
  onChange?.(nextValue);
33
38
  prevValueRef.current = nextValue;
@@ -15,7 +15,7 @@ vi.mock("@lynx-js/react", async (importOriginal) => {
15
15
  return {
16
16
  ...actual,
17
17
  runOnMainThread:
18
- (_worklet: (...args: never[]) => unknown) =>
18
+ () =>
19
19
  (...args: unknown[]) => {
20
20
  testState.workletCalls.push(args.length);
21
21
  },
@@ -36,14 +36,14 @@ describe("useIconColor", () => {
36
36
  expect(result.current["main-thread:binduiappear"]).toBeDefined();
37
37
  });
38
38
 
39
- it("schedules tint color synchronization when the system theme changes", () => {
39
+ it("avoids cross-thread calls on mount and resynchronizes after theme changes", () => {
40
40
  const { rerender } = renderHook(() => useIconColor([]));
41
41
 
42
- expect(testState.workletCalls).toEqual([3]);
42
+ expect(testState.workletCalls).toEqual([]);
43
43
 
44
44
  testState.theme = "dark";
45
45
  rerender();
46
46
 
47
- expect(testState.workletCalls).toEqual([3, 1, 3]);
47
+ expect(testState.workletCalls).toEqual([3]);
48
48
  });
49
49
  });
@@ -1,4 +1,4 @@
1
- import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef } from "@lynx-js/react";
1
+ import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef, useRef } from "@lynx-js/react";
2
2
  import type { MainThread } from "@lynx-js/types";
3
3
  import type { DependencyList, RefObject } from "@lynx-js/react";
4
4
 
@@ -90,14 +90,20 @@ export function useIconColor(
90
90
  const enabled = options?.enabled ?? true;
91
91
  const frameRef = useMainThreadRef<number>(0);
92
92
  const theme = (useGlobalProps() as { theme?: unknown } | undefined)?.theme;
93
+ const hasMountedRef = useRef(false);
93
94
 
94
95
  function syncOnUiAppear() {
95
96
  "main thread";
96
97
  if (!enabled) return;
97
- syncTintColorOnce(ref, sourceRef);
98
+ scheduleTintColorSync(ref, sourceRef, frameRef);
98
99
  }
99
100
 
100
101
  useEffect(() => {
102
+ if (!hasMountedRef.current) {
103
+ hasMountedRef.current = true;
104
+ return;
105
+ }
106
+
101
107
  if (!enabled) return;
102
108
 
103
109
  runOnMainThread(scheduleTintColorSync)(ref, sourceRef, frameRef);