logo dile-components dile-components

dile-tabs

Web Component to create easily a generic tabs interface.

In this package there are two components, to implement a singular tab item and a complete group of tabs. You could use this component with more complex component to create a navigation between several options.

It is possible, for example, to use dile-tabs with a dile-pages, to change the view of the component when one tab is selected.

Installation

npm i @dile/ui

Usage

Import the component.

import '@dile/ui/components/tabs/tabs.js';

Use the component

<dile-tabs selected="2">
  <dile-tab>One</dile-tab>
  <dile-tab>Two</dile-tab>
  <dile-tab>Three</dile-tab>
  <dile-tab>Four</dile-tab>
</dile-tabs>

Properties

dile-tab properties

More complex example

The next example show how to use the attrForSelected property.

<dile-tabs selected="posts" attrForSelected="name">
  <dile-tab name="users">Users</dile-tab>
  <dile-tab name="posts">Posts</dile-tab>
  <dile-tab name="articles">Articles</dile-tab>
  <dile-tab name="faq">FAQ</dile-tab>
  <dile-tab name="contact">Contact</dile-tab>
</dile-tabs>

Tabs as links

Set the href attribute on each dile-tab to use the tabs as a navigation menu with regular links. Set selected on dile-tabs to mark the tab of the current page.

<dile-tabs attrForSelected="name" selected="posts">
  <dile-tab name="users" href="/users">Users</dile-tab>
  <dile-tab name="posts" href="/posts">Posts</dile-tab>
  <dile-tab name="docs" href="https://example.com/docs" target="_blank">Docs</dile-tab>
</dile-tabs>

Events

dile-selected-changed:

When selected property changes by a user interaction inside the <dile-tabs> component, it dispatch the dile-selected-changed custom event. You will recive the new selected value in the selected attribute in the detail event object property.

The detail object has this properties:

{
  selected: "tab_name"
  selectorId: "selector_id"
}

Methods

changeSelectedTo(elem)

This method allows you to programmatically change the selected tab. It updates the selected property and dispatches the dile-selected-changed event.

Parameters:

Example:

const tabsComponent = document.querySelector('dile-tabs');
// Change to second tab (index 1)
tabsComponent.changeSelectedTo(1);

// Or if using attrForSelected
tabsComponent.changeSelectedTo('posts');

CSS custom properties

You can customize the tabs using the CSS custom properties bellow.

Custom property Description Default
--dile-tab-text-color The tab text color var(--dile-on-primary-color) fallback to #666
--dile-tab-background-color The tab background color var(--dile-primary-color) fallback to #eee
--dile-tab-selected-text-color The selected tab text color var(--dile-on-primary-dark-color) fallback to #000
--dile-tab-selected-background-color The selected tab background color var(--dile-primary-dark-color) fallback to #888
--dile-tab-padding Content padding of the tab 8px 12px 6px 12px
--dile-tab-selected-line-height Defines selected tab line height 5px
--dile-tab-selected-line-color Defines selected tab line color var(--dile-secondary-color) fallback to #333
--dile-tab-border-radius Tab top-left & top-right border radius 4px
--dile-tab-border Unselected tab border none
--dile-tab-selected-border Selected tab border none
--dile-tab-font-weight Tab font weight normal
--dile-tab-font-size Tab font size 1rem
--dile-tab-text-transform Tab text transform uppercase
--dile-tab-focus-ring-width Width of the keyboard focus ring 2px
--dile-tab-focus-ring-color Color of the keyboard focus ring currentColor
--dile-tab-focus-ring-offset Offset of the keyboard focus ring. Negative values draw it inside the tab -2px

Accessibility

The component follows the WAI-ARIA tabs pattern with manual activation: dile-tabs has the tablist role, each tab the tab role and the selected one aria-selected="true". The arrows only move the focus, so dile-selected-changed isn't dispatched and links aren't followed until the user activates the tab.

Key Action
Tab Moves the focus to the selected tab (or to the first one if there is no selection). The tabs are only one stop in the tab sequence, and the next Tab leaves them
Arrow Right / Arrow Left Moves to the next / previous tab, wrapping at the ends. They are swapped in right to left pages
Home / End Moves to the first / last tab
Enter / Space Selects the focused tab and, if it has a href, follows the link

Give the group a name with aria-label:

<dile-tabs aria-label="Account sections" selected="0">
  <dile-tab>Profile</dile-tab>
  <dile-tab>Billing</dile-tab>
</dile-tabs>

Linking tabs and panels

The element with the tab role is inside the shadow DOM of dile-tab, so a regular aria-controls="id" can't reach the panel. Use the controls attribute (or the controlsElement property) instead: the tab links its panel with ARIA element reflection, in the browsers that support it.

When the panels are a dile-pages linked by selectorId, add its tabpanels attribute and the link is done for you: each page gets role="tabpanel" and aria-labelledby pointing to its tab, and each tab controls its page.

<dile-tabs aria-label="Account sections" selectorId="account" selected="0">
  <dile-tab>Profile</dile-tab>
  <dile-tab>Billing</dile-tab>
</dile-tabs>
<dile-pages tabpanels selectorId="account" selected="0">
  <section>Profile content</section>
  <section>Billing content</section>
</dile-pages>

dile-tabs demo

Simple Example

In this example we are using the selected element index. Try it with the keyboard: move with the arrows, Home and End, and select with Enter or Space.

<script type="module">
import { LitElement, html, css } from 'lit';

class MyComponent extends LitElement {

  render() {
    return html`
      <dile-tabs id="select1">
        <dile-tab>One</dile-tab>
        <dile-tab>Two</dile-tab>
        <dile-tab>Three</dile-tab>
        <dile-tab>Four</dile-tab>
      </dile-tabs>
      <p id="msg1"></p>
    `
  }
  firstUpdated() {
    this.shadowRoot.getElementById('select1').addEventListener('dile-selected-changed', (e) => {
      let textElement = this.shadowRoot.getElementById('msg1');
      textElement.innerText = "The value selected is: " + e.detail.selected;
    });
  }
}
customElements.define('my-component', MyComponent);
</script>
<my-component></my-component>

Selection by name Example

In this example we are using the selected name attribute in the dile-tab.

<script type="module">
import { LitElement, html, css } from 'lit';

class SecondComponent extends LitElement {

  static get styles() {
    return css`
      :host {
        --dile-tab-text-color: #ccc;
        --dile-tab-background-color: transparent;
        --dile-tab-selected-text-color: #396;
        --dile-tab-selected-background-color: transparent;
        --dile-tab-selected-line-color: #396;
      }
    `
  }

  render() {
    return html`
      <dile-tabs id="select2" attrForSelected="name" selected="users">
        <dile-tab name="users">Users</dile-tab>
        <dile-tab name="posts">Posts</dile-tab>
        <dile-tab name="articles">Articles</dile-tab>
      </dile-tabs>
      <p id="msg1">The value selected is: users</p>
    `
  }
  firstUpdated() {
    this.shadowRoot.getElementById('select2').addEventListener('dile-selected-changed', (e) => {
      let textElement = this.shadowRoot.getElementById('msg1');
      textElement.innerText = "The value selected is: " + e.detail.selected;
    });
  }
}
customElements.define('second-component', SecondComponent);
</script>
<second-component></second-component>

dile-tabs with dile-pages demo

Using dile-tabs in association with a dile-pages element.

Home About Contact

Homepage

I am the home page content...

About

I am the about page content...

Contact

I am the contact page content...

<style>
main.selectionlayout {
  display: flex; 
  flex-direction: column;
  row-gap: 1.8rem;
  --dile-tab-selected-background-color: #333;
  --dile-tab-background-color: #ddd;
  --dile-tab-border-radius: 0;
  --dile-tab-selected-line-color: orange;
  --dile-tab-selected-line-height: 3px;
  --dile-tab-selected-text-color: orange;
  --dile-tab-text-transform: none;
}
main.selectionlayout dile-tabs {
  margin-top: 0.4rem;
  width: 150px
}
main.selectionlayout dile-pages div {
  margin: 0;
  padding: 0;
}
main.selectionlayout dile-pages h2 {
  margin-top: 0;
}
</style>
<main class="selectionlayout">
  <dile-tabs id="select2" attrForSelected="name" selectorId="selector" selected="home">
    <dile-tab icon="label_important" name="home">Home</dile-tab>
    <dile-tab icon="label_important" name="about">About</dile-tab>
    <dile-tab icon="label_important" name="contact">Contact</dile-tab>
  </dile-tabs>
  <dile-pages tabpanels attrForSelected="name" selectorId="selector" selected="home">
    <div name="home">
      <h2>Homepage</h2>
      <p>I am the home page content...</p>
    </div>
    <div name="about">
      <h2>About</h2>
      <p>I am the about page content...</p>
    </div>
    <div name="contact">
      <h2>Contact</h2>
      <p>I am the contact page content...</p>
    </div>
  </dile-pages>
</main>

Tabs as links

Each tab navigates to its href like a regular link, keeping the tab styles.

Tabs Lit MDN
<dile-tabs attrForSelected="name" selected="tabs">
  <dile-tab name="tabs" href="#tabs-as-links">Tabs</dile-tab>
  <dile-tab name="lit" href="https://lit.dev" target="_blank">Lit</dile-tab>
  <dile-tab name="mdn" href="https://developer.mozilla.org" target="_blank">MDN</dile-tab>
</dile-tabs>