Sections

Tabs

Make it easier for visitors to find what they’re after by grouping content.

Although they are most often seen within the content area of an article, tabs are also used as part of larger patterns throughout our theme collection. The Tabs plugin is responsible for generating the HTML markup for a tabs element, based on an existing list.

The Tab plugin, which can be used independently of this plugin, is responsible for showing and hiding tabs.

Usage

Via data attributes

When editing an existing article, create a list (<ul> or <ol> element) and click the Source Code (Code icon) button on the editor toolbar to view the source code of the page. Add the data-element="tabs" attribute to the list and optional titles to each list item using the data-title attribute:

<ul data-element="tabs">
  <li data-title="The first tab">...</li>
  <li data-title="The second tab">...</li>
  <li data-title="The third tab">...</li>
</ul>

Both the Tab and Tabs plugin JavaScript code is included within the plugins.min.js file by default. If you have edited this file ensure that both are still present. Because data attributes are used, you will need to ensure that the allow unsage HTML setting is enabled within Guide.

Via JavaScript

The Tabs plugin can be instantiated against a list using JavaScript:

<ul class="js-tabs">...</ul>

<script type="text/javascript">
  ready(function() {
    each('.js-tabs', function(el) {
      new Tabs(el, {
        // Options go here
      });
    });
  });
</script>

Examples

The following example uses the default template, which can replaced using the plugin’s template setting.

Alternatively you can copy-and-paste your custom tab HTML markup onto any page, eliminating the need to use the Tabs plugin.

An optional fade effect can be added by using .fade on each tab. The active tab must also have .is-shown in order to be visible.

Options

Options can be passed via data attributes or JavaScript.

For data attributes, append the option name to data-. For example, data-template="my-tabs" instructs the plugin to use the custom my-tabs template for rendering markup.

Name Type Default Description
initial number 0 The index of the tab that should be shown on initialization.
template string
null
null The name of the template to use.
templateData object {} Additional data to provide to the template rendering function.

Events

Name Description
tabs.render Fires when the tabs markup has been rendered and inserted into the DOM.

Because the original list is replaced with the HTML from the template, the event contains a reference to the new element:

document.addEventListener('tabs.render', function(e) {
  
  // The newly rendered tabs element
  var tabs = e.detail.relatedTarget;
});