Switch theme btn

This commit is contained in:
T'JAMPENS QUENTIN p2406187
2025-12-07 01:54:32 +01:00
parent 0188ac43bf
commit b0de796c45
3 changed files with 40 additions and 6 deletions
+16 -4
View File
@@ -13,9 +13,6 @@ const Background = ({ children, className }) => {
}; };
useEffect(() => { useEffect(() => {
setTheme(localStorage.getItem("theme") ? localStorage.getItem("theme") : "light");
circlesRef.current.forEach((circle) => { circlesRef.current.forEach((circle) => {
if (circle) { if (circle) {
const { x, y } = getRandomTranslation(); const { x, y } = getRandomTranslation();
@@ -25,7 +22,22 @@ const Background = ({ children, className }) => {
}); });
}, []); }, []);
return ( useEffect(() => {
const updateTheme = () => {
setTheme(localStorage.getItem("theme") || "light");
};
updateTheme(); // Initial load
window.addEventListener("theme-changed", updateTheme);
return () => {
window.removeEventListener("theme-changed", updateTheme);
};
}, []);
return (
<div className={`${styles.backgroundContainer} ${className || ''} ${theme === "light" ? styles.backgroundLight : styles.backgroundDark}`}> <div className={`${styles.backgroundContainer} ${className || ''} ${theme === "light" ? styles.backgroundLight : styles.backgroundDark}`}>
<div <div
ref={(el) => (circlesRef.current[0] = el)} ref={(el) => (circlesRef.current[0] = el)}
+17 -1
View File
@@ -15,6 +15,19 @@ function ProfilePage() {
logout(); logout();
} }
const switchTheme = () => {
const currentTheme = localStorage.getItem("theme");
let newTheme = currentTheme === "light" ? "dark" : "light";
if(currentTheme === null) newTheme = "dark";
localStorage.setItem("theme", newTheme);
window.dispatchEvent(new Event("theme-changed"));
};
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={`${styles.profileboard} glassCard`}> <div className={`${styles.profileboard} glassCard`}>
@@ -29,7 +42,10 @@ function ProfilePage() {
<div className={styles.description}> <div className={styles.description}>
<div className={styles.headerProfile}> <div className={styles.headerProfile}>
<h2> {user.name} {user.lastname} </h2> <h2> {user.name} {user.lastname} </h2>
<Button variant={"danger"} onClick={handleLogout}> Déconnexion </Button> <div className={styles.settingsBtns}>
<Button variant={"default"} onClick={switchTheme}> Changer de thème </Button>
<Button variant={"danger"} onClick={handleLogout}> Déconnexion </Button>
</div>
</div> </div>
<div className={styles.topDescription}> <div className={styles.topDescription}>
<div className={styles.infoBlock}> <div className={styles.infoBlock}>
+7 -1
View File
@@ -70,7 +70,13 @@
} }
.profilePicture{ .profilePicture{
width:90%; width:90%;
} }
.settingsBtns {
display: flex;
gap: 5px;
justify-content: right;
}
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
.contentWrapper { .contentWrapper {