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
- selected: This property sets the currently selected tab of the interface. By default the selected value need to be an integer, specifying th index of the selected tab (starting at 0 for the first tab, 1 for the second...).
- attrForSelected: This property tells the
<dile-tabs>component which attribute need to match theselectedproperty to set the active tab. By defaultatrrForSelectedisundefined. In that case theselectedproperty should be an integer and match to the index of the tab. If you setatrrForSelectedwith a value, then theselectedproperty will be matched with the value of the attribute named inattrForSelected. - selectorId (optional): A property to link this component to a dile-pages component and change automaticaly the page when this component updates. The dile-pages component also needs to have a selectorId attribute with the same value. When a form shows an error in a field inside a hidden page,
dile-pagesselects that page and its tab (see fields inside collapsed containers). - hashSelection: Boolean property. Connects the tabs component with the URL hash. When the component detects changes in the URL hash, it automatically changes its selected tab. Additionally, when a tab is selected, the component updates the URL hash.
dile-tab properties
- href: Optional. When set, the tab renders a native link (
<a>) pointing to this URL, so clicking it performs a classic browser navigation. The tab keeps exactly the same look as any other tab. - target: Optional. The
targetattribute for the link (e.g._blank). Only used whenhrefis set. - tabbable: Boolean,
trueby default. If the tab is in the tab sequence of the page.dile-tabsmanages it, keeping ittrueon only one of its tabs. - controls: Optional. The id of the panel controlled by the tab, used to link them for assistive technologies (
aria-controls). The panel must be in the same DOM scope as the tab. - controlsElement: Optional, property only. The panel element controlled by the tab. Takes precedence over
controls.dile-pagessets it automatically intabpanelsmode.
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"
}
- The
selectedattribute is the tab name. Usually you will use this value. It can be the tab index or the value defined by the attribute with theattrForSelectedname. - The
selectorIdattribute is the value of theselectorIdcomponent property.
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:
elem(String or Number): The new selected value. This can be either a tab index (whenattrForSelectedis not defined) or the attribute value (whenattrForSelectedis defined).
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.
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.
<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>
dile-components