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 Selector null
data-hs-overlay-close-on-backdrop Close offcanvas on backdrop click false | true true
data-hs-overlay-backdrop Enable backdrop false | true true
data-hs-overlay-scroll Enable scroll body when offcanvas is open false | true false

Classes

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

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