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

40 lines
911 B
React
Raw Normal View History

2024-12-15 23:11:50 +08:00
import React, { useEffect, useState } from "react";
import { IconButton } from "@mui/material";
import SunAndMoonIcon from "./SunAndMoonIcon";
2024-12-15 23:52:07 +08:00
import "./index.css";
2024-12-17 01:24:15 +08:00
import { useDispatch, useSelector } from "react-redux";
import { setMode } from "../../Features/UI/uiSlice";
2024-12-15 23:11:50 +08:00
const ThemeSwitch = () => {
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
useEffect(() => {
2024-12-17 01:24:15 +08:00
document.body.setAttribute("data-theme", mode);
}, [mode]);
2024-12-15 23:11:50 +08:00
return (
<IconButton
id="theme-toggle"
title="Toggles light & dark"
aria-label="auto"
aria-live="polite"
onClick={toggleTheme}
sx={{
width: 48,
height: 48,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<SunAndMoonIcon />
</IconButton>
);
};
export default ThemeSwitch;