UNPKG

11.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var slicedToArray = require('./slicedToArray-0711941d.js');
8require('./unsupportedIterableToArray-68db1d3b.js');
9var React = require('react');
10var React__default = _interopDefault(React);
11require('./_commonjsHelpers-72d386ba.js');
12var index = require('./index-b0606964.js');
13require('./defineProperty-0921a47c.js');
14require('./toConsumableArray-d8a4a2c3.js');
15var _styled = require('styled-components');
16var _styled__default = _interopDefault(_styled);
17require('./getPrototypeOf-2a661a20.js');
18require('./color.js');
19require('./components.js');
20require('./contains-component.js');
21var css = require('./css.js');
22require('./dayjs.min-e07657bf.js');
23require('./date.js');
24require('./miscellaneous.js');
25var environment = require('./environment.js');
26require('./font.js');
27require('./math-f4029164.js');
28require('./characters.js');
29require('./format.js');
30require('./keycodes.js');
31require('./url.js');
32require('./web3.js');
33var constants = require('./constants.js');
34require('./breakpoints.js');
35require('./springs.js');
36var textStyles = require('./text-styles.js');
37require('./theme-dark.js');
38require('./theme-light.js');
39var Theme = require('./Theme.js');
40var _extends = require('./extends-40571110.js');
41var objectWithoutProperties = require('./objectWithoutProperties-35db8ab0.js');
42var index$1 = require('./index-ecc57c9f.js');
43require('./isObject-ec755c87.js');
44require('./Viewport-15101437.js');
45var Layout = require('./Layout.js');
46require('./FocusVisible.js');
47var ButtonBase = require('./ButtonBase.js');
48
49// See src/icons/icon-size.js for the corresponding icon sizes.
50
51var SIZE_STYLES = {
52 medium: {
53 textStyleName: 'body2',
54 height: 5 * constants.GU,
55 padding: 3 * constants.GU,
56 iconPadding: 2 * constants.GU,
57 minWidth: 14.5 * constants.GU,
58 middleSpace: 1 * constants.GU
59 },
60 small: {
61 textStyleName: 'body2',
62 height: 4 * constants.GU,
63 padding: 2 * constants.GU,
64 iconPadding: 1.5 * constants.GU,
65 minWidth: 13 * constants.GU,
66 middleSpace: 1 * constants.GU
67 },
68 mini: {
69 textStyleName: 'body4',
70 height: 3 * constants.GU,
71 padding: 1.5 * constants.GU,
72 iconPadding: 1 * constants.GU,
73 minWidth: 9.25 * constants.GU,
74 middleSpace: 0.5 * constants.GU
75 }
76};
77
78function getPadding(size, displayIcon, displayLabel) {
79 var _SIZE_STYLES$size = SIZE_STYLES[size],
80 padding = _SIZE_STYLES$size.padding,
81 iconPadding = _SIZE_STYLES$size.iconPadding;
82
83 if (displayIcon && !displayLabel) {
84 return '0';
85 }
86
87 if (displayIcon && displayLabel) {
88 return "0 ".concat(padding, "px 0 ").concat(iconPadding, "px");
89 }
90
91 return "0 ".concat(padding, "px");
92}
93
94function getWidth(size, displayIconOnly, wide) {
95 var height = SIZE_STYLES[size].height;
96
97 if (wide) {
98 return '100%';
99 }
100
101 if (displayIconOnly) {
102 return "".concat(height, "px");
103 }
104
105 return 'auto';
106}
107
108function getMinWidth(size, displayLabelOnly) {
109 var minWidth = SIZE_STYLES[size].minWidth;
110 return displayLabelOnly ? "".concat(minWidth, "px") : '0';
111} // CSS styles related to the current size
112
113
114function sizeStyles(size, wide, displayIcon, displayLabel) {
115 var _SIZE_STYLES$size2 = SIZE_STYLES[size],
116 height = _SIZE_STYLES$size2.height,
117 textStyleName = _SIZE_STYLES$size2.textStyleName,
118 middleSpace = _SIZE_STYLES$size2.middleSpace;
119 return {
120 height: "".concat(height, "px"),
121 middleSpace: displayIcon && displayLabel ? "".concat(middleSpace, "px") : '0',
122 minWidth: getMinWidth(size, !displayIcon && displayLabel),
123 padding: getPadding(size, displayIcon, displayLabel),
124 textStyleCss: textStyles.textStyle(textStyleName),
125 width: getWidth(size, displayIcon && !displayLabel, wide)
126 };
127} // CSS styles related to the current mode
128
129
130function modeStyles(theme, mode, disabled) {
131 if (disabled) {
132 return {
133 background: theme.disabled,
134 color: theme.disabledContent,
135 iconColor: theme.disabledContent,
136 border: '0'
137 };
138 }
139
140 if (mode === 'strong') {
141 return {
142 background: "\n linear-gradient(\n 190deg,\n ".concat(theme.accentStart, " -100%,\n ").concat(theme.accentEnd, " 80%\n )\n "),
143 color: theme.accentContent,
144 iconColor: theme.accentContent,
145 border: '0'
146 };
147 }
148
149 if (mode === 'positive') {
150 return {
151 background: theme.positive,
152 color: theme.positiveContent,
153 iconColor: theme.positiveContent,
154 border: '0'
155 };
156 }
157
158 if (mode === 'negative') {
159 return {
160 background: theme.negative,
161 color: theme.negativeContent,
162 iconColor: theme.negativeContent,
163 border: '0'
164 };
165 }
166
167 return {
168 background: theme.surfaceInteractive,
169 color: theme.surfaceContent,
170 iconColor: theme.surfaceIcon,
171 border: "1px solid ".concat(theme.border)
172 };
173}
174
175var _StyledButtonBase = _styled__default(ButtonBase.default).withConfig({
176 displayName: "Button___StyledButtonBase",
177 componentId: "sc-8npd5h-0"
178})(["display:", ";align-items:center;justify-content:center;width:", ";height:", ";min-width:", ";padding:", ";", ";", ";background:", ";color:", ";white-space:nowrap;border:", ";box-shadow:", ";transition-property:transform,box-shadow;transition-duration:50ms;transition-timing-function:ease-in-out;&:active{transform:", ";box-shadow:", ";}"], function (p) {
179 return p._css;
180}, function (p) {
181 return p._css2;
182}, function (p) {
183 return p._css3;
184}, function (p) {
185 return p._css4;
186}, function (p) {
187 return p._css5;
188}, function (p) {
189 return p._css6;
190}, css.unselectable, function (p) {
191 return p._css7;
192}, function (p) {
193 return p._css8;
194}, function (p) {
195 return p._css9;
196}, function (p) {
197 return p._css10;
198}, function (p) {
199 return p._css11;
200}, function (p) {
201 return p._css12;
202});
203
204var _StyledSpan = _styled__default("span").withConfig({
205 displayName: "Button___StyledSpan",
206 componentId: "sc-8npd5h-1"
207})(["position:relative;top:-1px;display:flex;color:", ";margin-right:", ";"], function (p) {
208 return p._css13;
209}, function (p) {
210 return p._css14;
211});
212
213function Button(_ref) {
214 var children = _ref.children,
215 disabled = _ref.disabled,
216 display = _ref.display,
217 icon = _ref.icon,
218 iconOnly = _ref.iconOnly,
219 innerRef = _ref.innerRef,
220 label = _ref.label,
221 mode = _ref.mode,
222 size = _ref.size,
223 wide = _ref.wide,
224 props = objectWithoutProperties._objectWithoutProperties(_ref, ["children", "disabled", "display", "icon", "iconOnly", "innerRef", "label", "mode", "size", "wide"]);
225
226 // backward compatibility and deprecated props
227 if (iconOnly) {
228 environment.warnOnce('Button:iconOnly', 'Button: "iconOnly" is deprecated, please use "display".');
229 display = 'icon';
230 }
231
232 if (mode === 'outline' || mode === 'secondary') {
233 environment.warnOnce('Button:mode', "Button: the mode \"".concat(mode, "\" is deprecated, please use \"normal\"."));
234 mode = 'normal';
235 }
236
237 if (size === 'normal' || size === 'large') {
238 environment.warnOnce('Button:size', "Button: the size \"".concat(size, "\" is deprecated, please use \"medium\"."));
239 size = 'medium';
240 } // prop warnings
241
242
243 if (display === 'icon' && !icon) {
244 environment.warn('Button: the display "icon" was used without providing an icon.');
245 }
246
247 if (!children && !label) {
248 environment.warn('Button: please provide a label.');
249 }
250
251 var theme = Theme.useTheme();
252
253 var _useLayout = Layout.useLayout(),
254 layoutName = _useLayout.layoutName;
255
256 var _useInside = index$1.o('EmptyStateCard'),
257 _useInside2 = slicedToArray._slicedToArray(_useInside, 1),
258 insideEmptyStateCard = _useInside2[0];
259
260 var _useInside3 = index$1.o('Header:secondary'),
261 _useInside4 = slicedToArray._slicedToArray(_useInside3, 1),
262 insideHeaderSecondary = _useInside4[0]; // Always wide + strong when used as an empty state card action
263
264
265 if (insideEmptyStateCard) {
266 mode = 'strong';
267 wide = true;
268 } // Alternate between icon and label automatically when used in Header
269
270
271 if (insideHeaderSecondary && display === 'auto' && icon && label) {
272 display = layoutName === 'small' ? 'icon' : 'label';
273 } // Otherwise, display both
274
275
276 if (display === 'auto') {
277 display = 'all';
278 }
279
280 var displayIcon = icon && (display === 'all' || display === 'icon');
281 var displayLabel = label && (display === 'all' || display === 'label'); // Mode styles
282
283 var _useMemo = React.useMemo(function () {
284 return modeStyles(theme, mode, disabled);
285 }, [mode, theme, disabled]),
286 background = _useMemo.background,
287 color = _useMemo.color,
288 iconColor = _useMemo.iconColor,
289 border = _useMemo.border; // Size styles
290
291
292 var _useMemo2 = React.useMemo(function () {
293 return sizeStyles(size, wide, displayIcon, displayLabel);
294 }, [size, wide, displayIcon, displayLabel]),
295 height = _useMemo2.height,
296 middleSpace = _useMemo2.middleSpace,
297 minWidth = _useMemo2.minWidth,
298 padding = _useMemo2.padding,
299 textStyleCss = _useMemo2.textStyleCss,
300 width = _useMemo2.width; // Use the label as a title when only the icon is displayed
301
302
303 if (displayIcon && !displayLabel && label && typeof label === 'string') {
304 props.title = label;
305 }
306
307 var insideData = {
308 size: size
309 };
310 return /*#__PURE__*/React__default.createElement(_StyledButtonBase, _extends._extends({
311 ref: innerRef,
312 focusRingSpacing: border === '0' ? 0 : 1,
313 focusRingRadius: constants.RADIUS,
314 disabled: disabled
315 }, props, {
316 _css: wide ? 'flex' : 'inline-flex',
317 _css2: width,
318 _css3: height,
319 _css4: minWidth,
320 _css5: padding,
321 _css6: textStyleCss,
322 _css7: background,
323 _css8: color,
324 _css9: border,
325 _css10: disabled ? 'none' : '0 1px 3px rgba(0, 0, 0, 0.1)',
326 _css11: disabled ? 'none' : 'translateY(1px)',
327 _css12: disabled ? 'none' : '0px 1px 2px rgba(0, 0, 0, 0.08)'
328 }), /*#__PURE__*/React__default.createElement(index$1.i, {
329 name: "Button",
330 data: insideData
331 }, children || /*#__PURE__*/React__default.createElement(React__default.Fragment, null, displayIcon && /*#__PURE__*/React__default.createElement(index$1.i, {
332 name: "Button:icon",
333 data: insideData
334 }, /*#__PURE__*/React__default.createElement(_StyledSpan, {
335 _css13: iconColor,
336 _css14: middleSpace
337 }, icon)), displayLabel && /*#__PURE__*/React__default.createElement(index$1.i, {
338 name: "Button:label",
339 data: insideData
340 }, label))));
341}
342
343Button.propTypes = {
344 children: index.PropTypes.node,
345 disabled: index.PropTypes.bool,
346 display: index.PropTypes.oneOf(['auto', 'all', 'icon', 'label']),
347 icon: index.PropTypes.node,
348 innerRef: index.PropTypes.any,
349 label: index.PropTypes.string,
350 mode: index.PropTypes.oneOf(['normal', 'strong', 'positive', 'negative', // deprecated
351 'outline', 'secondary', 'text']),
352 size: index.PropTypes.oneOf(['medium', 'small', 'mini', // deprecated
353 'large', 'normal']),
354 wide: index.PropTypes.bool,
355 // deprecated
356 iconOnly: index.PropTypes.bool
357};
358Button.defaultProps = {
359 disabled: false,
360 display: 'auto',
361 mode: 'normal',
362 size: 'medium',
363 wide: false
364};
365var ButtonWithRef = React__default.forwardRef(function (props, ref) {
366 return /*#__PURE__*/React__default.createElement(Button, _extends._extends({
367 innerRef: ref
368 }, props));
369});
370ButtonWithRef.Anchor = React__default.forwardRef(function (props, ref) {
371 environment.warnOnce('Button.Anchor', 'Button.Anchor is deprecated: please use Button with a href prop instead.');
372 return /*#__PURE__*/React__default.createElement(ButtonWithRef, _extends._extends({
373 ref: ref
374 }, props));
375});
376
377exports.default = ButtonWithRef;
378//# sourceMappingURL=Button.js.map