Skip to content

Commit 10b26ff

Browse files
authored
Merge pull request #24 from Abhi0049k/feat/light-dark-mode
Feat: add dark and light mode for website
2 parents e570f80 + 414dcb9 commit 10b26ff

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/components/Navbar.jsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import logo from '@/assets/images/logo.png';
33
import { Link, useNavigate } from 'react-router-dom';
44
import courses from '@/data/courseData';
5+
import { useTheme } from '@/context/ThemeContext';
6+
57

68
function Navbar({ loggedin }) {
79
const [searchTerm, setSearchTerm] = useState('');
810
const [suggestions, setSuggestions] = useState([]);
11+
const { theme, toggleTheme } = useTheme();
912
const navigate = useNavigate();
1013

1114
const handleSearchChange = (event) => {
@@ -39,7 +42,7 @@ function Navbar({ loggedin }) {
3942
};
4043

4144
return (
42-
<nav className="navbar navbar-expand-lg sticky-top bg-body-tertiary">
45+
<nav className={`navbar navbar-expand-lg sticky-top bg-body-tertiary navbar-${theme}`}>
4346
<div className="container-fluid">
4447
<button
4548
className="navbar-toggler"
@@ -111,9 +114,23 @@ function Navbar({ loggedin }) {
111114
</ul>
112115
</li>
113116
</ul>
114-
117+
<span className="me-3">
118+
<button
119+
className="btn clk rounded-circle"
120+
onClick={toggleTheme}
121+
data-bs-toggle="tooltip"
122+
title={`Switch to ${theme === 'light' ? 'Dark' : 'Light'} Mode`}
123+
>
124+
{theme === 'light' ? (
125+
<i className="fa-solid fa-moon"></i>
126+
) : (
127+
<i className="fa-solid fa-sun"></i>
128+
)}
129+
</button>
130+
</span>
115131
{loggedin === 'true' ? (
116132
<div className="d-flex align-items-center flex-column flex-lg-row">
133+
117134
<div className="me-2 mb-2 mb-lg-0 position-relative">
118135
<form className="input-group" onSubmit={handleSearchSubmit}>
119136
<span className="input-group-text" id="basic-addon1">

src/context/ThemeContext.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export const ThemeProvider = ({ children }) => {
66
const [theme, setTheme] = useState('light');
77

88
const toggleTheme = () => {
9-
setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light'));
9+
const newTheme = theme === 'light' ? 'dark' : 'light';
10+
setTheme(newTheme);
11+
document.body.setAttribute('data-bs-theme', newTheme);
1012
};
1113

1214
return (

src/main.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import App from './App.jsx';
44
import './styles/index.css';
55
import 'bootstrap/dist/css/bootstrap.min.css';
66
import 'bootstrap/dist/js/bootstrap.bundle.min.js';
7+
import { ThemeProvider } from './context/ThemeContext.jsx';
78

89
createRoot(document.getElementById('root')).render(
910
<StrictMode>
10-
<App />
11+
<ThemeProvider>
12+
<App />
13+
</ThemeProvider>
1114
</StrictMode>
1215
);

src/styles/index.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77
justify-content: center;
88
align-items: center;
99
}
10+
11+
[data-bs-theme="dark"] {
12+
--bs-body-color: #f8f9fa;
13+
--bs-body-bg: #212529;
14+
}
15+
16+
[data-bs-theme="light"] {
17+
--bs-body-color: #212529;
18+
--bs-body-bg: #f8f9fa;
19+
}

0 commit comments

Comments
 (0)