Skip to content

Commit b9fb557

Browse files
authored
[LG-5601] fix(modal): restore conditional rendering of children when closed (#3189)
* fix(modal): conditionally render children when open toggles * chore(modal): changeset * fix(modal): tests
1 parent b088208 commit b9fb557

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

.changeset/two-dogs-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/modal': patch
3+
---
4+
5+
[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.

packages/confirmation-modal/src/ConfirmationModal/ConfirmationModal.spec.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('packages/confirmation-modal', () => {
7474
});
7575

7676
test('does not render if closed', () => {
77-
const { getByText } = renderModal();
78-
expect(getByText('Content text')).not.toBeVisible();
77+
const { queryByText } = renderModal();
78+
expect(queryByText('Content text')).toBeNull();
7979
});
8080

8181
test('renders if open', () => {
@@ -329,14 +329,18 @@ describe('packages/confirmation-modal', () => {
329329
</ConfirmationModal>,
330330
);
331331

332+
const rerenderedConfirmationButton = await findByTestId(
333+
lgIds.confirm,
334+
);
335+
332336
if (requiredInputText) {
333337
textInput = getByLabelText(
334338
`Type "${requiredInputText}" to confirm your action`,
335339
);
336340
expect(textInput).toHaveValue('');
337341
}
338342

339-
expect(confirmationButton).toHaveAttribute(
343+
expect(rerenderedConfirmationButton).toHaveAttribute(
340344
'aria-disabled',
341345
disabledAfterReopeningModal.toString(),
342346
);

packages/marketing-modal/src/MarketingModal/MarketingModal.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ describe('packages/marketing-modal', () => {
7777
});
7878
});
7979
test('does not render if closed', () => {
80-
const { getByText } = renderModal();
81-
expect(getByText('Content text')).not.toBeVisible();
80+
const { queryByText } = renderModal();
81+
expect(queryByText('Content text')).toBeNull();
8282
});
8383

8484
test('renders if open', () => {

packages/modal/src/Modal/Modal.spec.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ describe('packages/modal', () => {
253253
});
254254

255255
describe('when "open" prop is false', () => {
256-
test('renders to DOM but dialog is not open', () => {
257-
const { queryModal } = renderModal({ open: false });
256+
test('renders dialog element but children are not rendered', () => {
257+
const { queryModal, queryByText } = renderModal({ open: false });
258258
const modal = queryModal();
259+
expect(modal).toBeInTheDocument();
259260
expect(modal).not.toHaveAttribute('open');
260261
expect(modal).not.toBeVisible();
262+
expect(queryByText(modalContent)).not.toBeInTheDocument();
261263
});
262264
});
263265

packages/modal/src/Modal/ModalView.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,23 @@ const ModalView = React.forwardRef<HTMLDialogElement, ModalProps>(
108108
// @ts-ignore React17 - `onCancel` is unavailable in React 17 types
109109
onCancel={e => e.preventDefault()}
110110
>
111-
{children}
112-
<CloseButton
113-
data-lgid={lgIds.close}
114-
data-testid={lgIds.close}
115-
closeIconColor={closeIconColor}
116-
onClick={handleClose}
117-
/>
118-
{/* Backdrop portal container for floating elements that need to escape dialog overflow */}
119-
{open && dialogEl && (
120-
<div className={portalContainerStyles} ref={setPortalContainerEl} />
111+
{open && (
112+
<>
113+
{children}
114+
<CloseButton
115+
data-lgid={lgIds.close}
116+
data-testid={lgIds.close}
117+
closeIconColor={closeIconColor}
118+
onClick={handleClose}
119+
/>
120+
{/* Backdrop portal container for floating elements that need to escape dialog overflow */}
121+
{dialogEl && (
122+
<div
123+
className={portalContainerStyles}
124+
ref={setPortalContainerEl}
125+
/>
126+
)}
127+
</>
121128
)}
122129
</dialog>
123130
</LeafyGreenProvider>

0 commit comments

Comments
 (0)