modal

modal docs

Table of Contents

HTML structure

A modal consists of an element with the class modal. This modal comprises:

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">&times;</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.

The modal object is constructed with the modal constructor. It has following methods / properties:

Open

Opens the modal.

Close

Closes the modal.

Toggle

Toggles a modal: If the modal is closed, it will be opened. Otherwise the modal is closed.

On

Adds an event-listener for a specific event.

RemoveEvent

Removes an event-listener for a specific event.

Reference to modal object passed as element or selector to the modal constructor.

Backdrop

Reference to the backdrop element of the modal. The backdrop element is generated when constructing the modal object if it doesn’t exist.

Content

Reference to the modal content (.modal__content).

CloseBtn

Array containing a reference to modal close button(s) (all elements in the modal with the class modal__close).

State

String reflecting the state of the modal. Is either "open" or "closed".

Events

Object containing list(s) of callback functions for different types of events

There are two modal events you can listen for with the on method:

Close Event

Emitted when modal is closed (by the user or programmatically).

Open Event

Emitted when the modal is opened (by the user or programmatically).

Customizing

You 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:

Scale

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

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

No overflow

Hide scrollbars of the background when opening a modal.

body.modal-open {
    overflow: hidden;
}

Best Practices

Placement

It is recommended to place your modals after your main content.

Description

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">&times;</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">&times;</button>
</div>