@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com's products.
138 lines (137 loc) • 4.74 kB
JavaScript
"use client";
import * as React from "react";
import cx from "clsx";
import InformationCircle from "../icons/InformationCircle";
import AlertCircle from "../icons/AlertCircle";
import AlertOctagon from "../icons/AlertOctagon";
import CheckCircle from "../icons/CheckCircle";
import Close from "../icons/Close";
import ButtonLink from "../ButtonLink";
import { TYPE_OPTIONS, CLOSE_BUTTON_DATA_TEST } from "./consts";
import { spaceAfterClasses } from "../common/tailwind";
import { alertDescendantClasses } from "../TextLink/helpers/twClasses";
const COLORS = {
info: "bg-blue-light border border-alert-info",
success: "bg-green-light border border-alert-success",
warning: "bg-orange-light border border-alert-warning",
critical: "bg-red-light border border-alert-critical"
};
const ICON_COLOR = {
info: "text-blue-normal",
success: "text-green-normal",
warning: "text-orange-normal",
critical: "text-red-normal"
};
const StyledIcon = ({
icon,
type
}) => {
// Icon should be boolean and TRUE
if (typeof icon === "boolean" && icon) {
if (type === TYPE_OPTIONS.INFO) {
return /*#__PURE__*/React.createElement(InformationCircle, {
size: "small",
ariaHidden: true
});
}
if (type === TYPE_OPTIONS.SUCCESS) {
return /*#__PURE__*/React.createElement(CheckCircle, {
size: "small",
ariaHidden: true
});
}
if (type === TYPE_OPTIONS.WARNING) {
return /*#__PURE__*/React.createElement(AlertCircle, {
size: "small",
ariaHidden: true
});
}
if (type === TYPE_OPTIONS.CRITICAL) {
return /*#__PURE__*/React.createElement(AlertOctagon, {
size: "small",
ariaHidden: true
});
}
}
if ( /*#__PURE__*/React.isValidElement(icon) && typeof icon !== "boolean") {
// @ts-expect-error TODO
return /*#__PURE__*/React.cloneElement(icon, {
size: "small"
});
}
return /*#__PURE__*/React.createElement(React.Fragment, null, icon);
};
const ContentWrapper = ({
type,
inlineActions,
children
}) => {
return /*#__PURE__*/React.createElement("div", {
className: cx("min-h-icon-medium flex items-center", !inlineActions && "w-full", "[&_.orbit-list-item]:text-ink-dark [&_.orbit-text]:text-ink-dark [&_.orbit-heading]:text-ink-dark", ...alertDescendantClasses[type])
}, children);
};
const AlertCloseButton = ({
hasChildren,
dataTest,
onClick,
labelClose,
icon
}) => {
return /*#__PURE__*/React.createElement("div", {
className: cx("end-0", hasChildren && "top-0")
}, /*#__PURE__*/React.createElement(ButtonLink, {
dataTest: dataTest,
onClick: onClick,
size: "small",
iconLeft: icon,
type: "secondary",
title: labelClose
}));
};
const Alert = props => {
const {
type = TYPE_OPTIONS.INFO,
title,
icon,
closable,
onClose,
children,
dataTest,
id,
spaceAfter,
suppressed,
inlineActions,
labelClose
} = props;
return /*#__PURE__*/React.createElement("div", {
className: cx("orbit-alert", "rounded-300 text-ink-dark font-base text-normal p-300 relative box-border flex w-full leading-normal", Boolean(inlineActions) && "items-center", suppressed ? "bg-cloud-light" : COLORS[type], spaceAfter && spaceAfterClasses[spaceAfter]),
id: id,
"data-test": dataTest
}, icon && /*#__PURE__*/React.createElement("div", {
className: cx("me-200 m-0 shrink-0 leading-none", Boolean(inlineActions) && "lm:mt-150 flex items-center self-baseline", ICON_COLOR[type], "tb:me-200 tb:[&_svg]:size-icon-medium")
}, /*#__PURE__*/React.createElement(StyledIcon, {
type: type,
icon: icon
})), /*#__PURE__*/React.createElement("div", {
className: cx("flex flex-1", title && inlineActions ? "flex-row" : "flex-col", !title && "items-center", Boolean(inlineActions) && "justify-between")
}, title && /*#__PURE__*/React.createElement("div", {
className: cx("text-ink-dark min-h-icon-medium flex items-center font-bold", !!children && (inlineActions ? "mb-0" : "mb-100"), Boolean(inlineActions) && "grow basis-0")
}, title), children && !inlineActions && /*#__PURE__*/React.createElement(ContentWrapper, {
type: type
}, children), inlineActions && /*#__PURE__*/React.createElement(ContentWrapper, {
type: type,
inlineActions: !!inlineActions
}, inlineActions)), closable && /*#__PURE__*/React.createElement(AlertCloseButton, {
hasChildren: !!children,
dataTest: CLOSE_BUTTON_DATA_TEST,
onClick: onClose,
labelClose: labelClose,
icon: /*#__PURE__*/React.createElement(Close, {
size: "small",
color: type,
ariaHidden: true
})
}));
};
export { default as AlertButton } from "./AlertButton";
export default Alert;