Skip to content

Commit

Permalink
initial dhairyagothi#469: add hook and toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
jainaryan04 committed Oct 31, 2024
1 parent 6c5b560 commit 14da9f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion frontend/src/Pages/Herosection.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import "./Herosection.css";
import ThemeToggle from "../components/ThemeToggle"
import logo from "../assets/stationsaarthi.svg";
import navigationsvg from "../assets/svg/navigation.svg";
import bookingsvg from "../assets/svg/bookings.svg";
Expand Down Expand Up @@ -56,8 +57,9 @@ const Herosection = () => {
return (
<>
<div className="relative z-50 flex items-center justify-between gap-[88vw]">
<div>
<div className="flex flex-row gap-x-2">
<Navbar />
<ThemeToggle />
</div>

<div className="flex items-center justify-center">
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/components/ThemeToggle.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FaSun, FaMoon } from 'react-icons/fa';
import useDarkMode from '../hooks/useDarkMode';
import "../../src/index.css";

const ThemeSwitcher = () => {
const [theme, setTheme] = useDarkMode();
const isDarkMode = theme === 'dark';

return (
<div className="flex flex-row justify-center items-center">
<label className="relative inline-block w-16 h-9">
<input
type="checkbox"
className="sr-only peer"
checked={isDarkMode}
onChange={() => setTheme(isDarkMode ? 'light' : 'dark')}
/>
<span className="block w-full h-full bg-blue-500 rounded-3xl border-2 border-blue-400 cursor-pointer peer-checked:bg-slate-800 peer-checked:border-blue-400 transition-colors duration-400"></span>

{/* Sun Icon for Light Mode */}
<span className="absolute top-1/2 left-[0.3em] w-4 h-4 bg-gradient-to-r from-pink-500 to-orange-400 rounded-full transform -translate-y-1/2 transition-all duration-400 opacity-100 peer-checked:opacity-0"></span>

{/* Moon Icon for Dark Mode */}
<span className="absolute top-1/2 left-[calc(100%-1.3em)] w-4 h-4 bg-transparent rounded-full transform -translate-y-1/2 transition-all duration-400 opacity-0 peer-checked:opacity-100 peer-checked:shadow-[inset_-3px_-2px_5px_-2px_#8983f7,inset_-8px_-4px_0_0_#a3dafb]"></span>
</label>


</div>
);
};

export default ThemeSwitcher;
16 changes: 16 additions & 0 deletions frontend/src/hooks/useDarkMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useState, useEffect } from 'react';

const useDarkMode = () => {
const [theme, setTheme] = useState(() => localStorage.getItem('theme') || 'light');

useEffect(() => {
const root = document.documentElement;
const isDark = theme === 'dark';
root.classList.toggle('dark', isDark);
localStorage.setItem('theme', theme);
}, [theme]);

return [theme, setTheme];
};

export default useDarkMode;

0 comments on commit 14da9f5

Please sign in to comment.