@mbao01/common 0.0.32 → 0.0.33

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,3 +1,4 @@
1
1
  export type Theme = "dark" | "light";
2
+ export declare const THEME_COOKIE_NAME = "data-theme";
2
3
  export declare const getTheme: () => Theme | null;
3
4
  export declare const saveTheme: (theme: Theme) => void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mbao01/common",
3
3
  "private": false,
4
- "version": "0.0.32",
4
+ "version": "0.0.33",
5
5
  "type": "module",
6
6
  "author": "Ayomide Bakare",
7
7
  "license": "MIT",
@@ -95,6 +95,7 @@
95
95
  "sonner": "^1.4.41",
96
96
  "tailwind-merge": "^2.2.1",
97
97
  "tailwindcss-animate": "^1.0.7",
98
+ "universal-cookie": "^7.1.4",
98
99
  "vaul": "^0.9.0"
99
100
  },
100
101
  "devDependencies": {
@@ -142,5 +143,5 @@
142
143
  "react-dom": "^18.2.0",
143
144
  "typescript": "^5.2.2"
144
145
  },
145
- "gitHead": "3646dbdb6bcfc492b8ec63094f6d16f98c73d4dc"
146
+ "gitHead": "169aeecca51d4544bfed02de64e0d9806dd83c6c"
146
147
  }
@@ -1,17 +1,19 @@
1
+ import Cookies from "universal-cookie";
2
+
1
3
  export type Theme = "dark" | "light";
2
4
 
5
+ export const THEME_COOKIE_NAME = "data-theme";
6
+
3
7
  export const getTheme = () => {
4
8
  if (typeof window === "undefined") return null;
5
9
 
6
- let t = window.localStorage.getItem("__theme") as Theme;
7
- if (!t)
8
- t = window.matchMedia("(prefers-color-scheme: dark)")?.matches
9
- ? "dark"
10
- : "light";
11
- return t;
10
+ const cookies = new Cookies();
11
+ const theme = cookies.get(THEME_COOKIE_NAME) as Theme;
12
+ return theme;
12
13
  };
13
14
 
14
15
  export const saveTheme = (theme: Theme) => {
15
- document.body.setAttribute("data-theme", theme);
16
- window.localStorage.setItem("__theme", theme);
16
+ const cookies = new Cookies();
17
+ cookies.set(THEME_COOKIE_NAME, theme, { secure: true });
18
+ document.body.setAttribute(THEME_COOKIE_NAME, theme);
17
19
  };