Why a page freezes after a modal closes
A modal blocks the page behind it while open, and closing has to undo every step. When a close path skips one, such as an unmount during the exit animation or a route change, the block stays. Nothing throws, so this is one of the bugs that raise no error. What stayed behind sets the symptom:
- A backdrop, sometimes fully transparent after its fade-out, catches every click.
pointer-events: noneonbody: no clicks or hover anywhere on the page.overflow: hiddenonbodyorhtml, orposition: fixedon either: no scrolling.inerton the app's root: no clicks or focus, and the page looks normal.aria-hidden="true"on the root: fine for the mouse, an empty page for screen readers.- A focus trap still on: clicks outside the old modal are blocked.
- Two overlapping modals: the last one to close restored the other one's lock.
Find what is blocking the page
Reproduce the freeze, paste this in the DevTools Console, then click once where nothing happened:
addEventListener("click", (e) => {
console.log("on top:", document.elementFromPoint(e.clientX, e.clientY));
for (const el of [document.documentElement, document.body]) {
const s = getComputedStyle(el);
console.log(el.tagName, s.pointerEvents, s.overflowY, s.position, el.className);
}
console.log("inert:", document.querySelectorAll("[inert]"));
console.log("aria-hidden:", document.querySelectorAll('body > [aria-hidden="true"]'));
console.log("focus:", document.activeElement);
}, { capture: true, once: true });
- If "on top" is an empty
divsuch as a backdrop, delete it in the Elements panel. If clicks work again, it was the cause. - elementFromPoint() skips elements with
pointer-events: none, and the HTML standard has browsers hit-test inert content the same way. Sohtmlorbodywhere a button is drawn points tononeon the BODY line or to the inert list. hiddenorfixedon the HTML or BODY line with no modal open is a stuck scroll lock. The class name may point to the library.- If "on top" is the text field you clicked but "focus" names another element, a listener such as a focus trap stopped focus from moving.
To find the code behind it, reload, right-click body or html in the Elements panel, choose Break on, then Attribute modifications (Chrome DevTools), and reproduce the freeze. DevTools pauses on each attribute change, style and class included, and the call stack shows which code made it.
Bootstrap modal backdrop not removed
A Bootstrap 5 modal appends div.modal-backdrop to body and puts the modal-open class, overflow: hidden and padding-right on body. hide() removes them after the fade-out, just before hidden.bs.modal fires (modal.js), and returns to the caller before that (Bootstrap docs). If the modal's element is removed while open (a re-render, a route change) and nothing calls hide(), they stay. dispose() on an open modal removes the backdrop but leaves the class and styles on body. Hide the modal first, and remove its element after the event:
const el = document.querySelector("#editModal");
const modal = bootstrap.Modal.getOrCreateInstance(el);
el.addEventListener("hidden.bs.modal", () => {
modal.dispose();
el.remove();
}, { once: true });
modal.hide();
For an overlay of your own, give its closed state pointer-events: none and remove it when its closing transition ends.
pointer-events: none stuck on body
A Radix UI Dialog or DropdownMenu is modal by default and blocks the page with an inline pointer-events: none on body. The first such layer to open saves the old value, and the last one to close writes it back (source). Version 1.1.12 of @radix-ui/react-dismissable-layer fixed none being left on body after overlapping modal layers close (changelog).
Both projects have reports of a Dialog opened from a DropdownMenu item (radix-ui/primitives#3317, shadcn-ui/ui#1912), which Radix tracked in #3645 and closed with the 1.1.12 fix. Run npm ls @radix-ui/react-dismissable-layer and update the Radix packages that pull in a copy older than 1.1.12 (@radix-ui/react-dialog has it from 1.1.16). If it still happens, open the dialog from state, outside the menu, and make the menu non-modal:
<>
<DropdownMenu.Root modal={false}>
<DropdownMenu.Trigger>Actions</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Item onSelect={() => setEditOpen(true)}>Edit</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
<Dialog.Root open={editOpen} onOpenChange={setEditOpen}>
{/* Dialog.Portal, Dialog.Overlay and Dialog.Content as usual */}
</Dialog.Root>
</>
The trade-off: a non-modal menu leaves the rest of the page clickable and visible to screen readers.
Scroll lock not removed after the modal closes
Bootstrap, MUI and the scroll lock Radix uses put overflow: hidden on body, which the browser applies to the viewport unless html sets its own overflow (CSS Overflow, section 3.3). What each one sets:
| Library | While a modal is open |
|---|---|
| Bootstrap 5 | div.modal-backdrop; modal-open, overflow: hidden and padding-right on body. Source |
| Radix UI | pointer-events: none and data-scroll-locked on body; aria-hidden on the rest of the page. Source |
| MUI Modal, Dialog, Drawer | overflow: hidden on body, plus padding-right when the page has a scrollbar. Source |
| react-modal | The ReactModal__Body--open class on body; your CSS decides what it locks. Docs |
| Angular CDK and Material dialogs | The cdk-global-scrollblock class (position: fixed), top and left on html. Source |
Release the lock in a cleanup that runs on every way out, unmounts and route changes included.
Two modals open, one closed
A lock that saves the old value on open and writes it back on close breaks when modals overlap. A opens and saves an empty value. B opens on top and saves hidden. A closes, and the page scrolls behind B. B closes and writes hidden back, so the page stays locked with nothing open. Bootstrap supports one modal at a time, so open the next one after hidden.bs.modal. For your own modals, keep one counter for the whole app:
let locks = 0;
let saved = "";
export function lockScroll() {
if (locks++ === 0) {
saved = document.documentElement.style.overflow;
document.documentElement.style.overflow = "hidden";
}
let released = false;
return function release() {
if (released) return; // a second call must not release another modal's lock
released = true;
if (--locks === 0) document.documentElement.style.overflow = saved;
};
}
// React: the cleanup runs when open turns false and when the component unmounts
useEffect(() => {
if (open) return lockScroll();
}, [open]);
MUI's modal manager and the scroll lock Radix uses count the same way.
inert, aria-hidden or a focus trap left behind
inert left on the app's root stops clicks, focus and text selection in the whole subtree, and by default nothing on screen shows it (MDN). aria-hidden="true" on the root leaves the mouse working but removes the content from the accessibility tree (MDN), so screen reader users get an empty page that mouse testing never shows.
A focus trap that was never turned off keeps listening on the document. The focus-trap library prevents clicks outside its container unless allowOutsideClick or clickOutsideDeactivates is set, and its isolateSubtrees option marks the rest of the page inert or aria-hidden. Undo all of it in the same cleanup as the scroll lock.
Use the native dialog element
A <dialog> opened with showModal() blocks the page with nothing for your code to undo. The browser draws it in the top layer over a ::backdrop and makes the rest of the document inert until it closes. Escape closes it (MDN), focus goes back to the element that had it before (HTML standard), and removing an open dialog from the DOM ends the block too.
<button id="edit">Edit title</button>
<dialog id="edit-dialog">
<form method="dialog">
<label>Title <input name="title"></label>
<button>Close</button>
</form>
</dialog>
<script>
document.getElementById("edit").addEventListener("click", () => {
document.getElementById("edit-dialog").showModal();
});
</script>
A form with method="dialog" closes the dialog on submit. The page behind it can still scroll (web.dev). To stop that, tie the lock to the dialog's state with :modal, so it cannot outlive the dialog:
html:has(dialog:modal) { overflow: hidden; }
Where GenClass fits
GenClass's measured visual checks look for these leftovers while you use the page. pointer-events-leak reports pointer-events: none on html or body for a second or more with no dialog or menu open. scroll-lock-stuck reports a scroll lock on html or body with nothing open and a scroll attempt that moved nothing. click-blocked reports a transparent layer over 30% or more of the viewport that takes a second click within 3 seconds, and dialog-covered an open dialog that other elements cover. Each prints a [GenClass] Check (measured): line with the element and a suggested fix.
On GenClass's own bench, with faults it planted into its demo, template and compatibility apps in Chromium, pointer-events-leak found 17 of 18, scroll-lock-stuck 24 of 36, click-blocked 12 of 18 and dialog-covered 18 of 18, with 0 false reports in 128 page views of the same apps unmodified. The two weaker checks missed on pages whose DOM never stops changing, and the false-report rate on other sites is not known. The checks do not report a leftover inert, aria-hidden or focus trap, and by default they run only on development hosts such as localhost.
Set up GenClass with npx genclass, then reproduce the freeze on localhost. For a control that does nothing with no modal involved, see why a button does nothing when clicked.
Questions
Why is pointer-events: none set on the body?
A modal component set it to block clicks outside the dialog and did not restore it on close. In Radix UI, overlapping modal layers could leave it behind until @radix-ui/react-dismissable-layer 1.1.12.
Why can't I scroll after closing a modal?
The modal's scroll lock is still on: overflow: hidden or position: fixed on body or html. Check the computed styles of both in DevTools.
How do I remove a Bootstrap modal backdrop that stays after closing?
Call hide() and remove the modal's element only after the hidden.bs.modal event, because Bootstrap removes the backdrop at the end of hiding.
Does the native dialog element lock page scrolling?
No. The page behind a modal dialog can still scroll; html:has(dialog:modal) { overflow: hidden; } locks it only while one is open.