-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIcon.js
76 lines (66 loc) · 1.49 KB
/
Icon.js
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
import React from 'react'
import T from 'prop-types'
import styled from 'styled-components'
import icons from '../theme/icons'
const Container = styled.span`
user-select: none; /* Stops nearby text being selected if icon clicked quickly */
display: inline-block;
vertical-align: middle;
width: ${({ size }) => size};
height: ${({ size }) => size};
${({ theme, active }) => active && `color: ${theme.color('highlight')};`}
${({ theme, disabled }) =>
disabled &&
`
color: ${theme.color('text', 3)};
`}
${({ onClick, theme, disabled }) =>
onClick &&
!disabled &&
`
cursor: pointer;
&:hover {
backgroundColor: ${theme.color('background', 1, 0.3)};
}
`}
`
const Empty = styled.span`
display: inline-block;
`
function Icon({
type,
onClick,
active,
disabled,
altText,
size = '2em',
override = {},
...props
}) {
const IconSvg = type ? icons[type] : Empty
if (!IconSvg) throw new Error(`No icon found named "${type}"`)
const isButton = onClick && !disabled
return (
<Container
role={isButton ? 'button' : 'img'}
onClick={isButton ? onClick : null}
active={active}
disabled={disabled}
size={size}
as={override.Container}
{...props}
>
<IconSvg alt={altText} as={override.IconSvg} />
</Container>
)
}
Icon.propTypes = {
type: T.string,
onClick: T.func,
altText: T.string,
active: T.bool,
disabled: T.bool,
size: T.string,
override: T.object,
}
export default Icon