feat: apply Popover in MainLayout for theme configure

This commit is contained in:
p-sw 2024-06-01 08:55:12 +09:00
parent b2cd9ba866
commit 39adc916d4

View File

@ -1,24 +1,26 @@
import { useState } from "react"; import { useEffect, useState } from "react";
import { Link, Outlet, useLocation } from "react-router-dom"; import { Link, Outlet, useLocation } from "react-router-dom";
import { Button } from "../components/Button"; import { Button } from "../components/Button";
import RouteObject from "./RouteObject"; import RouteObject from "./RouteObject";
import { Toaster } from "@components/Toast"; import { Toaster } from "@components/Toast";
import { Popover, PopoverContent, PopoverTrigger } from "@components/Popover";
type Theme = "light" | "dark"; type Theme = "light" | "dark" | "system";
function ThemeButton() { function ThemeButton() {
const [theme, _setTheme] = useState<Theme>("light"); const [theme, setTheme] = useState<Theme>(
function setTheme(t: Theme) { (localStorage.getItem("theme") as Theme) || "system"
_setTheme(t); );
document.documentElement.classList.toggle("dark", t === "dark"); useEffect(() => {
} document.documentElement.classList.toggle("dark", theme === "dark");
document.documentElement.classList.toggle("system", theme === "system");
localStorage.setItem("theme", theme);
}, [theme]);
return ( return (
<Button <Popover>
preset="ghost" <PopoverTrigger>
size="icon" <Button preset="ghost" size="icon">
onClick={() => setTheme(theme === "light" ? "dark" : "light")}
>
{/* material-symbols:light-mode */} {/* material-symbols:light-mode */}
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
@ -46,6 +48,31 @@ function ThemeButton() {
/> />
</svg> </svg>
</Button> </Button>
</PopoverTrigger>
<PopoverContent anchor="bottomLeft" className="w-32">
<Button
preset="ghost"
onClick={() => setTheme("light")}
className="w-full px-2 py-1.5 text-sm"
>
Light
</Button>
<Button
preset="ghost"
onClick={() => setTheme("dark")}
className="w-full px-2 py-1.5 text-sm"
>
Dark
</Button>
<Button
preset="ghost"
onClick={() => setTheme("system")}
className="w-full px-2 py-1.5 text-sm"
>
System
</Button>
</PopoverContent>
</Popover>
); );
} }