import { cookies } from "next/headers";
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
// add cookieStore in here
const cookieStore = await cookies();
const mode = cookieStore.get("mode")?.value || "light";
return (
<html>
{ /* add className={mode} in body tag */ }
<body className={mode}>
{children}
</body>
</html>
);
}
// app/api/set-mode
import { saveScreenMode } from "stayedcss/client";
export async function POST(request: Request) {
const { mode } = await request.json();
return saveScreenMode(mode);
}
import { st, stDark } from "stayedcss";
export default function DarkModeExample() {
return (
<div className={style.container}>
{ /* your code */ }
</div>
);
}
const style = st({
componentId: "component/darkmode-example",
container: {
backgroundColor: "white",
},
});
// apply DarkMode
// same componentId with lightmode.
stDark({
componentId: "component/darkmode-example",
container: {
backgroundColor: "black",
},
});
"use client";
import { stClient, stClientDark } from "stayedcss/client";
export default function DarkModeClientExample() {
return (
<div className={style.container}>
{ /* your code */ }
</div>
);
}
const style = stClient({
componentId: "component/darkmode-client-example",
container: {
backgroundColor: "white",
},
});
// apply DarkMode
// same componentId with lightmode.
stClientDark({
componentId: "component/darkmode-client-example",
container: {
backgroundColor: "black",
},
});
"use client";
import { changeScreenMode } from "stayedcss/client";
export default function ModeButton() {
return (
<div className={style.container}>
<button onClick={() => changeScreenMode("light")}>
LightMode
</button>
<button onClick={() => changeScreenMode("dark")}>
DarkMode
</button>
</div>
);
}