-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextLink.tsx
More file actions
60 lines (52 loc) · 1.24 KB
/
Copy pathNextLink.tsx
File metadata and controls
60 lines (52 loc) · 1.24 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
import Link from 'next/link'
import {useRouter} from 'next/router'
import React, {HTMLAttributes} from 'react'
import styled from 'styled-components'
import {UrlObject} from 'url'
import {AlignItems} from './Flex'
type Props = {
className?: string
href: string | UrlObject
alignSelf?: AlignItems
styleIfActive?: boolean // used in NavBar
blankTarget?: boolean
} & HTMLAttributes<HTMLElement>
const NextLink = ({
className,
href,
alignSelf,
styleIfActive,
children,
blankTarget,
...props
}: Props) => {
const router = useRouter()
const isActive = router.asPath === href
return (
<StyledLink href={href} {...props} passHref>
<StyledA
className={className}
alignSelf={alignSelf}
active={styleIfActive && isActive}
target={blankTarget ? '_blank' : undefined}
>
{children}
</StyledA>
</StyledLink>
)
}
const StyledLink = styled(Link)`
text-decoration: none;
`
const StyledA = styled.a<{alignSelf?: AlignItems; active?: boolean}>`
text-decoration: none;
color: unset;
align-self: ${(props) => props.alignSelf};
& > span {
text-decoration: ${(props) => props.active && 'underline'};
}
& > span:hover {
opacity: 0.7;
}
`
export default NextLink