Migrate from old project
This commit is contained in:
18
components/Helper/Theme.ts
Normal file
18
components/Helper/Theme.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
"use server";
|
||||
|
||||
import {cookies} from "next/headers";
|
||||
|
||||
// ✅ Get theme from cookies OR detect system preference
|
||||
export async function getInitialTheme(): Promise<"dark" | "light"> {
|
||||
const themeCookie = (await cookies()).get("theme")?.value;
|
||||
|
||||
if (themeCookie === "dark" || themeCookie === "light") {
|
||||
return themeCookie;
|
||||
}
|
||||
|
||||
// Detect system preference
|
||||
const prefersDarkMode =
|
||||
typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
|
||||
return prefersDarkMode ? "dark" : "light";
|
||||
}
|
||||
Reference in New Issue
Block a user