32 lines
1019 B
TypeScript
32 lines
1019 B
TypeScript
"use client";
|
|
|
|
import { MoonIcon, SunIcon, SunMoonIcon } from "lucide-react";
|
|
import { useTheme } from "next-themes";
|
|
import { useSyncExternalStore } from "react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
const subscribe = () => () => {};
|
|
const clientSnapshot = () => true;
|
|
const serverSnapshot = () => false;
|
|
|
|
export function ThemeSwitcher() {
|
|
const { resolvedTheme, setTheme } = useTheme();
|
|
const mounted = useSyncExternalStore(subscribe, clientSnapshot, serverSnapshot);
|
|
const isDark = resolvedTheme === "dark";
|
|
const label = mounted ? (isDark ? "เปลี่ยนเป็นโหมดสว่าง" : "เปลี่ยนเป็นโหมดมืด") : "เปลี่ยนธีม";
|
|
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
aria-label={label}
|
|
title={label}
|
|
disabled={!mounted}
|
|
onClick={() => setTheme(isDark ? "light" : "dark")}
|
|
>
|
|
{!mounted ? <SunMoonIcon /> : isDark ? <SunIcon /> : <MoonIcon />}
|
|
</Button>
|
|
);
|
|
}
|