Fix useConfirm host element leaking into the DOM and tests#7935
Fix useConfirm host element leaking into the DOM and tests#7935Copilot wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 87ab9c3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
|
|
this is a fix for now ideally we wouldn't be creating a new react root for this, but we don't have a good path to avoid that currently |
There was a problem hiding this comment.
Pull request overview
This PR fixes a DOM leak in useConfirm() / confirm() where the detached createRoot() host <div> appended to document.body was never removed, accumulating empty containers and interfering with downstream consumers (notably RTL test cleanup).
Changes:
- Remove the module-level reusable
hostElementand create a fresh host element perconfirm()call. - On dialog close, unmount the React root and remove the host element from
document.body. - Add a regression test and a patch changeset documenting the behavior change.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/ConfirmationDialog/ConfirmationDialog.tsx | Switch confirm() to per-call host creation and explicit host removal on close. |
| packages/react/src/ConfirmationDialog/ConfirmationDialog.test.tsx | Add regression test intended to ensure the confirm() host element is removed on close. |
| .changeset/confirm-dialog-host-cleanup.md | Patch changeset describing the host cleanup behavior change. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
useConfirm()/confirm()renderedConfirmationDialoginto a detachedcreateRootwhose host<div>, appended todocument.body, was never removed on close. The empty host accumulated across calls and leaked into consumers—most visibly poisoning subsequent@testing-library/reacttests whosecleanup()cannot reach a sibling root.This is the surgical, non-breaking subset of the issue's proposals (#3 + dropping the stale module-level host). The full in-tree provider/portal rewrite (#1) and exported teardown handle (#2) remain follow-ups for a major release.
Changelog
New
useConfirmdescribe block asserting the host element is detached from<body>on close.Changed
confirm()now creates a fresh host element per call and removes it fromdocument.bodyafterroot.unmount():Removed
let hostElementthat was reused across calls and lingered on<body>.Rollout strategy
Testing & Reviewing
Verify the new test fails on
main(leaked host persists) and passes here. Behavior is unchanged on the happy path; only the orphaned container is now cleaned up. Worth confirming no consumer relied on scraping/reusing the persistent[role="alertdialog"]host.Merge checklist