3.0.2 • Published 6 years ago

a11yoffcanvas v3.0.2

Weekly downloads
14
License
MIT
Repository
github
Last release
6 years ago

A11yOffCanvas

A fully accessible and customizable off-canvas front-end component. Use your own markup and styles and let A11yOffCanvas do the hard stuff for you.

DEMO

Installation

A11yOffCanvas is available at:

Source
NPMnpm install a11yoffcanvas --save
Yarnyarn add a11yoffcanvas
unpkghttps://unpkg.com/a11yoffcanvas

Usage

A11yOffCanvas does require minimal amount of markup to function:

<!--
  - Trigger for drawer must have `data-a11yoffcanvas-trigger` attribute must point to the id of it's corresponding drawer
-->
<button data-a11yoffcanvas-trigger="drawerLeft">Open drawer</button>
<!--
  - Although the drawer is not required to be an `<aside>`, semantically it's an appropriate tag to use
  - Drawers must have an unique id
  - Drawers must have `data-a11yoffcanvas-drawer` attribute
-->
<aside id="drawerLeft" data-a11yoffcanvas-drawer>
  <!--
    - Add optional `data-a11yoffcanvas-close` attribute to be able to close drawers
  -->
  <a href="#" data-a11yoffcanvas-close>x</a>
</aside>
// Import A11yOffCanvas if utilizing JS modules
import A11yOffCanvas from 'a11yoffcanvas';

// Create a new instance of A11yOffCanvas
// All options and default values shown
const drawerTrigger = document.querySelector('button');
const offcanvas = new A11yOffCanvas(drawerTrigger, {
  drawerCloseClass: null,
  drawerOpenClass: null,
  closeOnClick: false,
  afterCloseFunction: null,
  afterOpenFunction: null,
  beforeCloseFunction: null,
  beforeOpenFunction: null,
  addEvents: false,
  trapFocus: true,
});

// Initialize your new tabs
offcanvas.init();

A11yOffCanvas will handle all ARIA roles/attributes, focus management, and events, which transform the original HTML into the following:

<button data-a11yoffcanvas-toggle="drawerLeft" aria-controls="drawerLeft" aria-expanded="false">Open drawer</button>
<aside id="drawerLeft" data-a11yoffcanvas-drawer aria-hidden="true">
  <a href="#" data-a11yoffcanvas-close>x</a>
</aside>

A11yOffCanvas API

Options

PropertyTypeDefaultDescription
drawerCloseClassStringnullClass to add to drawer when closed
drawerOpenClassStringnullClass to add to drawer when opened
closeOnClickStringnullClose drawer when clicking outside outside of it
afterCloseFunctionFunctionnullFunction to run after drawer closes
afterOpenFunctionFunctionnullFunction to run after drawer opens
beforeCloseFunctionFunctionnullFunction to run before drawer closes
beforeOpenFunctionFunctionnullFunction to run before drawer opens
addEventsBooleanfalseAdd custom A11yOffCanvas events
trapFocusBooleantrueTrap focus within opened drawer

Methods

NameDescription
initInitializes instance of A11yOffCanvas
destroyKills instance of A11yOffCanvas
openOpen drawer
closeClose drawer

License

MIT License

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago