UNPKG

@spaced-out/ui-design-system

Version:
284 lines (278 loc) 9.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToastTitle = exports.ToastFooter = exports.ToastBody = exports.Toast = exports.TOAST_SEMANTIC = void 0; var React = _interopRequireWildcard(require("react")); var _react2 = require("@floating-ui/react"); var _classify = _interopRequireDefault(require("../../utils/classify")); var _qa = require("../../utils/qa"); var _Icon = require("../Icon"); var _Text = require("../Text"); var _ToastModule = _interopRequireDefault(require("./Toast.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); } const TOAST_SEMANTIC = exports.TOAST_SEMANTIC = Object.freeze({ success: 'success', information: 'information', warning: 'warning', danger: 'danger', primary: 'primary' }); // Define the expected props for button elements in ToastFooter // Type for a button element that can be cloned // Define the expected props for toast child elements // Type for toast child elements that can be cloned const ToastIcon = _ref => { let { semantic, testId } = _ref; switch (semantic) { case 'success': return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: "circle-check", size: "medium", color: _Text.TEXT_COLORS.success, type: "solid", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case TOAST_SEMANTIC.information: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: "circle-info", size: "medium", color: _Text.TEXT_COLORS.information, type: "solid", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case TOAST_SEMANTIC.warning: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: "circle-exclamation", size: "medium", color: _Text.TEXT_COLORS.warning, type: "solid", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case TOAST_SEMANTIC.danger: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: "shield-exclamation", size: "medium", color: _Text.TEXT_COLORS.danger, type: "solid", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); default: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { color: _Text.TEXT_COLORS.neutral, name: "face-smile", size: "medium", type: "solid", testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); } }; const ToastTitle = exports.ToastTitle = /*#__PURE__*/React.forwardRef((_ref2, ref) => { let { children, className, testId, ...props } = _ref2; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: (0, _classify.default)(_ToastModule.default.toastTitle, className), ...props, ref: ref, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'title' }), children: children }); }); ToastTitle.displayName = 'ToastTitle'; const ToastBody = exports.ToastBody = /*#__PURE__*/React.forwardRef((_ref3, ref) => { let { children, className, testId, ...props } = _ref3; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: (0, _classify.default)(_ToastModule.default.toastBody, className), ...props, ref: ref, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'body' }), children: children }); }); ToastBody.displayName = 'ToastBody'; const ToastFooter = _ref4 => { let { children, onClose, testId } = _ref4; const arrayChildren = React.Children.toArray(children); const footerActions = React.Children.map(children, (child, index) => { const isLast = index === arrayChildren.length - 1; if (/*#__PURE__*/React.isValidElement(child)) { // Cast to our typed button element const buttonElement = child; const buttonProps = buttonElement.props; const { onClick } = buttonProps; const buttonClickHandler = e => { onClose?.(); onClick?.(e); }; if (buttonElement?.type?.displayName === 'Button' && isLast) { return /*#__PURE__*/React.cloneElement(buttonElement, { size: 'small', type: 'primary', ...buttonProps, onClick: buttonClickHandler, testId: (0, _qa.generateTestId)({ base: testId, slot: 'button', index: index.toString() }) }); } else if (buttonElement?.type?.displayName === 'Button') { return /*#__PURE__*/React.cloneElement(buttonElement, { size: 'small', type: 'tertiary', ...buttonProps, onClick: buttonClickHandler, testId: (0, _qa.generateTestId)({ base: testId, slot: 'button', index: index.toString() }) }); } } return child; }); return React.Children.count(children) > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: _ToastModule.default.toastFooterActions, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'footer' }), children: footerActions }) : null; }; exports.ToastFooter = ToastFooter; ToastFooter.displayName = 'ToastFooter'; const Toast = _ref5 => { let { classNames, children, time, semantic = TOAST_SEMANTIC.success, onClose, initialFocus = -1, customIcon, hideCloseIcon, testId } = _ref5; const { refs, context } = (0, _react2.useFloating)({ open: true }); const getComp = comp => { const childrenArray = React.Children.toArray(children); if (childrenArray.length) { const nodes = []; for (const child of childrenArray) { // Type guard to ensure child is a ReactElement if (/*#__PURE__*/React.isValidElement(child)) { // Cast to our typed toast child element const toastChildElement = child; if (toastChildElement?.type?.displayName === comp) { nodes.push(/*#__PURE__*/React.cloneElement(toastChildElement, { semantic, testId })); } } } return nodes.length > 1 ? nodes : nodes[0]; } return null; }; const footer = getComp('ToastFooter'); const footerWithClose = footer ? /*#__PURE__*/React.cloneElement(footer, { onClose, testId }) : null; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.FloatingFocusManager, { context: context, initialFocus: initialFocus, children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: (0, _classify.default)(_ToastModule.default.toastContainer, classNames?.wrapper), ref: refs.setFloating, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'root' }), children: [customIcon || /*#__PURE__*/(0, _jsxRuntime.jsx)(ToastIcon, { semantic: semantic, testId: testId }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: _ToastModule.default.toastMidSection, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'content' }), children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: _ToastModule.default.contentWrap, children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: _ToastModule.default.titleBodyWrap, children: [getComp('ToastTitle'), getComp('ToastBody')] }), time && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.SubTitleExtraSmall, { className: (0, _classify.default)(_ToastModule.default.toastTime, classNames?.time), testId: (0, _qa.generateTestId)({ base: testId, slot: 'time' }), children: time })] }), footerWithClose] }), !hideCloseIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.CloseIcon, { classNames: { button: _ToastModule.default.closeIcon }, onClick: onClose, ariaLabel: "Close Button", testId: (0, _qa.generateTestId)({ base: testId, slot: 'close' }) })] }) }); }; exports.Toast = Toast;