A custom element for building accessible tabbed interfaces.
import { TabsElement } from 'inclusive-elements';
window.customElements.define('ui-tabs', TabsElement);
<ui-tabs>
<div role="tablist" aria-label="Tabs">
<button type="button" role="tab">Tab 1</button>
<button type="button" role="tab">Tab 2</button>
<button type="button" role="tab">Tab 3</button>
</div>
<div role="tabpanel">Tab Panel 1</div>
<div role="tabpanel" hidden>Tab Panel 2</div>
<div role="tabpanel" hidden>Tab Panel 3</div>
</ui-tabs>
-
Descendants with
role="tab"
androle="tabpanel"
will have appropriateid
,aria-controls
, andaria-labelledby
attributes generated if they are not already set. -
The active
tab
will have thearia-selected="true"
attribute set. Inactive tabs will have theirtabindex
set to-1
so that focus remains on the active tab. -
When focus is on the active
tab
, pressing theLeft Arrow
,Right Arrow
,Home
, andEnd
keys can be used for navigation. If thetablist
hasaria-orientation="vertical"
,Down Arrow
andUp Arrow
are used instead. -
The
tab
with focus is automatically activated, and its correspondingtabpanel
will become visible.