Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Modal): unmount when hidden #2165

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export interface ModalProps {
* z-index attribute of the container
*/
zIndex?: number;
/**
* If true, the modal will unmount when it's not shown
*/
unmountOnClose?: boolean;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a ticket to remove it next major

}

const Modal: FC<ModalProps> & { width?: typeof ModalWidth } = ({
Expand All @@ -95,6 +99,7 @@ const Modal: FC<ModalProps> & { width?: typeof ModalWidth } = ({
closeButtonAriaLabel = "Close",
contentSpacing = false,
zIndex = 10000,
unmountOnClose,
"data-testid": dataTestId
}) => {
const childrenArray: ReactElement[] = useMemo(
Expand All @@ -118,7 +123,7 @@ const Modal: FC<ModalProps> & { width?: typeof ModalWidth } = ({
useBodyScrollLock({ instance });

// show/hide and animate the modal
useShowHideModal({
const { shouldShow } = useShowHideModal({
instance,
show,
triggerElement,
Expand Down Expand Up @@ -187,6 +192,9 @@ const Modal: FC<ModalProps> & { width?: typeof ModalWidth } = ({
document.body
);

if (unmountOnClose && !shouldShow) {
return null;
}
return ReactDOM.createPortal(dialog, document.body);
};

Expand Down
23 changes: 15 additions & 8 deletions packages/core/src/components/Modal/useShowHideModal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect } from "react";
import React, { useState, useCallback, useEffect } from "react";
import useAnimationProps from "./useAnimationProps";
import useKeyEvent from "../../hooks/useKeyEvent/index";
import { A11yDialogType } from "./ModalHelper";
Expand All @@ -19,6 +19,7 @@ export default function useShowHideModal({
onClose: () => void;
alertDialog: boolean;
}) {
const [isAnimating, setIsAnimating] = useState(false);
const getAnimationProps = useAnimationProps(triggerElement, instance);

const closeOnEsc = useCallback(
Expand All @@ -37,22 +38,28 @@ export default function useShowHideModal({
keys: KEYS
});

// show/hide and animate the modal
useEffect(() => {
if (!instance) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you removed this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is unnecessary here, it's already checked inside the conditions. If it exists here, when the modal is not being rendered, it will not render at all

return;
}
const animate = instance?.$el.childNodes[1].animate;
if (show) {
setIsAnimating(true);
instance?.show();
if (animate) instance?.$el.childNodes[1].animate?.(...getAnimationProps());
const animate = instance?.$el.childNodes[1].animate;
if (animate) {
instance?.$el.childNodes[1].animate?.(...getAnimationProps());
}
} else {
const animate = instance?.$el.childNodes[1].animate;
if (animate) {
const animation = instance?.$el.childNodes[1]?.animate(...getAnimationProps(true));
animation?.finished.then(() => instance?.hide());
animation?.finished.then(() => {
setIsAnimating(false);
instance?.hide();
});
} else {
setIsAnimating(false);
instance?.hide();
}
}
}, [show, instance, getAnimationProps]);

return { shouldShow: isAnimating };
}