Skip to content

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: fixed elements, 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-opened is set on <html>.
  • Open modals with data-modal links 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

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);
});

new Modal(): Modal

Modal

destroy(): void

Defined in: components/modal/modal.ts:162

Removes the event listeners and the overlay, and resets the modals.

void


hide(): void

Defined in: components/modal/modal.ts:147

Hides every modal.

void


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.

void

If there are no .modal elements on the page.


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.

string

The id of the modal to display.

ModalShowOptions

Overrides the defaults. Only width is used.

void

If Modal.init has not been called, or no element has that id.