Modals
Pop-Up or overlay elements that appear on top of the main content on a webpage to display additional information or prompt user interaction without navigating to a new page.
How It Works:
- Default JS scripts are applied. If different behavior is desired, then a modal can be opened programmatically defined later in the page.
- To open a modal, the trigger element must include the data-modal-target attribute with the modal's ID as its value (e.g., data-modal-target="#modalId"). However, if you're opening the modal programmatically, the trigger attribute must be removed.
- When closing a modal, the modal close trigger should include the data-modal-dismiss without any values. However, if you're closing the modal programmatically, the trigger attribute must be removed.
- Clicking on the modal backdrop and pressing the Escape key will automatically close the modal.
- When a modal is opened, the body scrolling is disabled by default to let the modal contents to scroll instead.
- Whenever possible, place your modal HTML in a top-level position to avoid code interference against other elements.
Default Modal
The default modal style that comes with a basic, predefined look and behavior. This kind of modal has specific default settings and styles, making it quick and easy to implement without extensive customization.
Vertically Centered
Add the .centered class to .modal-dialog to vertically center a modal.
Remove Fading Animation
By default, everytime a modal is opened, it fades in from top to bottom, and fades out from bottom to top. To disable animation, just remove the .fade class from the parent .hpu-modal class.
Scrolling Long Contents
When the modal content is lengthy, the modal height expands to fit it, which can make the content inaccessible if it exceeds the viewport. Since scrolling is disabled by default when a modal is open, users are unable to scroll through extended content:
To address this issue, add the .scrollable class to the .modal-dialog class:
Modal Sizing
Modals sizes are also available to use. Just add the .sm for small, or lg for large modals to .modal-dialog class.
Toggle Between Modals
You can open a new modal right after another one closes! Simply add both the data-modal-target attribute with the target value and the data-dismiss-modal attribute to the trigger element. This will close the first modal and immediately open the second one in sequence.
In the example below, the modal trigger for the next modal is in .modal-footer, but any modal dismiss trigger can be also a trigger for multi-modals.
Programmatic Modals
Modals can be activated or dismissed programmatically, with options available to customize their behavior. When using this approach, avoid defining data-modal-target and data-modal-dismiss attributes on trigger elements, as they’re not needed.
Modal Options
Set of configurable parameters that define the behavior and functionality of a modal. To change the modal's behavior, simply add an option right next to the selector as shown:
JS
modalnew HPU'#modalId',
Event Methods
Functions or handlers that respond to event listeners and triggers on the modal. Simply call the methods to make the modal show or hide:
JS
trigger'click'
modal(); // Use .hideModal(); method to hide the modal
Modal With no Backdrop and Escape Key Dismiss
In this example, backdrop and escape dismiss are disabled.
Modal With Callback Function
In this example, the option onModalShow is used for a callback function.
Modal Accessibility Features
Our modals are not only rich in features, but also built with accessibility in mind. As demonstrated in the code samples, please ensure your implementation includes the appropriate aria-*, tabindex, data-*, and role attributes.
Additionally, our modals support multiple interaction methods, including:
- Dismissing the modal with the Escape key
- Closing the modal by clicking outside the modal area
- Navigating and interacting with modal content using the Tab key (e.g., buttons, inputs, links, etc.)
More importantly, when a modal is opened, keyboard focus is trapped within the modal to support accessible navigation. While this focus-trap behavior can be disabled via configuration options above, it is strongly recommended keeping it enabled unless absolutely necessary.
Try interacting with the modal using the methods listed above to experience these accessibility features in action: