-
Notifications
You must be signed in to change notification settings - Fork 34
/
NavigationLink.tsx
93 lines (80 loc) · 1.97 KB
/
NavigationLink.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
import { useRef, FC } from 'react'
import { NavLink, NavLinkProps } from 'react-router-dom'
import { useTransitionNavigate, useMedia } from 'frontend-essentials'
import { css, cx } from '@emotion/css'
import { MOBILE_VIEWPORT } from 'styles/constants'
export type Data = Partial<Request> & {
url: string
}
export type NavigationLinkProps = NavLinkProps & {
to: string
data?: Data[]
}
const NavigationLink: FC<NavigationLinkProps> = ({
className,
to,
replace,
state,
preventScrollReset,
relative,
data,
onClick,
children,
...otherProps
}) => {
const ref = useRef<HTMLAnchorElement>(null)
const navigate = useTransitionNavigate()
const { hoverable } = useMedia({ hoverable: '(hover: hover) and (pointer: fine)' })
const baseURL = to.replace('/*', '')
const onLinkClick = event => {
event.preventDefault()
navigate(baseURL, { replace, state, preventScrollReset, relative })
onClick?.(event)
}
return (
<NavLink
className={({ isActive }) => cx(style.item, { [style.activeItem]: isActive }, className as string)}
ref={ref}
to={baseURL}
end
onClick={onLinkClick}
onMouseEnter={() => {
// @ts-ignore
hoverable ? data?.forEach(({ url, ...request }) => fetch(url, { ...request, preload: true })) : undefined
}}
{...otherProps}
>
{children}
</NavLink>
)
}
const style = {
item: css`
display: flex;
flex-direction: column;
align-items: center;
margin-right: 20px;
padding-left: 5px;
border-left: 5px solid transparent;
color: #b8b8b8;
font-size: 15px;
font-weight: 600;
text-align: center;
text-decoration: none;
@media ${MOBILE_VIEWPORT} {
margin: 0;
padding: 8px 0;
border: none;
font-size: 20px;
}
`,
activeItem: css`
border-left: 5px solid dodgerblue;
color: dodgerblue;
cursor: default;
@media ${MOBILE_VIEWPORT} {
border: none;
}
`
}
export default NavigationLink