Skip to content

Commit 51f1d30

Browse files
committed
convert Nav.js to styled components
1 parent a4dbeee commit 51f1d30

File tree

3 files changed

+294
-285
lines changed

3 files changed

+294
-285
lines changed

components/layout/Nav/Nav.js

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { useState, useEffect, useRef } from 'react';
22
import { useRouter } from 'next/router';
33
import Link from 'next/link';
44
import Container from '@/components/containers/Container/Container';
5-
import styles from '@/styles/Nav.module.scss';
5+
// import styles from '@/styles/Nav.module.scss';
66
import { linksNav } from '@/utils/links';
7+
import S from './styles';
78

89
export default function Nav() {
910
const router = useRouter();
@@ -17,10 +18,8 @@ export default function Nav() {
1718
([entry]) => {
1819
if (!containerRef.current) return;
1920
if (!entry.isIntersecting) {
20-
containerRef.current.classList.add(styles.sticky);
2121
setIsSticky(true);
2222
} else {
23-
containerRef.current.classList.remove(styles.sticky);
2423
setIsSticky(false);
2524
}
2625
},
@@ -54,62 +53,59 @@ export default function Nav() {
5453
}, []);
5554

5655
return (
57-
<header className={styles.header} ref={headerRef}>
56+
<S.Header ref={headerRef}>
5857
<Container>
59-
<div ref={containerRef}>
60-
<nav className={styles.nav}>
58+
<S.NavWrapper ref={containerRef} $isSticky={isSticky}>
59+
<S.Nav $isSticky={isSticky}>
6160
<Link href='/'>
62-
<img
63-
className={styles.nav__logo}
61+
<S.Logo
62+
$isSticky={isSticky}
6463
src='/images/svg/logo.svg'
6564
alt='Logo'
6665
title='Go to the Homepage'
6766
/>
6867
</Link>
69-
<ul
70-
className={`${styles.nav__links} ${active ? styles.active : ''}`}
68+
<S.Links
69+
className={`${active ? 'active' : ''}`}
70+
$isSticky={isSticky}
7171
>
7272
{linksNav.map(({ text, href, id }) => {
7373
return (
74-
<li className={styles.nav__item} key={id}>
75-
<a
74+
<S.Item key={id}>
75+
<S.Link
7676
href={href}
77-
className={`${styles.nav__link} ${
78-
router.pathname == href ? `${styles.current}` : ''
79-
}`}
77+
$isSticky={isSticky}
78+
className={`${router.pathname == href ? `current` : ''}`}
8079
title={text}
8180
>
8281
{text}
83-
</a>
84-
</li>
82+
</S.Link>
83+
</S.Item>
8584
);
8685
})}
87-
<li className={styles.nav__item}>
88-
<a
86+
<S.Item>
87+
<S.Button
88+
$isSticky={isSticky}
8989
href='mailto:[email protected]?subject=Project collaborator application'
90-
className={`${styles.nav__button} ${
91-
active ? styles.active : ''
92-
}`}
90+
className={`${active ? `active` : ''}`}
9391
title='Join us'
9492
>
9593
Join us
96-
</a>
97-
</li>
98-
</ul>
99-
<button
100-
className={`${styles.nav__hamburger} ${
101-
active ? styles.active : ''
102-
}`}
94+
</S.Button>
95+
</S.Item>
96+
</S.Links>
97+
<S.Hamburger
98+
className={`${active ? `active` : ''}`}
10399
onClick={() => setActive(active => !active)}
104100
aria-label='toggle navigation'
105101
>
106-
<span className={styles.nav__hamburger__bar}></span>
107-
<span className={styles.nav__hamburger__bar}></span>
108-
<span className={styles.nav__hamburger__bar}></span>
109-
</button>
110-
</nav>
111-
</div>
102+
<S.HamburgerBar $isSticky={isSticky}></S.HamburgerBar>
103+
<S.HamburgerBar $isSticky={isSticky}></S.HamburgerBar>
104+
<S.HamburgerBar $isSticky={isSticky}></S.HamburgerBar>
105+
</S.Hamburger>
106+
</S.Nav>
107+
</S.NavWrapper>
112108
</Container>
113-
</header>
109+
</S.Header>
114110
);
115111
}

components/layout/Nav/styles.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ const Nav = styled.nav`
5454
justify-content: space-between;
5555
align-items: center;
5656
57-
&:hover {
58-
text-decoration: none;
59-
color: ${$white};
60-
background-color: ${$transparent};
61-
border: 1px solid ${$white};
62-
}
63-
64-
//media query mixins
65-
${m.desktop(css`
66-
font-size: 1.5rem;
67-
`)}
68-
6957
//check props for sticky behavior
7058
${props => (props.$isSticky ? NavSticky : '')}
7159
`;
@@ -89,6 +77,18 @@ const Button = styled.a`
8977
border: 1px solid ${$transparent};
9078
${m.transition('all 0.3s ease')};
9179
80+
&:hover {
81+
text-decoration: none;
82+
color: ${$white};
83+
background-color: ${$transparent};
84+
border: 1px solid ${$white};
85+
}
86+
87+
//media query mixins
88+
${m.desktop(css`
89+
font-size: 1.5rem;
90+
`)}
91+
9292
&.active {
9393
color: ${$white};
9494
background-color: ${$darkBgColor};
@@ -252,13 +252,13 @@ const Hamburger = styled.button`
252252
display: none;
253253
`)}
254254
255-
&.active &__bar:nth-child(2) {
255+
&.active span:nth-child(2) {
256256
opacity: 0;
257257
}
258-
&.active &__bar:nth-child(1) {
258+
&.active span:nth-child(1) {
259259
transform: translateY(8px) rotate(45deg);
260260
}
261-
&.active &__bar:nth-child(3) {
261+
&.active span:nth-child(3) {
262262
transform: translateY(-6px) rotate(-45deg);
263263
}
264264
`;
@@ -278,3 +278,16 @@ const HamburgerBar = styled.span`
278278
const HamburgerBarSticky = css`
279279
background-color: ${$primaryContentColor};
280280
`;
281+
282+
export default {
283+
Header,
284+
NavWrapper,
285+
Nav,
286+
Logo,
287+
Links,
288+
Item,
289+
Link,
290+
Button,
291+
Hamburger,
292+
HamburgerBar,
293+
};

0 commit comments

Comments
 (0)