Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/two-dogs-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/modal': patch
---

[LG-5601](https://jira.mongodb.org/browse/LG-5601) Fix regression where `Modal` children remained mounted when closed. Restores v19 behavior where children are conditionally rendered based on the `open` prop.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('packages/confirmation-modal', () => {
});

test('does not render if closed', () => {
const { getByText } = renderModal();
expect(getByText('Content text')).not.toBeVisible();
const { queryByText } = renderModal();
expect(queryByText('Content text')).toBeNull();
});

test('renders if open', () => {
Expand Down Expand Up @@ -329,14 +329,18 @@ describe('packages/confirmation-modal', () => {
</ConfirmationModal>,
);

const rerenderedConfirmationButton = await findByTestId(
lgIds.confirm,
);

if (requiredInputText) {
textInput = getByLabelText(
`Type "${requiredInputText}" to confirm your action`,
);
expect(textInput).toHaveValue('');
}

expect(confirmationButton).toHaveAttribute(
expect(rerenderedConfirmationButton).toHaveAttribute(
'aria-disabled',
disabledAfterReopeningModal.toString(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('packages/marketing-modal', () => {
});
});
test('does not render if closed', () => {
const { getByText } = renderModal();
expect(getByText('Content text')).not.toBeVisible();
const { queryByText } = renderModal();
expect(queryByText('Content text')).toBeNull();
});

test('renders if open', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/modal/src/Modal/Modal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ describe('packages/modal', () => {
});

describe('when "open" prop is false', () => {
test('renders to DOM but dialog is not open', () => {
const { queryModal } = renderModal({ open: false });
test('renders dialog element but children are not rendered', () => {
const { queryModal, queryByText } = renderModal({ open: false });
const modal = queryModal();
expect(modal).toBeInTheDocument();
expect(modal).not.toHaveAttribute('open');
expect(modal).not.toBeVisible();
expect(queryByText(modalContent)).not.toBeInTheDocument();
});
});

Expand Down
27 changes: 17 additions & 10 deletions packages/modal/src/Modal/ModalView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,23 @@ const ModalView = React.forwardRef<HTMLDialogElement, ModalProps>(
// @ts-ignore React17 - `onCancel` is unavailable in React 17 types
onCancel={e => e.preventDefault()}
>
{children}
<CloseButton
data-lgid={lgIds.close}
data-testid={lgIds.close}
closeIconColor={closeIconColor}
onClick={handleClose}
/>
{/* Backdrop portal container for floating elements that need to escape dialog overflow */}
{open && dialogEl && (
<div className={portalContainerStyles} ref={setPortalContainerEl} />
{open && (
<>
{children}
<CloseButton
data-lgid={lgIds.close}
data-testid={lgIds.close}
closeIconColor={closeIconColor}
onClick={handleClose}
/>
{/* Backdrop portal container for floating elements that need to escape dialog overflow */}
{dialogEl && (
<div
className={portalContainerStyles}
ref={setPortalContainerEl}
/>
)}
</>
)}
</dialog>
</LeafyGreenProvider>
Expand Down
Loading