From 6438db74e00e66b1ae3a973365a8018d6499b965 Mon Sep 17 00:00:00 2001 From: NgocTho Date: Sat, 2 Nov 2024 21:07:59 +0700 Subject: [PATCH] update the Theme Change button applying the theme change to the entire website. --- app/(root)/page.tsx | 42 ++++++++++++-- components/custom/RootNavbar.tsx | 94 ++++++++++++++++++++++++-------- 2 files changed, 109 insertions(+), 27 deletions(-) diff --git a/app/(root)/page.tsx b/app/(root)/page.tsx index e0f82ef..612db62 100644 --- a/app/(root)/page.tsx +++ b/app/(root)/page.tsx @@ -1,18 +1,52 @@ +"use client"; +import React, { useState, useEffect } from "react"; import Link from "next/link"; export default function Home() { + const [darkMode, setDarkMode] = useState(false); + const loadDarkMode = () => { + setDarkMode((prevMode) => { + const newMode = !prevMode; + localStorage.getItem("darkMode"); + return newMode; + }); + }; + + useEffect(() => { + const savedTheme = localStorage.getItem("darkMode"); + if (savedTheme !== null) { + setDarkMode(JSON.parse(savedTheme)); + } + window.addEventListener("darkModeChanged", loadDarkMode); + return () => { + window.removeEventListener("darkModeChanged", loadDarkMode); + }; + }, []); + + useEffect(() => { + if (darkMode) { + document.body.classList.add("dark"); + } else { + document.body.classList.remove("dark"); + } + }, [darkMode]); + return (

Welcome to ML4E

- Discover learning resources, datasets, and a vibrant community for machine learning enthusiasts. + Discover learning resources, datasets, and a vibrant community for + machine learning enthusiasts.

- - Get Started + + + Get Started
); } - diff --git a/components/custom/RootNavbar.tsx b/components/custom/RootNavbar.tsx index 666af3f..1b76879 100644 --- a/components/custom/RootNavbar.tsx +++ b/components/custom/RootNavbar.tsx @@ -3,7 +3,12 @@ import Link from "next/link"; import { useState, useEffect, useRef, useCallback } from "react"; import { Button } from "@/components/ui/button"; -import { Dialog, DialogTrigger, DialogContent, DialogTitle } from "@/components/ui/dialog"; +import { + Dialog, + DialogTrigger, + DialogContent, + DialogTitle, +} from "@/components/ui/dialog"; import Image from "next/image"; import { Search, Moon, Sun, MoreVertical } from "lucide-react"; @@ -65,26 +70,34 @@ function Navbar({ darkMode, toggleDarkMode, isOpen, setIsOpen }: NavbarProps) { /> ML4E - Learn - Datasets - Community + + Learn + + + Datasets + + + Community +
- - - + + + button]:hidden p-6 rounded-lg shadow-lg w-full max-w-2xl outline-none border-none mb-12 max-h-full ${ @@ -121,13 +134,47 @@ function Navbar({ darkMode, toggleDarkMode, isOpen, setIsOpen }: NavbarProps) {
- - + + {dropdownOpen && ( -
- setDropdownOpen(false)} className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg">Home - setDropdownOpen(false)} className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg">Learn - setDropdownOpen(false)} className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg">About +
+ setDropdownOpen(false)} + className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg" + > + Home + + setDropdownOpen(true)} + className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg" + > + Learn + + setDropdownOpen(false)} + className="block w-full px-4 py-2 hover:bg-gray-200 rounded-lg" + > + About +
)}
@@ -163,6 +210,7 @@ export default function RootNavbar() { localStorage.setItem("darkMode", JSON.stringify(newMode)); return newMode; }); + window.dispatchEvent(new Event("darkModeChanged")); }; return (