Positions

Offcanvas can be positioned from the left, right, top, or bottom

Navigation Menu

Typical use case of offcanvas for mobile navigation

Wide Variant

Wider version for more detailed content

With Form

Offcanvas with focus trap works perfectly with forms

Usage

Basic HTML Structure:

<!-- Trigger button -->
<button data-offcanvas-trigger="my-offcanvas">Open</button>

<!-- Offcanvas -->
<div data-offcanvas id="my-offcanvas">
    <div data-position="left" data-offcanvas-panel>
        <div data-offcanvas-header>
            <h2 data-offcanvas-title>Title</h2>
            <button data-offcanvas-close aria-label="Close">×</button>
        </div>

        <div data-offcanvas-body>
            Offcanvas content...
        </div>
    </div>

    <div data-offcanvas-backdrop></div>
</div>

JavaScript Initialization:

1. ES6 Module Import - init functions (Recommended)

import { initOffcanvas } from './a11y-kit/a11y-offcanvas.js';

// Initialize after DOM is ready
document.addEventListener('DOMContentLoaded', () => {
  initOffcanvas();
});

2. ES6 Module Import - Bootstrap Style

import { Offcanvas } from './a11y-kit/a11y-offcanvas.js';

const offcanvas = new Offcanvas(element, {
    closeOnBackdrop: true,
    closeOnEscape: true,
    trapFocus: true,
    scrollLock: true,
    onOpen: (instance) => console.log('Opened'),
    onClose: (instance) => console.log('Closed')
});

3. NPM package (Module bundlers: Astro, Vite, Webpack)

npm install accessible-kit

// Import only what you need (best for tree-shaking)
import { initOffcanvas, Offcanvas } from 'accessible-kit/offcanvas';

// Initialize after DOM is ready
document.addEventListener('DOMContentLoaded', () => {
  initOffcanvas(); // Auto-init all offcanvas

  // OR create instances manually
  const offcanvas = new Offcanvas(element, options);
});

Positions:

<!-- From left -->
<div data-position="left" data-offcanvas-panel>...</div>

<!-- From right -->
<div data-position="right" data-offcanvas-panel>...</div>

<!-- From top -->
<div data-position="top" data-offcanvas-panel>...</div>

<!-- From bottom -->
<div data-position="bottom" data-offcanvas-panel>...</div>

<!-- Wide variant -->
<div data-position="left" data-width="wide" data-offcanvas-panel>...</div>

Keyboard Navigation

  • Tab - Navigate between focusable elements (with focus trap)
  • Shift + Tab - Reverse navigation (with focus trap)
  • Esc - Close offcanvas and return focus to trigger

Features

Offcanvas from Left

This is an offcanvas panel opened from the left side.

Focus is trapped - try pressing Tab to navigate through focusable elements.

Offcanvas from Right

This is an offcanvas panel opened from the right side.

Press Esc to close.

Offcanvas from Top

This is an offcanvas panel opened from the top.

Offcanvas from Bottom

This is an offcanvas panel opened from the bottom.

Navigation

Wide Panel

This is a wider offcanvas panel (480px instead of 320px).

Suitable for more detailed content or wider forms.

Contact Form