@seed-design/lynx-react 0.6.0 → 0.6.2

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;
@@ -6,8 +6,8 @@ export interface UseIconColorOptions {
6
6
  }
7
7
  /**
8
8
  * Lynx `<image>` 의 tint color 를 recipe 의 CSS `color` 로부터 main-thread 에서 읽어
9
- * `tint-color` attribute 로 mirror. `deps` 가 바뀌면 native class patch가 반영된
10
- * 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
9
+ * `tint-color` attribute 로 mirror. `deps` 또는 시스템 테마가 바뀌면 native class
10
+ * patch가 반영된 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
11
11
  *
12
12
  * ```tsx
13
13
  * const iconColorProps = useIconColor([variant, disabled, loading]);
@@ -1,4 +1,4 @@
1
- import { runOnMainThread, useEffect, useMainThreadRef } from "@lynx-js/react";
1
+ import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef } from "@lynx-js/react";
2
2
  //#region src/hooks/useIconColor.ts
3
3
  function syncTintColorOnce(targetRef, sourceRef) {
4
4
  "main thread";
@@ -31,8 +31,8 @@ function cancelTintColorSync(frameRef) {
31
31
  }
32
32
  /**
33
33
  * Lynx `<image>` 의 tint color 를 recipe 의 CSS `color` 로부터 main-thread 에서 읽어
34
- * `tint-color` attribute 로 mirror. `deps` 가 바뀌면 native class patch가 반영된
35
- * 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
34
+ * `tint-color` attribute 로 mirror. `deps` 또는 시스템 테마가 바뀌면 native class
35
+ * patch가 반영된 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
36
36
  *
37
37
  * ```tsx
38
38
  * const iconColorProps = useIconColor([variant, disabled, loading]);
@@ -44,6 +44,7 @@ function useIconColor(deps, options) {
44
44
  const sourceRef = options?.sourceRef;
45
45
  const enabled = options?.enabled ?? true;
46
46
  const frameRef = useMainThreadRef(0);
47
+ const theme = useGlobalProps()?.theme;
47
48
  function syncOnUiAppear() {
48
49
  "main thread";
49
50
  if (!enabled) return;
@@ -55,7 +56,11 @@ function useIconColor(deps, options) {
55
56
  return () => {
56
57
  runOnMainThread(cancelTintColorSync)(frameRef);
57
58
  };
58
- }, [...deps, enabled]);
59
+ }, [
60
+ ...deps,
61
+ enabled,
62
+ theme
63
+ ]);
59
64
  return {
60
65
  ref,
61
66
  "main-thread:binduiappear": syncOnUiAppear
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seed-design/lynx-react",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
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.0",
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",
@@ -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;
@@ -1,13 +1,49 @@
1
+ import type * as ReactLynx from "@lynx-js/react";
1
2
  import { renderHook } from "@lynx-js/react/testing-library";
2
- import { describe, expect, it } from "vitest";
3
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
4
 
4
5
  import { useIconColor } from "./useIconColor";
5
6
 
7
+ const testState = vi.hoisted(() => ({
8
+ theme: "light",
9
+ workletCalls: [] as number[],
10
+ }));
11
+
12
+ vi.mock("@lynx-js/react", async (importOriginal) => {
13
+ const actual = await importOriginal<typeof ReactLynx>();
14
+
15
+ return {
16
+ ...actual,
17
+ runOnMainThread:
18
+ (_worklet: (...args: never[]) => unknown) =>
19
+ (...args: unknown[]) => {
20
+ testState.workletCalls.push(args.length);
21
+ },
22
+ useGlobalProps: () => ({ theme: testState.theme }),
23
+ };
24
+ });
25
+
6
26
  describe("useIconColor", () => {
27
+ beforeEach(() => {
28
+ testState.theme = "light";
29
+ testState.workletCalls.length = 0;
30
+ });
31
+
7
32
  it("returns icon tint sync props", () => {
8
33
  const { result } = renderHook(() => useIconColor([]));
9
34
 
10
35
  expect(result.current).toHaveProperty("ref");
11
36
  expect(result.current["main-thread:binduiappear"]).toBeDefined();
12
37
  });
38
+
39
+ it("schedules tint color synchronization when the system theme changes", () => {
40
+ const { rerender } = renderHook(() => useIconColor([]));
41
+
42
+ expect(testState.workletCalls).toEqual([3]);
43
+
44
+ testState.theme = "dark";
45
+ rerender();
46
+
47
+ expect(testState.workletCalls).toEqual([3, 1, 3]);
48
+ });
13
49
  });
@@ -1,4 +1,4 @@
1
- import { runOnMainThread, useEffect, useMainThreadRef } from "@lynx-js/react";
1
+ import { runOnMainThread, useEffect, useGlobalProps, useMainThreadRef } 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
 
@@ -70,8 +70,8 @@ function cancelTintColorSync(frameRef: RefObject<number>) {
70
70
 
71
71
  /**
72
72
  * Lynx `<image>` 의 tint color 를 recipe 의 CSS `color` 로부터 main-thread 에서 읽어
73
- * `tint-color` attribute 로 mirror. `deps` 가 바뀌면 native class patch가 반영된
74
- * 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
73
+ * `tint-color` attribute 로 mirror. `deps` 또는 시스템 테마가 바뀌면 native class
74
+ * patch가 반영된 다음 frame에 재동기화하고, UI 표시 이벤트에서는 즉시 동기화.
75
75
  *
76
76
  * ```tsx
77
77
  * const iconColorProps = useIconColor([variant, disabled, loading]);
@@ -89,6 +89,7 @@ export function useIconColor(
89
89
  const sourceRef = options?.sourceRef as RefObject<IconElement> | undefined;
90
90
  const enabled = options?.enabled ?? true;
91
91
  const frameRef = useMainThreadRef<number>(0);
92
+ const theme = (useGlobalProps() as { theme?: unknown } | undefined)?.theme;
92
93
 
93
94
  function syncOnUiAppear() {
94
95
  "main thread";
@@ -96,7 +97,6 @@ export function useIconColor(
96
97
  syncTintColorOnce(ref, sourceRef);
97
98
  }
98
99
 
99
- // deps 는 caller 가 책임.
100
100
  useEffect(() => {
101
101
  if (!enabled) return;
102
102
 
@@ -104,7 +104,7 @@ export function useIconColor(
104
104
  return () => {
105
105
  runOnMainThread(cancelTintColorSync)(frameRef);
106
106
  };
107
- }, [...deps, enabled]);
107
+ }, [...deps, enabled, theme]);
108
108
 
109
109
  return { ref, "main-thread:binduiappear": syncOnUiAppear };
110
110
  }