fix themes

This commit is contained in:
skidoodle 2024-10-29 09:28:27 +01:00
parent db560d1133
commit e70d71134f
2 changed files with 38 additions and 16 deletions

View file

@ -4,6 +4,7 @@ import Link from "next/link";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import { useTheme } from "next-themes"; import { useTheme } from "next-themes";
import type { IconType } from "@/utils/types"; import type { IconType } from "@/utils/types";
import { useEffect, useState } from "react";
export const Icon = ({ export const Icon = ({
children, children,
@ -11,29 +12,43 @@ export const Icon = ({
copyValue, copyValue,
ariaLabel, ariaLabel,
}: IconType) => { }: IconType) => {
const { theme } = useTheme() as { theme: "light" | "dark" }; const { theme } = useTheme() as { theme: "light" | "dark" | "system" };
const [resolvedTheme, setResolvedTheme] = useState<"light" | "dark">("light");
useEffect(() => {
if (!localStorage.getItem("theme")) {
localStorage.setItem("theme", "system");
}
if (theme === "system") {
const prefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
setResolvedTheme(prefersDark ? "dark" : "light");
} else {
setResolvedTheme(theme);
}
}, [theme]);
const toastStyle = {
light: {
background: "var(--light-primary)",
color: "var(--light-text)",
},
dark: {
background: "var(--dark-primary)",
color: "var(--dark-text)",
},
};
const handleCopy = () => { const handleCopy = () => {
toast.remove(); toast.remove();
const toastStyle = {
light: {
background: "var(--light-primary)",
color: "var(--light-text)",
},
dark: {
background: "var(--dark-primary)",
color: "var(--dark-text)",
},
};
toast.success("Copied to clipboard", { toast.success("Copied to clipboard", {
style: { style: {
...toastStyle[theme || "light"], ...toastStyle[resolvedTheme],
fontSize: "1em", fontSize: "1em",
}, },
}); });
copy(reference); copy(reference);
}; };

View file

@ -11,10 +11,17 @@ export const ThemeSwitcher = () => {
useEffect(() => { useEffect(() => {
setMounted(true); setMounted(true);
if (!localStorage.getItem("theme")) {
localStorage.setItem("theme", "system");
}
}, []); }, []);
const toggleTheme = useCallback(() => { const toggleTheme = useCallback(() => {
setTheme(theme === "light" ? "dark" : "light"); const newTheme =
theme === "light" ? "dark" : theme === "dark" ? "light" : "dark";
setTheme(newTheme);
localStorage.setItem("theme", newTheme);
}, [theme, setTheme]); }, [theme, setTheme]);
if (!mounted) return null; if (!mounted) return null;