-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.tsx
155 lines (148 loc) · 6.12 KB
/
Navigation.tsx
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
import { NavigationProps, TogglerProps } from '@/types/components/global';
import styles from '@/styles/modules/Navigation.module.scss';
import gsap from 'gsap';
import Link from 'next/link';
import useNavigationContext from '@/context/navigationContext';
import useElementSize from '@/hooks/useElementSize';
import useIsMounted from '@/hooks/useIsMounted';
import { useTheme } from 'next-themes';
import useIsomorphicLayoutEffect from '@/hooks/useIsomorphicLayoutEffect';
import { useRef } from 'react';
import Logo from './icons/Logo';
import MobileNavigation from './MobileNavigation';
import NavItem from './NavItem';
import { DarkModeSwitch } from 'react-toggle-dark-mode';
import classNames from 'classnames';
export default function Navigation({ routes }: NavigationProps) {
const { navigationRef, open, sticky, hidden, toggle } =
useNavigationContext();
const [headerRef, { height }] = useElementSize();
const isMounted = useIsMounted();
const { resolvedTheme, setTheme } = useTheme();
const navItemsRef = useRef<HTMLAnchorElement[] | null[]>([]);
useIsomorphicLayoutEffect(() => {
const ctx = gsap.context(() => {
/* Intro animation */
const increment = 0.2;
let initialDelay = 0.7;
navItemsRef.current.forEach((item) => {
gsap.fromTo(
item,
{
y: '100%',
},
{
opacity: 1,
y: 0,
willChange: 'transform',
ease: 'sine.out',
delay: initialDelay,
duration: 0.35,
},
);
initialDelay += increment;
});
});
return () => ctx.revert();
}, []);
return (
<>
<style jsx global>{`
:root {
--navigation-height: ${height}px;
}
`}</style>
<header
className={classNames(styles['c-navigation'], {
[styles['is-sticky']]: sticky,
[styles['is-hidden']]: hidden,
[styles['is-open']]: open,
})}
ref={(el: HTMLDivElement) => {
headerRef(el);
navigationRef.current = el;
}}
>
<div className="o-container">
<div className={styles['c-navigation__row']}>
<div className={styles['c-navigation__logo']}>
<Link href="/" title="Next.js TypeScript starter">
<Logo />
</Link>
</div>
<Toggler open={open} toggle={toggle} />
<MobileNavigation routes={routes} />
<nav className={styles['c-navigation__nav']}>
<div
className={styles['c-navigation__nav__primary']}
>
<div
className={
styles[
'c-navigation__nav__primary--list'
]
}
>
<ul>
{routes.map(({ href, title }, i) => (
<li key={i}>
<NavItem
href={href}
title={title}
className={
styles[
'is-current-page'
]
}
ref={(el) => {
navItemsRef.current[i] =
el;
}}
style={{ opacity: 0 }}
/>
</li>
))}
</ul>
</div>
</div>
</nav>
<div className={styles['c-navigation__switch']}>
{isMounted() && (
<DarkModeSwitch
checked={
resolvedTheme === 'dark' ? true : false
}
aria-label="Theme toggler"
onChange={() =>
setTheme(
resolvedTheme === 'dark'
? 'light'
: 'dark',
)
}
size={35}
/>
)}
</div>
</div>
</div>
</header>
</>
);
}
function Toggler({ open, toggle }: TogglerProps) {
return (
<button
className={classNames(styles['m-toggler'], {
[styles['is-nav-active']]: open,
})}
type="button"
aria-label="Menu toggler"
onClick={toggle}
>
<div className={styles['m-toggler__lines']}>
<span></span>
</div>
</button>
);
}