Files
Checkmate/Client/src/Components/ThemeSwitch/index.jsx
T

51 lines
1.2 KiB
React
Raw Normal View History

2024-12-17 23:57:12 +08:00
/**
* ThemeSwitch Component
* Dark and Light Theme Switch
* Original Code: https://web.dev/patterns/theming/theme-switch
* License: Apache License 2.0
* Copyright © Google LLC
*
* This code has been adapted for use in this project.
* Apache License: https://www.apache.org/licenses/LICENSE-2.0
*/
2024-12-15 23:11:50 +08:00
import { IconButton } from "@mui/material";
import SunAndMoonIcon from "./SunAndMoonIcon";
2024-12-17 01:24:15 +08:00
import { useDispatch, useSelector } from "react-redux";
import { setMode } from "../../Features/UI/uiSlice";
2024-12-18 18:57:38 +08:00
import "./index.css";
2024-12-15 23:11:50 +08:00
2025-01-11 18:06:47 -05:00
const ThemeSwitch = ({ width = 48, height = 48, color }) => {
2024-12-17 01:24:15 +08:00
const mode = useSelector((state) => state.ui.mode);
const dispatch = useDispatch();
const toggleTheme = () => {
dispatch(setMode(mode === "light" ? "dark" : "light"));
};
2024-12-15 23:11:50 +08:00
return (
<IconButton
id="theme-toggle"
title="Toggles light & dark"
2024-12-18 18:57:38 +08:00
className={`theme-${mode}`}
2024-12-15 23:11:50 +08:00
aria-label="auto"
aria-live="polite"
onClick={toggleTheme}
sx={{
2024-12-18 18:57:38 +08:00
width,
height,
2024-12-15 23:11:50 +08:00
display: "flex",
alignItems: "center",
justifyContent: "center",
2025-01-11 18:06:47 -05:00
"& svg >:is(circle, g)": {
fill: color,
stroke: color,
},
2024-12-15 23:11:50 +08:00
}}
>
<SunAndMoonIcon />
</IconButton>
);
};
export default ThemeSwitch;