Modal Layout คือ view ที่มีการแสดงผล ซ้อนทับ view อื่นๆ และมีกล่องข้อความแสดงผลลอยอยู่กลางหน้าจอ
import { useEffect } from "react";
import styled from "styled-components";
import IconPlus from "../element/icon/IconPlus";
export default function Modal({
body,
title,
footer,
onClosed,
width = "390px",
maxHeight = "calc(100vh - 64px)",
children,
className,
style,
backgroundModal = "#fff",
fontColor = "#444",
backdropColor = "rgba(0, 0, 0, 0.5)",
borderColor = "#efefef",
closeIconBackgroundColor = "#efefef",
closeIconColor = "#444",
hideCloseButton,
hideModalBorder = true,
titleAlign = "left",
paddingGap = "20px",
}) {
useEffect(() => {
document.body.style.overflow = "hidden";
return () => {
document.body.removeAttribute("style");
};
}, []);
return (
<Styled
className={className}
style={style}
$width={width}
$backdropColor={backdropColor}
$backgroundModal={backgroundModal}
$fontColor={fontColor}
$borderColor={borderColor}
$hideModalBorder={hideModalBorder}
$closeIconBackgroundColor={closeIconBackgroundColor}
$titleAlign={titleAlign}
$paddingGap={paddingGap}
$maxHeight={maxHeight}
$title={title}
>
{children ? (
children
) : (
<div className={"modal"}>
{!hideCloseButton && (
<div
className={"close-icon"}
onClick={() => {
if (onClosed) {
onClosed();
}
}}
>
<IconPlus width="20" degree="45" color={closeIconColor} />
</div>
)}
{title && <div className={"title-modal"}>{title}</div>}
<div className={"content"}>{body}</div>
{footer && <div className={"footer"}>{footer}</div>}
</div>
)}
</Styled>
);
}
const Styled = styled.div`
position: fixed;
background-color: ${({ $backdropColor }) => $backdropColor};
inset: 0;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
z-index: 999999;
color: ${({ $fontColor }) => $fontColor};
.wrp_float {
position: fixed;
visibility: hidden;
}
.modal {
background-color: ${({ $backgroundModal }) => $backgroundModal};
overflow: hidden;
display: flex;
flex-direction: column;
width: 100%;
max-width: ${({ $width }) => $width};
max-height: ${({ $maxHeight }) => $maxHeight};
border-radius: 10px;
position: relative;
}
.title-modal {
font-size: 20px;
line-height: 32px;
text-align: ${({ $titleAlign }) => $titleAlign};
font-weight: bolder;
padding: ${({ $paddingGap }) => $paddingGap};
border-bottom: ${({ $hideModalBorder }) => (!$hideModalBorder ? "1px" : 0)}
solid ${({ $borderColor }) => $borderColor};
}
.close-icon {
position: absolute;
right: 15px;
top: 15px;
width: 40px;
height: 40px;
background-color: ${({ $closeIconBackgroundColor }) =>
$closeIconBackgroundColor};
display: flex;
justify-content: center;
align-items: center;
border-radius: 100px;
}
.content {
flex: 1;
overflow: auto;
padding-left: ${({ $paddingGap }) => $paddingGap};
padding-right: ${({ $paddingGap }) => $paddingGap};
padding-top: ${({ $title, $paddingGap }) => ($title ? "0" : $paddingGap)};
}
.footer {
padding: ${({ $paddingGap }) => $paddingGap};
border-top: ${({ $hideModalBorder }) => (!$hideModalBorder ? "1px" : 0)}
solid ${({ $borderColor }) => $borderColor};
display: flex;
justify-content: center;
align-items: center;
}
.footer > span {
margin-right: 5px;
}
`;
import Modal from "../../example/Modal";
export default function TestView() {
return (
<div className="test-view">
<Modal
height="200px"
width="300px"
title={"this is modal"}
body={<div>This is Body</div>}
footer={<div>This is Footer</div>}
onClosed={() => {
console.log("modal closed");
}}
/>
</div>
);
}
Property | Description | Type | Default |
---|---|---|---|
body | view ส่วนเนื้อหาตรงกลางของกล่อง modal | ReactElement | |
title | ข้อความหัวเรื่องของ view | string | |
footer | view ส่วนล่างของกล่อง modal | ReactElement | |
onClosed | ฟังก็ชันทำงานเมื่อมีการกดปุ่มปิด | function | |
width | ความกว้างมากที่สุดของ modal view | string | "390px" |
maxHeight | ความสูงมากที่สุดของ modal view | string | "calc(100vh - 64px)" |
children | custom children แทนกล่อง modal เดิม | ReactElement | |
className | ใช้กำหนดลักษณะเฉพาะของ view | string | |
style | inline style ของ view | CSSProperties | |
backdropColor | สีพื้นหลังด้านหลังของ modal view | string | "rgba(0, 0, 0, 0.5)" |
backgroundModal | สีพื้นหลัง modal view | string | '#fff' |
fontColor | สีตัวอักษรของ modal view | string | "#444" |
borderColor | สีเส้นขอบของ view | string | '#efefef' |
closeIconBackgroundColor | สีพื้นหลัง icon ปุ่มปิด | string | "#efefef" |
closeIconColor | สี icon ปุ่มปิด | string | "#444" |
hideCloseButton | กำหนดค่าเป็น true เมื่อต้องการซ่อนปุ่มปิด | boolean | |
hideModalBorder | กำหนดค่าเป็น false เมื่อต้องการแสดงเส้นขอบของ view | boolean | true |
titleAlign | จัดเรียงข้อความ | "left" | |
paddingGap | ขนาด padding ของ title body และ footer | "20px" |