feat: added dark mode functionality

This commit is contained in:
Peter Carl Pardo committed 2024-12-17 01:24:15 +08:00
1 parent 0e8b82be97
commit 663f2334d1
3 files changed
+17 -23

No files matched your search

@@ -1,6 +1,9 @@
import { useTheme } from "@mui/material";
import "./index.css"; import "./index.css";
const SunAndMoonIcon = () => { const SunAndMoonIcon = () => {
const theme = useTheme();
return ( return (
<svg <svg
className="sun-and-moon" className="sun-and-moon"
@@ -18,13 +21,13 @@ const SunAndMoonIcon = () => {
y="0" y="0"
width="100%" width="100%"
height="100%" height="100%"
fill="white" fill="#fff"
/> />
<circle <circle
cx="24" cx="24"
cy="10" cy="10"
r="6" r="6"
fill="black" fill="#000"
/> />
</mask> </mask>
<circle <circle
@@ -32,12 +35,12 @@ const SunAndMoonIcon = () => {
cx="12" cx="12"
cy="12" cy="12"
r="6" r="6"
fill="currentColor" fill={theme.palette.text.secondary}
mask="url(#moon-mask)" mask="url(#moon-mask)"
/> />
<g <g
className="sun-beams" className="sun-beams"
stroke="currentColor" stroke={theme.palette.text.secondary}
> >
<line <line
x1="12" x1="12"
@@ -2,23 +2,10 @@
transform-origin: center; transform-origin: center;
} }
.sun-and-moon > :is(.moon, .sun) {
fill: var(--icon-fill);
}
.theme-toggle:is(:hover, :focus-visible) > .sun-and-moon > :is(.moon, .sun) {
fill: var(--icon-fill-hover);
}
.theme-toggle .sun-and-moon > .sun-beams { .theme-toggle .sun-and-moon > .sun-beams {
stroke: var(--icon-fill);
stroke-width: 2px; stroke-width: 2px;
} }
.theme-toggle:is(:hover, :focus-visible) .sun-and-moon > .sun-beams {
stroke: var(--icon-fill-hover);
}
[data-theme="dark"] .sun-and-moon > .sun { [data-theme="dark"] .sun-and-moon > .sun {
transform: scale(1.75); transform: scale(1.75);
} }
+10 -6
View File
@@ -2,16 +2,20 @@ import React, { useEffect, useState } from "react";
import { IconButton } from "@mui/material"; import { IconButton } from "@mui/material";
import SunAndMoonIcon from "./SunAndMoonIcon"; import SunAndMoonIcon from "./SunAndMoonIcon";
import "./index.css"; import "./index.css";
import { useDispatch, useSelector } from "react-redux";
import { setMode } from "../../Features/UI/uiSlice";
const ThemeSwitch = () => { const ThemeSwitch = () => {
// TODO: change to use the MUI theme const mode = useSelector((state) => state.ui.mode);
const [currentTheme, setCurrentTheme] = useState("light"); const dispatch = useDispatch();
const toggleTheme = () =>
setCurrentTheme((prevState) => (prevState === "light" ? "dark" : "light")); const toggleTheme = () => {
dispatch(setMode(mode === "light" ? "dark" : "light"));
};
useEffect(() => { useEffect(() => {
document.body.setAttribute("data-theme", currentTheme); document.body.setAttribute("data-theme", mode);
}, [currentTheme]); }, [mode]);
return ( return (
<IconButton <IconButton