-
Notifications
You must be signed in to change notification settings - Fork 51
/
Link.jsx
97 lines (89 loc) · 2.05 KB
/
Link.jsx
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
94
95
96
97
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { colorGreyShark, colorWhite } from '@tds/core-colours'
import { links } from '@tds/shared-styles'
import { safeRest } from '@tds/util-helpers'
import { withForwardedRef } from '@tds/shared-hocs'
import { warn } from '../../shared/utils/warn'
const base = {
...links.focusOutline,
'&:link,&:visited': {
color: colorGreyShark,
textDecoration: 'underline',
},
'&:hover': {
textDecoration: 'none',
},
}
const StyledLink = styled.a(base, ({ invert, context }) => {
if (context.inheritColor) {
return {
'&:link,&:visited': {
color: 'inherit',
},
}
}
if (invert) {
return {
'&:link,&:visited': {
color: colorWhite,
},
}
}
return {}
})
/**
* @version ./package.json
*/
const Link = ({ reactRouterLinkComponent, invert, children, forwardedRef, ...rest }, context) => {
if (!(reactRouterLinkComponent && rest.to) && (reactRouterLinkComponent || rest.to)) {
warn('Link', 'The props `reactRouterLinkComponent` and `to` must be used together.')
}
return (
<StyledLink
{...safeRest(rest)}
as={reactRouterLinkComponent || 'a'}
invert={invert}
context={context}
ref={forwardedRef}
>
{children}
</StyledLink>
)
}
Link.propTypes = {
/**
* React Router Link component.
*/
reactRouterLinkComponent: PropTypes.func,
/**
* Target URL (if using 'reactRouterLinkComponent')
*/
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
/**
* Target URL.
*/
href: PropTypes.string,
/**
* Invert link style to appear light on dark backgrounds.
*/
invert: PropTypes.bool,
/**
* Link text.
*/
children: PropTypes.node.isRequired,
/* @ignore */
forwardedRef: PropTypes.object,
}
Link.defaultProps = {
reactRouterLinkComponent: null,
to: null,
href: null,
invert: undefined,
forwardedRef: undefined,
}
Link.contextTypes = {
inheritColor: PropTypes.bool,
}
export default withForwardedRef(Link)