PromucFlow_constructor/app/client/src/components/designSystems/blueprint/ModalComponent.tsx

112 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-03-27 10:56:26 +00:00
import React, { ReactNode, RefObject, useRef, useEffect } from "react";
import { Overlay, Classes } from "@blueprintjs/core";
import styled from "styled-components";
import { getCanvasClassName } from "utils/generators";
2021-01-19 06:17:15 +00:00
const Container = styled.div<{
width: number;
height: number;
2020-05-28 18:10:26 +00:00
top?: number;
left?: number;
zIndex?: number;
}>`
&&& {
.${Classes.OVERLAY} {
.${Classes.OVERLAY_BACKDROP} {
2020-12-24 04:32:25 +00:00
z-index: ${(props) => props.zIndex || 2 - 1};
}
position: fixed;
top: 0;
right: 0;
bottom: 0;
height: 100vh;
2020-12-24 04:32:25 +00:00
z-index: ${(props) => props.zIndex};
width: 100%;
display: flex;
justify-content: center;
align-items: center;
& .${Classes.OVERLAY_CONTENT} {
2020-12-24 04:32:25 +00:00
margin-top: -${(props) => props.theme.headerHeight};
width: ${(props) => props.width}px;
min-height: ${(props) => props.height}px;
background: white;
2020-12-24 04:32:25 +00:00
border-radius: ${(props) => props.theme.radii[1]}px;
top: ${(props) => props.top}px;
left: ${(props) => props.left}px;
}
}
}
`;
2020-03-27 10:56:26 +00:00
const Content = styled.div<{
height: number;
scroll: boolean;
ref: RefObject<HTMLDivElement>;
}>`
2020-12-24 04:32:25 +00:00
overflow-y: ${(props) => (props.scroll ? "visible" : "hidden")};
overflow-x: hidden;
width: 100%;
2020-12-24 04:32:25 +00:00
height: ${(props) => props.height}px;
`;
export type ModalComponentProps = {
isOpen: boolean;
onClose: (e: any) => void;
children: ReactNode;
width: number;
className?: string;
canOutsideClickClose: boolean;
canEscapeKeyClose: boolean;
scrollContents: boolean;
height: number;
2020-05-28 18:10:26 +00:00
top?: number;
left?: number;
hasBackDrop?: boolean;
zIndex?: number;
};
/* eslint-disable react/display-name */
export const ModalComponent = (props: ModalComponentProps) => {
2020-03-27 10:56:26 +00:00
const modalContentRef: RefObject<HTMLDivElement> = useRef<HTMLDivElement>(
null,
);
useEffect(() => {
if (!props.scrollContents) {
modalContentRef.current?.scrollTo({ top: 0, behavior: "smooth" });
}
}, [props.scrollContents]);
return (
2020-05-28 18:10:26 +00:00
<Container
width={props.width}
height={props.height}
top={props.top}
left={props.left}
zIndex={props.zIndex !== undefined ? props.zIndex : 2}
2020-05-28 18:10:26 +00:00
>
<Overlay
isOpen={props.isOpen}
onClose={props.onClose}
canOutsideClickClose={props.canOutsideClickClose}
canEscapeKeyClose={props.canEscapeKeyClose}
usePortal={false}
enforceFocus={false}
2020-05-28 18:10:26 +00:00
hasBackdrop={
props.hasBackDrop !== undefined ? !!props.hasBackDrop : true
}
>
<div>
<Content
2020-03-27 10:56:26 +00:00
scroll={props.scrollContents}
className={`${getCanvasClassName()} ${props.className}`}
height={props.height}
2020-03-27 10:56:26 +00:00
ref={modalContentRef}
>
{props.children}
</Content>
</div>
</Overlay>
</Container>
);
};
export default ModalComponent;