1. Plugins
  2. Overlay

Plugins

Tailwind CSS Overlay

The Overlay Tailwind plugin is an unstyled JavaScript utility for creating modals, drawers, and more, enhancing user interaction and content layout.

Image Description

Installation

To get started, install Overlay plugin via npm, else you can skip this step if you are already using Preline UI as a package.

                      
                        npm i @preline/overlay
                      
                    

Example

Click "Open modal" button to trigger a modal/popup window on top of the current page.

Basic usage

Prefer to create your own style? Here is a completely unstylized example.

                      
                        <button type="button" data-hs-overlay="#hs-unstyled-modal">
                          Open modal
                        </button>

                        <div id="hs-unstyled-modal" class="hs-overlay hidden w-full h-full fixed top-0 start-0 z-[60] overflow-x-hidden overflow-y-auto">
                          <div class="hs-overlay-open:opacity-100 hs-overlay-open:duration-500 opacity-0 transition-all sm:max-w-lg sm:w-full m-3 sm:mx-auto">
                            Modal content
                          </div>
                        </div>
                      
                    

Options

Name Description Options Default value
data-hs-overlay Defines the modal.
:hiddenClass Defines which classes will be added/removed when modal toggle. string hidden
data-hs-overlay-backdrop-container Backdrop element selector. null
data-hs-overlay-keyboard When set to true, the modal will not close when clicking on ESC keyboard button. boolean true
autofocus Focus the first input in a modal with the autofocus attribute on opening. Must be added to an input element inside a modal window.
[--overlay-backdrop:*] When backdrop is set to static, the modal will not close when clicking outside it. "static" | null null
[--auto-hide:*] Milliseconds for auto-closing a modal.When set to 0, the modal will not close. number 0
[--body-scroll:*] When set to false, the body scroll will be hidden as soon as the modal opens. boolean true
[--tab-accessibility-limited:*] Restricts focus to elements within an overlay (or modal). "true" | "false" true
[--has-autofocus:*] Disables autofocus on the first focusable element when opening an overlay. "true" | "false" true

Classes

Name Description
hs-overlay Modal content
hs-overlay-open Modal open
hs-overlay-toggle Modal toggle
hs-overlay-backdrop-open Modal backdrop open

Methods

The HSOverlay object is contained within the global window object

Method Description
Public methods
open() Force open modal.
close() Force close modal.
Static methods
HSOverlay.getInstance(target, isInstance) Returns the element associated to the target.
  • target should be a Node or string (valid selector)
  • isInstance boolean. Returns the instance instead of Node if true
HSOverlay.open(target) Open modal.
  • target should be a Node
HSOverlay.close(target) Close modal.
  • target should be a Node

Open item (public method).

                      
                        const modal = new HSOverlay(document.querySelector('#modal'));
                        const openBtn = document.querySelector('#open-btn');

                        openBtn.addEventListener('click', () => {
                          modal.open();
                        });
                      
                    

Open item (static method).

                      
                        const openBtn = document.querySelector('#open-btn');

                        openBtn.addEventListener('click', () => {
                          HSOverlay.open('#modal');
                        });
                      
                    

Open item (mixed).

                      
                        const { element } = HSOverlay.getInstance('#modal', true);
                        const openBtn = document.querySelector('#open-btn');

                        openBtn.addEventListener('click', () => {
                          element.open();
                        });
                      
                    

Events

Method Description
on:open Called when modal is open
on:close Called when modal is closed

Open any modal event example.

                      
                        const el = HSOverlay.getInstance('#modal');

                        el.on('open', (instance) => {...});
                      
                    

Demo examples

Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.

Image Description
Check out Preline UI Modals