html-native-modal
Dead simple modal library based on the HTML 5.2 native <dialog>
.
- APIs are based on the
HTMLDialogElement
spec.
(See also HTMLDialogElement - Web APIs | MDN)<dialog>
tagdialogElement.showModal()
methoddialogElement.close()
methodcancel
event
- No dependencies. No polyfills. Works with any framework.
- Tiny. JS < 100 lines, CSS < 100 lines.
- Works in IE 11.
Preact + TypeScript (TSX) example on CodeSandbox
Nested modals example on CodePen
Installation
npm
npm i html-native-modal
CDN
Download
Usage
Open Close Lorem ipsum dolor sit amet, consectetur adipiscing elit, ...
Canceling Canceling
By default, pressing the Esc
key or clicking on the background will close the dialog.
To disable this behavior, listen for the cancel
event and call event.preventDefault()
.
Close Lorem ipsum dolor sit amet, consectetur adipiscing elit, ...
Inlined <dialog oncancel="arguments[0].preventDefault()">...</dialog>
does not work on the browsers that don't support HTMLDialogElement
natively.