1. Plugins
  2. Offcanvas

Plugins

Tailwind CSS Offcanvas

Build hidden sidebars into your project for navigation, shopping carts, and more.

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

Use the buttons below to show and hide an offcanvas element via JavaScript that toggles the .show class on an element with the .hs-overlay class.

  • .hs-overlay hides content (default)
  • .hs-overlay.show shows content

Basic usage

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

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

                        <div id="hs-unstyled-offcanvas" class="hs-overlay hs-overlay-open:translate-x-0 hidden -translate-x-full fixed top-0 left-0 transition-all duration-300 transform h-full max-w-xs w-full z-[60] bg-white" tabindex="-1">
                          Offcanvas content
                        </div>
                      
                    

Options

Parameters Description Options Default value
data-hs-overlay-backdrop-container Backdrop element selector null
[--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. true | false true
data-hs-overlay-keyboard When set to true, the modal will not close when clicking on ESC keyboard button. true | false 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.

Classes

Name Description
hs-overlay Offcanvas container
hs-overlay-toggle Offcanvas toggle
hs-overlay-backdrop-open Offcanvas backdrop open

Methods

The HSOverlay object is contained within the global window object

Method Description
HSOverlay.open($offcanvasEl) Open offcanvas
HSOverlay.close($offcanvasEl) Close offcanvas
HSOverlay.toggle($offcanvasEl) Toggle offcanvas

Events

Method Description
open.hs.overlay Called when offcanvas is open.
close.hs.overlay Called when offcanvas is closed.

Open any offcanvas event example.

                      
                        window.addEventListener('open.hs.overlay', ($offcanvasEl) => {...})

                        // OR

                        HSOverlay.on('open', ($offcanvasEl) => {...})
                      
                    

Open specific offcanvas event example.

                      
                        $offcanvasEl.addEventListener('open.hs.overlay', (evt) => {...})
                      
                    

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 Offcanvas