-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
47 lines (41 loc) · 1.44 KB
/
index.jsx
File metadata and controls
47 lines (41 loc) · 1.44 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
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import PropTypes from 'prop-types'
import {Link as ChakraLink} from '@chakra-ui/react'
import {Link as SPALink, NavLink as NavSPALink} from 'react-router-dom'
import useMultiSite from '../../hooks/use-multi-site'
const Link = React.forwardRef(({href, to, useNavLink = false, css, children, ...props}, ref) => {
const _href = to || href
const {buildUrl} = useMultiSite()
const updatedHref = buildUrl(_href)
const isActive = useNavLink
? (_, location) => {
return location.pathname.endsWith(_href)
}
: undefined
return (
<ChakraLink asChild {...props} css={css} ref={ref}>
{useNavLink ? (
<NavSPALink to={updatedHref} isActive={isActive}>
{children}
</NavSPALink>
) : (
<SPALink to={updatedHref}>{children}</SPALink>
)}
</ChakraLink>
)
})
Link.displayName = 'Link'
Link.propTypes = {
href: PropTypes.string,
to: PropTypes.string,
useNavLink: PropTypes.bool,
children: PropTypes.node,
css: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.array, PropTypes.string])
}
export default React.memo(Link)