-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathindex.tsx
More file actions
320 lines (276 loc) · 8.26 KB
/
index.tsx
File metadata and controls
320 lines (276 loc) · 8.26 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { Component } from 'react'
import keycode from 'keycode'
import { testable } from '@instructure/ui-testable'
import {
getElementType,
getInteraction,
passthroughProps,
callRenderProp
} from '@instructure/ui-react-utils'
import { isActiveElement } from '@instructure/ui-dom-utils'
import { hasVisibleChildren } from '@instructure/ui-a11y-utils'
import { View } from '@instructure/ui-view'
import type { ViewProps } from '@instructure/ui-view'
import { withStyle } from '@instructure/emotion'
import generateStyles from './styles'
import generateComponentTheme from './theme'
import { propTypes, allowedProps } from './props'
import type { BaseButtonProps, BaseButtonStyleProps } from './props'
/**
---
category: components/utilities
---
**/
@withStyle(generateStyles, generateComponentTheme)
@testable()
class BaseButton extends Component<BaseButtonProps> {
static readonly componentId = 'BaseButton'
static propTypes = propTypes
static allowedProps = allowedProps
static defaultProps = {
type: 'button',
size: 'medium',
as: 'button',
// Leave interaction default undefined so that `disabled` and `readOnly` can also be supplied
interaction: undefined,
color: 'secondary',
shape: 'rectangle',
display: 'inline-block',
textAlign: 'start',
withBackground: true,
withBorder: true,
isCondensed: false,
margin: '0',
cursor: 'pointer'
} as const
ref: Element | null = null
componentDidMount() {
this.props.makeStyles?.(this.makeStylesVariables)
}
componentDidUpdate() {
this.props.makeStyles?.(this.makeStylesVariables)
}
get makeStylesVariables(): BaseButtonStyleProps {
return {
isDisabled: this.isDisabled,
hasOnlyIconVisible: this.hasOnlyIconVisible
}
}
get hasOnlyIconVisible() {
const { children, renderIcon } = this.props
return !!(renderIcon && !hasVisibleChildren(children))
}
get elementType() {
return getElementType(BaseButton, this.props)
}
get interaction() {
return getInteraction({ props: this.props })
}
get isDisabled() {
return this.interaction === 'disabled'
}
get isReadOnly() {
return this.interaction === 'readonly'
}
get isEnabled() {
return this.interaction === 'enabled'
}
get focusColor() {
const { color, focusColor, withBackground } = this.props
// Give user specified focusColor preference
if (focusColor) {
return focusColor
}
// The `primary-inverse` background has an info focus outline
// by default since it is replacing the `light` button variant.
// Override the focus color with info even though it is
// an inverse color
if (color === 'primary-inverse' && withBackground) {
return 'info'
}
return color!.includes('inverse') ? 'inverse' : 'info'
}
get focused() {
return isActiveElement(this.ref)
}
focus() {
this.ref && (this.ref as HTMLElement).focus()
}
handleElementRef = (el: Element | null) => {
const { elementRef } = this.props
this.ref = el
if (typeof elementRef === 'function') {
elementRef(el)
}
}
handleClick = (event: React.MouseEvent<ViewProps & Element>) => {
const { onClick } = this.props
const { interaction } = this
if (interaction !== 'enabled') {
event.preventDefault()
event.stopPropagation()
return
}
if (typeof onClick === 'function') {
onClick(event)
}
}
handleKeyDown = (event: React.KeyboardEvent<ViewProps & Element>) => {
const { onClick, onKeyDown, href } = this.props
const { interaction } = this
if (typeof onKeyDown === 'function') {
onKeyDown(event)
}
// behave like a button when space key is pressed
const { space, enter } = keycode.codes
if (
this.elementType !== 'button' &&
[space, enter].includes(event.keyCode)
) {
event.preventDefault()
event.stopPropagation()
if (typeof onClick === 'function' && interaction === 'enabled') {
onClick(event)
}
if (href) {
this.ref && (this.ref as HTMLElement).click()
}
}
}
renderChildren() {
const { renderIcon, children, styles } = this.props
const wrappedChildren = <span css={styles?.children}>{children}</span>
if (!renderIcon) {
return wrappedChildren
}
const { hasOnlyIconVisible } = this
const wrappedIcon = (
<span css={styles?.iconSVG}>{callRenderProp(renderIcon)}</span>
)
const flexChildren = hasOnlyIconVisible ? (
<span css={styles?.iconOnly}>
{wrappedIcon}
{children}
</span>
) : (
[
<span key="icon" css={styles?.iconWrapper}>
{wrappedIcon}
</span>,
<span key="children" css={styles?.childrenWrapper}>
{wrappedChildren}
</span>
]
)
return <span css={styles?.childrenLayout}>{flexChildren}</span>
}
render() {
const {
type,
size,
elementRef,
as,
href,
color,
focusColor,
textAlign,
shape,
display,
withBackground,
withBorder,
isCondensed,
margin,
cursor,
onClick,
renderIcon,
tabIndex,
styles,
makeStyles,
withFocusOutline,
...props
} = this.props
const { isDisabled, isEnabled, isReadOnly, elementType } = this
// only add 0 tabIndex value if it doesn't have it by default, see
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
let needsZeroTabIndex = true
if (typeof elementType == 'string') {
if (
[
'button',
'frame',
'iframe',
'input',
'object',
'select',
'textarea',
'summary'
].includes(elementType)
) {
needsZeroTabIndex = false
}
if (href && (elementType === 'a' || elementType === 'area')) {
needsZeroTabIndex = false
}
}
let tabIndexValue = tabIndex
if (onClick && as && needsZeroTabIndex) {
tabIndexValue = tabIndex || 0
}
return (
<View
{...passthroughProps(props)}
as={elementType}
focusColor={this.focusColor}
position="relative"
display={display}
width={display === 'block' ? '100%' : 'auto'}
borderRadius={shape === 'circle' ? 'circle' : 'medium'}
background="transparent"
padding="none"
borderWidth="none"
margin={margin}
cursor={isDisabled ? 'not-allowed' : cursor}
href={href}
type={href ? undefined : type}
elementRef={this.handleElementRef}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
role={onClick && as !== 'button' ? 'button' : undefined}
tabIndex={tabIndexValue}
disabled={isDisabled || isReadOnly}
css={isEnabled ? styles?.baseButton : null}
focusRingBorderRadius={String(
(styles?.content as { borderRadius?: string | number })?.borderRadius
)}
withFocusOutline={withFocusOutline}
>
<span css={styles?.content}>{this.renderChildren()}</span>
</View>
)
}
}
export { BaseButton }
export default BaseButton