forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebar.tsx
More file actions
69 lines (59 loc) 路 1.95 KB
/
Copy pathSidebar.tsx
File metadata and controls
69 lines (59 loc) 路 1.95 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
'use client';
import { useEffect, useRef } from 'react';
interface SidebarProps {
open: boolean;
onClose: () => void;
children: React.ReactNode;
}
/**
* Mobile-first collapsible sidebar with swipe-to-close support.
* On desktop it renders children inline; on mobile it slides in as a drawer.
*/
export default function Sidebar({ open, onClose, children }: SidebarProps) {
const startXRef = useRef<number | null>(null);
// Swipe-left to close
useEffect(() => {
if (!open) return;
function onTouchStart(e: TouchEvent) {
startXRef.current = e.touches[0].clientX;
}
function onTouchEnd(e: TouchEvent) {
if (startXRef.current === null) return;
const delta = startXRef.current - e.changedTouches[0].clientX;
if (delta > 60) onClose();
startXRef.current = null;
}
document.addEventListener('touchstart', onTouchStart, { passive: true });
document.addEventListener('touchend', onTouchEnd, { passive: true });
return () => {
document.removeEventListener('touchstart', onTouchStart);
document.removeEventListener('touchend', onTouchEnd);
};
}, [open, onClose]);
// Lock body scroll when drawer is open on mobile
useEffect(() => {
document.body.style.overflow = open ? 'hidden' : '';
return () => { document.body.style.overflow = ''; };
}, [open]);
return (
<>
{/* Backdrop */}
{open && (
<div
className="fixed inset-0 z-40 bg-black/40 lg:hidden"
onClick={onClose}
aria-hidden="true"
/>
)}
{/* Drawer panel */}
<aside
className={`sidebar-transition fixed inset-y-0 left-0 z-50 w-64 overflow-y-auto border-r border-gray-200 bg-white dark:border-gray-800 dark:bg-gray-900 lg:static lg:z-auto lg:block lg:translate-x-0 lg:border-0 ${
open ? 'translate-x-0' : '-translate-x-full'
}`}
aria-label="Sidebar navigation"
>
{children}
</aside>
</>
);
}