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.

JavascriptData Integrity

Code

javascriptsnippet
18 lines
// 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

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.