UNPKG

@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.

168 lines (167 loc) 10.2 kB
"use strict"; "use client"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; exports.__esModule = true; exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _clsx = _interopRequireDefault(require("clsx")); var _Loading = _interopRequireDefault(require("../../Loading")); var _createRel = _interopRequireDefault(require("./common/createRel")); var _handleKeyDown = _interopRequireDefault(require("../../utils/handleKeyDown")); var _tailwind = require("../../common/tailwind"); const ButtonPrimitiveComponent = /*#__PURE__*/React.forwardRef(function Component({ asComponent = "button", dataTest, submit, disabled, ariaControls, ariaExpanded, ariaLabelledby, ariaCurrent, title, rel, href, external, onClick, children, ...props }, ref) { const isButtonWithHref = asComponent === "button" && href; const Element = isButtonWithHref ? "a" : asComponent; const buttonType = submit ? "submit" : "button"; const handleKeyDown = ev => asComponent === "div" ? (0, _handleKeyDown.default)(onClick)(ev) : undefined; return /*#__PURE__*/React.createElement(Element, (0, _extends2.default)({ ref: ref, "data-test": dataTest, "aria-controls": ariaControls, "aria-current": ariaCurrent, "aria-expanded": ariaExpanded, "aria-label": title, "aria-labelledby": ariaLabelledby, type: !isButtonWithHref ? buttonType : undefined, disabled: disabled, onKeyDown: handleKeyDown, href: !disabled ? href : null, target: !disabled && href && external ? "_blank" : undefined, rel: (0, _createRel.default)({ external, href, rel }) // Needs to be here for non <button> elements , onClick: disabled ? undefined : onClick }, props), children); }); ButtonPrimitiveComponent.displayName = "ButtonPrimitiveComponent"; const ButtonPrimitive = /*#__PURE__*/React.forwardRef(({ // Elements children, iconLeft, iconRight, // Container vars height, padding, contentAlign = "center", fontWeight, fontSize, foreground, foregroundHover, foregroundActive, foregroundFocus, background, backgroundHover, backgroundActive, backgroundFocus, boxShadow, boxShadowHover, boxShadowFocus, boxShadowActive, // Content vars contentWidth, // Icon vars icons, // Modificators fullWidth, centered, underlined, circled, spaceAfter, // Status loading, disabled, // Rest className, ...props }, ref) => { const isLink = props.href != null || props.asComponent === "a"; const isDisabled = loading === true || disabled === true; const varsButton = { "--button-height": height, "--button-padding": padding, "--button-content-align": contentAlign, "--button-font-weight": fontWeight, "--button-font-size": fontSize, "--button-foreground": foreground, "--button-foreground-hover": foregroundHover, "--button-foreground-active": foregroundActive, "--button-foreground-focus": foregroundFocus, "--button-background": background, "--button-background-hover": backgroundHover, "--button-background-active": backgroundActive, "--button-background-focus": backgroundFocus, "--button-box-shadow": boxShadow, "--button-box-shadow-hover": boxShadowHover, "--button-box-shadow-focus": boxShadowFocus, "--button-box-shadow-active": boxShadowActive, // loader "--button-icon-width": icons?.width, "--button-icon-height": icons?.height }; const varsContent = { "--button-content-width": contentWidth }; const varsIcons = { "--button-icon-width": icons?.width, "--button-icon-height": icons?.height, "--button-icon-left-margin": icons?.leftMargin, "--button-icon-right-margin": icons?.rightMargin, "--button-icon-foreground": icons?.foreground, "--button-icon-foreground-focus": icons?.foregroundFocus, "--button-icon-foreground-hover": icons?.foregroundHover, "--button-icon-foreground-active": icons?.foregroundActive }; return /*#__PURE__*/React.createElement(ButtonPrimitiveComponent, (0, _extends2.default)({ ref: ref }, props, { disabled: isDisabled, className: (0, _clsx.default)(className, "orbit-button-primitive font-base duration-fast group relative max-w-full items-center border-none text-center leading-none transition-all [&>*]:align-middle [&_.orbit-loading-spinner]:stroke-[currentColor]", fullWidth && "w-full", centered || children == null ? "justify-center" : "justify-[var(--button-content-align)]", circled !== true && "rounded-large tb:rounded-normal", isDisabled ? "cursor-not-allowed opacity-30" : "cursor-pointer hover:no-underline focus:no-underline active:no-underline", isLink ? "inline-flex" : "flex", underlined && "underline", boxShadowFocus != null && "outline-none", spaceAfter != null && (0, _tailwind.getSpaceAfterClasses)(spaceAfter), // loader varsButton["--button-icon-height"] != null && "[&_.orbit-loading-spinner]:h-[var(--button-icon-height)]", varsButton["--button-icon-width"] != null && "[&_.orbit-loading-spinner]:w-[var(--button-icon-width)]", // button vars varsButton["--button-height"] != null && "h-[var(--button-height)]", varsButton["--button-height"] != null && circled === true && "rounded-[var(--button-height)]", varsButton["--button-height"] != null && children == null && "w-[var(--button-height)]", varsButton["--button-padding"] != null && "p-[var(--button-padding)]", varsButton["--button-font-weight"] == null ? "font-medium" : "font-[var(--button-font-weight)]", varsButton["--button-font-size"] != null && "text-[length:var(--button-font-size)]", varsButton["--button-foreground"] != null && "text-[color:var(--button-foreground)]", varsButton["--button-foreground-hover"] != null && !isDisabled && "hover:text-[color:var(--button-foreground-hover)]", varsButton["--button-foreground-active"] != null && !isDisabled && "active:text-[color:var(--button-foreground-active)] active:focus:text-[color:var(--button-foreground-active)]", varsButton["--button-foreground-focus"] != null && !isDisabled && "focus:text-[color:var(--button-foreground-focus)]", varsButton["--button-background"] != null && "bg-[var(--button-background)]", varsButton["--button-background-hover"] != null && !isDisabled && "hover:bg-[var(--button-background-hover)] hover:focus:bg-[var(--button-background-hover)]", varsButton["--button-background-active"] != null && !isDisabled && "active:bg-[var(--button-background-active)] active:focus:bg-[var(--button-background-active)]", varsButton["--button-background-focus"] != null && !isDisabled && "focus:bg-[var(--button-background-focus)]", varsButton["--button-box-shadow"] != null && "[box-shadow:var(--button-box-shadow)]", varsButton["--button-box-shadow-hover"] != null && !isDisabled && "hover:[box-shadow:var(--button-box-shadow-hover)]", varsButton["--button-box-shadow-focus"] != null && !isDisabled && "focus:[box-shadow:var(--button-box-shadow-focus)]", varsButton["--button-box-shadow-active"] != null && !isDisabled && "active:[box-shadow:var(--button-box-shadow-active)] active:focus:[box-shadow:var(--button-box-shadow-active)]"), style: varsButton }), loading && /*#__PURE__*/React.createElement(_Loading.default, { type: "buttonLoader" }), iconLeft != null && /*#__PURE__*/React.createElement("div", { className: (0, _clsx.default)("orbit-button-primitive-icon flex items-center justify-center", loading === true && "invisible", // icon vars varsIcons["--button-icon-height"] != null && "[&>svg]:h-[var(--button-icon-height)]", varsIcons["--button-icon-width"] != null && "[&>svg]:w-[var(--button-icon-width)]", varsIcons["--button-icon-left-margin"] != null && children != null && "m-[var(--button-icon-left-margin)]", varsIcons["--button-icon-foreground"] != null && "text-[color:var(--button-icon-foreground)]", varsIcons["--button-icon-foreground-hover"] != null && !isDisabled && "group-hover:text-[color:var(--button-icon-foreground-hover)]", varsIcons["--button-icon-foreground-focus"] != null && !isDisabled && "group-focus:text-[color:var(--button-icon-foreground-focus)]", varsIcons["--button-icon-foreground-active"] != null && !isDisabled && "group-active:text-[color:var(--button-icon-foreground-active)] group-active:group-focus:text-[color:var(--button-icon-foreground-active)]"), style: varsIcons }, iconLeft), children != null && /*#__PURE__*/React.createElement("div", { className: (0, _clsx.default)("orbit-button-primitive-content inline-block", (iconLeft != null || iconRight != null) && "text-start", loading === true && "invisible", centered !== true && "flex-1", // content vars varsContent["--button-content-width"] != null && "w-[var(--button-content-width)]"), style: varsContent }, children), iconRight != null && /*#__PURE__*/React.createElement("div", { className: (0, _clsx.default)("orbit-button-primitive-icon flex items-center justify-center", loading === true && "invisible", // icon vars varsIcons["--button-icon-height"] != null && "[&>svg]:h-[var(--button-icon-height)]", varsIcons["--button-icon-width"] != null && "[&>svg]:w-[var(--button-icon-width)]", varsIcons["--button-icon-right-margin"] != null && "m-[var(--button-icon-right-margin)]", varsIcons["--button-icon-foreground"] != null && "text-[color:var(--button-icon-foreground)]", varsIcons["--button-icon-foreground-hover"] != null && !isDisabled && "group-hover:text-[color:var(--button-icon-foreground-hover)]", varsIcons["--button-icon-foreground-focus"] != null && !isDisabled && "group-focus:text-[color:var(--button-icon-foreground-focus)]", varsIcons["--button-icon-foreground-active"] != null && !isDisabled && "group-active:text-[color:var(--button-icon-foreground-active)] group-active:group-focus:text-[color:var(--button-icon-foreground-active)]"), style: varsIcons }, iconRight)); }); ButtonPrimitive.displayName = "ButtonPrimitive"; var _default = ButtonPrimitive; exports.default = _default;