-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollNav.tsx
More file actions
67 lines (59 loc) · 2.2 KB
/
ScrollNav.tsx
File metadata and controls
67 lines (59 loc) · 2.2 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
import React, { useRef, useEffect, useState } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/router';
interface NavItem {
path: string;
label: string;
}
const ScrollNav: React.FC = () => {
const router = useRouter();
const currentPath = router.pathname;
const scrollRef = useRef<HTMLDivElement>(null);
const [showLeftFade, setShowLeftFade] = useState(false);
const [showRightFade, setShowRightFade] = useState(false);
const navItems: NavItem[] = [
{ path: '/feed', label: 'Feed' },
{ path: '/trending', label: 'Trending' },
{ path: '/discover', label: 'Discover' },
{ path: '/categories', label: 'Categories' },
{ path: '/bookmarks', label: 'Bookmarks' },
{ path: '/following', label: 'Following' },
];
const checkScroll = () => {
if (scrollRef.current) {
const { scrollLeft, scrollWidth, clientWidth } = scrollRef.current;
setShowLeftFade(scrollLeft > 0);
setShowRightFade(scrollLeft < scrollWidth - clientWidth - 10);
}
};
useEffect(() => {
const scrollContainer = scrollRef.current;
if (scrollContainer) {
checkScroll();
scrollContainer.addEventListener('scroll', checkScroll);
window.addEventListener('resize', checkScroll);
return () => {
scrollContainer.removeEventListener('scroll', checkScroll);
window.removeEventListener('resize', checkScroll);
};
}
}, []);
return (
<nav className="scroll-nav">
{showLeftFade && <div className="scroll-fade-left" />}
<div className="scroll-nav-container" ref={scrollRef}>
{navItems.map((item) => (
<Link
key={item.path}
href={item.path}
className={`scroll-nav-item ${currentPath === item.path ? 'active' : ''}`}
>
{item.label}
</Link>
))}
</div>
{showRightFade && <div className="scroll-fade-right" />}
</nav>
);
};
export default ScrollNav;