
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
dialog-closedby-polyfill
Advanced tools
A polyfill for the HTMLDialogElement closedby attribute, providing control over how modal dialogs can be dismissed.
Note: The HTML attribute is
closedby(lowercase), while the JavaScript property isclosedBy(camelCase).
closedby attribute for <dialog> elementsany, closerequest, and none<dialog> supportnpm install dialog-closedby-polyfill
The polyfill is automatically applied when imported:
// ES Modules (auto-applies if needed)
import "dialog-closedby-polyfill";
// CommonJS (auto-applies if needed)
require("dialog-closedby-polyfill");
If you need more control over when the polyfill is applied:
import { apply, isSupported } from "dialog-closedby-polyfill";
if (!isSupported()) {
apply();
}
Or include it via CDN:
<!-- Polyfill applied automatically -->
<script
type="module"
src="https://cdn.jsdelivr.net/npm/dialog-closedby-polyfill/index.js"
></script>
<!-- polyfill manually -->
<script type="module">
import {
isSupported,
apply,
} from "https://cdn.jsdelivr.net/npm/dialog-closedby-polyfill/index.js";
if (!isSupported()) apply();
</script>
https://tak-dcxi.github.io/github-pages-demo/closedby.html
The closedby attribute controls how a modal dialog can be dismissed:
closedby value | ESC key | Backdrop click | close() method |
|---|---|---|---|
"any" | ✅ | ✅ | ✅ |
"closerequest" | ✅ | ❌ | ✅ |
"none" | ❌ | ❌ | ✅ |
closedby="any" (default)The dialog can be closed by:
close() method<dialog
id="dialog-any"
closedby="any"
aria-labelledby="dialog-any-title"
autofocus
>
<h1 id="dialog-any-title">any</h1>
<p>This dialog can be closed in any way</p>
<button type="button" commandfor="dialog-any" command="close">Close</button>
</dialog>
closedby="closerequest"The dialog can be closed by:
close() method<dialog
id="dialog-closerequest"
closedby="closerequest"
aria-labelledby="dialog-closerequest-title"
autofocus
>
<h1 id="dialog-closerequest-title">closerequest</h1>
<p>This dialog cannot be closed by clicking outside</p>
<button type="button" commandfor="dialog-closerequest" command="close">
Close
</button>
</dialog>
closedby="none"The dialog can only be closed by:
close() method<dialog
id="dialog-none"
closedby="none"
aria-labelledby="dialog-none-title"
autofocus
>
<h1 id="dialog-none-title">none</h1>
<p>This dialog can only be closed programmatically</p>
<button type="button" commandfor="dialog-none" command="close">Close</button>
</dialog>
You can also set the attribute via JavaScript:
const dialog = document.querySelector("dialog");
// Using setAttribute
dialog.setAttribute("closedby", "none");
// Using the property (when polyfill is loaded)
dialog.closedBy = "closerequest";
The closedby attribute can be changed while the dialog is open:
const dialog = document.querySelector("dialog");
dialog.showModal();
// Change behavior while dialog is open
setTimeout(() => {
dialog.closedBy = "none"; // Now only closeable via close() method
}, 3000);
This polyfill works in all browsers that support the native <dialog> element.
Native closedby support:
Dialog element support (required for polyfill):
Note: For browsers without native
closedbysupport, this polyfill provides the functionality. For older browsers without<dialog>element support, you'll also need a dialog element polyfill.
isSupported(): booleanCheck if the browser natively supports the closedby attribute.
import { isSupported } from "dialog-closedby-polyfill";
if (isSupported()) {
console.log("Native closedby support available!");
}
isPolyfilled(): booleanCheck if the polyfill has already been applied.
import { isPolyfilled } from "dialog-closedby-polyfill";
if (isPolyfilled()) {
console.log("Polyfill has been applied");
}
apply(): voidManually apply the polyfill. This is called automatically when importing the main module.
import { apply } from "dialog-closedby-polyfill";
apply(); // Apply the polyfill
TypeScript definitions are included. The polyfill extends the HTMLDialogElement interface:
interface HTMLDialogElement {
closedBy: "any" | "closerequest" | "none";
}
The polyfill works by:
closedby property to dialog elementsshowModal(): Sets up event listeners when a modal dialog is openedkeydown event for ESC key detectionclick event on the dialog for backdrop clickscancel event prevention based on closedby valueThis polyfill aims to match the native implementation as closely as possible. However, there might be minor differences in edge cases. Please report any discrepancies you find.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)MIT License - see the LICENSE file for details
Declarative command/commandfor attributes to provide dialog button operations with markup only.
<!-- Works seamlessly with this polyfill -->
<button type="button" commandfor="my-dialog" command="show-modal">
Open Dialog
</button>
<dialog
id="my-dialog"
closedby="closerequest"
aria-labelledby="my-dialog-heading"
autofocus
>
<h1 id="my-dialog-heading">Heading</h1>
<p>Content</p>
<button type="button" commandfor="my-dialog" command="close">Close</button>
</dialog>
This polyfill is inspired by the native implementation and the work of the web standards community.
FAQs
Polyfill for the HTMLDialogElement closedBy attribute
The npm package dialog-closedby-polyfill receives a total of 698 weekly downloads. As such, dialog-closedby-polyfill popularity was classified as not popular.
We found that dialog-closedby-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.