A modal consists of an element with the class modal. This modal comprises:
modal__content containing the contentoptionally a button with the classes modal__close and
modal__close-icon to provide a close icon to the user
Note: This icon can be placed directly inside the modal element or in
the modal__content element.
Placing the icon directly inside the modal element results in the icon
being placed in the top-right corner of the screen (over the backdrop),
whereas the close icon in the modal__content results in the icon being
placed in the top-right corner of the content box (the actual modal).
Note: You can use two close icons (in directly in modal and one in
modal__content) if you want and as many close elements (modal__close)
in a modal.
Tipp: You can use the HTML entity × to create a “x” icon.
modal__backdrop or
modal__background. This element is automatically inserted by the script
when using the modal constructor if it isn’t present
in the modal already.Your modal could look like this:
<div class="modal" aria-label="Example Description">
<div class="modal__content">
<h1>Hello World</h1>
This is my modal!
</div>
<button class="modal__close modal__close-icon">×</button>
</div>
See the examples folder for more examples.
The modal constructor allows you to activate and accessibilize your modal. With the modal object you can for example control the modal or listen to events.
ModalExample:
import Modal from "modal";
let myModal = new Modal("#myModal");
The modal object is constructed with the modal constructor. It has following methods / properties:
Opens the modal.
openundefinedExample:
myModal.open();
Closes the modal.
closeundefinedExample:
myModal.close();
Toggles a modal: If the modal is closed, it will be opened. Otherwise the modal is closed.
toggleundefinedExample:
myModal.toggle();
Adds an event-listener for a specific event.
onundefinedExample:
myModal.on("open", modal => console.log("Modal opened!"));
Removes an event-listener for a specific event.
removeEventundefinedExample:
let myCallback = modal => console.log("Modal opened!");
myModal.on("open", myCallback);
myModal.removeEvent("open", myCallback);
Reference to modal object passed as element or selector to the modal constructor.
modalReference to the backdrop element of the modal. The backdrop element is generated when constructing the modal object if it doesn’t exist.
backdropReference to the modal content (.modal__content).
contentArray containing a reference to modal close button(s) (all elements in the modal
with the class modal__close).
closeBtnString reflecting the state of the modal. Is either "open" or "closed".
stateObject containing list(s) of callback functions for different types of events
eventsThere are two modal events you can listen for with the on method:
Emitted when modal is closed (by the user or programmatically).
closeEmitted when the modal is opened (by the user or programmatically).
openYou can customize the appearance of the modal by overriding its css:
<head>
...
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/umcconnell/modal@1/src/modal.css"
/>
<style>
/*Your Custom Modal Styles */
</style>
</head>
Here are some practical examples to copy-paste:
Zoom in effect when opening the modal, zoom out when closing it (see: /examples/scale).
.modal__content {
transform: scale(1.3);
opacity: 0;
}
.modal.open .modal__content {
transform: scale(1);
opacity: 1;
}
Zoom-out the background when opening a modal (see: /examples/zoom-out).
main {
transition: transform 0.3s;
}
body.modal-open main {
transform: scale(0.97);
}
Hide scrollbars of the background when opening a modal.
body.modal-open {
overflow: hidden;
}
It is recommended to place your modals after your main content.
To keep your modal accessible for screen-readers you should add a aria-label
attribute describing the content of the modal briefly:
<div class="modal" aria-label="Example Description">
<div class="modal__content">
<h1>Hello World</h1>
This is my modal!
</div>
<button class="modal__close modal__close-icon">×</button>
</div>
Alternatively you can add a aria-labelledby attribute referencing an element
describing your modal by id:
<div class="modal" aria-labelledby="modaldescription">
<div class="modal__content">
<p hidden id="modaldescription">Example Description</p>
<h1>Hello World</h1>
This is my modal!
</div>
<button class="modal__close modal__close-icon">×</button>
</div>