@spaced-out/ui-design-system
Version:
Sense UI components library
248 lines (247 loc) • 9.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UnstyledButton = exports.Button = exports.BUTTON_TYPES = exports.BUTTON_SIZE = exports.BUTTON_ACTION_TYPE = void 0;
var React = _interopRequireWildcard(require("react"));
var _classify = require("../../utils/classify");
var _qa = require("../../utils/qa");
var _CircularLoader = require("../CircularLoader");
var _Icon = require("../Icon");
var _Truncate = require("../Truncate");
var _ButtonModule = _interopRequireDefault(require("./Button.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
* Note(Nishant): Although Button supports gradient as a type, its not currently customizable really.
* This only supports pre-defined gradient that moves from left to right.
* If someone wants to add more gradients, the expectation is that they would add it through a wrapper className.
*
* We could have taken an extra prop to take in the Gradient colors but that should not be encouraged
* as it would add an additional overhead on the component to figure out exact color values from string tokens
* and since this is rarely used type anyway, it should be avoided.
*/
const BUTTON_TYPES = exports.BUTTON_TYPES = Object.freeze({
primary: 'primary',
secondary: 'secondary',
tertiary: 'tertiary',
ghost: 'ghost',
danger: 'danger',
gradient: 'gradient',
tertiaryGradient: 'tertiaryGradient'
});
const BUTTON_ACTION_TYPE = exports.BUTTON_ACTION_TYPE = Object.freeze({
button: 'button',
submit: 'submit',
reset: 'reset'
});
const BUTTON_SIZE = exports.BUTTON_SIZE = Object.freeze({
small: 'small',
medium: 'medium'
});
const ButtonTypeToIconColorMap = {
primary: 'inversePrimary',
secondary: 'clickable',
tertiary: 'primary',
ghost: 'primary',
danger: 'inversePrimary',
gradient: 'inversePrimary',
tertiaryGradient: 'clickable'
};
const ButtonTypeToLoaderColorMap = {
primary: 'colorTextInversePrimary',
secondary: 'colorTextClickable',
tertiary: 'colorTextPrimary',
ghost: 'colorTextPrimary',
danger: 'colorTextInversePrimary',
gradient: 'colorTextInversePrimary',
tertiaryGradient: 'colorTextClickable'
};
const UnstyledButton = exports.UnstyledButton = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
disabled,
onClick,
className,
ariaLabel,
actionType,
tabIndex = 0,
isLoading,
role = 'button',
testId,
...props
} = _ref;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
...props,
...(ariaLabel ? {
'aria-label': ariaLabel
} : {}),
className: className,
ref: ref,
role: role,
disabled: disabled,
tabIndex: disabled ? -1 : tabIndex,
type: actionType,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'root'
}),
onClick: event => {
if (disabled || isLoading) {
event.preventDefault();
} else if (onClick) {
onClick(event);
}
}
});
});
const Button = exports.Button = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
let {
classNames,
children,
iconLeftName = '',
iconLeftType = 'regular',
iconRightName = '',
iconRightType = 'regular',
type = 'primary',
isFluid = false,
disabled = false,
actionType = 'button',
size = 'medium',
isLoading,
testId,
...props
} = _ref2;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(UnstyledButton, {
...props,
actionType: actionType,
disabled: disabled,
isLoading: isLoading,
testId: testId,
className: (0, _classify.classify)(_ButtonModule.default.button, {
[]: type === 'primary',
[]: type === 'secondary',
[]: type === 'tertiary',
[]: type === 'ghost',
[]: type === 'danger',
[]: type === 'gradient',
[]: type === 'tertiaryGradient',
[]: disabled,
[]: size === 'small',
[]: size === 'medium',
[]: isFluid === true,
[]: !!iconLeftName,
[]: !!iconRightName,
[]: !!(iconLeftName && iconRightName),
[]: (iconLeftName || iconRightName) && !children
}, classNames?.wrapper),
ref: ref,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _ButtonModule.default.buttonRow,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'content'
}),
children: !(iconLeftName || iconRightName) ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: (0, _classify.classify)(_ButtonModule.default.textContainer, classNames?.text),
children: [isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _ButtonModule.default.loader,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CircularLoader.CircularLoader, {
size: size,
colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type],
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'loader'
})
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, {
className: (0, _classify.classify)({
[]: isLoading
}),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'text'
}),
children: children
})]
}) :
// has icon, but no child
children == null ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _ButtonModule.default.loader,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CircularLoader.CircularLoader, {
size: size,
colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type],
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'loader'
})
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
name: iconLeftName || iconRightName,
color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type],
size: size === 'medium' ? 'medium' : 'small',
type: iconLeftName ? iconLeftType : iconRightType,
className: (0, _classify.classify)({
[]: isLoading
}, classNames?.icon),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'icon'
})
})]
}) :
// has icon _and_ child
(iconLeftName || iconRightName) && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [iconLeftName && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
name: iconLeftName,
color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type],
size: size === 'medium' ? 'medium' : 'small',
type: iconLeftType,
className: (0, _classify.classify)({
[]: isLoading
}, classNames?.icon),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'icon-left'
})
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: (0, _classify.classify)(_ButtonModule.default.textContainer, classNames?.text),
children: [isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _ButtonModule.default.loader,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CircularLoader.CircularLoader, {
size: size,
colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type],
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'loader'
})
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, {
className: (0, _classify.classify)({
[]: isLoading
}),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'text'
}),
children: children
})]
}), iconRightName && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
name: iconRightName,
color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type],
size: size === 'medium' ? 'medium' : 'small',
type: iconRightType,
className: (0, _classify.classify)({
[]: isLoading
}, classNames?.icon),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'icon-right'
})
})]
})
})
});
});
Button.displayName = 'Button';