UNPKG

@spaced-out/ui-design-system

Version:
184 lines (183 loc) 8.08 kB
"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 _CircularLoader = require("../CircularLoader"); var _Icon = require("../Icon"); var _Truncate = require("../Truncate"); var _ButtonModule = _interopRequireDefault(require("./Button.module.css")); 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); } 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); } /** * 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' }); 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' }; const ButtonTypeToLoaderColorMap = { primary: 'colorTextInversePrimary', secondary: 'colorTextClickable', tertiary: 'colorTextPrimary', ghost: 'colorTextPrimary', danger: 'colorTextInversePrimary', gradient: 'colorTextInversePrimary' }; const UnstyledButton = exports.UnstyledButton = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { disabled, onClick, className, ariaLabel, actionType, tabIndex = 0, isLoading, role = 'button', ...props } = _ref; return /*#__PURE__*/React.createElement("button", _extends({}, props, ariaLabel ? { 'aria-label': ariaLabel } : {}, { className: className, ref: ref, role: role, disabled: disabled, tabIndex: disabled ? -1 : tabIndex, type: actionType, 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, ...props } = _ref2; return /*#__PURE__*/React.createElement(UnstyledButton, _extends({}, props, { actionType: actionType, disabled: disabled, isLoading: isLoading, className: (0, _classify.classify)(_ButtonModule.default.button, { [_ButtonModule.default.primary]: type === 'primary', [_ButtonModule.default.secondary]: type === 'secondary', [_ButtonModule.default.tertiary]: type === 'tertiary', [_ButtonModule.default.ghost]: type === 'ghost', [_ButtonModule.default.danger]: type === 'danger', [_ButtonModule.default.gradient]: type === 'gradient', [_ButtonModule.default.disabled]: disabled, [_ButtonModule.default.small]: size === 'small', [_ButtonModule.default.medium]: size === 'medium', [_ButtonModule.default.isFluid]: isFluid === true, [_ButtonModule.default.withIconLeft]: !!iconLeftName, [_ButtonModule.default.withIconRight]: !!iconRightName, [_ButtonModule.default.withBothIcon]: !!(iconLeftName && iconRightName), [_ButtonModule.default.onlyIcon]: (iconLeftName || iconRightName) && !children }, classNames?.wrapper), ref: ref }), /*#__PURE__*/React.createElement("div", { className: _ButtonModule.default.buttonRow }, !(iconLeftName || iconRightName) ? /*#__PURE__*/React.createElement("div", { className: (0, _classify.classify)(_ButtonModule.default.textContainer, classNames?.text) }, isLoading && /*#__PURE__*/React.createElement("div", { className: _ButtonModule.default.loader }, /*#__PURE__*/React.createElement(_CircularLoader.CircularLoader, { size: size, colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type] })), /*#__PURE__*/React.createElement(_Truncate.Truncate, { className: (0, _classify.classify)({ [_ButtonModule.default.hidden]: isLoading }) }, children)) : // has icon, but no child children == null ? /*#__PURE__*/React.createElement(React.Fragment, null, isLoading && /*#__PURE__*/React.createElement("div", { className: _ButtonModule.default.loader }, /*#__PURE__*/React.createElement(_CircularLoader.CircularLoader, { size: size, colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type] })), /*#__PURE__*/React.createElement(_Icon.Icon, { name: iconLeftName || iconRightName, color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type], size: size === 'medium' ? 'medium' : 'small', type: iconLeftName ? iconLeftType : iconRightType, className: (0, _classify.classify)({ [_ButtonModule.default.hidden]: isLoading }, classNames?.icon) })) : // has icon _and_ child (iconLeftName || iconRightName) && /*#__PURE__*/React.createElement(React.Fragment, null, iconLeftName && /*#__PURE__*/React.createElement(_Icon.Icon, { name: iconLeftName, color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type], size: size === 'medium' ? 'medium' : 'small', type: iconLeftType, className: (0, _classify.classify)({ [_ButtonModule.default.hidden]: isLoading }, classNames?.icon) }), /*#__PURE__*/React.createElement("div", { className: (0, _classify.classify)(_ButtonModule.default.textContainer, classNames?.text) }, isLoading && /*#__PURE__*/React.createElement("div", { className: _ButtonModule.default.loader }, /*#__PURE__*/React.createElement(_CircularLoader.CircularLoader, { size: size, colorToken: disabled ? 'colorTextDisabled' : ButtonTypeToLoaderColorMap[type] })), /*#__PURE__*/React.createElement(_Truncate.Truncate, { className: (0, _classify.classify)({ [_ButtonModule.default.hidden]: isLoading }) }, children)), iconRightName && /*#__PURE__*/React.createElement(_Icon.Icon, { name: iconRightName, color: disabled ? 'disabled' : ButtonTypeToIconColorMap[type], size: size === 'medium' ? 'medium' : 'small', type: iconRightType, className: (0, _classify.classify)({ [_ButtonModule.default.hidden]: isLoading }, classNames?.icon) })))); }); Button.name = Button.displayName = 'Button';