-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppMobileMenu.tsx
More file actions
159 lines (140 loc) · 5.3 KB
/
AppMobileMenu.tsx
File metadata and controls
159 lines (140 loc) · 5.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use client';
import { Menu, X } from 'lucide-react';
import { useEffect, useMemo, useState } from 'react';
import { Link } from '@/i18n/routing';
import { SITE_LINKS } from '@/lib/navigation';
import { NAV_LINKS } from '@/components/shop/header/nav-links';
import { BlogCategoryLinks } from '@/components/blog/BlogCategoryLinks';
import { LogoutButton } from '@/components/auth/logoutButton';
export type AppMobileMenuVariant = 'platform' | 'shop' | 'blog';
type Props = {
variant: AppMobileMenuVariant;
userExists: boolean;
showAdminLink?: boolean;
blogCategories?: Array<{ _id: string; title: string }>;
};
export function AppMobileMenu({
variant,
userExists,
showAdminLink = false,
blogCategories = [],
}: Props) {
const [open, setOpen] = useState(false);
const close = () => setOpen(false);
const toggle = () => setOpen(prev => !prev);
useEffect(() => {
if (!open) return;
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') close();
};
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, [open]);
const links = useMemo(() => {
if (variant === 'shop') return NAV_LINKS;
if (variant === 'platform') return SITE_LINKS;
return [];
}, [variant]);
return (
<>
<button
type="button"
onClick={toggle}
className="flex h-9 w-9 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
aria-label="Toggle menu"
aria-expanded={open ? 'true' : 'false'}
aria-controls={open ? 'app-mobile-nav' : undefined}
>
{open ? <X className="h-5 w-5" /> : <Menu className="h-5 w-5" />}
</button>
{open && (
<>
<button
type="button"
aria-label="Close menu"
className="fixed inset-0 z-40 md:hidden"
onClick={close}
/>
<nav
id="app-mobile-nav"
className="fixed left-0 right-0 top-16 z-50 border-t border-border bg-background px-4 py-4 md:hidden"
>
<div className="flex flex-col gap-1">
{variant === 'shop' ? (
<Link
href="/"
onClick={close}
className="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
Home
</Link>
) : null}
{variant === 'blog' ? (
<BlogCategoryLinks
categories={blogCategories}
className="flex flex-col gap-1"
linkClassName="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
onNavigate={close}
/>
) : (
links.map(link => (
<Link
key={link.href}
href={link.href}
onClick={close}
className="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
{link.label}
</Link>
))
)}
{variant === 'shop' && showAdminLink ? (
<Link
href="/shop/admin/products/new"
onClick={close}
className="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
New product
</Link>
) : null}
<div className="my-2 h-px bg-border" />
{userExists ? (
<>
<Link
href="/dashboard"
onClick={close}
className="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
Dashboard
</Link>
{showAdminLink ? (
<Link
href="/shop/admin"
aria-label="Shop admin"
title="Shop admin"
className="inline-flex items-center rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
Admin
</Link>
) : null}
{/* LogoutButton стилізується сам; ми тільки позиціонуємо як пункт меню */}
<div className="px-3 py-2" onClick={close}>
<LogoutButton />
</div>
</>
) : (
<Link
href="/login"
onClick={close}
className="rounded-md px-3 py-2 text-sm font-medium text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
>
Log in
</Link>
)}
</div>
</nav>
</>
)}
</>
);
}