@zyncat/ui 0.15.0 → 0.17.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.
Files changed (290) hide show
  1. package/README.md +32 -202
  2. package/dist/alert.css +1 -1
  3. package/dist/alert.js +18 -10
  4. package/dist/alert.js.map +1 -1
  5. package/dist/avatar.css +3 -2
  6. package/dist/badge.css +1 -1
  7. package/dist/bloom-HV23M4AW.js +81 -0
  8. package/dist/bloom-HV23M4AW.js.map +1 -0
  9. package/dist/button.css +21 -24
  10. package/dist/button.js +3 -1
  11. package/dist/checkbox.css +1 -1
  12. package/dist/chunk-2YGHPQ66.js +16 -0
  13. package/dist/chunk-2YGHPQ66.js.map +1 -0
  14. package/dist/{chunk-K45CWVHS.js → chunk-3MLUH2QL.js} +4 -4
  15. package/dist/{chunk-K45CWVHS.js.map → chunk-3MLUH2QL.js.map} +1 -1
  16. package/dist/{chunk-FVPNICF4.js → chunk-4PQDVK23.js} +4 -4
  17. package/dist/{chunk-FVPNICF4.js.map → chunk-4PQDVK23.js.map} +1 -1
  18. package/dist/{chunk-FLQVOSFC.js → chunk-6IXLW7QA.js} +7 -7
  19. package/dist/{chunk-FLQVOSFC.js.map → chunk-6IXLW7QA.js.map} +1 -1
  20. package/dist/chunk-7NHY2CRA.js +132 -0
  21. package/dist/chunk-7NHY2CRA.js.map +1 -0
  22. package/dist/{chunk-BH4JFWXQ.js → chunk-7VTWJL72.js} +19 -16
  23. package/dist/chunk-7VTWJL72.js.map +1 -0
  24. package/dist/chunk-7ZSIUUC3.js +50 -0
  25. package/dist/chunk-7ZSIUUC3.js.map +1 -0
  26. package/dist/{chunk-6X3G4GIM.js → chunk-DH7DT6EO.js} +3 -3
  27. package/dist/{chunk-6X3G4GIM.js.map → chunk-DH7DT6EO.js.map} +1 -1
  28. package/dist/{chunk-QGVFFT2B.js → chunk-EOWWVFBF.js} +3 -3
  29. package/dist/{chunk-QGVFFT2B.js.map → chunk-EOWWVFBF.js.map} +1 -1
  30. package/dist/{chunk-RBSHRRNO.js → chunk-FLDIRXFU.js} +5 -5
  31. package/dist/{chunk-RBSHRRNO.js.map → chunk-FLDIRXFU.js.map} +1 -1
  32. package/dist/{chunk-5GBV45YD.js → chunk-GWCBTD63.js} +3 -3
  33. package/dist/{chunk-5GBV45YD.js.map → chunk-GWCBTD63.js.map} +1 -1
  34. package/dist/chunk-GZ6UMHCK.js +103 -0
  35. package/dist/chunk-GZ6UMHCK.js.map +1 -0
  36. package/dist/{chunk-FEXUS4SV.js → chunk-HWDIVGME.js} +11 -5
  37. package/dist/chunk-HWDIVGME.js.map +1 -0
  38. package/dist/chunk-KSAJ6DED.js +16 -0
  39. package/dist/chunk-KSAJ6DED.js.map +1 -0
  40. package/dist/{chunk-UYSXJ7MH.js → chunk-LH2UIEKN.js} +7 -17
  41. package/dist/chunk-LH2UIEKN.js.map +1 -0
  42. package/dist/{chunk-CAXQJSIE.js → chunk-M2A6PZ7J.js} +6 -6
  43. package/dist/{chunk-CAXQJSIE.js.map → chunk-M2A6PZ7J.js.map} +1 -1
  44. package/dist/chunk-NYQQH4R5.js +40 -0
  45. package/dist/chunk-NYQQH4R5.js.map +1 -0
  46. package/dist/{chunk-BTF6DAE3.js → chunk-ODRO3KS6.js} +4 -4
  47. package/dist/{chunk-BTF6DAE3.js.map → chunk-ODRO3KS6.js.map} +1 -1
  48. package/dist/{chunk-GJTHWB6O.js → chunk-QDAN43B6.js} +3 -3
  49. package/dist/{chunk-GJTHWB6O.js.map → chunk-QDAN43B6.js.map} +1 -1
  50. package/dist/{chunk-MDSRZ4QR.js → chunk-RY35IU3G.js} +3 -3
  51. package/dist/{chunk-MDSRZ4QR.js.map → chunk-RY35IU3G.js.map} +1 -1
  52. package/dist/{chunk-GOLSYGRA.js → chunk-TBLPZQSI.js} +84 -48
  53. package/dist/chunk-TBLPZQSI.js.map +1 -0
  54. package/dist/{chunk-5X3MJRIM.js → chunk-UKPX3RIP.js} +3 -3
  55. package/dist/{chunk-5X3MJRIM.js.map → chunk-UKPX3RIP.js.map} +1 -1
  56. package/dist/{chunk-CFRGQJ23.js → chunk-VZXML7EH.js} +4 -4
  57. package/dist/{chunk-CFRGQJ23.js.map → chunk-VZXML7EH.js.map} +1 -1
  58. package/dist/{chunk-PWQVSVXR.js → chunk-WC45VSST.js} +8 -8
  59. package/dist/{chunk-PWQVSVXR.js.map → chunk-WC45VSST.js.map} +1 -1
  60. package/dist/chunk-WQKWPI6V.js +196 -0
  61. package/dist/chunk-WQKWPI6V.js.map +1 -0
  62. package/dist/{chunk-N2GJEO5Z.js → chunk-ZHMOH62S.js} +4 -4
  63. package/dist/chunk-ZHMOH62S.js.map +1 -0
  64. package/dist/cli.js +259 -43
  65. package/dist/collapse.css +8 -5
  66. package/dist/confetti.css +3 -3
  67. package/dist/confetti.js +2 -2
  68. package/dist/date-field.js +16 -14
  69. package/dist/date-field.js.map +1 -1
  70. package/dist/date-picker.css +168 -43
  71. package/dist/date-range-field.js +38 -23
  72. package/dist/date-range-field.js.map +1 -1
  73. package/dist/datetime-field.js +17 -15
  74. package/dist/datetime-field.js.map +1 -1
  75. package/dist/dialog.css +4 -3
  76. package/dist/dialog.js +8 -8
  77. package/dist/digit-strip.css +1 -1
  78. package/dist/dropdown.css +1 -1
  79. package/dist/dropdown.js +8 -8
  80. package/dist/emoji-picker.js +14 -14
  81. package/dist/facebook-feed.css +1 -1
  82. package/dist/flow-field.css +4 -4
  83. package/dist/flow-field.js +3 -3
  84. package/dist/flow-field.js.map +1 -1
  85. package/dist/glide.js +3 -3
  86. package/dist/icon.css +1 -1
  87. package/dist/input.css +7 -4
  88. package/dist/instagram-feed.css +1 -1
  89. package/dist/instagram-feed.js +2 -2
  90. package/dist/lens.css +1 -2
  91. package/dist/lens.js +2 -2
  92. package/dist/mcp.js +1 -1
  93. package/dist/menu-surface.css +1 -1
  94. package/dist/modal.css +1 -1
  95. package/dist/modal.js +6 -6
  96. package/dist/morphing-text.css +2 -2
  97. package/dist/morphing-text.js +2 -2
  98. package/dist/motion-devtools.css +77 -2
  99. package/dist/motion-devtools.js +194 -37
  100. package/dist/motion-devtools.js.map +1 -1
  101. package/dist/motion-tokens.js +1 -1
  102. package/dist/multi-select.js +9 -9
  103. package/dist/number-field.js +3 -3
  104. package/dist/odometer.css +1 -1
  105. package/dist/odometer.js +2 -2
  106. package/dist/overlay.css +1 -1
  107. package/dist/pagination.css +1 -1
  108. package/dist/pagination.js +6 -4
  109. package/dist/pagination.js.map +1 -1
  110. package/dist/paint-CJQCM6ZO.js +260 -0
  111. package/dist/paint-CJQCM6ZO.js.map +1 -0
  112. package/dist/popover.css +1 -1
  113. package/dist/popover.js +6 -6
  114. package/dist/radio-group.css +6 -4
  115. package/dist/radio-group.js +6 -6
  116. package/dist/select.css +1 -1
  117. package/dist/select.js +9 -9
  118. package/dist/sheet.css +1 -1
  119. package/dist/sheet.js +7 -7
  120. package/dist/spinner.css +115 -0
  121. package/dist/spinner.js +4 -0
  122. package/dist/spinner.js.map +1 -0
  123. package/dist/status-badge.js +2 -2
  124. package/dist/styles.css +1 -1
  125. package/dist/support-rail.css +76 -238
  126. package/dist/support-rail.js +97 -105
  127. package/dist/support-rail.js.map +1 -1
  128. package/dist/table.css +2 -2
  129. package/dist/table.js +8 -6
  130. package/dist/table.js.map +1 -1
  131. package/dist/tabs.css +10 -14
  132. package/dist/tabs.js +6 -6
  133. package/dist/tag.css +1 -1
  134. package/dist/tag.js +6 -6
  135. package/dist/text-field.js +3 -3
  136. package/dist/textarea.css +1 -1
  137. package/dist/textarea.js +2 -2
  138. package/dist/theme-grid.js +11 -0
  139. package/dist/theme-grid.js.map +1 -0
  140. package/dist/theme-switcher.css +107 -0
  141. package/dist/theme-switcher.js +70 -0
  142. package/dist/theme-switcher.js.map +1 -0
  143. package/dist/theme-transition-LLC644ZR.js +346 -0
  144. package/dist/theme-transition-LLC644ZR.js.map +1 -0
  145. package/dist/theme-transition.css +48 -0
  146. package/dist/theme.js +57 -26
  147. package/dist/theme.js.map +1 -1
  148. package/dist/tide-DJZMDAFT.js +57 -0
  149. package/dist/tide-DJZMDAFT.js.map +1 -0
  150. package/dist/tiktok.css +2 -3
  151. package/dist/tiktok.js +2 -2
  152. package/dist/time-field.js +4 -4
  153. package/dist/toast.css +2 -14
  154. package/dist/toast.js +11 -9
  155. package/dist/toast.js.map +1 -1
  156. package/dist/toggle-tag.js +2 -2
  157. package/dist/toggle.css +1 -1
  158. package/dist/tooltip.css +1 -1
  159. package/dist/tooltip.js +5 -5
  160. package/dist/trigger.css +1 -1
  161. package/dist/types/components/composites/date-picker/month-jump.d.ts +11 -0
  162. package/dist/types/components/composites/theme-switcher/ThemeGrid.d.ts +20 -0
  163. package/dist/types/components/composites/theme-switcher/ThemeSwitcher.d.ts +24 -0
  164. package/dist/types/components/compound/support-rail/SupportRail.d.ts +10 -8
  165. package/dist/types/components/dev/MotionDevtools.d.ts +10 -5
  166. package/dist/types/components/expressive/theme-transition/bloom.d.ts +2 -0
  167. package/dist/types/components/expressive/theme-transition/paint.d.ts +2 -0
  168. package/dist/types/components/expressive/theme-transition/scene.d.ts +24 -0
  169. package/dist/types/components/expressive/theme-transition/theme-transition.d.ts +19 -0
  170. package/dist/types/components/expressive/theme-transition/tide.d.ts +2 -0
  171. package/dist/types/components/internal/icon/Icon.d.ts +1 -0
  172. package/dist/types/components/primitives/button/Button.d.ts +9 -12
  173. package/dist/types/components/primitives/button/button-style.d.ts +15 -0
  174. package/dist/types/components/primitives/spinner/Spinner.d.ts +27 -0
  175. package/dist/types/engine/animate.d.ts +1 -0
  176. package/dist/types/tokens/component-styles.generated.d.ts +35 -38
  177. package/dist/types/tokens/motion-tokens.d.ts +2 -2
  178. package/dist/types/tokens/theme-store.d.ts +47 -0
  179. package/dist/types/tokens/theme-sync.d.ts +7 -1
  180. package/dist/types/tokens/theme-tokens.generated.d.ts +121 -109
  181. package/dist/types/tokens/theme.d.ts +28 -5
  182. package/dist/typing-lines.css +1 -1
  183. package/dist/typing-lines.js +2 -2
  184. package/dist/weight-field.css +2 -2
  185. package/dist/youtube.css +1 -1
  186. package/dist/youtube.js +2 -2
  187. package/package.json +25 -31
  188. package/skills/zyncat-ui/references/components.md +5 -2
  189. package/src/components/composites/alert/Alert.tsx +7 -3
  190. package/src/components/composites/alert/alert.css +1 -1
  191. package/src/components/composites/date-picker/calendar-panel.tsx +10 -4
  192. package/src/components/composites/date-picker/date-picker.css +168 -43
  193. package/src/components/composites/date-picker/month-jump.tsx +192 -0
  194. package/src/components/composites/date-picker/range-panel.tsx +20 -5
  195. package/src/components/composites/dialog/dialog.css +4 -3
  196. package/src/components/composites/dropdown/dropdown.css +1 -1
  197. package/src/components/composites/emoji-picker/styles.css +1 -1
  198. package/src/components/composites/modal/modal.css +1 -1
  199. package/src/components/composites/pagination/pagination.css +1 -1
  200. package/src/components/composites/popover/popover.css +1 -1
  201. package/src/components/composites/select/select.css +1 -1
  202. package/src/components/composites/sheet/sheet.css +1 -1
  203. package/src/components/composites/table/table.css +2 -2
  204. package/src/components/composites/tabs/tabs.css +10 -14
  205. package/src/components/composites/theme-switcher/ThemeGrid.tsx +133 -0
  206. package/src/components/composites/theme-switcher/ThemeGrid.usage.md +20 -0
  207. package/src/components/composites/theme-switcher/ThemeSwitcher.tsx +75 -0
  208. package/src/components/composites/theme-switcher/ThemeSwitcher.usage.md +21 -0
  209. package/src/components/composites/theme-switcher/theme-switcher.css +107 -0
  210. package/src/components/composites/toast/Toast.tsx +2 -1
  211. package/src/components/composites/toast/toast.css +2 -14
  212. package/src/components/composites/tooltip/tooltip.css +1 -1
  213. package/src/components/compound/support-rail/SupportRail.tsx +116 -128
  214. package/src/components/compound/support-rail/SupportRail.usage.md +7 -6
  215. package/src/components/compound/support-rail/support-rail.css +76 -238
  216. package/src/components/dev/MotionDevtools.tsx +252 -42
  217. package/src/components/dev/MotionDevtools.usage.md +12 -4
  218. package/src/components/dev/motion-devtools.css +77 -2
  219. package/src/components/dev/slowmo-engine.ts +1 -1
  220. package/src/components/expressive/confetti/confetti.css +3 -3
  221. package/src/components/expressive/facebook-feed/facebook-feed.css +1 -1
  222. package/src/components/expressive/flow-field/FlowField.tsx +1 -1
  223. package/src/components/expressive/flow-field/flow-field.css +4 -4
  224. package/src/components/expressive/instagram-feed/instagram-feed.css +1 -1
  225. package/src/components/expressive/lens/lens.css +1 -2
  226. package/src/components/expressive/morphing-text/morphing-text.css +2 -2
  227. package/src/components/expressive/odometer/odometer.css +1 -1
  228. package/src/components/expressive/theme-transition/bloom.ts +88 -0
  229. package/src/components/expressive/theme-transition/paint.ts +296 -0
  230. package/src/components/expressive/theme-transition/scene.ts +38 -0
  231. package/src/components/expressive/theme-transition/theme-transition.css +48 -0
  232. package/src/components/expressive/theme-transition/theme-transition.ts +423 -0
  233. package/src/components/expressive/theme-transition/tide.ts +65 -0
  234. package/src/components/expressive/tiktok/tiktok.css +2 -3
  235. package/src/components/expressive/typing-lines/typing-lines.css +1 -1
  236. package/src/components/expressive/weight-field/weight-field.css +2 -2
  237. package/src/components/expressive/youtube/youtube.css +1 -1
  238. package/src/components/internal/glass/glass.css +1 -1
  239. package/src/components/internal/icon/Icon.tsx +2 -0
  240. package/src/components/internal/icon/icon.css +1 -1
  241. package/src/components/internal/menu/menu-surface.css +1 -1
  242. package/src/components/internal/overlay/overlay.css +1 -1
  243. package/src/components/internal/overlay/trigger.css +1 -1
  244. package/src/components/primitives/avatar/avatar.css +3 -2
  245. package/src/components/primitives/badge/badge.css +1 -1
  246. package/src/components/primitives/badge/digit-strip.css +1 -1
  247. package/src/components/primitives/button/Button.tsx +12 -26
  248. package/src/components/primitives/button/button-style.ts +28 -0
  249. package/src/components/primitives/button/button.css +21 -24
  250. package/src/components/primitives/checkbox/checkbox.css +1 -1
  251. package/src/components/primitives/collapse/collapse.css +8 -5
  252. package/src/components/primitives/input/input.css +7 -4
  253. package/src/components/primitives/radio-group/radio-group.css +6 -4
  254. package/src/components/primitives/spinner/Spinner.tsx +70 -0
  255. package/src/components/primitives/spinner/Spinner.usage.md +19 -0
  256. package/src/components/primitives/spinner/spinner.css +115 -0
  257. package/src/components/primitives/tag/tag.css +1 -1
  258. package/src/components/primitives/textarea/textarea.css +1 -1
  259. package/src/components/primitives/toggle/toggle.css +1 -1
  260. package/src/engine/animate.ts +10 -2
  261. package/src/mcp/server.ts +2 -1
  262. package/src/styles.css +3 -3
  263. package/src/tokens/base.css +5 -5
  264. package/src/tokens/color.css +8 -9
  265. package/src/tokens/component-styles.generated.ts +36 -38
  266. package/src/tokens/dark.css +8 -13
  267. package/src/tokens/decisions.css +5 -8
  268. package/src/tokens/elevation.css +18 -26
  269. package/src/tokens/glass.css +3 -2
  270. package/src/tokens/motion-defaults.generated.ts +1 -1
  271. package/src/tokens/motion-tokens.ts +3 -3
  272. package/src/tokens/motion.css +9 -9
  273. package/src/tokens/radius.css +3 -2
  274. package/src/tokens/reset.css +36 -0
  275. package/src/tokens/semantic.css +10 -18
  276. package/src/tokens/spacing.css +1 -1
  277. package/src/tokens/theme-store.ts +187 -0
  278. package/src/tokens/theme-sync.tsx +49 -2
  279. package/src/tokens/theme-tokens.generated.ts +124 -110
  280. package/src/tokens/theme.tsx +96 -24
  281. package/src/tokens/theme.usage.md +25 -24
  282. package/src/tokens/typography.css +4 -3
  283. package/tailwind.css +7 -13
  284. package/dist/chunk-6SUHSSOK.js +0 -25
  285. package/dist/chunk-6SUHSSOK.js.map +0 -1
  286. package/dist/chunk-BH4JFWXQ.js.map +0 -1
  287. package/dist/chunk-FEXUS4SV.js.map +0 -1
  288. package/dist/chunk-GOLSYGRA.js.map +0 -1
  289. package/dist/chunk-N2GJEO5Z.js.map +0 -1
  290. package/dist/chunk-UYSXJ7MH.js.map +0 -1
@@ -0,0 +1,187 @@
1
+ import { sharedSlot } from '../shared-slot';
2
+
3
+ export type Polarity = 'light' | 'dark';
4
+ export type PolarityPreference = Polarity | 'system';
5
+
6
+ export interface ThemeState {
7
+ /** The active palette - `default` when no other is named. */
8
+ theme: string;
9
+ /** The stored side, or `system` to follow the OS setting. */
10
+ polarity: PolarityPreference;
11
+ /** The side the page is painted in - `system` resolved through the OS setting. */
12
+ resolvedPolarity: Polarity;
13
+ /** Every palette `ZyncatTheme` declared, `default` first. */
14
+ themes: readonly string[];
15
+ /** Display name per palette - the palette's `name`, else its key title-cased. */
16
+ themeNames: Readonly<Record<string, string>>;
17
+ }
18
+
19
+ export interface ThemeControls extends ThemeState {
20
+ /** Switch the palette; the side stays. */
21
+ setTheme(theme: string): void;
22
+ /** Pick a side, or `system` to follow the OS setting. */
23
+ setPolarity(polarity: PolarityPreference): void;
24
+ }
25
+
26
+ export type ThemeBootConfig = { key: string; declared: ThemeState };
27
+
28
+ export interface ThemeChange {
29
+ current: ThemeState;
30
+ next: ThemeState;
31
+ origin?: readonly [number, number];
32
+ commit(): void;
33
+ revert(): void;
34
+ }
35
+
36
+ export interface ThemePreferenceOptions {
37
+ /** Viewport point the change spreads from - the centre of the pressed control. */
38
+ origin?: readonly [number, number];
39
+ }
40
+
41
+ export type ThemeTransitionRunner = (change: ThemeChange) => void;
42
+
43
+ export const DEFAULT_THEME = 'default';
44
+ export const THEME_STORAGE_KEY = 'zyncat-theme';
45
+
46
+ /* Serialised with String() into the inline boot script, so it closes over nothing: every name it needs
47
+ arrives in config, and the attribute names and the polarity words stay literal inside it. */
48
+ export function bootTheme(config: ThemeBootConfig, stored?: unknown): Omit<ThemeState, 'themes' | 'themeNames'> {
49
+ let { theme, polarity, themes } = config.declared;
50
+ if (stored === undefined) {
51
+ try {
52
+ stored = JSON.parse(localStorage.getItem(config.key) || 'null');
53
+ /* Named binding, not `catch {}`: the client target downlevels an omitted binding and the server
54
+ target does not, so String(bootTheme) differs and the inline script mismatches on hydration. */
55
+ } catch (error) {}
56
+ }
57
+ const saved = (stored || {}) as { theme?: unknown; polarity?: unknown };
58
+ if (typeof saved.theme === 'string' && themes.includes(saved.theme)) theme = saved.theme;
59
+ if (saved.polarity === 'light' || saved.polarity === 'dark' || saved.polarity === 'system') polarity = saved.polarity;
60
+ const resolvedPolarity: Polarity =
61
+ polarity === 'system' ? (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : polarity;
62
+ const root = document.documentElement;
63
+ if (theme === themes[0]) root.removeAttribute('data-theme');
64
+ else root.setAttribute('data-theme', theme);
65
+ root.setAttribute('data-polarity', resolvedPolarity);
66
+ let meta = document.querySelector('meta[name="theme-color"]') as HTMLMetaElement | null;
67
+ if (!meta) {
68
+ meta = document.head.appendChild(document.createElement('meta'));
69
+ meta.name = 'theme-color';
70
+ meta.setAttribute('data-zyncat-theme-color', '');
71
+ }
72
+ if (meta.hasAttribute('data-zyncat-theme-color')) {
73
+ meta.style.backgroundColor = 'var(--bg-app)';
74
+ const painted = getComputedStyle(meta).backgroundColor;
75
+ meta.style.backgroundColor = '';
76
+ if (painted && painted !== 'rgba(0, 0, 0, 0)') meta.content = painted;
77
+ }
78
+ return { theme, polarity, resolvedPolarity };
79
+ }
80
+
81
+ const DEFAULT_CONFIG: ThemeBootConfig = {
82
+ key: THEME_STORAGE_KEY,
83
+ declared: {
84
+ theme: DEFAULT_THEME,
85
+ polarity: 'system',
86
+ resolvedPolarity: 'light',
87
+ themes: [DEFAULT_THEME],
88
+ themeNames: { [DEFAULT_THEME]: 'Default' },
89
+ },
90
+ };
91
+
92
+ const store = sharedSlot('tokens.theme@1', () => ({
93
+ json: '',
94
+ config: DEFAULT_CONFIG,
95
+ state: null as ThemeState | null,
96
+ attached: false,
97
+ listeners: new Set<() => void>(),
98
+ transition: null as ThemeTransitionRunner | null,
99
+ pending: null as ThemeState | null,
100
+ }));
101
+
102
+ const notify = () => {
103
+ for (const listener of store.listeners) listener();
104
+ };
105
+
106
+ export function configureThemeStore(json: string): void {
107
+ if (json === store.json) return;
108
+ store.json = json;
109
+ store.config = JSON.parse(json);
110
+ store.state = null;
111
+ }
112
+
113
+ export function applyTheme(stored?: unknown): void {
114
+ const { themes, themeNames } = store.config.declared;
115
+ store.state = { ...bootTheme(store.config, stored), themes, themeNames };
116
+ notify();
117
+ }
118
+
119
+ export const getThemeSnapshot = (): ThemeState => store.state ?? store.config.declared;
120
+ export const getThemeTargetSnapshot = (): ThemeState => store.pending ?? getThemeSnapshot();
121
+ export const getServerThemeSnapshot = (): ThemeState => store.config.declared;
122
+
123
+ export function setThemeTransition(runner: ThemeTransitionRunner | null): void {
124
+ store.transition = runner;
125
+ }
126
+
127
+ const resolvePolarity = (polarity: PolarityPreference): Polarity =>
128
+ polarity === 'system' ? (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : polarity;
129
+
130
+ const persistTheme = (choice: Pick<ThemeState, 'theme' | 'polarity'>) => {
131
+ try {
132
+ localStorage.setItem(store.config.key, JSON.stringify(choice));
133
+ } catch {}
134
+ applyTheme(choice);
135
+ };
136
+
137
+ const settlePending = (choice: Pick<ThemeState, 'theme' | 'polarity'>) => {
138
+ if (store.pending && store.pending.theme === choice.theme && store.pending.polarity === choice.polarity) {
139
+ store.pending = null;
140
+ }
141
+ };
142
+
143
+ export function setThemePreference(
144
+ patch: Partial<Pick<ThemeState, 'theme' | 'polarity'>>,
145
+ options: ThemePreferenceOptions = {},
146
+ ): void {
147
+ const current = getThemeSnapshot();
148
+ const base = store.pending ?? current;
149
+ const theme = patch.theme && current.themes.includes(patch.theme) ? patch.theme : base.theme;
150
+ const polarity = patch.polarity ?? base.polarity;
151
+ const choice = { theme, polarity };
152
+ if (!store.transition) {
153
+ persistTheme(choice);
154
+ return;
155
+ }
156
+ const next: ThemeState = { ...current, theme, polarity, resolvedPolarity: resolvePolarity(polarity) };
157
+ store.pending = next;
158
+ notify();
159
+ store.transition({
160
+ current,
161
+ next,
162
+ origin: options.origin,
163
+ commit: () => {
164
+ settlePending(choice);
165
+ persistTheme(choice);
166
+ },
167
+ revert: () => {
168
+ store.pending = null;
169
+ notify();
170
+ },
171
+ });
172
+ }
173
+
174
+ export function subscribeTheme(listener: () => void): () => void {
175
+ store.listeners.add(listener);
176
+ if (!store.attached) {
177
+ store.attached = true;
178
+ matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
179
+ if (store.state?.polarity === 'system') applyTheme(store.state);
180
+ });
181
+ window.addEventListener('storage', (event) => {
182
+ if (event.key === null || event.key === store.config.key) applyTheme();
183
+ });
184
+ }
185
+ if (!store.state) applyTheme();
186
+ return () => store.listeners.delete(listener);
187
+ }
@@ -1,15 +1,62 @@
1
1
  'use client';
2
2
 
3
- import { useEffect } from 'react';
3
+ import { useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from 'react';
4
4
 
5
+ import type { ThemeTransitionSetting } from '../components/expressive/theme-transition/theme-transition';
5
6
  import { refreshMotionTokens } from './motion-tokens';
7
+ import * as store from './theme-store';
8
+
9
+ const loadThemeTransition = () => import('../components/expressive/theme-transition/theme-transition');
10
+
11
+ const setTheme = (theme: string) => store.setThemePreference({ theme });
12
+ const setPolarity = (polarity: store.PolarityPreference) => store.setThemePreference({ polarity });
13
+
14
+ export function useTheme(): store.ThemeControls {
15
+ const state = useSyncExternalStore(store.subscribeTheme, store.getThemeSnapshot, store.getServerThemeSnapshot);
16
+ return useMemo(() => ({ ...state, setTheme, setPolarity }), [state]);
17
+ }
18
+
19
+ export function useThemeTarget(): store.ThemeState {
20
+ return useSyncExternalStore(store.subscribeTheme, store.getThemeTargetSnapshot, store.getServerThemeSnapshot);
21
+ }
22
+
23
+ export function ThemeSync({
24
+ css,
25
+ config,
26
+ transition,
27
+ }: {
28
+ css: string;
29
+ config: string | null;
30
+ transition?: ThemeTransitionSetting;
31
+ }): null {
32
+ if (config) store.configureThemeStore(config);
33
+ const transitionKey = config && transition ? JSON.stringify(transition) : null;
34
+
35
+ useLayoutEffect(() => {
36
+ if (config) store.applyTheme();
37
+ }, [config]);
38
+
39
+ useEffect(() => {
40
+ if (!transitionKey) return;
41
+ const setting = JSON.parse(transitionKey) as ThemeTransitionSetting;
42
+ let live = true;
43
+ void loadThemeTransition().then((module) => {
44
+ if (!live) return;
45
+ module.preloadThemeTransition(setting);
46
+ store.setThemeTransition((change) => module.runThemeTransition(change, setting));
47
+ });
48
+ return () => {
49
+ live = false;
50
+ store.setThemeTransition(null);
51
+ };
52
+ }, [transitionKey]);
6
53
 
7
- export function ThemeSync({ css }: { css: string }): null {
8
54
  useEffect(() => {
9
55
  refreshMotionTokens();
10
56
  return () => {
11
57
  refreshMotionTokens();
12
58
  };
13
59
  }, [css]);
60
+
14
61
  return null;
15
62
  }