@bug-on/md3-react 3.0.0 → 3.0.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.
@@ -9,7 +9,7 @@ import {
9
9
  useMemo,
10
10
  useState,
11
11
  } from "react";
12
- import { applyTheme, type ThemeMode } from "../../lib/theme-utils";
12
+ import { applyTheme, resolveMode, type ThemeMode } from "../../lib/theme-utils";
13
13
  import {
14
14
  SnackbarContext,
15
15
  SnackbarHost,
@@ -28,6 +28,8 @@ interface ThemeContextValue {
28
28
  setSourceColor: (color: string) => void;
29
29
  mode: ThemeMode;
30
30
  setMode: (mode: ThemeMode) => void;
31
+ /** The resolved color scheme actually applied — always "light" or "dark". */
32
+ effectiveMode: "light" | "dark";
31
33
  }
32
34
 
33
35
  const ThemeContext = createContext<ThemeContextValue | undefined>(undefined);
@@ -104,7 +106,12 @@ export function MD3ThemeProvider({
104
106
  ) as ThemeMode | null;
105
107
 
106
108
  if (savedColor) setSourceColor(savedColor);
107
- if (savedMode === "light" || savedMode === "dark") setMode(savedMode);
109
+ if (
110
+ savedMode === "light" ||
111
+ savedMode === "dark" ||
112
+ savedMode === "system"
113
+ )
114
+ setMode(savedMode);
108
115
 
109
116
  setIsHydrated(true);
110
117
  }, [persistToLocalStorage]);
@@ -120,9 +127,24 @@ export function MD3ThemeProvider({
120
127
  }
121
128
  }, [sourceColor, mode, persistToLocalStorage, isHydrated]);
122
129
 
130
+ // ── System preference subscription ───────────────────────────────────────
131
+ // When mode is "system", listen for OS-level dark/light changes in real time
132
+ useEffect(() => {
133
+ if (mode !== "system" || typeof window === "undefined") return;
134
+
135
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
136
+ const handleChange = () => applyTheme(sourceColor, "system");
137
+
138
+ mediaQuery.addEventListener("change", handleChange);
139
+ return () => mediaQuery.removeEventListener("change", handleChange);
140
+ }, [mode, sourceColor]);
141
+
142
+ // ── Derived effective mode (no extra state needed) ────────────────────────
143
+ const effectiveMode = resolveMode(mode);
144
+
123
145
  const themeValue = useMemo<ThemeContextValue>(
124
- () => ({ sourceColor, setSourceColor, mode, setMode }),
125
- [sourceColor, mode],
146
+ () => ({ sourceColor, setSourceColor, mode, setMode, effectiveMode }),
147
+ [sourceColor, mode, effectiveMode],
126
148
  );
127
149
 
128
150
  // ── Typography value ─────────────────────────────────────────────────────
@@ -184,7 +206,10 @@ export function useTheme(): ThemeContextValue {
184
206
  return context;
185
207
  }
186
208
 
187
- export function useThemeMode(): Pick<ThemeContextValue, "mode" | "setMode"> {
188
- const { mode, setMode } = useTheme();
189
- return { mode, setMode };
209
+ export function useThemeMode(): Pick<
210
+ ThemeContextValue,
211
+ "mode" | "setMode" | "effectiveMode"
212
+ > {
213
+ const { mode, setMode, effectiveMode } = useTheme();
214
+ return { mode, setMode, effectiveMode };
190
215
  }
package/tsup.config.ts CHANGED
@@ -2,18 +2,18 @@ import type { Options } from "tsup";
2
2
  import { defineConfig } from "tsup";
3
3
 
4
4
  const config: Options = {
5
- entry: ["src/index.ts"],
5
+ entry: ["src/index.ts", "src/plugin.ts"],
6
6
  format: ["cjs", "esm"],
7
7
  dts: true,
8
8
  clean: false, // Tắt clean để tránh nuke mất typography.css khi Next.js đang khởi động
9
9
  // externalize peer dependencies — user project sẽ cung cấp
10
- external: ["react", "react-dom", "motion"],
10
+ external: ["react", "react-dom", "motion", "tailwindcss"],
11
11
  // Tree-shaking tối đa, không split vì thư viện nhỏ
12
12
  treeshake: true,
13
13
  splitting: false,
14
14
  sourcemap: true,
15
15
  outDir: "dist",
16
- // "use client" sẽ được chèn thủ công bằng script phía sau (copy-assets.js)
16
+ // "use client" được inject sau build bằng copy-assets.js (banner của tsup gây esbuild warning cho library)
17
17
  onSuccess: "node scripts/copy-assets.js",
18
18
  };
19
19
 
package/test_output.txt DELETED
@@ -1,164 +0,0 @@
1
-
2
- > @bug-on/md3-react@2.0.3 test
3
- > vitest run src/ui/menu/menu.test.tsx
4
-
5
-
6
- RUN v4.1.0 /Users/stark/Documents/GitHub/bug-on-md3-expressive/packages/react
7
-
8
- ❯ src/ui/menu/menu.test.tsx (28 tests | 8 failed) 16014ms
9
- ✓ renders children when open 277ms
10
- × MenuItem applies correct shape class for each itemPosition 82ms
11
- ✓ MenuItem shows check icon when selected 83ms
12
- ✓ MenuItem applies disabled state — opacity class and aria-disabled 66ms
13
- × MenuGroup auto-injects correct itemPosition based on child index 56ms
14
- × MenuDivider renders with correct role and classes 51ms
15
- × Standard variant applies surface-container-low on MenuGroup 49ms
16
- × Vibrant variant applies tertiary-container on MenuGroup 49ms
17
- ✓ Keyboard ArrowDown: menu opens via click 91ms
18
- ✓ Keyboard Escape closes menu 77ms
19
- ✓ renders children directly without a trigger 4ms
20
- ✓ gap separatorStyle renders no divider elements between groups 3ms
21
- ✓ divider separatorStyle inserts an hr between each pair of groups 5ms
22
- ✓ VerticalMenuDivider renders as hr with correct classes 2ms
23
- ✓ auto-injects index and count props into group children for shape morphing 3ms
24
- ✓ standard colorVariant gap: root is transparent, MenuGroup has surface-container-low 3ms
25
- ✓ vibrant colorVariant gap: root is transparent, MenuGroup has tertiary-container 2ms
26
- ✓ standard colorVariant divider: VerticalMenuContent has surface-container-low 2ms
27
- ✓ VerticalMenu root has role=menu and aria-orientation=vertical 2ms
28
- ✓ ArrowDown key moves focus to next menuitem 14ms
29
- ✓ ArrowUp from first item wraps to last item 9ms
30
- ✓ MenuItem inside VerticalMenu shows check icon when selected=true 4ms
31
- ✓ renders trigger item correctly 35ms
32
- × opens after hoverOpenDelay (default 200ms) 5009ms
33
- × closes after hoverCloseDelay (default 300ms) 5008ms
34
- ✓ useMenuContext returns default values when used outside Provider 4ms
35
- × MenuGroup updates state on hover for shape morphing 5003ms
36
- ✓ FAST_SPATIAL_SPRING has correct spring parameters 0ms
37
-
38
- ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯
39
-
40
- FAIL src/ui/menu/menu.test.tsx > Menu > MenuItem applies correct shape class for each itemPosition
41
- AssertionError: expected 'relative flex w-full cursor-pointer s…' to contain 'rounded-t-[12px]'
42
-
43
- Expected: "rounded-t-[12px]"
44
- Received: "relative flex w-full cursor-pointer select-none items-center outline-none min-h-12 min-w-28 max-w-70 px-3 rounded-none transition-[border-radius,background-color] duration-150 ease-in text-m3-on-surface hover:bg-m3-on-surface/8 focus:bg-m3-on-surface/12 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-m3-primary"
45
-
46
- ❯ src/ui/menu/menu.test.tsx:90:29
47
- 88|
48
- 89| // Shape classes based on ITEM_SHAPE_CLASSES token
49
- 90| expect(leading.className).toContain("rounded-t-[12px]");
50
- | ^
51
- 91| expect(leading.className).toContain("rounded-b-[4px]");
52
- 92| expect(middle.className).toContain("rounded-[4px]");
53
-
54
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/8]⎯
55
-
56
- FAIL src/ui/menu/menu.test.tsx > Menu > MenuGroup auto-injects correct itemPosition based on child index
57
- AssertionError: expected 'relative flex w-full cursor-pointer s…' to contain 'rounded-t-[12px]'
58
-
59
- Expected: "rounded-t-[12px]"
60
- Received: "relative flex w-full cursor-pointer select-none items-center outline-none min-h-12 min-w-28 max-w-70 px-3 rounded-none transition-[border-radius,background-color] duration-150 ease-in text-m3-on-surface hover:bg-m3-on-surface/8 focus:bg-m3-on-surface/12 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-m3-primary"
61
-
62
- ❯ src/ui/menu/menu.test.tsx:146:27
63
- 144|
64
- 145| // First item should be "leading" shape: rounded-t-[12px] rounded-b-…
65
- 146| expect(first.className).toContain("rounded-t-[12px]");
66
- | ^
67
- 147| // Last item should be "trailing" shape: rounded-t-[4px] rounded-b-[…
68
- 148| expect(last.className).toContain("rounded-b-[12px]");
69
-
70
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/8]⎯
71
-
72
- FAIL src/ui/menu/menu.test.tsx > Menu > MenuDivider renders with correct role and classes
73
- AssertionError: expected 'my-2 mx-0 h-px border-0 bg-m3-outline…' to contain 'mx-3'
74
-
75
- Expected: "mx-3"
76
- Received: "my-2 mx-0 h-px border-0 bg-m3-outline-variant"
77
-
78
- ❯ src/ui/menu/menu.test.tsx:167:29
79
- 165| const divider = screen.getByTestId("divider");
80
- 166| expect(divider.getAttribute("role")).toBe("separator");
81
- 167| expect(divider.className).toContain("mx-3");
82
- | ^
83
- 168| expect(divider.className).toContain("bg-m3-outline-variant");
84
- 169| });
85
-
86
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/8]⎯
87
-
88
- FAIL src/ui/menu/menu.test.tsx > Menu > Standard variant applies surface-container-low on MenuGroup
89
- AssertionError: expected 'relative overflow-hidden py-1 bg-tran…' to contain 'bg-m3-surface-container-low'
90
-
91
- Expected: "bg-m3-surface-container-low"
92
- Received: "relative overflow-hidden py-1 bg-transparent"
93
-
94
- ❯ src/ui/menu/menu.test.tsx:186:27
95
- 184| );
96
- 185| const group = screen.getByTestId("group-standard");
97
- 186| expect(group.className).toContain("bg-m3-surface-container-low");
98
- | ^
99
- 187| });
100
- 188|
101
-
102
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/8]⎯
103
-
104
- FAIL src/ui/menu/menu.test.tsx > Menu > Vibrant variant applies tertiary-container on MenuGroup
105
- AssertionError: expected 'relative overflow-hidden py-1 bg-tran…' to contain 'bg-m3-tertiary-container'
106
-
107
- Expected: "bg-m3-tertiary-container"
108
- Received: "relative overflow-hidden py-1 bg-transparent"
109
-
110
- ❯ src/ui/menu/menu.test.tsx:204:27
111
- 202| );
112
- 203| const group = screen.getByTestId("group-vibrant");
113
- 204| expect(group.className).toContain("bg-m3-tertiary-container");
114
- | ^
115
- 205| });
116
- 206|
117
-
118
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/8]⎯
119
-
120
- FAIL src/ui/menu/menu.test.tsx > SubMenu > opens after hoverOpenDelay (default 200ms)
121
- Error: Test timed out in 5000ms.
122
- If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
123
- ❯ src/ui/menu/menu.test.tsx:513:2
124
- 511|
125
- 512| // 2. SubMenu opens after hover delay
126
- 513| it("opens after hoverOpenDelay (default 200ms)", async () => {
127
- | ^
128
- 514| vi.useFakeTimers();
129
- 515| const user = userEvent.setup({ delay: null });
130
-
131
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/8]⎯
132
-
133
- FAIL src/ui/menu/menu.test.tsx > SubMenu > closes after hoverCloseDelay (default 300ms)
134
- Error: Test timed out in 5000ms.
135
- If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
136
- ❯ src/ui/menu/menu.test.tsx:553:2
137
- 551|
138
- 552| // 3. SubMenu closes after hover close delay
139
- 553| it("closes after hoverCloseDelay (default 300ms)", async () => {
140
- | ^
141
- 554| vi.useFakeTimers();
142
- 555| const user = userEvent.setup({ delay: null });
143
-
144
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/8]⎯
145
-
146
- FAIL src/ui/menu/menu.test.tsx > Menu Internals > MenuGroup updates state on hover for shape morphing
147
- Error: Test timed out in 5000ms.
148
- If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
149
- ❯ src/ui/menu/menu.test.tsx:616:2
150
- 614|
151
- 615| // 2. MenuGroup shape morphing triggers on hover
152
- 616| it("MenuGroup updates state on hover for shape morphing", async () =>…
153
- | ^
154
- 617| const user = userEvent.setup();
155
- 618| render(
156
-
157
- ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/8]⎯
158
-
159
-
160
- Test Files 1 failed (1)
161
- Tests 8 failed | 20 passed (28)
162
- Start at 09:33:33
163
- Duration 17.69s (transform 152ms, setup 97ms, import 567ms, tests 16.01s, environment 805ms)
164
-
@@ -1,5 +0,0 @@
1
- npm error Missing script: "test"
2
- npm error
3
- npm error To see a list of scripts, run:
4
- npm error npm run
5
- npm error A complete log of this run can be found in: /Users/stark/.npm/_logs/2026-04-20T02_34_50_686Z-debug-0.log