Class: Modal

Modal()

A Modal class which can display programatically-generated content, or pull in content from an existing DOM node.

Constructor

Source:
Example
const myModal = new Modal();
const triggerButton = document.querySelector('trigger');

myModal.optionalClass = "modal--myModal";
myModal.content = '<p>Some sample content!</p>';
myModal.focusOnClose = triggerButton;

triggerButton.addEventListener('click', () => {
  myModal.open();
});

Members

(static) hash

Get current url hash

Source:

closeButtonContent

Sets the content of the close button, useful for localizing.
Setter. Usage: modalInstance.closeButtonContent = "<String of HTML!>"

Source:

content

Sets the content of the modal.
Setter. Usage: modalInstance.content = MyHTMLElement

Source:

focusOnClose

Gets the element that will be focused when the modal closes

Source:

focusOnClose

Sets the element that will be focused when the modal closes.
Setter. Usage: modalInstance.focusOnClose = myElement

Source:

onClose

Get the function that is called when the modal closes

Source:

onClose

Sets the function that is called when the modal closes.
Setter. Usage: modalInstance.onClose = myFunction

Source:

onCloseStart

Get the function that is called just before the modal closes

Source:

onCloseStart

Sets the function that is called just before the modal closes. If this is set, when modalInstance.close()` is called it will run the set function with a the modal DOM element and a callback. It will then wait for that callback to be run before completing the close function and calling onClose.

Setter. Usage: `modalInstance.onCloseStart = (modalElement, cb) => { // do some animation with modalElement cb();
}

modalInstance.close(); `

Source:

onOpen

Gets the function that is called when the modal opens

Source:

onOpen

Sets the function that is called when the modal opens. The function gets called with the modals DOM element. Setter. Usage: modalInstance.onOpen = (modalElement) => {}

Source:

optionalClass

Sets an optional class name on the modal for custom styling.
Setter. Usage: modalInstance.optionalClass = "modal--myclass"

Source:

optionalClass

Gets the optional class name

Source:

Methods

close()

Closes modal.

If onCloseStartis defined that is called and waits for the callback. Otherwise it removes content and optional class, and shifts user focus back to triggering element, if specified.

Source:

focusFirstElement()

Shifts focus to the first element inside the content

Source:

focusLastElement()

Shifts focus to the last element inside the content

Source:

open()

Opens modal, adds content and optional CSS class

Source: