@braineet/ui
Version:
Braineet design system
113 lines (112 loc) • 4.49 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _blockEvents = require("./blockEvents");
var _styles = require("./styles");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["disabled", "color", "styleType", "type", "children", "size", "icon", "direction", "isLoading", "loadingText", "reverseColor", "title", "shouldFitContainer", "as", "hasShadow"];
/* eslint-disable no-nested-ternary */
/**
* The `Button` component.
*/
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var Button = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
var disabled = _ref.disabled,
color = _ref.color,
styleType = _ref.styleType,
type = _ref.type,
children = _ref.children,
size = _ref.size,
icon = _ref.icon,
direction = _ref.direction,
isLoading = _ref.isLoading,
loadingText = _ref.loadingText,
reverseColor = _ref.reverseColor,
title = _ref.title,
shouldFitContainer = _ref.shouldFitContainer,
as = _ref.as,
hasShadow = _ref.hasShadow,
restProps = _objectWithoutPropertiesLoose(_ref, _excluded);
var globalSize = ['medium', 'large'].includes(size) ? size : 'medium';
var btnIcon = isLoading ? 'spinner' : icon;
var a11yProps = isLoading ? {
'aria-live': 'polite',
'aria-atomic': true
} : {};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.StyledButton, _extends({
ref: ref,
disabled: disabled,
$textColor: color,
size: globalSize,
type: type,
$styleType: styleType,
$direction: direction,
$isLoading: isLoading,
$reverseColor: reverseColor,
forwardedAs: as,
$shouldFitContainer: shouldFitContainer,
$hasShadow: hasShadow
}, a11yProps, restProps, (0, _blockEvents.blockEvents)({
isLoading: isLoading
}), {
children: isLoading ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [children && typeof children === 'string' && children !== '' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.TextButton, {
fontFamily: "display",
size: _styles.sizes[globalSize].text,
$direction: direction,
styleType: styleType,
icon: btnIcon,
children: loadingText
}) : /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: "sr-only",
children: title
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.LoadingIcon, {
$hasText: typeof children === 'string' && children !== '',
$size: globalSize,
$direction: direction,
size: _styles.sizes[globalSize].icon,
"aria-hidden": "true",
focusable: "false"
})]
}) : typeof children === 'string' && children !== '' || typeof btnIcon === 'string' && btnIcon !== '' ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [typeof children === 'string' && children !== '' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.TextButton, {
fontFamily: "display",
size: _styles.sizes[globalSize].text,
$direction: direction,
styleType: styleType,
icon: btnIcon,
children: children
}) : title && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: "sr-only",
children: title
}), typeof btnIcon === 'string' && btnIcon !== '' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.IconButton, {
name: btnIcon,
$hasText: typeof children === 'string' && children !== '',
$size: globalSize,
$direction: direction,
size: _styles.sizes[globalSize].icon,
"aria-hidden": "true",
focusable: "false"
})]
}) : children
}));
});
Button.defaultProps = {
title: '',
children: null,
color: 'brand',
direction: 'left',
loadingText: 'Loading...',
disabled: false,
hasShadow: true,
icon: '',
isLoading: false,
size: 'medium',
styleType: 'solid',
as: 'button',
type: 'button'
};
var _default = exports.default = Button;