Repository navigation
Expand file tree
/
Copy pathAppbar.tsx
More file actions
138 lines (124 loc) · 4.1 KB
/
Copy pathAppbar.tsx
File metadata and controls
138 lines (124 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use client';
import {
Bookmark,
MessageSquare,
History,
Home,
SidebarOpen,
SidebarClose,
} from 'lucide-react';
import { SidebarItems } from './ui/sidebar-items';
import { useState, useEffect, useRef } from 'react';
import { motion } from 'framer-motion';
export const menuOptions = [
{ id: 1, name: 'Home', Component: Home, href: '/home' },
{ id: 3, name: 'Bookmarks', Component: Bookmark, href: '/bookmark' },
{ id: 4, name: 'Questions', Component: MessageSquare, href: '/question' },
{ id: 5, name: 'Watch History', Component: History, href: '/watch-history' },
];
//Added Eventlistener
const useMediaQuery = (query: string): boolean => {
const [matches, setMatches] = useState<boolean>(false);
useEffect(() => {
const media = window.matchMedia(query);
const listener = (e: MediaQueryListEvent) => setMatches(e.matches);
// Use addEventListener instead of addListener
media.addEventListener('change', listener);
// Set the initial match state
setMatches(media.matches);
// Cleanup function to remove the event listener
return () => {
media.removeEventListener('change', listener);
};
}, [query]);
return matches;
};
export default useMediaQuery;
export const Appbar = () => {
const [isCollapsed, setIsCollapsed] = useState(true);
const [isMounted, setIsMounted] = useState(false);
const isMediumToXL = useMediaQuery(
'(min-width: 768px) and (max-width: 1535px)',
);
const sidebarRef = useRef<HTMLDivElement>(null);
useEffect(() => {
setIsMounted(true);
const handleClickOutside = (event: MouseEvent) => {
if (
sidebarRef.current &&
!sidebarRef.current.contains(event.target as Node)
) {
setIsCollapsed(true);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);
const toggleCollapse = () => setIsCollapsed(!isCollapsed);
const sidebarVariants = {
expanded: { width: '20vw' },
collapsed: { width: '4vw' },
};
return (
<>
{/* Desktop Sidebar */}
<motion.nav
ref={sidebarRef}
initial={false}
animate={isMounted && (isCollapsed ? 'collapsed' : 'expanded')}
variants={sidebarVariants}
transition={{
duration: 0.3,
type: 'spring',
stiffness: 200,
damping: 20,
}}
className="fixed left-0 top-0 z-[999] hidden h-full min-w-16 flex-col border-r border-primary/10 bg-background dark:bg-background lg:flex"
>
<div className="flex h-full flex-col gap-4">
<div className="flex w-full items-center border-b border-primary/10 px-2 py-3">
<div>
<motion.button
onClick={toggleCollapse}
className="ml-auto flex items-center rounded-lg p-3 text-center transition-all duration-300 hover:bg-blue-600/5 hover:text-blue-500"
>
{isCollapsed ? <SidebarOpen /> : <SidebarClose />}
</motion.button>
</div>
<div>
{!isCollapsed && (
<h3 className="text-xl font-bold tracking-tighter text-primary lg:text-2xl">
Menu
</h3>
)}
</div>
</div>
<div className="flex flex-col gap-8 p-2">
<SidebarItems
items={menuOptions}
isCollapsed={isCollapsed}
setIsCollapsed={setIsCollapsed}
/>
</div>
</div>
</motion.nav>
{/* Mobile Bottom Navigation */}
<motion.nav
initial={{ y: 100 }}
animate={{ y: 0 }}
transition={{ duration: 0.3, ease: 'easeInOut' }}
className="fixed bottom-0 left-0 right-0 z-[999] lg:hidden"
>
<div className="flex items-center justify-around border-t border-primary/10 bg-background p-4 shadow-xl">
<SidebarItems
items={menuOptions}
isCollapsed={!isMediumToXL}
setIsCollapsed={setIsCollapsed}
/>
</div>
</motion.nav>
</>
);
};