forked from KappaSigmaMu/kappasigmamu.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkWithQuery.tsx
30 lines (25 loc) · 878 Bytes
/
LinkWithQuery.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
import React from 'react'
import { Link, LinkProps, useLocation } from 'react-router-dom'
import styled from 'styled-components'
interface LinkWithQueryProps extends Omit<LinkProps, 'to'> {
to: string
children: React.ReactNode
}
const LinkWithQuery: React.FC<LinkWithQueryProps> = ({ children, to, ...props }) => {
const { search, pathname } = useLocation()
const isSelected = pathname === to || pathname === to + '/'
return (
<StyledLink to={to + search} {...props} $selected={isSelected}>
{children}
</StyledLink>
)
}
interface StyledLinkProps {
$selected?: boolean
}
const StyledLink = styled(Link)<StyledLinkProps>`
color: ${(props) => props.$selected && 'white !important'};
pointer-events: ${(props) => (props.$selected ? 'none' : 'auto')};
cursor: ${(props) => (props.$selected ? 'default' : 'pointer')};
`
export { LinkWithQuery }