mirror of
https://github.com/skidoodle/albert.lol.git
synced 2025-02-15 06:09:15 +01:00
fix themes
This commit is contained in:
parent
db560d1133
commit
e70d71134f
2 changed files with 38 additions and 16 deletions
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue