-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.tsx
178 lines (171 loc) · 3.68 KB
/
index.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, {
Attributes,
ComponentType,
MouseEventHandler,
ReactNode,
SVGProps,
} from 'react'
import { css } from 'glamor'
import { mUp } from '../../theme/mediaQueries'
import { fontStyles } from '../../theme/fonts'
import { useColorContext } from '../Colors/ColorContext'
const ICON_SIZE = 24
const IconButton = React.forwardRef<
HTMLAnchorElement & HTMLButtonElement,
{
children?: ReactNode
Icon: ComponentType<SVGProps<SVGSVGElement> & { size?: number | string }>
href?: string
target?: string
label?: string
labelShort?: string
title?: string
fill?: string
fillColorName?: string
onClick?: MouseEventHandler<HTMLAnchorElement & HTMLButtonElement>
onMouseDown?: MouseEventHandler<HTMLAnchorElement & HTMLButtonElement>
style?: React.CSSProperties
size?: number
disabled?: boolean
attributes?: Attributes
invert?: boolean
}
>(
(
{
Icon,
href,
target,
label,
labelShort,
title,
fill,
fillColorName,
onClick,
onMouseDown,
children,
style: customStyles,
size,
disabled,
attributes,
invert,
},
ref,
) => {
const Element = href ? 'a' : 'button'
const [colorScheme] = useColorContext()
const fillValue = disabled ? 'disabled' : fill || fillColorName || 'text'
return (
<Element
{...styles.button}
{...(invert && styles.invertFlex)}
{...((onClick || onMouseDown || href) && styles.hover)}
{...attributes}
style={{
cursor:
(href || onClick || onMouseDown) && !disabled ? 'pointer' : 'auto',
...customStyles,
}}
onClick={onClick}
onMouseDown={onMouseDown}
href={href}
target={target}
rel={target === '_blank' ? 'noreferrer' : ''}
ref={ref}
title={title}
disabled={disabled}
>
<Icon
size={size || ICON_SIZE}
width={size || ICON_SIZE}
height={size || ICON_SIZE}
{...colorScheme.set('fill', fillValue)}
/>
{label && (
<span
{...styles.label}
{...styles.long}
{...colorScheme.set('color', fillValue)}
>
{label}
</span>
)}
{labelShort && (
<span
{...styles.label}
{...styles.short}
{...colorScheme.set('color', fillValue)}
>
{labelShort}
</span>
)}
{children}
</Element>
)
},
)
const styles = {
button: css({
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
textDecoration: 'none',
marginRight: 20,
border: 0,
padding: 0,
color: 'inherit',
backgroundColor: 'transparent',
transition: 'opacity 0.3s',
':focus': {
outline: 'none',
},
':last-child': {
marginRight: 0,
},
':only-child': {
margin: 0,
},
[mUp]: {
marginRight: 24,
},
':disabled': {
cursor: 'default',
},
'& > *:not(:last-child)': {
marginRight: 8,
marginLeft: 0,
},
}),
invertFlex: css({
flexDirection: 'row-reverse',
'& > *:not(:last-child)': {
marginRight: 0,
marginLeft: 8,
},
}),
hover: css({
'@media(hover)': {
':hover:not(:disabled) > *': {
opacity: 0.6,
},
},
}),
label: css({
...fontStyles.sansSerifMedium,
fontSize: 14,
whiteSpace: 'nowrap',
}),
long: css({
display: 'none',
[mUp]: {
display: 'inline',
},
}),
short: css({
display: 'inline',
[mUp]: {
display: 'none',
},
}),
}
export default IconButton