Add icon to ThemeSwitcher.jsx

This commit is contained in:
T'JAMPENS QUENTIN p2406187
2025-12-09 11:43:57 +01:00
parent 1c80e61329
commit 16e91eed9f
4 changed files with 90 additions and 5 deletions
@@ -1,19 +1,30 @@
import Button from "../button/button.jsx";
import React from "react";
import { useState, useEffect } from "react";
import styles from "./ThemeSwitcher.module.css";
export default function ThemeSwitcher() {
const [theme, setTheme] = useState("");
useEffect(() => {
setTheme(localStorage.getItem("theme"));
}, []);
const switchTheme = () => {
const currentTheme = localStorage.getItem("theme");
let newTheme = currentTheme === "light" ? "dark" : "light";
let newTheme = theme === "light" ? "dark" : "light";
if(currentTheme === null) newTheme = "dark";
if(theme === null || theme === "") newTheme = "dark";
localStorage.setItem("theme", newTheme);
setTheme(newTheme);
window.dispatchEvent(new Event("theme-changed"));
};
return <Button variant={"default"} onClick={switchTheme}> Changer de thème </Button>
return <Button variant={"default"} onClick={switchTheme} className={styles.themeBtn}>
<img src={`/public/icons/theme/${theme}.svg`} alt={`${theme} icon`} className={styles.themeIcon} />
<p>Changer de thème </p>
</Button>
}
@@ -0,0 +1,12 @@
.themeBtn {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
gap: 5px;
}
.themeIcon {
height: 20px;
width: 20px;
}