Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the dropdown to auto close on choosing any link #259

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/components/shared/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { useContext, useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { MdOutlineDarkMode, MdOutlineLightMode } from "react-icons/md";
import { Collapse } from 'bootstrap'; // Ensure Bootstrap's JavaScript is loaded in your project
import { Context } from '../../context/Context';

const Navbar = () => {
const { isDarkMode, toggleTheme } = useContext(Context);
const [isSticky, setIsSticky] = useState(false);

const handleScroll = () => {
if (window.scrollY > 50) setIsSticky(true);
else setIsSticky(false);
setIsSticky(window.scrollY > 50);
};

useEffect(() => {
Expand All @@ -19,15 +19,40 @@ const Navbar = () => {
};
}, []);

useEffect(() => {
const handleNavItemClick = () => {
const navbarCollapse = document.getElementById('navbarSupportedContent');
if (navbarCollapse.classList.contains('show')) {
const bsCollapse = new Collapse(navbarCollapse, { toggle: true });
bsCollapse.hide();
}
};

const navLinks = document.querySelectorAll('#navbarSupportedContent .nav-link');
const dropdownItems = document.querySelectorAll('#navbarSupportedContent .dropdown-item');

navLinks.forEach(link => link.addEventListener('click', handleNavItemClick));
dropdownItems.forEach(item => item.addEventListener('click', handleNavItemClick));

return () => {
navLinks.forEach(link => link.removeEventListener('click', handleNavItemClick));
dropdownItems.forEach(item => item.removeEventListener('click', handleNavItemClick));
};
}, []);

return (
<nav className={`navbar navbar-expand-lg ${isSticky ? 'mt-2 fixed-top' : ''} ${isDarkMode ? 'navbar-dark' : 'navbar-light'}`} style={{ backgroundColor: `${isDarkMode ? 'rgba(0, 0, 0, 0.4)' : 'rgba(223, 223, 176, 0.4)'}` }}>
<nav
className={`navbar navbar-expand-lg ${isSticky ? 'mt-2 fixed-top' : ''} ${isDarkMode ? 'navbar-dark' : 'navbar-light'}`}
style={{ backgroundColor: `${isDarkMode ? 'rgba(0, 0, 0, 0.4)' : 'rgba(223, 223, 176, 0.4)'}` }}
>
<div className="container-fluid">
<Link className="navbar-brand d-flex align-items-center px-4" to="/">
<img src="logo.webp" alt="Chanakya Image" className="me-2" style={{ width: '30px', height: '30px' }} />
<span style={{ fontSize: '1.2rem', fontWeight: 'bold' }}>चाणक्य नीति</span>
</Link>

<button className="navbar-toggler"
<button
className="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
Expand All @@ -43,7 +68,14 @@ const Navbar = () => {
<li className="nav-item"><Link className="nav-link" to="/about">About</Link></li>

<li className="nav-item dropdown">
<Link className="nav-link dropdown-toggle" to="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<Link
className="nav-link dropdown-toggle"
to="#"
id="navbarDropdown"
role="button"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Resources
</Link>
<ul className="dropdown-menu" aria-labelledby="navbarDropdown">
Expand All @@ -59,7 +91,7 @@ const Navbar = () => {
<li className="nav-item"><Link className="nav-link" to="/contributor">Contributors</Link></li>
<li className="nav-item"><Link className="nav-link" to="/auth/login">Login</Link></li>

<li className='nav-item nav-link px-4' onClick={toggleTheme} style={{ cursor: 'pointer' }}>
<li className="nav-item nav-link px-4" onClick={toggleTheme} style={{ cursor: 'pointer' }}>
{isDarkMode ? (
<MdOutlineLightMode style={{ fontSize: '1.5rem', color: 'white' }} />
) : (
Expand All @@ -71,6 +103,6 @@ const Navbar = () => {
</div>
</nav>
);
}
};

export default Navbar;