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.

154 lines (148 loc) 7.05 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["asComponent", "dataTest", "submit", "disabled", "ariaControls", "ariaExpanded", "ariaLabelledby", "ariaCurrent", "title", "className", "rel", "href", "target", "external", "tabIndex", "onClick", "role"]; import * as React from "react"; import styled, { css } from "styled-components"; import defaultTheme from "../../defaultTheme"; import Loading, { StyledSpinner } from "../../Loading"; import getSpacingToken from "../../common/getSpacingToken"; import ButtonPrimitiveContent from "./components/ButtonPrimitiveContent"; import ButtonPrimitiveIconContainer, { StyledButtonPrimitiveIconContainer } from "./components/ButtonPrimitiveIconContainer"; import ButtonPrimitiveContentChildren from "./components/ButtonPrimitiveContentChildren"; import mq from "../../utils/mediaQuery"; import createRel from "./common/createRel"; import onKeyDown from "../../utils/handleKeyDown"; const iconContainerColor = (color, important = true) => css(["", "{color:", " ", ";}"], StyledButtonPrimitiveIconContainer, color, important && "!important"); export const StyledButtonPrimitive = styled( /*#__PURE__*/React.forwardRef((_ref, ref) => { let { asComponent = "button", dataTest, submit, disabled, ariaControls, ariaExpanded, ariaLabelledby, ariaCurrent, title, className, rel, href, target, external, tabIndex, onClick, role } = _ref, props = _objectWithoutProperties(_ref, _excluded); const isButtonWithHref = asComponent === "button" && href; const Component = isButtonWithHref ? "a" : asComponent; const buttonType = submit ? "submit" : "button"; const handleKeyDown = ev => asComponent === "div" ? onKeyDown(onClick)(ev) : undefined; return /*#__PURE__*/React.createElement(Component, { ref: ref, "data-test": dataTest, "aria-controls": ariaControls, "aria-current": ariaCurrent, "aria-expanded": ariaExpanded, "aria-label": title, "aria-labelledby": ariaLabelledby, type: !isButtonWithHref ? buttonType : undefined, className: className, disabled: disabled, onKeyDown: handleKeyDown, href: !disabled ? href : null, target: !disabled && href && external ? "_blank" : undefined, rel: createRel({ external, href, rel }), tabIndex: tabIndex, onClick: !disabled ? onClick : null, role: role }, props.children); })).withConfig({ displayName: "ButtonPrimitive__StyledButtonPrimitive", componentId: "sc-1lbd19y-0" })(["", ";"], ({ foreground, disabled, fullWidth, href, theme, asComponent, circled, padding, background, fontWeight, fontSize, height, width, onlyIcon, icons, foregroundHover, foregroundActive, foregroundFocus, backgroundHover, backgroundActive, backgroundFocus, boxShadow, boxShadowHover, boxShadowFocus, boxShadowActive, underlined }) => css(["height:", ";position:relative;display:", ";justify-content:space-between;align-items:center;box-sizing:border-box;appearance:none !important;text-align:center;text-decoration:", ";flex:", ";max-width:100%;background:", ";color:", "!important;border:0;outline:", ";padding:", ";border-radius:", ";font-family:", ";font-weight:", ";font-size:", ";cursor:", ";transition:all ", " ease-in-out !important;opacity:", ";margin-bottom:", ";width:", ";box-shadow:", ";& > *{vertical-align:middle;}", " ", ";", "{width:", ";height:", ";}&:hover{", ";}:focus{box-shadow:", ";background:", ";color:", "!important;text-decoration:none;", ";}:focus:not(:focus-visible){box-shadow:none;background:", ";color:", "!important;text-decoration:none;", ";}:-moz-focusring,:focus-visible{box-shadow:", ";background:", ";color:", "!important;text-decoration:none;", ";}&:hover:focus{background:", ";}&:active,&:active:focus{", ";}"], height, href || asComponent === "a" ? "inline-flex" : "flex", underlined ? "underline" : "none", fullWidth ? "1 1 auto" : "0 0 auto", background, foreground, boxShadowFocus && "0", padding, circled ? height : "6px", theme.orbit.fontFamily, fontWeight || theme.orbit.fontWeightMedium, fontSize, disabled ? "not-allowed" : "pointer", theme.orbit.durationFast, disabled && theme.orbit.opacityButtonDisabled, getSpacingToken, fullWidth ? "100%" : width || onlyIcon && height || "auto", boxShadow, mq.tablet(css(["border-radius:", ";"], circled ? height : theme.orbit.borderRadiusNormal)), iconContainerColor(icons && icons.foreground, false), StyledSpinner, icons && icons.width, icons && icons.height, !disabled && css(["background:", ";color:", "!important;box-shadow:", ";text-decoration:none;", ";"], backgroundHover, foregroundHover, boxShadowHover, iconContainerColor(icons && icons.foregroundHover)), boxShadowFocus, backgroundFocus, foregroundFocus, iconContainerColor(icons && icons.foregroundFocus), background, foregroundFocus, iconContainerColor(icons && icons.foregroundFocus), boxShadowFocus, backgroundFocus, foregroundFocus, iconContainerColor(icons && icons.foregroundFocus), backgroundHover, !disabled && css(["background:", ";box-shadow:", ";color:", "!important;text-decoration:none;", ";"], backgroundActive, boxShadowActive, foregroundActive, iconContainerColor(icons && icons.foregroundActive)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledButtonPrimitive.defaultProps = { theme: defaultTheme }; const ButtonPrimitive = /*#__PURE__*/React.forwardRef((props, ref) => { const { loading, disabled, children, iconLeft, iconRight, // Need to setup null values so the object exits by default icons = { width: null, height: null, leftMargin: null, rightMargin: null }, contentAlign = "center", contentWidth } = props; const { width, height, leftMargin, rightMargin } = icons; const isDisabled = loading || disabled; const onlyIcon = Boolean(iconLeft && !children); return /*#__PURE__*/React.createElement(StyledButtonPrimitive, _extends({ ref: ref, onlyIcon: onlyIcon }, props, { disabled: isDisabled, className: undefined }), loading && /*#__PURE__*/React.createElement(Loading, { type: "buttonLoader" }), /*#__PURE__*/React.createElement(ButtonPrimitiveContent, { loading: loading, contentAlign: contentAlign }, iconLeft && /*#__PURE__*/React.createElement(ButtonPrimitiveIconContainer, { width: width, height: height, margin: leftMargin }, iconLeft), children && /*#__PURE__*/React.createElement(ButtonPrimitiveContentChildren, { hasIcon: Boolean(iconLeft || iconRight), contentWidth: contentWidth }, children), iconRight && /*#__PURE__*/React.createElement(ButtonPrimitiveIconContainer, { width: width, height: height, margin: rightMargin }, iconRight))); }); ButtonPrimitive.displayName = "ButtonPrimitive"; export default ButtonPrimitive;