@instructure/ui-buttons 11.6.0 → 11.6.1-snapshot-129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/CHANGELOG.md +57 -323
  2. package/es/BaseButton/{index.js → v1/index.js} +2 -2
  3. package/es/BaseButton/v2/index.js +304 -0
  4. package/es/BaseButton/v2/props.js +26 -0
  5. package/es/BaseButton/v2/styles.js +578 -0
  6. package/es/Button/{index.js → v1/index.js} +2 -2
  7. package/es/{CondensedButton → Button/v1}/theme.js +1 -1
  8. package/es/Button/v2/index.js +121 -0
  9. package/es/Button/v2/props.js +26 -0
  10. package/es/CloseButton/{index.js → v1/index.js} +2 -2
  11. package/es/CloseButton/v2/index.js +130 -0
  12. package/es/CloseButton/v2/props.js +26 -0
  13. package/es/CloseButton/v2/styles.js +68 -0
  14. package/es/CondensedButton/{index.js → v1/index.js} +2 -2
  15. package/es/{Button → CondensedButton/v1}/theme.js +1 -1
  16. package/es/CondensedButton/v2/index.js +111 -0
  17. package/es/CondensedButton/v2/props.js +26 -0
  18. package/es/IconButton/{index.js → v1/index.js} +2 -2
  19. package/es/IconButton/{theme.js → v1/theme.js} +1 -1
  20. package/es/IconButton/v2/index.js +121 -0
  21. package/es/IconButton/v2/props.js +26 -0
  22. package/es/ToggleButton/{index.js → v1/index.js} +2 -2
  23. package/es/ToggleButton/v2/index.js +121 -0
  24. package/es/ToggleButton/v2/props.js +26 -0
  25. package/es/exports/a.js +29 -0
  26. package/es/exports/b.js +29 -0
  27. package/lib/BaseButton/{index.js → v1/index.js} +3 -3
  28. package/lib/BaseButton/v2/index.js +311 -0
  29. package/lib/BaseButton/v2/props.js +31 -0
  30. package/lib/BaseButton/v2/styles.js +584 -0
  31. package/lib/Button/v1/index.js +128 -0
  32. package/lib/Button/{theme.js → v1/theme.js} +1 -1
  33. package/lib/Button/{index.js → v2/index.js} +3 -4
  34. package/lib/Button/v2/props.js +31 -0
  35. package/lib/CloseButton/{index.js → v1/index.js} +4 -4
  36. package/lib/CloseButton/v2/index.js +136 -0
  37. package/lib/CloseButton/v2/props.js +31 -0
  38. package/lib/CloseButton/v2/styles.js +74 -0
  39. package/lib/CondensedButton/{index.js → v1/index.js} +3 -3
  40. package/lib/CondensedButton/{theme.js → v1/theme.js} +1 -1
  41. package/lib/CondensedButton/v2/index.js +116 -0
  42. package/lib/CondensedButton/v2/props.js +31 -0
  43. package/lib/IconButton/{index.js → v1/index.js} +3 -3
  44. package/lib/IconButton/{theme.js → v1/theme.js} +1 -1
  45. package/lib/IconButton/v2/index.js +126 -0
  46. package/lib/IconButton/v2/props.js +31 -0
  47. package/lib/ToggleButton/{index.js → v1/index.js} +4 -4
  48. package/lib/ToggleButton/v2/index.js +127 -0
  49. package/lib/ToggleButton/v2/props.js +31 -0
  50. package/lib/{index.js → exports/a.js} +12 -12
  51. package/lib/exports/b.js +47 -0
  52. package/package.json +46 -24
  53. package/src/BaseButton/{index.tsx → v1/index.tsx} +3 -3
  54. package/src/BaseButton/{props.ts → v1/props.ts} +1 -1
  55. package/src/BaseButton/v2/README.md +15 -0
  56. package/src/BaseButton/v2/index.tsx +343 -0
  57. package/src/BaseButton/v2/props.ts +226 -0
  58. package/src/BaseButton/v2/styles.ts +640 -0
  59. package/src/Button/{index.tsx → v1/index.tsx} +2 -2
  60. package/src/Button/{props.ts → v1/props.ts} +1 -1
  61. package/src/{CondensedButton → Button/v1}/theme.ts +1 -1
  62. package/src/Button/v2/README.md +338 -0
  63. package/src/Button/v2/index.tsx +137 -0
  64. package/src/Button/v2/props.ts +159 -0
  65. package/src/CloseButton/{index.tsx → v1/index.tsx} +2 -2
  66. package/src/CloseButton/{props.ts → v1/props.ts} +1 -1
  67. package/src/CloseButton/v2/README.md +70 -0
  68. package/src/CloseButton/v2/index.tsx +142 -0
  69. package/src/CloseButton/v2/props.ts +148 -0
  70. package/src/CloseButton/v2/styles.ts +81 -0
  71. package/src/CondensedButton/{index.tsx → v1/index.tsx} +2 -2
  72. package/src/CondensedButton/{props.ts → v1/props.ts} +1 -1
  73. package/src/{Button → CondensedButton/v1}/theme.ts +1 -1
  74. package/src/CondensedButton/v2/README.md +75 -0
  75. package/src/CondensedButton/v2/index.tsx +129 -0
  76. package/src/CondensedButton/v2/props.ts +134 -0
  77. package/src/IconButton/{index.tsx → v1/index.tsx} +2 -2
  78. package/src/IconButton/{props.ts → v1/props.ts} +1 -1
  79. package/src/IconButton/{theme.ts → v1/theme.ts} +1 -1
  80. package/src/IconButton/v2/README.md +86 -0
  81. package/src/IconButton/v2/index.tsx +138 -0
  82. package/src/IconButton/v2/props.ts +166 -0
  83. package/src/ToggleButton/{index.tsx → v1/index.tsx} +2 -2
  84. package/src/ToggleButton/{props.ts → v1/props.ts} +1 -1
  85. package/src/ToggleButton/v2/README.md +85 -0
  86. package/src/ToggleButton/v2/index.tsx +133 -0
  87. package/src/ToggleButton/v2/props.ts +143 -0
  88. package/src/exports/a.ts +40 -0
  89. package/src/exports/b.ts +40 -0
  90. package/tsconfig.build.tsbuildinfo +1 -1
  91. package/types/BaseButton/{index.d.ts → v1/index.d.ts} +1 -1
  92. package/types/BaseButton/v1/index.d.ts.map +1 -0
  93. package/types/BaseButton/{props.d.ts → v1/props.d.ts} +1 -1
  94. package/types/BaseButton/v1/props.d.ts.map +1 -0
  95. package/types/BaseButton/v1/styles.d.ts.map +1 -0
  96. package/types/BaseButton/v1/theme.d.ts.map +1 -0
  97. package/types/BaseButton/v2/index.d.ts +48 -0
  98. package/types/BaseButton/v2/index.d.ts.map +1 -0
  99. package/types/BaseButton/v2/props.d.ts +118 -0
  100. package/types/BaseButton/v2/props.d.ts.map +1 -0
  101. package/types/BaseButton/v2/styles.d.ts +5 -0
  102. package/types/BaseButton/v2/styles.d.ts.map +1 -0
  103. package/types/Button/{index.d.ts → v1/index.d.ts} +2 -2
  104. package/types/Button/v1/index.d.ts.map +1 -0
  105. package/types/Button/{props.d.ts → v1/props.d.ts} +1 -1
  106. package/types/Button/v1/props.d.ts.map +1 -0
  107. package/types/Button/v1/theme.d.ts +2 -0
  108. package/types/Button/v1/theme.d.ts.map +1 -0
  109. package/types/Button/v2/index.d.ts +51 -0
  110. package/types/Button/v2/index.d.ts.map +1 -0
  111. package/types/Button/v2/props.d.ts +82 -0
  112. package/types/Button/v2/props.d.ts.map +1 -0
  113. package/types/CloseButton/{index.d.ts → v1/index.d.ts} +1 -1
  114. package/types/CloseButton/v1/index.d.ts.map +1 -0
  115. package/types/CloseButton/{props.d.ts → v1/props.d.ts} +1 -1
  116. package/types/CloseButton/v1/props.d.ts.map +1 -0
  117. package/types/CloseButton/v1/styles.d.ts.map +1 -0
  118. package/types/CloseButton/v1/theme.d.ts.map +1 -0
  119. package/types/CloseButton/v2/index.d.ts +46 -0
  120. package/types/CloseButton/v2/index.d.ts.map +1 -0
  121. package/types/CloseButton/v2/props.d.ts +77 -0
  122. package/types/CloseButton/v2/props.d.ts.map +1 -0
  123. package/types/CloseButton/v2/styles.d.ts +15 -0
  124. package/types/CloseButton/v2/styles.d.ts.map +1 -0
  125. package/types/CondensedButton/{index.d.ts → v1/index.d.ts} +2 -2
  126. package/types/CondensedButton/v1/index.d.ts.map +1 -0
  127. package/types/CondensedButton/{props.d.ts → v1/props.d.ts} +1 -1
  128. package/types/CondensedButton/v1/props.d.ts.map +1 -0
  129. package/types/CondensedButton/v1/theme.d.ts +2 -0
  130. package/types/CondensedButton/v1/theme.d.ts.map +1 -0
  131. package/types/CondensedButton/v2/index.d.ts +45 -0
  132. package/types/CondensedButton/v2/index.d.ts.map +1 -0
  133. package/types/CondensedButton/v2/props.d.ts +69 -0
  134. package/types/CondensedButton/v2/props.d.ts.map +1 -0
  135. package/types/IconButton/{index.d.ts → v1/index.d.ts} +2 -2
  136. package/types/IconButton/v1/index.d.ts.map +1 -0
  137. package/types/IconButton/{props.d.ts → v1/props.d.ts} +1 -1
  138. package/types/IconButton/v1/props.d.ts.map +1 -0
  139. package/types/IconButton/v1/theme.d.ts +2 -0
  140. package/types/IconButton/v1/theme.d.ts.map +1 -0
  141. package/types/IconButton/v2/index.d.ts +51 -0
  142. package/types/IconButton/v2/index.d.ts.map +1 -0
  143. package/types/IconButton/v2/props.d.ts +85 -0
  144. package/types/IconButton/v2/props.d.ts.map +1 -0
  145. package/types/ToggleButton/{index.d.ts → v1/index.d.ts} +1 -1
  146. package/types/ToggleButton/v1/index.d.ts.map +1 -0
  147. package/types/ToggleButton/{props.d.ts → v1/props.d.ts} +1 -1
  148. package/types/ToggleButton/v1/props.d.ts.map +1 -0
  149. package/types/ToggleButton/v2/index.d.ts +43 -0
  150. package/types/ToggleButton/v2/index.d.ts.map +1 -0
  151. package/types/ToggleButton/v2/props.d.ts +75 -0
  152. package/types/ToggleButton/v2/props.d.ts.map +1 -0
  153. package/types/exports/a.d.ts +13 -0
  154. package/types/exports/a.d.ts.map +1 -0
  155. package/types/exports/b.d.ts +13 -0
  156. package/types/exports/b.d.ts.map +1 -0
  157. package/es/index.js +0 -29
  158. package/src/index.ts +0 -37
  159. package/types/BaseButton/index.d.ts.map +0 -1
  160. package/types/BaseButton/props.d.ts.map +0 -1
  161. package/types/BaseButton/styles.d.ts.map +0 -1
  162. package/types/BaseButton/theme.d.ts.map +0 -1
  163. package/types/Button/index.d.ts.map +0 -1
  164. package/types/Button/props.d.ts.map +0 -1
  165. package/types/Button/theme.d.ts +0 -2
  166. package/types/Button/theme.d.ts.map +0 -1
  167. package/types/CloseButton/index.d.ts.map +0 -1
  168. package/types/CloseButton/props.d.ts.map +0 -1
  169. package/types/CloseButton/styles.d.ts.map +0 -1
  170. package/types/CloseButton/theme.d.ts.map +0 -1
  171. package/types/CondensedButton/index.d.ts.map +0 -1
  172. package/types/CondensedButton/props.d.ts.map +0 -1
  173. package/types/CondensedButton/theme.d.ts +0 -2
  174. package/types/CondensedButton/theme.d.ts.map +0 -1
  175. package/types/IconButton/index.d.ts.map +0 -1
  176. package/types/IconButton/props.d.ts.map +0 -1
  177. package/types/IconButton/theme.d.ts +0 -2
  178. package/types/IconButton/theme.d.ts.map +0 -1
  179. package/types/ToggleButton/index.d.ts.map +0 -1
  180. package/types/ToggleButton/props.d.ts.map +0 -1
  181. package/types/index.d.ts +0 -13
  182. package/types/index.d.ts.map +0 -1
  183. /package/es/BaseButton/{props.js → v1/props.js} +0 -0
  184. /package/es/BaseButton/{styles.js → v1/styles.js} +0 -0
  185. /package/es/BaseButton/{theme.js → v1/theme.js} +0 -0
  186. /package/es/Button/{props.js → v1/props.js} +0 -0
  187. /package/es/CloseButton/{props.js → v1/props.js} +0 -0
  188. /package/es/CloseButton/{styles.js → v1/styles.js} +0 -0
  189. /package/es/CloseButton/{theme.js → v1/theme.js} +0 -0
  190. /package/es/CondensedButton/{props.js → v1/props.js} +0 -0
  191. /package/es/IconButton/{props.js → v1/props.js} +0 -0
  192. /package/es/ToggleButton/{props.js → v1/props.js} +0 -0
  193. /package/lib/BaseButton/{props.js → v1/props.js} +0 -0
  194. /package/lib/BaseButton/{styles.js → v1/styles.js} +0 -0
  195. /package/lib/BaseButton/{theme.js → v1/theme.js} +0 -0
  196. /package/lib/Button/{props.js → v1/props.js} +0 -0
  197. /package/lib/CloseButton/{props.js → v1/props.js} +0 -0
  198. /package/lib/CloseButton/{styles.js → v1/styles.js} +0 -0
  199. /package/lib/CloseButton/{theme.js → v1/theme.js} +0 -0
  200. /package/lib/CondensedButton/{props.js → v1/props.js} +0 -0
  201. /package/lib/IconButton/{props.js → v1/props.js} +0 -0
  202. /package/lib/ToggleButton/{props.js → v1/props.js} +0 -0
  203. /package/src/BaseButton/{README.md → v1/README.md} +0 -0
  204. /package/src/BaseButton/{styles.ts → v1/styles.ts} +0 -0
  205. /package/src/BaseButton/{theme.ts → v1/theme.ts} +0 -0
  206. /package/src/Button/{README.md → v1/README.md} +0 -0
  207. /package/src/CloseButton/{README.md → v1/README.md} +0 -0
  208. /package/src/CloseButton/{styles.ts → v1/styles.ts} +0 -0
  209. /package/src/CloseButton/{theme.ts → v1/theme.ts} +0 -0
  210. /package/src/CondensedButton/{README.md → v1/README.md} +0 -0
  211. /package/src/IconButton/{README.md → v1/README.md} +0 -0
  212. /package/src/ToggleButton/{README.md → v1/README.md} +0 -0
  213. /package/types/BaseButton/{styles.d.ts → v1/styles.d.ts} +0 -0
  214. /package/types/BaseButton/{theme.d.ts → v1/theme.d.ts} +0 -0
  215. /package/types/CloseButton/{styles.d.ts → v1/styles.d.ts} +0 -0
  216. /package/types/CloseButton/{theme.d.ts → v1/theme.d.ts} +0 -0
@@ -0,0 +1,343 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Component } from 'react'
26
+ import keycode from 'keycode'
27
+
28
+ import {
29
+ getElementType,
30
+ getInteraction,
31
+ passthroughProps
32
+ } from '@instructure/ui-react-utils'
33
+ import { isActiveElement } from '@instructure/ui-dom-utils'
34
+ import { hasVisibleChildren } from '@instructure/ui-a11y-utils'
35
+ import { renderIconWithProps } from '@instructure/ui-icons'
36
+ import { View } from '@instructure/ui-view/latest'
37
+ import type { ViewProps } from '@instructure/ui-view/latest'
38
+
39
+ // TODO these have to be imported in separate lines because otherwise `isSafari` will be missing from the babel build
40
+ // this bug is very likely caused by `babel-plugin-transform-imports` and can be reverted once it is removed from the codebase
41
+ import { isSafari } from '@instructure/ui-utils'
42
+ import { combineDataCid } from '@instructure/ui-utils'
43
+
44
+ import { withStyle } from '@instructure/emotion'
45
+
46
+ import generateStyles from './styles'
47
+
48
+ import { allowedProps } from './props'
49
+ import type { BaseButtonProps, BaseButtonStyleProps } from './props'
50
+
51
+ const buttonSizeToIconSize = {
52
+ small: 'sm',
53
+ medium: 'md',
54
+ large: 'lg',
55
+ condensedSmall: 'xs',
56
+ condensedMedium: 'xs'
57
+ } as const
58
+
59
+ const buttonColorToIconColor = {
60
+ 'primary': 'inherit',
61
+ 'primary-inverse': 'inherit',
62
+ 'secondary': 'inherit',
63
+ 'success': 'inherit',
64
+ 'danger': 'inherit',
65
+ 'ai-primary': 'inherit',
66
+ 'ai-secondary': 'ai'
67
+ } as const
68
+
69
+ /**
70
+ ---
71
+ category: components/utilities
72
+ ---
73
+ **/
74
+ @withStyle(generateStyles)
75
+ class BaseButton extends Component<BaseButtonProps> {
76
+ static readonly componentId = 'BaseButton'
77
+
78
+ static allowedProps = allowedProps
79
+ static defaultProps = {
80
+ type: 'button',
81
+ size: 'medium',
82
+ as: 'button',
83
+ // Leave interaction default undefined so that `disabled` and `readOnly` can also be supplied
84
+ interaction: undefined,
85
+ color: 'secondary',
86
+ shape: 'rectangle',
87
+ display: 'inline-block',
88
+ textAlign: 'start',
89
+ withBackground: true,
90
+ withBorder: true,
91
+ isCondensed: false,
92
+ margin: '0',
93
+ cursor: 'pointer'
94
+ } as const
95
+
96
+ ref: HTMLElement | null = null
97
+
98
+ componentDidMount() {
99
+ this.props.makeStyles?.(this.makeStylesVariables)
100
+ }
101
+
102
+ componentDidUpdate() {
103
+ this.props.makeStyles?.(this.makeStylesVariables)
104
+ }
105
+
106
+ get makeStylesVariables(): BaseButtonStyleProps {
107
+ return {
108
+ isDisabled: this.isDisabled,
109
+ hasOnlyIconVisible: this.hasOnlyIconVisible,
110
+ isEnabled: this.isEnabled
111
+ }
112
+ }
113
+
114
+ get hasOnlyIconVisible() {
115
+ const { children, renderIcon } = this.props
116
+ return !!(renderIcon && !hasVisibleChildren(children))
117
+ }
118
+
119
+ get elementType() {
120
+ return getElementType(BaseButton, this.props)
121
+ }
122
+
123
+ get interaction() {
124
+ return getInteraction({ props: this.props })
125
+ }
126
+
127
+ get isDisabled() {
128
+ return this.interaction === 'disabled'
129
+ }
130
+
131
+ get isReadOnly() {
132
+ return this.interaction === 'readonly'
133
+ }
134
+
135
+ get isEnabled() {
136
+ return this.interaction === 'enabled'
137
+ }
138
+
139
+ get focusColor() {
140
+ const { color, focusColor, withBackground } = this.props
141
+
142
+ // Give user specified focusColor preference
143
+ if (focusColor) {
144
+ return focusColor
145
+ }
146
+
147
+ // The `primary-inverse` background has an info focus outline
148
+ // by default since it is replacing the `light` button variant.
149
+ // Override the focus color with info even though it is
150
+ // an inverse color
151
+ if (color === 'primary-inverse' && withBackground) {
152
+ return 'info'
153
+ }
154
+
155
+ return color!.includes('inverse') ? 'inverse' : 'info'
156
+ }
157
+
158
+ get focused() {
159
+ return isActiveElement(this.ref)
160
+ }
161
+
162
+ focus() {
163
+ this.ref && this.ref.focus()
164
+ }
165
+
166
+ handleElementRef = (el: HTMLElement | null) => {
167
+ const { elementRef } = this.props
168
+
169
+ this.ref = el
170
+
171
+ if (typeof elementRef === 'function') {
172
+ elementRef(el)
173
+ }
174
+ }
175
+
176
+ handleClick = (event: React.MouseEvent<ViewProps & Element>) => {
177
+ const { onClick } = this.props
178
+ const { interaction } = this
179
+
180
+ if (interaction !== 'enabled') {
181
+ event.preventDefault()
182
+ event.stopPropagation()
183
+ return
184
+ }
185
+
186
+ if (typeof onClick === 'function') {
187
+ onClick(event)
188
+ }
189
+ }
190
+
191
+ handleKeyDown = (event: React.KeyboardEvent<ViewProps & Element>) => {
192
+ const { onClick, onKeyDown, href } = this.props
193
+ const { interaction } = this
194
+
195
+ if (typeof onKeyDown === 'function') {
196
+ onKeyDown(event)
197
+ }
198
+
199
+ // behave like a button when space key is pressed
200
+ const { space, enter } = keycode.codes
201
+
202
+ if (
203
+ this.elementType !== 'button' &&
204
+ [space, enter].includes(event.keyCode)
205
+ ) {
206
+ event.preventDefault()
207
+ event.stopPropagation()
208
+
209
+ if (typeof onClick === 'function' && interaction === 'enabled') {
210
+ onClick(event)
211
+ }
212
+
213
+ if (href) {
214
+ this.ref && (this.ref as HTMLElement).click()
215
+ }
216
+ }
217
+ }
218
+
219
+ renderChildren() {
220
+ const { renderIcon, children, styles, size, color } = this.props
221
+
222
+ const wrappedChildren = <span css={styles?.children}>{children}</span>
223
+
224
+ if (!renderIcon) {
225
+ return wrappedChildren
226
+ }
227
+
228
+ const { hasOnlyIconVisible } = this
229
+ const iconSize = buttonSizeToIconSize[size!]
230
+ const iconColor = buttonColorToIconColor[color!]
231
+ const wrappedIcon = (
232
+ <span css={styles?.iconSVG}>
233
+ {renderIconWithProps(renderIcon, iconSize, iconColor)}
234
+ </span>
235
+ )
236
+
237
+ const flexChildren = hasOnlyIconVisible ? (
238
+ <span css={styles?.iconOnly}>
239
+ {wrappedIcon}
240
+ {children}
241
+ </span>
242
+ ) : (
243
+ [
244
+ <span key="icon" css={styles?.iconWrapper}>
245
+ {wrappedIcon}
246
+ </span>,
247
+ <span key="children" css={styles?.childrenWrapper}>
248
+ {wrappedChildren}
249
+ </span>
250
+ ]
251
+ )
252
+ return <span css={styles?.childrenLayout}>{flexChildren}</span>
253
+ }
254
+
255
+ render() {
256
+ const {
257
+ type,
258
+ size,
259
+ elementRef,
260
+ as,
261
+ href,
262
+ color,
263
+ focusColor,
264
+ textAlign,
265
+ shape,
266
+ display,
267
+ withBackground,
268
+ withBorder,
269
+ isCondensed,
270
+ margin,
271
+ cursor,
272
+ onClick,
273
+ renderIcon,
274
+ tabIndex,
275
+ styles,
276
+ makeStyles,
277
+ withFocusOutline,
278
+ ...props
279
+ } = this.props
280
+
281
+ const { isDisabled, isReadOnly, elementType } = this
282
+ // only add 0 tabIndex value if it doesn't have it by default, see
283
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
284
+ let needsZeroTabIndex = true
285
+ if (typeof elementType == 'string') {
286
+ if (
287
+ [
288
+ 'button',
289
+ 'frame',
290
+ 'iframe',
291
+ 'input',
292
+ 'object',
293
+ 'select',
294
+ 'textarea',
295
+ 'summary'
296
+ ].includes(elementType)
297
+ ) {
298
+ needsZeroTabIndex = false
299
+ }
300
+ if (href && (elementType === 'a' || elementType === 'area')) {
301
+ needsZeroTabIndex = false
302
+ }
303
+ }
304
+ let tabIndexValue = tabIndex
305
+ // In Safari, a button cannot get focus unless it has an explicit 0 tabindex
306
+ if ((onClick && as && needsZeroTabIndex) || (isSafari() && as)) {
307
+ tabIndexValue = tabIndex || 0
308
+ }
309
+
310
+ return (
311
+ <View
312
+ {...passthroughProps(props)}
313
+ as={elementType}
314
+ focusColor={this.focusColor}
315
+ position="relative"
316
+ display={display}
317
+ width={display === 'block' ? '100%' : 'auto'}
318
+ borderRadius={shape === 'circle' ? 'circle' : 'medium'}
319
+ background="transparent"
320
+ padding="none"
321
+ borderWidth="none"
322
+ margin={margin}
323
+ cursor={isDisabled ? 'not-allowed' : cursor}
324
+ href={href}
325
+ type={href ? undefined : type}
326
+ elementRef={this.handleElementRef}
327
+ onClick={this.handleClick}
328
+ onKeyDown={this.handleKeyDown}
329
+ role={onClick && as !== 'button' ? 'button' : undefined}
330
+ tabIndex={tabIndexValue}
331
+ disabled={isDisabled || isReadOnly}
332
+ css={styles?.baseButton}
333
+ withFocusOutline={withFocusOutline}
334
+ data-cid={combineDataCid('BaseButton', this.props)}
335
+ >
336
+ <span css={styles?.content}>{this.renderChildren()}</span>
337
+ </View>
338
+ )
339
+ }
340
+ }
341
+
342
+ export { BaseButton }
343
+ export default BaseButton
@@ -0,0 +1,226 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import type { InteractionType } from '@instructure/ui-react-utils'
27
+ import type {
28
+ Spacing,
29
+ WithStyleProps,
30
+ ComponentStyle
31
+ } from '@instructure/emotion'
32
+ import type {
33
+ ToProp,
34
+ AsElementType,
35
+ BaseButtonTheme,
36
+ OtherHTMLAttributes
37
+ } from '@instructure/shared-types'
38
+ import type { Cursor } from '@instructure/shared-types'
39
+ import type { ViewProps } from '@instructure/ui-view/latest'
40
+ import { Renderable } from '@instructure/shared-types'
41
+
42
+ type BaseButtonOwnProps = {
43
+ /**
44
+ * Specifies the `Button` children.
45
+ */
46
+ children?: React.ReactNode
47
+
48
+ /**
49
+ * Specifies the type of the `Button`'s underlying html element.
50
+ */
51
+ type?: 'button' | 'submit' | 'reset'
52
+
53
+ /**
54
+ * The size of the `Button`
55
+ */
56
+ size?: 'small' | 'medium' | 'large' | 'condensedSmall' | 'condensedMedium'
57
+
58
+ /**
59
+ * Provides a reference to the `Button`'s underlying html element.
60
+ */
61
+ elementRef?: (element: HTMLElement | null) => void
62
+
63
+ /**
64
+ * The element to render as the component root, `Button` by default.
65
+ */
66
+ as?: AsElementType
67
+
68
+ /**
69
+ * Specifies if interaction with the `Button` is enabled, disabled, or readonly.
70
+ */
71
+ interaction?: InteractionType
72
+
73
+ /**
74
+ * Specifies the color for the `Button`.
75
+ */
76
+ color?:
77
+ | 'primary'
78
+ | 'primary-inverse'
79
+ | 'secondary'
80
+ | 'success'
81
+ | 'danger'
82
+ | 'ai-primary'
83
+ | 'ai-secondary'
84
+
85
+ /**
86
+ * Override the `Button`'s default focus outline color.
87
+ */
88
+ focusColor?: 'info' | 'inverse'
89
+
90
+ /**
91
+ * The `Button` display property. When set to `inline-block`, the `Button` displays inline with other elements.
92
+ * When set to block, the `Button` expands to fill the width of the container.
93
+ */
94
+ display?: 'inline-block' | 'block'
95
+
96
+ /**
97
+ * Sets the alignment of the `Button` children and/or icon.
98
+ */
99
+ textAlign?: 'start' | 'center'
100
+
101
+ /**
102
+ * Specifies if the `Button` shape should be a circle or rectangle.
103
+ */
104
+ shape?: 'rectangle' | 'circle'
105
+
106
+ /**
107
+ * Specifies if the `Button` should render with a solid background. When false, the background is transparent.
108
+ */
109
+ withBackground?: boolean
110
+
111
+ /**
112
+ * Specifies if the `Button` should render with a border.
113
+ */
114
+ withBorder?: boolean
115
+
116
+ /**
117
+ * Designates if the `Button` should render without padding. This option should only be set when `withBorder` and
118
+ * `withBackground` are also set to false.
119
+ */
120
+ isCondensed?: boolean
121
+
122
+ /**
123
+ * Valid values are `0`, `none`, `auto`, `xxx-small`, `xx-small`, `x-small`,
124
+ * `small`, `medium`, `large`, `x-large`, `xx-large`. Apply these values via
125
+ * familiar CSS-like shorthand. For example: `margin="small auto large"`.
126
+ */
127
+ margin?: Spacing
128
+
129
+ /**
130
+ * Specify a mouse cursor to use when hovering over the button.
131
+ * The `pointer` cursor is used by default.
132
+ */
133
+ cursor?: Cursor
134
+
135
+ /**
136
+ * Specifies an href attribute for the `Button`'s underlying html element.
137
+ */
138
+ href?: string
139
+
140
+ /**
141
+ * Callback fired when the `Button` is clicked.
142
+ */
143
+ onClick?: (
144
+ event: React.KeyboardEvent<ViewProps> | React.MouseEvent<ViewProps>
145
+ ) => void
146
+
147
+ /**
148
+ * Callback fired when the `Button` receives a keydown event.
149
+ */
150
+ onKeyDown?: (event: React.KeyboardEvent<ViewProps>) => void
151
+
152
+ /**
153
+ * An icon, or function that returns an icon.
154
+ */
155
+ renderIcon?: Renderable
156
+
157
+ /**
158
+ * Specifies the tabindex of the `Button`.
159
+ */
160
+ tabIndex?: number
161
+
162
+ /**
163
+ * Manually control if the `Button` should display a focus outline.
164
+ *
165
+ * When left `undefined` (which is the default) the focus outline will display
166
+ * if this component is focusable and receives focus.
167
+ */
168
+ withFocusOutline?: boolean
169
+ }
170
+
171
+ type BaseButtonStyleProps = {
172
+ isDisabled: boolean
173
+ hasOnlyIconVisible: boolean
174
+ isEnabled: boolean
175
+ }
176
+
177
+ type PropKeys = keyof BaseButtonOwnProps
178
+
179
+ type AllowedPropKeys = Readonly<Array<PropKeys>>
180
+
181
+ type BaseButtonProps = BaseButtonOwnProps &
182
+ WithStyleProps<BaseButtonTheme, BaseButtonStyle> &
183
+ OtherHTMLAttributes<BaseButtonOwnProps> &
184
+ ToProp
185
+
186
+ type BaseButtonStyle = ComponentStyle<
187
+ | 'baseButton'
188
+ | 'content'
189
+ | 'children'
190
+ | 'iconSVG'
191
+ | 'childrenLayout'
192
+ | 'iconOnly'
193
+ | 'iconWrapper'
194
+ | 'childrenWrapper'
195
+ >
196
+ const allowedProps: AllowedPropKeys = [
197
+ 'as',
198
+ 'children',
199
+ 'color',
200
+ 'cursor',
201
+ 'display',
202
+ 'elementRef',
203
+ 'focusColor',
204
+ 'href',
205
+ 'interaction',
206
+ 'isCondensed',
207
+ 'margin',
208
+ 'onClick',
209
+ 'onKeyDown',
210
+ 'renderIcon',
211
+ 'shape',
212
+ 'size',
213
+ 'tabIndex',
214
+ 'textAlign',
215
+ 'type',
216
+ 'withBackground',
217
+ 'withBorder'
218
+ ]
219
+
220
+ export type {
221
+ BaseButtonProps,
222
+ BaseButtonOwnProps,
223
+ BaseButtonStyleProps,
224
+ BaseButtonStyle
225
+ }
226
+ export { allowedProps }