8
8
SidebarClose ,
9
9
} from 'lucide-react' ;
10
10
import { SidebarItems } from './ui/sidebar-items' ;
11
- import { useState , useEffect } from 'react' ;
11
+ import { useState , useEffect , useRef } from 'react' ;
12
12
import { motion } from 'framer-motion' ;
13
13
14
14
export const menuOptions = [
@@ -49,9 +49,19 @@ export const Appbar = () => {
49
49
const isMediumToXL = useMediaQuery (
50
50
'(min-width: 768px) and (max-width: 1535px)' ,
51
51
) ;
52
+ const sidebarRef = useRef < HTMLDivElement > ( null ) ;
52
53
53
54
useEffect ( ( ) => {
54
55
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
+ } ;
55
65
} , [ ] ) ;
56
66
57
67
const toggleCollapse = ( ) => setIsCollapsed ( ! isCollapsed ) ;
@@ -65,6 +75,7 @@ export const Appbar = () => {
65
75
< >
66
76
{ /* Desktop Sidebar */ }
67
77
< motion . nav
78
+ ref = { sidebarRef }
68
79
initial = { false }
69
80
animate = { isMounted && ( isCollapsed ? 'collapsed' : 'expanded' ) }
70
81
variants = { sidebarVariants }
@@ -95,7 +106,7 @@ export const Appbar = () => {
95
106
</ div >
96
107
</ div >
97
108
< div className = "flex flex-col gap-8 p-2" >
98
- < SidebarItems items = { menuOptions } isCollapsed = { isCollapsed } />
109
+ < SidebarItems items = { menuOptions } isCollapsed = { isCollapsed } setIsCollapsed = { setIsCollapsed } />
99
110
</ div >
100
111
</ div >
101
112
</ motion . nav >
@@ -108,7 +119,7 @@ export const Appbar = () => {
108
119
className = "fixed bottom-0 left-0 right-0 z-[999] lg:hidden"
109
120
>
110
121
< 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 } />
112
123
</ div >
113
124
</ motion . nav >
114
125
</ >
0 commit comments