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

132 lines
3.5 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";
import { Layers } from "constants/Layers";
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;
bottom?: number;
right?: number;
2020-05-28 18:10:26 +00:00
zIndex?: number;
}>`
&&& {
.${Classes.OVERLAY} {
.${Classes.OVERLAY_BACKDROP} {
2020-12-24 04:32:25 +00:00
z-index: ${(props) => props.zIndex || 2 - 1};
}
2021-05-19 07:20:00 +00:00
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} {
max-width: 95%;
width: ${(props) => (props.width ? `${props.width}px` : "auto")};
min-height: ${(props) => (props.height ? `${props.height}px` : "auto")};
background: white;
border-radius: ${(props) => props.theme.radii[0]}px;
2020-12-24 04:32:25 +00:00
top: ${(props) => props.top}px;
left: ${(props) => props.left}px;
bottom: ${(props) => props.bottom}px;
right: ${(props) => props.right}px;
}
}
}
`;
2020-03-27 10:56:26 +00:00
const Content = styled.div<{
height?: number;
2020-03-27 10:56:26 +00:00
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%;
height: ${(props) => (props.height ? `${props.height}px` : "auto")};
`;
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;
bottom?: number;
right?: number;
2020-05-28 18:10:26 +00:00
hasBackDrop?: boolean;
zIndex?: number;
};
/* eslint-disable react/display-name */
export function 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 (
2021-05-19 07:20:00 +00:00
<Overlay
canEscapeKeyClose={false}
canOutsideClickClose={false}
enforceFocus={false}
2021-05-24 05:52:49 +00:00
hasBackdrop={false}
2021-05-19 07:20:00 +00:00
isOpen={props.isOpen}
onClose={props.onClose}
portalClassName="bp3-modal-widget"
usePortal
2020-05-28 18:10:26 +00:00
>
2021-05-19 07:20:00 +00:00
<Container
bottom={props.bottom}
2021-05-19 07:20:00 +00:00
height={props.height}
left={props.left}
right={props.bottom}
2021-05-19 07:20:00 +00:00
top={props.top}
width={props.width}
zIndex={props.zIndex !== undefined ? props.zIndex : Layers.modalWidget}
>
2021-05-19 07:20:00 +00:00
<Overlay
canEscapeKeyClose={props.canEscapeKeyClose}
canOutsideClickClose={props.canOutsideClickClose}
enforceFocus={false}
hasBackdrop={
props.hasBackDrop !== undefined ? !!props.hasBackDrop : true
}
isOpen={props.isOpen}
onClose={props.onClose}
usePortal={false}
>
<div>
<Content
className={`${getCanvasClassName()} ${props.className}`}
height={props.height}
ref={modalContentRef}
scroll={props.scrollContents}
>
{props.children}
</Content>
</div>
</Overlay>
</Container>
</Overlay>
);
}
export default ModalComponent;