@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.
153 lines (143 loc) • 6.03 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["width", "theme"];
import * as React from "react";
import styled, { css } from "styled-components";
import useFocusTrap from "../hooks/useFocusTrap";
import Portal from "../Portal";
import useTheme from "../hooks/useTheme";
import defaultTheme from "../defaultTheme";
import Heading from "../Heading";
import Text, { StyledText } from "../Text";
import Stack from "../Stack";
import useLockScrolling from "../hooks/useLockScrolling";
import mq from "../utils/mediaQuery";
import { StyledButtonPrimitive } from "../primitives/ButtonPrimitive";
import KEY_CODE_MAP from "../common/keyMaps";
import useRandomId from "../hooks/useRandomId";
import { left } from "../utils/rtl";
const StyledDialog = styled.div.withConfig({
displayName: "Dialog__StyledDialog",
componentId: "sc-1w083wr-0"
})(["", ""], ({
theme
}) => css(["font-family:", ";width:100%;height:100%;position:fixed;top:0;right:0;bottom:0;left:0;padding:", ";z-index:", ";box-sizing:border-box;outline:none;overflow-x:hidden;background-color:rgba(0,0,0,0.5);transition:opacity ", " ease-in-out;", ";"], theme.orbit.fontFamily, theme.orbit.spaceMedium, theme.orbit.zIndexModalOverlay, theme.orbit.durationFast, mq.largeMobile(css(["opacity:1;display:flex;justify-content:center;align-items:center;"])))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledDialog.defaultProps = {
theme: defaultTheme
};
const StyledDialogCenterWrapper = styled.div.withConfig({
displayName: "Dialog__StyledDialogCenterWrapper",
componentId: "sc-1w083wr-1"
})(["display:flex;align-items:center;min-height:100%;"]);
const StyledDialogContent = styled.div.withConfig({
displayName: "Dialog__StyledDialogContent",
componentId: "sc-1w083wr-2"
})(["", ""], ({
theme,
$maxWidth
}) => css(["display:block;width:100%;max-width:", "px;box-sizing:border-box;padding:", ";background:", ";border-radius:12px;bottom:", ";box-shadow:", ";text-align:center;", "{text-align:center;}", ";"], $maxWidth, `${theme.orbit.spaceLarge} ${theme.orbit.spaceMedium} ${theme.orbit.spaceMedium}`, theme.orbit.paletteWhite, ({
shown
}) => shown ? "0" : "-100%", theme.orbit.boxShadowOverlay, StyledText, mq.largeMobile(css(["min-width:", ";border-radius:9px;padding:", ";text-align:", ";", "{text-align:", ";}"], theme.orbit.widthModalSmall, theme.orbit.spaceLarge, left, StyledText, left)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledDialogContent.defaultProps = {
theme: defaultTheme
};
const StyledAction = styled(_ref => {
let {
width,
theme
} = _ref,
props = _objectWithoutProperties(_ref, _excluded);
return /*#__PURE__*/React.createElement("div", props);
}).withConfig({
displayName: "Dialog__StyledAction",
componentId: "sc-1w083wr-3"
})(["width:100%;", "{width:100%;flex:1 1 auto;}", ";"], StyledButtonPrimitive, mq.largeMobile(css(["width:auto;", "{width:auto;flex:0 0 auto;}"], StyledButtonPrimitive))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledAction.defaultProps = {
theme: defaultTheme
};
const IllustrationContainer = styled.div.withConfig({
displayName: "Dialog__IllustrationContainer",
componentId: "sc-1w083wr-4"
})(["margin-bottom:16px;"]);
const Dialog = ({
dataTest,
id,
title,
description,
primaryAction,
secondaryAction,
onClose,
maxWidth,
renderInPortal = true,
illustration,
lockScrolling = true
}) => {
const wrapperRef = React.useRef(null);
useLockScrolling(wrapperRef, lockScrolling);
const ref = React.useRef(null);
const theme = useTheme();
const transitionLength = React.useMemo(() => parseFloat(theme.orbit.durationFast) * 1000, [theme.orbit.durationFast]);
const [shown, setShown] = React.useState(false);
useFocusTrap(ref);
React.useEffect(() => {
const timer = setTimeout(() => {
setShown(true);
if (ref.current) {
ref.current.focus();
}
}, transitionLength);
return () => clearTimeout(timer);
}, [transitionLength]);
const handleClose = ev => {
if (ref.current && onClose) {
if (ref.current && !ref.current.contains(ev.target)) onClose();
}
};
React.useEffect(() => {
const handleKeyDown = ev => {
if (ev.keyCode === KEY_CODE_MAP.ESC && onClose) {
onClose();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [onClose]);
const dialogID = useRandomId();
const dialog = /*#__PURE__*/React.createElement(StyledDialog, {
ref: wrapperRef,
"data-test": dataTest,
id: id,
shown: shown,
onClick: handleClose,
tabIndex: "0",
role: "dialog",
"aria-modal": "true",
"aria-labelledby": dialogID
}, /*#__PURE__*/React.createElement(StyledDialogCenterWrapper, null, /*#__PURE__*/React.createElement(StyledDialogContent, {
shown: shown,
ref: ref,
id: dialogID,
$maxWidth: maxWidth
}, illustration && /*#__PURE__*/React.createElement(IllustrationContainer, null, illustration), /*#__PURE__*/React.createElement(Stack, {
spacing: "XSmall",
spaceAfter: "medium"
}, title && /*#__PURE__*/React.createElement(Heading, {
type: "title3",
align: "center",
largeMobile: {
align: "start"
}
}, title), description && /*#__PURE__*/React.createElement(Text, {
type: "secondary"
}, description)), /*#__PURE__*/React.createElement(Stack, {
direction: "column-reverse",
spacing: "XSmall",
largeMobile: {
direction: "row",
justify: "end"
}
}, secondaryAction && /*#__PURE__*/React.createElement(StyledAction, null, secondaryAction), /*#__PURE__*/React.createElement(StyledAction, null, primaryAction)))));
return renderInPortal ? /*#__PURE__*/React.createElement(Portal, {
renderInto: "modals"
}, dialog) : dialog;
};
export default Dialog;