Sliding side panel with focus trap and full a11y support
Offcanvas can be positioned from the left, right, top, or bottom
Typical use case of offcanvas for mobile navigation
Wider version for more detailed content
Offcanvas with focus trap works perfectly with forms
<!-- 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>
import { initOffcanvas } from './a11y-kit/a11y-offcanvas.js';
// Initialize after DOM is ready
document.addEventListener('DOMContentLoaded', () => {
initOffcanvas();
});
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')
});
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);
});
<!-- 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>
This is an offcanvas panel opened from the left side.
Focus is trapped - try pressing Tab to navigate through focusable elements.
This is an offcanvas panel opened from the right side.
Press Esc to close.
This is an offcanvas panel opened from the top.
This is an offcanvas panel opened from the bottom.
This is a wider offcanvas panel (480px instead of 320px).
Suitable for more detailed content or wider forms.