Back to Tabs-guidelines

JavaScriptThe latest version of this package is: 0.17.4, Opens in new window

JavaScript library for the Tabs Component

This component is compatible with ESM (ES6 module) and IIFE. See documentation examples below.

To be able to install this component, please refer to the Project Setup documentation.

$ npm i @ids-js/tabs@0.17.4

Name Age Position
John Wicker 38 Hitman
John Wicker 38 Hitman
John Wicker 32 Hitman
Expandable 38 Hitman
John Wicker 38 Hitman

Table of Contents

Features

  • Makes the Tab component usable
  • User can navigate tabs with arrow keys
  • Full accessible compliance

Usage

Install

  $ npm i @ids-js/tabs

Required markup

<div class="if tabs">
  <ul class="if" role="tablist">
    <li class="if" role="presentation">
      <a class="if" href="#<unique-id-for-section>-1" id="<unique-id-for-title>-1" role="tab" aria-selected="true"
        >Tab title</a
      >
    </li>
    <li class="if" role="presentation">
      <a class="if" href="#<unique-id-for-section>-2" id="<unique-id-for-title>-2" role="tab" tabindex="-1"
        >Tab title</a
      >
    </li>
  </ul>
  <section
    aria-labelledby="<unique-id-for-title>-1"
    id="<unique-id-for-section>-1"
    class="if tab-panel"
    role="tabpanel"
    tabindex="-1"
  ></section>
  <section
    aria-labelledby="<unique-id-for-title>-2"
    id="<unique-id-for-section>-2"
    class="if tab-panel"
    role="tabpanel"
    tabindex="-1"
    hidden=""
  ></section>
</div>

You preferrably want to wrap it in a block and a container:

<div class="if block">
  <div class="if container"></div>
</div>

Initialize one set of tabs

import idsInitTabs from '@ids-js/tabs';
import { debounce } from '@ids-js/utils';

const tabElement = document.querySelector('.if.tabs');

if (document.readyState === 'loading') {
  document.removeEventListener('DOMContentLoaded', () => {
    idsInitTabs(tabElement);
  });
  document.addEventListener('DOMContentLoaded', () => {
    idsInitTabs(tabElement);
  });
} else {
  idsInitTabs(tabElement);
}
const initTabs = debounce(function () {
  idsInitTabs(tabElement);
}, 300);

window.removeEventListener('resize', () => {
  idsInitTabs(tabElement);
});
window.addEventListener('resize', () => {
  idsInitTabs(tabElement);
});

Initialize several tabs

This will initialize all the tabs in the current DOM.

import idsInitTabs from '@ids-js/tabs';
import { debounce } from '@ids-js/utils';

const init = () => {
  const _tab_els = document.querySelectorAll('.if.tabs');

  _tab_els.forEach(idsInitTabs);
};

if (document.readyState === 'loading') {
  document.removeEventListener('DOMContentLoaded', init);
  document.addEventListener('DOMContentLoaded', init);
} else {
  init();
}
const initTabs = debounce(function () {
  init();
}, 300);

window.removeEventListener('resize', initTabs);
window.addEventListener('resize', initTabs);

Api

idsTabs(el)

Initializes the Tabs Component with the given arguments.

Param Type Description
el HTMLElement The Tab DOM Element

el : HTMLElement

The Tab DOM Element:

<div class="if tabs">
  <ul class="if" role="tablist">
    <li class="if" role="presentation">
      <a class="if" href="#<unique-id-for-section>-1" id="<unique-id-for-title>-1" role="tab" aria-selected="true"
        >Tab title</a
      >
    </li>
    <li class="if" role="presentation">
      <a class="if" href="#<unique-id-for-section>-2" id="<unique-id-for-title>-2" role="tab" tabindex="-1"
        >Tab title</a
      >
    </li>
  </ul>
  <section
    aria-labelledby="<unique-id-for-title>-1"
    id="<unique-id-for-section>-1"
    class="if tab-panel"
    role="tabpanel"
    tabindex="-1"
  ></section>
  <section
    aria-labelledby="<unique-id-for-title>-2"
    id="<unique-id-for-section>-2"
    class="if tab-panel"
    role="tabpanel"
    tabindex="-1"
    hidden=""
  ></section>
</div>
Contact us, Opens in new window