-
Notifications
You must be signed in to change notification settings - Fork 51
/
ButtonLink.jsx
102 lines (92 loc) · 2.38 KB
/
ButtonLink.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
98
99
100
101
102
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { componentWithName, or } from '@tds/util-prop-types'
import { StyledButton } from '@tds/core-button'
import { links } from '@tds/shared-styles'
import {
colorWhite as buttonTextColor,
colorPrimary as primaryBgColor,
colorSecondary as secondaryBgColor,
colorText,
} from '@tds/core-colours'
import { safeRest } from '@tds/util-helpers'
import { warn } from '../../shared/utils/warn'
const StyledButtonLink = styled(StyledButton)(
links.focusOutline,
{
textDecoration: 'none',
},
({ variant }) => {
let color
let hoverColor
if (variant === 'primary') {
color = buttonTextColor
hoverColor = primaryBgColor
} else if (variant === 'secondary') {
color = buttonTextColor
hoverColor = secondaryBgColor
} else {
color = colorText
hoverColor = buttonTextColor
}
return {
'&:link,&:visited': {
color,
},
'&:hover': {
color: hoverColor,
},
}
}
)
/**
* A link that is styled as a button.
*
* @version ./package.json
*/
const ButtonLink = forwardRef(({ reactRouterLinkComponent, variant, children, ...rest }, ref) => {
if ((reactRouterLinkComponent || rest.to) && !(reactRouterLinkComponent && rest.to)) {
warn('Link Button', 'The props `reactRouterLinkComponent` and `to` must be used together.')
}
return (
<StyledButtonLink
{...safeRest(rest)}
as={reactRouterLinkComponent || 'a'}
variant={variant}
ref={ref}
>
{children}
</StyledButtonLink>
)
})
ButtonLink.displayName = 'ButtonLink'
ButtonLink.propTypes = {
/**
* The style.
*/
variant: PropTypes.oneOf(['primary', 'secondary', 'inverted']),
/**
* React Router Link component.
*/
reactRouterLinkComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* Target URL (if using 'reactRouterLinkComponent')
*/
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
/**
* Target URL
*/
href: PropTypes.string,
/**
* The label. It can include the `A11yContent` component or strings.
*/
children: or([PropTypes.string, componentWithName('A11yContent')]).isRequired,
}
ButtonLink.defaultProps = {
variant: 'primary',
reactRouterLinkComponent: null,
to: null,
href: null,
}
export default ButtonLink