Skip to content

Commit

Permalink
fix: modal problem
Browse files Browse the repository at this point in the history
- fixed an issue where the content of the Confirmation modal disappeared when the browser was resized.
- removed unexpected scrollbar.
- resize parent modal' height when showing child modal.
  • Loading branch information
honeymaro committed Sep 7, 2023
1 parent a4091ea commit 115153e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
39 changes: 22 additions & 17 deletions src/components/Modal/ConfirmationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from "@emotion/styled";
import Button from "components/Button";
import Modal from "components/Modal";
import { MOBILE_SCREEN_CLASS, MODAL_CLOSE_TIMEOUT_MS } from "constants/layout";
import { useEffect, useRef } from "react";
import { useEffect, useRef, useState } from "react";
import { useScreenClass } from "react-grid-system";

const Content = styled.div`
Expand All @@ -13,10 +13,7 @@ const Content = styled.div`
& .cm-hidden {
display: none;
}
& button,
& input {
pointer-events: none;
}
pointer-events: none;
`;

interface ConfirmationModalProps extends Omit<ReactModal.Props, "children"> {
Expand All @@ -33,19 +30,23 @@ function ConfirmationModal({
}: ConfirmationModalProps) {
const divRef = useRef<HTMLDivElement>(null);
const screenClass = useScreenClass();

const [contentHTML, setContentHTML] = useState("");

useEffect(() => {
if (divRef.current) {
divRef.current.innerHTML = "";
if (node) {
divRef.current.appendChild(node);
const inputs = divRef.current.getElementsByTagName("input");
Array.from(inputs).forEach((elInput) => {
// eslint-disable-next-line no-param-reassign
elInput.size = elInput.value.length;
});
}
if (node) {
const elDiv = document.createElement("div");
elDiv.appendChild(node);
const inputs = elDiv.getElementsByTagName("input");
Array.from(inputs).forEach((elInput) => {
// eslint-disable-next-line no-param-reassign
elInput.size = elInput.value.length;
elInput.setAttribute("value", elInput.value);
});
setContentHTML(elDiv.innerHTML);
}
}, [node, modalProps.isOpen]);
}, [node]);

return (
<Modal
{...modalProps}
Expand All @@ -72,7 +73,11 @@ function ConfirmationModal({
hasCloseButton={screenClass === MOBILE_SCREEN_CLASS}
onGoBack={modalProps.onRequestClose}
>
<Content ref={divRef} className="cm" />
<Content
ref={divRef}
className="cm"
dangerouslySetInnerHTML={{ __html: contentHTML }}
/>
<Button
block
size="large"
Expand Down
38 changes: 37 additions & 1 deletion src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function Modal({
gradient,
style,
headerExtra,
contentRef: _contentRef,
className: _className,
overlayClassName: _overlayClassName,
...modalProps
Expand All @@ -96,6 +97,35 @@ function Modal({
() => !!modalProps.parentSelector,
[modalProps.parentSelector],
);
const elParent = useMemo(() => {
return modalProps.parentSelector?.();
}, [modalProps]);

const [elContent, setElContent] = useState<HTMLDivElement>();

useEffect(() => {
const originalMinHeight = elParent?.style.minHeight;
const handleResize = () => {
if (elParent && elContent) {
elParent.style.setProperty("min-height", `${elContent.scrollHeight}px`);
}
};
handleResize();
let resizeObserver: ResizeObserver;
if (elContent) {
resizeObserver = new ResizeObserver(handleResize);
resizeObserver.observe(elContent);
}

return () => {
if (elParent) {
elParent.style.setProperty("min-height", originalMinHeight || "");
}
if (resizeObserver) {
resizeObserver.disconnect();
}
};
}, [elParent, elContent]);

const overlayClassName = useMemo(() => {
const additionalClasses = [];
Expand Down Expand Up @@ -140,7 +170,7 @@ function Modal({
if (resizeTrigger) {
setResizeTrigger(false);
}
}, 1);
}, 100);
return () => {
clearTimeout(timerId);
};
Expand Down Expand Up @@ -205,6 +235,12 @@ function Modal({
...style?.content,
},
}}
contentRef={(instance) => {
if (_contentRef) {
_contentRef(instance);
}
setElContent(instance);
}}
{...modalProps}
>
<Container
Expand Down
2 changes: 1 addition & 1 deletion src/styles/GlobalStyles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function GlobalStyles() {
#root {
& > div {
overflow-x: hidden;
overflow: hidden;
}
}
Expand Down

0 comments on commit 115153e

Please sign in to comment.