@cloudflare/component-button 5.0.0 → 5.2.0

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.
package/lib/Button.js CHANGED
@@ -25,18 +25,24 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
25
25
 
26
26
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
27
 
28
- var standardColors = function standardColors(theme) {
28
+ var borderRadius = '4px';
29
+
30
+ var standardColors = function standardColors(theme, state) {
31
+ var _theme$colors$button$, _theme$colors$button$2, _theme$colors$button$3, _theme$colors$button$4, _theme$colors$button$5, _theme$colors$button$6;
32
+
29
33
  var color = theme.colors.gray[9];
30
34
  return {
31
35
  default: (0, _styleConst.isDarkMode)() ? color : theme.colors.gray[1],
32
- primary: color,
33
36
  success: color,
34
37
  warning: color,
35
- danger: color,
36
38
  inverted: {
37
- danger: theme.colors.red[3],
38
- primary: theme.colors.blue[3]
39
- }
39
+ danger: (_theme$colors$button$ = theme.colors.button.danger[state]) === null || _theme$colors$button$ === void 0 ? void 0 : _theme$colors$button$[state === 'active' ? 'text' : 'background'],
40
+ primary: (_theme$colors$button$2 = theme.colors.button.primary[state]) === null || _theme$colors$button$2 === void 0 ? void 0 : _theme$colors$button$2[state === 'active' ? 'text' : 'background']
41
+ },
42
+ primary: ((_theme$colors$button$3 = theme.colors.button.primary[state]) === null || _theme$colors$button$3 === void 0 ? void 0 : _theme$colors$button$3.text) || color,
43
+ danger: ((_theme$colors$button$4 = theme.colors.button.danger[state]) === null || _theme$colors$button$4 === void 0 ? void 0 : _theme$colors$button$4.text) || color,
44
+ plain: ((_theme$colors$button$5 = theme.colors.button.plain[state]) === null || _theme$colors$button$5 === void 0 ? void 0 : _theme$colors$button$5.text) || color,
45
+ plainMono: ((_theme$colors$button$6 = theme.colors.button.plainMono[state]) === null || _theme$colors$button$6 === void 0 ? void 0 : _theme$colors$button$6.text) || color
40
46
  };
41
47
  };
42
48
 
@@ -48,95 +54,73 @@ var buttonColors = function buttonColors(theme) {
48
54
  normal: {
49
55
  backgroundColor: {
50
56
  default: (0, _styleConst.isDarkMode)() ? theme.colors.gray[standardIndex] : theme.colors.gray[8],
51
- primary: theme.colors.blue[standardIndex],
57
+ primary: theme.colors.button.primary.normal.background,
52
58
  success: theme.colors.green[standardIndex],
53
59
  warning: theme.colors.orange[standardIndex],
54
- danger: theme.colors.red[standardIndex],
60
+ danger: theme.colors.button.danger.normal.background,
55
61
  inverted: {
56
62
  danger: 'transparent',
57
63
  primary: 'transparent'
58
- }
64
+ },
65
+ plain: theme.colors.button.plain.normal.background,
66
+ plainMono: theme.colors.button.plainMono.normal.background
59
67
  },
60
- color: standardColors(theme)
68
+ color: standardColors(theme, 'normal')
61
69
  },
62
70
  // Hover
63
71
  hover: {
64
72
  backgroundColor: {
65
73
  default: theme.colors.gray[(0, _styleConst.isDarkMode)() ? 2 : 7],
66
- primary: theme.colors.blue[hoverIndex],
74
+ primary: theme.colors.button.primary.hover.background,
67
75
  success: theme.colors.green[hoverIndex],
68
76
  warning: theme.colors.orange[hoverIndex],
69
- danger: theme.colors.red[hoverIndex],
70
- inverted: {
71
- danger: theme.colors.red[hoverIndex],
72
- primary: theme.colors.blue[hoverIndex]
73
- }
74
- },
75
- color: _objectSpread(_objectSpread({}, standardColors(theme)), {}, {
77
+ danger: theme.colors.button.danger.hover.background,
76
78
  inverted: {
77
79
  danger: theme.colors.white,
78
80
  primary: theme.colors.white
79
- }
80
- })
81
+ },
82
+ plain: theme.colors.button.plain.hover.background,
83
+ plainMono: theme.colors.button.plainMono.hover.background
84
+ },
85
+ color: standardColors(theme, 'hover')
81
86
  },
82
87
  // Active
83
88
  active: {
84
89
  backgroundColor: {
85
- default: theme.colors.gray[(0, _styleConst.isDarkMode)() ? 3 : 8],
86
- primary: theme.colors.blue[standardIndex],
90
+ default: theme.colors.gray[(0, _styleConst.isDarkMode)() ? 3 : 6],
91
+ primary: theme.colors.button.primary.active.background,
87
92
  success: theme.colors.green[standardIndex],
88
93
  warning: theme.colors.orange[standardIndex],
89
- danger: theme.colors.red[standardIndex],
94
+ danger: theme.colors.button.danger.active.background,
90
95
  inverted: {
91
- danger: theme.colors.red[standardIndex],
92
- primary: theme.colors.blue[standardIndex]
93
- }
96
+ danger: theme.colors.red[hoverIndex],
97
+ primary: theme.colors.blue[hoverIndex]
98
+ },
99
+ plain: theme.colors.button.plain.active.background,
100
+ plainMono: theme.colors.button.plainMono.active.background
94
101
  },
95
- color: _objectSpread(_objectSpread({}, standardColors(theme)), {}, {
96
- inverted: {
97
- danger: theme.colors.gray[9],
98
- primary: theme.colors.gray[9]
99
- }
100
- })
102
+ color: standardColors(theme, 'active')
101
103
  },
102
104
  // Focus
103
105
  focus: {
104
106
  backgroundColor: {
105
107
  default: theme.colors.gray[(0, _styleConst.isDarkMode)() ? 3 : 8],
106
- primary: theme.colors.blue[standardIndex],
108
+ primary: theme.colors.button.primary.focus.background,
107
109
  success: theme.colors.green[standardIndex],
108
110
  warning: theme.colors.orange[standardIndex],
109
- danger: theme.colors.red[standardIndex],
111
+ danger: theme.colors.button.danger.focus.background,
110
112
  inverted: {
111
113
  danger: 'transparent',
112
114
  primary: 'transparent'
113
- }
115
+ },
116
+ plain: theme.colors.button.plain.focus.background,
117
+ plainMono: theme.colors.button.plainMono.focus.background
114
118
  },
115
- color: standardColors(theme)
119
+ color: standardColors(theme, 'focus')
116
120
  }
117
121
  };
118
122
  };
119
123
 
120
- var getFocusOutlineColor = function getFocusOutlineColor(theme, type, inverted) {
121
- var _invertedOutlines$typ;
122
-
123
- var standardIndex = (0, _styleConst.isDarkMode)() ? 3 : 4;
124
- var FOCUS_OFFSET_INDEX = 2;
125
- var focusIndex = standardIndex - FOCUS_OFFSET_INDEX;
126
- var outlines = {
127
- default: (0, _styleConst.isDarkMode)() ? theme.colors.gray[focusIndex] : theme.colors.gray[8 - FOCUS_OFFSET_INDEX],
128
- primary: theme.colors.blue[focusIndex],
129
- success: theme.colors.green[focusIndex],
130
- warning: theme.colors.orange[focusIndex],
131
- danger: theme.colors.red[focusIndex]
132
- };
133
- var invertedOutlines = {
134
- danger: theme.colors.red[focusIndex],
135
- primary: theme.colors.blue[focusIndex]
136
- };
137
- return inverted ? (_invertedOutlines$typ = invertedOutlines[type]) !== null && _invertedOutlines$typ !== void 0 ? _invertedOutlines$typ : outlines[type] : outlines[type];
138
- };
139
-
140
124
  var colorAccessor = function colorAccessor(property, _ref) {
141
125
  var _ref$inverted = _ref.inverted,
142
126
  inverted = _ref$inverted === void 0 ? false : _ref$inverted,
@@ -151,13 +135,18 @@ var interactionStyleAccessor = function interactionStyleAccessor(interactionStat
151
135
  disabled = props.disabled;
152
136
  var isInteractive = !(loading || disabled);
153
137
  var stateStyles = buttonColors(theme)[interactionState];
138
+ var color = colorAccessor(stateStyles.color, props);
139
+ var backgroundColor = colorAccessor(stateStyles.backgroundColor, props);
154
140
  return isInteractive ? {
155
- backgroundColor: colorAccessor(stateStyles.backgroundColor, props),
156
- color: colorAccessor(stateStyles.color, props)
141
+ backgroundColor: backgroundColor,
142
+ color: color,
143
+ boxShadow: props.inverted ? "inset 0px 0px 0px 1px ".concat(interactionState === 'active' ? backgroundColor : color) : undefined
157
144
  } : undefined;
158
145
  };
159
146
 
160
147
  var buttonStyles = function buttonStyles(props) {
148
+ var _styles$buttonState, _styles$buttonState2;
149
+
161
150
  var theme = props.theme,
162
151
  _props$loading = props.loading,
163
152
  loading = _props$loading === void 0 ? false : _props$loading,
@@ -172,27 +161,32 @@ var buttonStyles = function buttonStyles(props) {
172
161
  _props$type = props.type,
173
162
  type = _props$type === void 0 ? 'default' : _props$type,
174
163
  stretch = props.stretch,
175
- noWrapText = props.noWrapText;
176
- var normalStyles = buttonColors(theme).normal;
177
- var hoverStyles = interactionStyleAccessor('hover', props);
178
- var activeStyles = interactionStyleAccessor('active', props);
179
- var focusStyles = interactionStyleAccessor('focus', props);
180
- return {
181
- '&:hover': hoverStyles,
182
- '&:active': activeStyles,
183
- '&:focus': focusStyles,
164
+ noWrapText = props.noWrapText,
165
+ buttonState = props.buttonState;
166
+ var styles = {
167
+ normal: buttonColors(theme).normal,
168
+ hover: interactionStyleAccessor('hover', props),
169
+ active: interactionStyleAccessor('active', props),
170
+ focus: interactionStyleAccessor('focus', props)
171
+ };
172
+ var color = buttonState && styles[buttonState] ? (_styles$buttonState = styles[buttonState]) === null || _styles$buttonState === void 0 ? void 0 : _styles$buttonState.color : colorAccessor(styles.normal.color, props);
173
+ var backgroundColor = buttonState && styles[buttonState] ? (_styles$buttonState2 = styles[buttonState]) === null || _styles$buttonState2 === void 0 ? void 0 : _styles$buttonState2.backgroundColor : colorAccessor(styles.normal.backgroundColor, props);
174
+ return _objectSpread(_objectSpread(_objectSpread({}, buttonState ? {} : {
175
+ '&:focus': styles.focus,
176
+ '&:hover': styles.hover,
177
+ '&:active': styles.active,
178
+ '&:focus:active': styles.active
179
+ }), {}, {
184
180
  '&:focus-visible': {
185
181
  outlineOffset: 3,
186
182
  outlineStyle: 'auto',
187
- outlineColor: getFocusOutlineColor(theme, type, inverted)
183
+ outlineColor: type === 'danger' ? theme.colors.red[(0, _styleConst.isDarkMode)() ? 3 : 4] : theme.colors.focus
188
184
  },
189
- '&:focus-within:hover': focusStyles,
190
- '&:active:hover': activeStyles,
191
185
  '&[title]': {
192
186
  pointerEvents: disabled ? 'auto' : 'none'
193
187
  },
194
- backgroundColor: colorAccessor(normalStyles.backgroundColor, props),
195
- color: loading ? 'transparent' : colorAccessor(normalStyles.color, props),
188
+ backgroundColor: backgroundColor,
189
+ color: loading ? 'transparent' : color,
196
190
  cursor: disabled || loading ? 'default' : 'pointer',
197
191
  display: 'inline-flex',
198
192
  flexGrow: stretch ? 1 : undefined,
@@ -202,22 +196,17 @@ var buttonStyles = function buttonStyles(props) {
202
196
  fontSize: 2,
203
197
  fontWeight: 400,
204
198
  lineHeight: 1.2,
205
- borderBottomWidth: inverted ? '1px' : 0,
206
- borderLeftWidth: inverted ? '1px' : 0,
207
- borderRightWidth: inverted ? '1px' : 0,
208
- borderTopWidth: inverted ? '1px' : 0,
209
- borderBottomStyle: 'solid',
210
- borderTopStyle: 'solid',
211
- borderLeftStyle: 'solid',
212
- borderRightStyle: 'solid',
213
- borderColor: inverted ? colorAccessor(normalStyles.color, props) : 'currentColor',
214
- borderTopRightRadius: (group === 'first' || group === 'inbetween') && !spaced ? 0 : theme.radii[2],
215
- borderTopLeftRadius: (group === 'last' || group === 'inbetween') && !spaced ? 0 : theme.radii[2],
216
- borderBottomLeftRadius: (group === 'last' || group === 'inbetween') && !spaced ? 0 : theme.radii[2],
217
- borderBottomRightRadius: (group === 'first' || group === 'inbetween') && !spaced ? 0 : theme.radii[2],
199
+ border: 'none'
200
+ }, inverted ? {
201
+ boxShadow: "inset 0px 0px 0px 1px ".concat(buttonState === 'active' ? backgroundColor : color)
202
+ } : {}), {}, {
203
+ borderTopRightRadius: (group === 'first' || group === 'inbetween') && !spaced ? 0 : borderRadius,
204
+ borderTopLeftRadius: (group === 'last' || group === 'inbetween') && !spaced ? 0 : borderRadius,
205
+ borderBottomLeftRadius: (group === 'last' || group === 'inbetween') && !spaced ? 0 : borderRadius,
206
+ borderBottomRightRadius: (group === 'first' || group === 'inbetween') && !spaced ? 0 : borderRadius,
218
207
  '&::before': loading ? {
219
208
  content: '"•••"',
220
- color: colorAccessor(normalStyles.color, props),
209
+ color: color,
221
210
  display: 'flex',
222
211
  alignItems: 'center',
223
212
  justifyContent: 'center',
@@ -245,7 +234,7 @@ var buttonStyles = function buttonStyles(props) {
245
234
  pointerEvents: disabled ? 'none' : 'auto',
246
235
  position: 'relative',
247
236
  textAlign: 'center',
248
- transitionProperty: loading ? 'background-color,border-color' : 'background-color,border-color,color',
237
+ transitionProperty: 'all',
249
238
  transitionDuration: '200ms',
250
239
  transitionTimingFunction: 'ease',
251
240
  userSelect: 'none',
@@ -253,13 +242,14 @@ var buttonStyles = function buttonStyles(props) {
253
242
  float: 'none',
254
243
  whiteSpace: noWrapText ? 'nowrap' : 'normal',
255
244
  width: direction === 'column' ? '100%' : 'auto'
256
- };
245
+ });
257
246
  };
258
247
 
259
248
  exports.buttonStyles = buttonStyles;
260
249
 
261
250
  var Button = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
262
- var _props$submit = props.submit,
251
+ var id = props.id,
252
+ _props$submit = props.submit,
263
253
  submit = _props$submit === void 0 ? false : _props$submit,
264
254
  className = props.className,
265
255
  disabled = props.disabled,
@@ -282,6 +272,7 @@ var Button = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
282
272
  return _objectSpread(_objectSpread({}, memo), {}, _defineProperty({}, propName, props[propName]));
283
273
  }, {});
284
274
  return /*#__PURE__*/_react.default.createElement("button", _extends({}, ariaAndDataProps, {
275
+ id: id,
285
276
  type: submit ? 'submit' : 'button',
286
277
  "data-testid": testId,
287
278
  disabled: disabled || loading,
@@ -305,6 +296,7 @@ var Button = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
305
296
  });
306
297
 
307
298
  Button.propTypes = {
299
+ id: _propTypes.default.string,
308
300
  onClick: _propTypes.default.func,
309
301
  submit: _propTypes.default.bool,
310
302
  spaced: _propTypes.default.bool,
@@ -312,7 +304,7 @@ Button.propTypes = {
312
304
  direction: _propTypes.default.oneOf(['column', 'row']),
313
305
  className: _propTypes.default.string,
314
306
  group: _propTypes.default.oneOf(['first', 'inbetween', 'last']),
315
- type: _propTypes.default.oneOf(['default', 'primary', 'success', 'warning', 'danger']),
307
+ type: _propTypes.default.oneOf(['default', 'primary', 'success', 'warning', 'danger', 'plain', 'plainMono']),
316
308
  testId: _propTypes.default.string,
317
309
  disabled: _propTypes.default.bool,
318
310
  loading: _propTypes.default.bool,
@@ -324,7 +316,8 @@ Button.propTypes = {
324
316
  tabIndex: _propTypes.default.number,
325
317
  ariaLabel: _propTypes.default.string,
326
318
  stretch: _propTypes.default.bool,
327
- noWrapText: _propTypes.default.bool
319
+ noWrapText: _propTypes.default.bool,
320
+ buttonState: _propTypes.default.oneOf(['hover', 'active', 'focus'])
328
321
  };
329
322
  Button.displayName = 'Button';
330
323
 
@@ -7,16 +7,26 @@ exports.default = void 0;
7
7
 
8
8
  var _styleContainer = require("@cloudflare/style-container");
9
9
 
10
- var UnstyledButton = (0, _styleContainer.createStyledComponent)(function () {
11
- return {
10
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
11
+
12
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
+
14
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+
16
+ var UnstyledButton = (0, _styleContainer.createStyledComponent)(function (_ref) {
17
+ var theme = _ref.theme,
18
+ focus = _ref.focus,
19
+ disabled = _ref.disabled;
20
+ return _objectSpread({
12
21
  background: 'none',
13
22
  color: 'inherit',
14
23
  border: 'none',
15
24
  padding: 0,
25
+ opacity: disabled ? 0.5 : undefined,
16
26
  font: 'inherit',
17
27
  cursor: 'pointer',
18
28
  outline: 'inherit'
19
- };
29
+ }, focus ? theme.mixins.focus() : {});
20
30
  }, 'button');
21
31
  var _default = UnstyledButton;
22
32
  exports.default = _default;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloudflare/component-button",
3
3
  "description": "Cloudflare Button Component",
4
- "version": "5.0.0",
4
+ "version": "5.2.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
7
7
  "author": "James Kyle <jkyle@cloudflare.com>",