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