margo-ui 5.1.0 → 5.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "margo-ui",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "React UI kit built on Tailwind CSS v4 design tokens, themeable through plain CSS custom properties.",
5
5
  "keywords": [
6
6
  "react",
@@ -1,5 +1,5 @@
1
1
  export const chipBaseClassName = `bg-on-main/8 margo-text-box-trim flex items-center justify-center px-3 py-0.5
2
- whitespace-nowrap lowercase flex-none leading-none rounded-margo-sm text-mc shadow-chip
2
+ whitespace-nowrap lowercase flex-none rounded-margo-sm text-mc leading-none shadow-chip
3
3
  focus-visible:outline focus-visible:outline-offset-1 focus-visible:outline-on-main`;
4
4
 
5
5
  export const chipActiveClassName = "bg-on-main text-main";
@@ -1,30 +1,38 @@
1
- import { useCallback, useEffect, useState } from "react";
1
+ import { useSyncExternalStore } from "react";
2
2
 
3
- import { margoThemeClient } from "../utils/theme/theme.js";
3
+ import { margoTheme } from "../utils/theme/theme.js";
4
4
 
5
5
  import type { MargoTheme } from "../utils/theme/type.js";
6
6
 
7
- export const useMargoTheme = (): [MargoTheme, (next: MargoTheme | ((current: MargoTheme) => MargoTheme)) => void] => {
8
- const [theme, setThemeState] = useState<MargoTheme>(() => margoThemeClient.get());
7
+ type UseMargoTheme = {
8
+ theme: MargoTheme | undefined;
9
+ set: (next: MargoTheme) => void;
10
+ toggle: () => void;
11
+ };
12
+
13
+ const getSnapshot = (): MargoTheme =>
14
+ document.documentElement.classList.contains(margoTheme.DARK) ? margoTheme.DARK : margoTheme.LIGHT;
9
15
 
10
- useEffect(() => {
11
- if (typeof document === "undefined") return;
16
+ const getServerSnapshot = () => undefined;
12
17
 
13
- const root = document.documentElement;
14
- setThemeState(margoThemeClient.get());
18
+ const subscribe = (onChange: () => void) => {
19
+ const observer = new MutationObserver(onChange);
20
+ observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
15
21
 
16
- const observer = new MutationObserver(() => {
17
- setThemeState(margoThemeClient.get());
18
- });
22
+ return () => observer.disconnect();
23
+ };
19
24
 
20
- observer.observe(root, { attributes: true, attributeFilter: ["class"] });
25
+ const applyTheme = (next: MargoTheme) => {
26
+ const { classList } = document.documentElement;
27
+
28
+ classList.remove(margoTheme.LIGHT, margoTheme.DARK);
29
+ classList.add(next);
30
+ };
21
31
 
22
- return () => observer.disconnect();
23
- }, []);
32
+ const toggle = () => applyTheme(getSnapshot() === margoTheme.DARK ? margoTheme.LIGHT : margoTheme.DARK);
24
33
 
25
- const setTheme = useCallback((next: MargoTheme | ((current: MargoTheme) => MargoTheme)) => {
26
- margoThemeClient.set(typeof next === "function" ? next(margoThemeClient.get()) : next);
27
- }, []);
34
+ export const useMargoTheme = (): UseMargoTheme => {
35
+ const theme = useSyncExternalStore<MargoTheme | undefined>(subscribe, getSnapshot, getServerSnapshot);
28
36
 
29
- return [theme, setTheme];
37
+ return { theme, set: applyTheme, toggle };
30
38
  };
package/src/index.ts CHANGED
@@ -33,7 +33,7 @@ export { useMargoTheme } from "./hooks/use_theme.js";
33
33
 
34
34
  export { cn } from "./utils/cn/cn.js";
35
35
  export { isMargoLayerSupported } from "./utils/support/support.js";
36
- export { margoTheme, margoThemeClient } from "./utils/theme/theme.js";
36
+ export { margoTheme } from "./utils/theme/theme.js";
37
37
 
38
38
  export type { BlockquoteProps } from "./components/blockquote/type.js";
39
39
 
@@ -83,4 +83,4 @@ export type { MaskGradientXProps } from "./hoc/mask_gradient_x/type.js";
83
83
  export type { MaskGradientYProps } from "./hoc/mask_gradient_y/type.js";
84
84
  export type { RippleProps } from "./hoc/ripple/type.js";
85
85
 
86
- export type { MargoTheme, MargoThemeClient } from "./utils/theme/type.js";
86
+ export type { MargoTheme } from "./utils/theme/type.js";
@@ -1,26 +1,6 @@
1
- import type { MargoTheme, MargoThemeClient, MargoThemeConstants } from "./type.js";
1
+ import type { MargoThemeConstants } from "./type.js";
2
2
 
3
3
  export const margoTheme: MargoThemeConstants = {
4
4
  LIGHT: "light",
5
5
  DARK: "dark",
6
6
  };
7
-
8
- const get = (): MargoTheme => {
9
- if (typeof document === "undefined") return margoTheme.DARK;
10
-
11
- return document.documentElement.classList.contains(margoTheme.DARK) ? margoTheme.DARK : margoTheme.LIGHT;
12
- };
13
-
14
- const set = (next: MargoTheme) => {
15
- if (typeof document === "undefined") return;
16
-
17
- document.documentElement.classList.toggle(margoTheme.DARK, next === margoTheme.DARK);
18
- };
19
-
20
- const toggle = () => set(get() === margoTheme.DARK ? margoTheme.LIGHT : margoTheme.DARK);
21
-
22
- export const margoThemeClient: MargoThemeClient = {
23
- get,
24
- set,
25
- toggle,
26
- };
@@ -4,9 +4,3 @@ export type MargoThemeConstants = {
4
4
  readonly LIGHT: MargoTheme;
5
5
  readonly DARK: MargoTheme;
6
6
  };
7
-
8
- export type MargoThemeClient = {
9
- get: () => MargoTheme;
10
- set: (next: MargoTheme) => void;
11
- toggle: () => void;
12
- };