inert Neutralizes Background UI Under Modals
Set element.inert on the page behind a modal so clicks, focus, and assistive tech skip it, restoring interactivity when the dialog closes.
Code
// inert removes a subtree from tab order, pointer targeting, and the
// accessibility tree — the correct way to neutralize a page behind a modal.
const app = document.querySelector('#app-shell');
const modal = document.querySelector('#invite-dialog');
function openInvite() {
app.inert = true; // clicks, focus, and AT skip the background
modal.inert = false;
modal.querySelector('input')?.focus(); // focus lands inside the dialog
}
function closeInvite() {
app.inert = false;
modal.inert = true;
}
modal?.addEventListener('close', closeInvite);
console.log('app interactive on load:', app.inert === false);How the code works
One way to read it: the functions openInvite and closeInvite, doing all the work.
Watch out for
- The import surface (if any) is all standard library, all versioned with the language itself.
- The snippet leans on
document— browser-only surfaces a bare Node process cannot provide.
Applying it
Entry signature: function openInvite()
Look for the app interactive on load: prefixes as the run proceeds; they tie every line to its step.
Statements that matter
modal?.addEventListener('close', closeInvite);
modal.querySelector('input')?.focus(); // focus lands inside the dialog
}
Interactive Tools for This Topic
WCAG Color Contrast CheckerCalculate WCAG 2.1 color contrast ratios between foreground and background colors. Verify AA and AAA compliance levels for normal and large text accessibility.Tailwind Color Palette GeneratorGenerate a balanced 50-950 Tailwind CSS custom property color scale from any hex, RGB, or HSL base color with WCAG 2.1 contrast ratios for every stop.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does inert Neutralizes Background UI Under Modals run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.