Modal
Defined in: components/modal/modal.ts:90
Dialogs for lightboxes, user notifications and custom content.
- Nested modals are not supported. Avoid nesting modals in
position: fixedelements, and place modal HTML at the top level of the page when possible. - Each modal needs a unique
id. - While a modal is open the class
js-modal-openedis set on<html>. - Open modals with
data-modallinks or with Modal.show.
| Event | Fired when | event.detail |
|---|---|---|
modal.opened |
a modal is opened | ModalEventDetail |
modal.closed |
a modal is closed | ModalEventDetail |
modal.destroyed |
Modal.destroy runs | ModalEventDetail |
Examples
Section titled “Examples”Markup for a modal
<div class="modal" id="unique-id3"> <div class="modal-header"> <h2 class="modal-header-title">Modal header</h2> <a href="#" class="modal-close">Close me</a> </div> <div class="modal-content"> <p>Modal content.</p> </div></div>Open from a link (the rel attribute must equal the modal id)
<a href="#" rel="unique-id3" data-modal="width:700">open modal</a>const modals = new Modal();modals.init();Open from JavaScript
modals.show('unique-id3', { width: 750 });Listen for events
document.addEventListener('modal.opened', (event) => { console.log('opened modal', (event as CustomEvent<ModalEventDetail>).detail.modal);});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Modal():
Modal
Returns
Section titled “Returns”Modal
Methods
Section titled “Methods”destroy()
Section titled “destroy()”destroy():
void
Defined in: components/modal/modal.ts:162
Removes the event listeners and the overlay, and resets the modals.
Returns
Section titled “Returns”void
hide()
Section titled “hide()”hide():
void
Defined in: components/modal/modal.ts:147
Hides every modal.
Returns
Section titled “Returns”void
init()
Section titled “init()”init():
void
Defined in: components/modal/modal.ts:102
Finds the [data-modal] links and .modal elements on the page, adds the
overlay and binds events. Calling it again re-scans the page.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If there are no .modal elements on the page.
show()
Section titled “show()”show(
id,options?):void
Defined in: components/modal/modal.ts:125
Shows a modal and hides any other. Use this to open a modal without
data-modal.
Parameters
Section titled “Parameters”string
The id of the modal to display.
options?
Section titled “options?”Overrides the defaults. Only width is used.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If Modal.init has not been called, or no element has that id.