2.0.3 • Published 6 years ago

a11ytab v2.0.3

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

A11yTab

A fully accessible and customizable front-end component for tabs. A11yTab has minimal markup requirements so it can be used however your project demands.

DEMO

Installation

A11yTab is available at the:

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

Usage

A11yTab does require minimal amount of markup to function:

<!--
  A containing element is required. This is used when initializing A11yTab
-->
<div id="a11ytab-list">
  <!--
    - A11yTab requires anchors to be used for accessibility reasons
    - Each link must have a unique id
    - The `href` attribute must point to the id of it's corresponding panel
    - Each link must have the `data-a11ytab-tab` attribute
  -->
  <a id="tab1" href="#panel1" data-a11ytab-tab>...</a>
  <a id="tab2" href="#panel2" data-a11ytab-tab>...</a>
  <a id="tab3" href="#panel3" data-a11ytab-tab>...</a>
</div>
<!--
  Each tab panel must have an unique id
-->
<div id="panel1">...</div>
<div id="panel2">...</div>
<div id="panel3">...</div>
// Import A11yTab if utilizing JS modules
import A11yTab from 'a11ytab';

// Create a new instance of A11yTab
// See all the A11yTab options below
const tabs = new A11yTab({
  selector: document.querySelector('#a11ytab-list'),
});

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

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

<div id="a11ytab-list" role="tablist">
  <a href="#panel1" id="tab1" data-a11ytab-tab role="tab" aria-controls="panel1" aria-selected="true" tabindex="0">...</a>
  <a href="#panel2" id="tab2" data-a11ytab-tab role="tab" aria-controls="panel2" aria-selected="false" tabindex="-1">...</a>
  <a href="#panel3" id="tab3" data-a11ytab-tab role="tab" aria-controls="panel3" aria-selected="false" tabindex="-1">...</a>
</div>
<div id="panel1" role="tabpanel" aria-labelledby="tab1" aria-hidden="false">...</div>
<div id="panel2" role="tabpanel" aria-labelledby="tab2" aria-hidden="true">...</div>
<div id="panel3" role="tabpanel" aria-labelledby="tab3" aria-hidden="true">...</div>

A11yTab API

Options

PropertyTypeDefaultDescription
selectorHTMLElementnullContaining element for tabs
tabFocusStringnullClass to add to tabs when focused
tabBlurStringnullClass to add to tabs when not focused
tabPanelFocusStringnullClass to add to panel when active
tabPanelBlurStringnullClass to add to panel when not active
focusOnLoadBooleanfalseMove users focus to tab component on page load
afterFocusFunctionFunctionnullFunction to run after focusing on tab
beforeFocusFunctionFunctionnullFunction to run before focusing on tab
addEventsBooleanfalseAdd custom A11yTab events
eventAfterFocusStringa11ytab:afterFocusName of custom event to fire after focusing on tab
eventBeforeBlurStringa11ytab:beforeBlurName of custom event to fire before leaving focus on tab
hashNavigationBooleanfalseAppend focused tab id to URL
tabToFocusHTMLElementnullTab element to initially make active

Methods

NameDescription
initInitializes instance of A11yTab
destroyKills instance of A11yTab
prevFocus on previously focusable tab
nextFocus on following focusable tab
focusFocus on given tab

License

MIT License

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago