Skip to content

Commit 49c9c6d

Browse files
committed
Feat: add dark and light mode for website
1 parent e570f80 commit 49c9c6d

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/components/Navbar.jsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
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';
55

66
function Navbar({ loggedin }) {
77
const [searchTerm, setSearchTerm] = useState('');
88
const [suggestions, setSuggestions] = useState([]);
9+
const [theme, setTheme] = useState('light');
910
const navigate = useNavigate();
1011

12+
const toggleTheme = () => {
13+
const newTheme = theme === 'light' ? 'dark' : 'light';
14+
setTheme(newTheme);
15+
document.body.setAttribute('data-bs-theme', newTheme)
16+
};
17+
1118
const handleSearchChange = (event) => {
1219
const searchInput = event.target.value;
1320
setSearchTerm(searchInput);
@@ -38,8 +45,13 @@ function Navbar({ loggedin }) {
3845
setSuggestions([]);
3946
};
4047

48+
useEffect(() => {
49+
document.body.setAttribute('data-bs-theme', theme);
50+
}, []);
51+
52+
4153
return (
42-
<nav className="navbar navbar-expand-lg sticky-top bg-body-tertiary">
54+
<nav className={`navbar navbar-expand-lg sticky-top bg-body-tertiary navbar-${theme}`}>
4355
<div className="container-fluid">
4456
<button
4557
className="navbar-toggler"
@@ -114,6 +126,20 @@ function Navbar({ loggedin }) {
114126

115127
{loggedin === 'true' ? (
116128
<div className="d-flex align-items-center flex-column flex-lg-row">
129+
<span className="me-3">
130+
<button
131+
className="btn clk rounded-circle"
132+
onClick={toggleTheme}
133+
data-bs-toggle="tooltip"
134+
title={`Switch to ${theme === 'light' ? 'Dark' : 'Light'} Mode`}
135+
>
136+
{theme === 'light' ? (
137+
<i className="fa-solid fa-moon"></i>
138+
) : (
139+
<i className="fa-solid fa-sun"></i>
140+
)}
141+
</button>
142+
</span>
117143
<div className="me-2 mb-2 mb-lg-0 position-relative">
118144
<form className="input-group" onSubmit={handleSearchSubmit}>
119145
<span className="input-group-text" id="basic-addon1">

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)