Skip to content

Commit f4a9673

Browse files
authored
Merge pull request #1799 from Venki1402/va/fix#1798
fix #1798
2 parents 6fc086c + 37462b6 commit f4a9673

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/components/Appbar.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SidebarClose,
99
} from 'lucide-react';
1010
import { SidebarItems } from './ui/sidebar-items';
11-
import { useState, useEffect } from 'react';
11+
import { useState, useEffect, useRef } from 'react';
1212
import { motion } from 'framer-motion';
1313

1414
export const menuOptions = [
@@ -49,9 +49,19 @@ export const Appbar = () => {
4949
const isMediumToXL = useMediaQuery(
5050
'(min-width: 768px) and (max-width: 1535px)',
5151
);
52+
const sidebarRef = useRef<HTMLDivElement>(null);
5253

5354
useEffect(() => {
5455
setIsMounted(true);
56+
const handleClickOutside = (event: MouseEvent) => {
57+
if (sidebarRef.current && !sidebarRef.current.contains(event.target as Node)) {
58+
setIsCollapsed(true);
59+
}
60+
};
61+
document.addEventListener('mousedown', handleClickOutside);
62+
return () => {
63+
document.removeEventListener('mousedown', handleClickOutside);
64+
};
5565
}, []);
5666

5767
const toggleCollapse = () => setIsCollapsed(!isCollapsed);
@@ -65,6 +75,7 @@ export const Appbar = () => {
6575
<>
6676
{/* Desktop Sidebar */}
6777
<motion.nav
78+
ref={sidebarRef}
6879
initial={false}
6980
animate={isMounted && (isCollapsed ? 'collapsed' : 'expanded')}
7081
variants={sidebarVariants}
@@ -95,7 +106,7 @@ export const Appbar = () => {
95106
</div>
96107
</div>
97108
<div className="flex flex-col gap-8 p-2">
98-
<SidebarItems items={menuOptions} isCollapsed={isCollapsed} />
109+
<SidebarItems items={menuOptions} isCollapsed={isCollapsed} setIsCollapsed={setIsCollapsed}/>
99110
</div>
100111
</div>
101112
</motion.nav>
@@ -108,7 +119,7 @@ export const Appbar = () => {
108119
className="fixed bottom-0 left-0 right-0 z-[999] lg:hidden"
109120
>
110121
<div className="flex items-center justify-around border-t border-primary/10 bg-background p-4 shadow-xl">
111-
<SidebarItems items={menuOptions} isCollapsed={!isMediumToXL} />
122+
<SidebarItems items={menuOptions} isCollapsed={!isMediumToXL} setIsCollapsed={setIsCollapsed} />
112123
</div>
113124
</motion.nav>
114125
</>

src/components/ui/sidebar-items.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { usePathname } from 'next/navigation';
1212
export const SidebarItems = ({
1313
items,
1414
isCollapsed,
15+
setIsCollapsed,
1516
}: {
1617
items: {
1718
id: number;
@@ -20,6 +21,7 @@ export const SidebarItems = ({
2021
href: string;
2122
}[];
2223
isCollapsed: boolean;
24+
setIsCollapsed: (isCollapsed: boolean) => void;
2325
}) => {
2426
const pathname = usePathname();
2527
return (
@@ -32,6 +34,7 @@ export const SidebarItems = ({
3234
<TooltipTrigger asChild>
3335
<Link
3436
href={item.href}
37+
onClick={() => setIsCollapsed(true)}
3538
className={`flex items-center rounded-lg p-3 text-center transition-all duration-300 ${
3639
isActive
3740
? 'bg-blue-600/15 text-blue-500'

0 commit comments

Comments
 (0)