4.32.2

Tabs

Tabs enable navigation between sections of related content

Demo

const tabObject = [{ id: "all-returns-tab", label: "All", tabPanelId: "all-results-panel", selected: true }, { id: "recent-returns-tab", label: "Recent", tabPanelId: "recent-results-panel" }, { id: "favorites-returns-tab", label: "Favorites", tabPanelId: "favorites-results-panel", count: 3 }]; const tabs = document.querySelector(".tabs-example"); tabs.setAttribute("tabitems", JSON.stringify(tabObject)); const tabObject2 = [{ id: "all-returns-tab-2", label: "All", tabPanelId: "all-results-panel-2", selected: true, iconName: "folder" }, { id: "recent-returns-tab-2", label: "Recent", tabPanelId: "recent-results-panel-2", iconName: "clock" }, { id: "favorites-returns-tab-2", label: "Favorites", tabPanelId: "favorites-results-panel-2", count: 3, iconName: "heart" }]; const tabs2 = document.querySelector(".tabs-example-2"); tabs2.setAttribute("tabitems", JSON.stringify(tabObject2));

API

Other than highlighting the selected Tab, the Tabs component has no default behavior. It is left up to the implementer. See the Displaying Tab content guideline below for more info.

Tags

Name Description
<s-tabs> Custom Element, accepts <s-tab> elements as content
<s-tab> Custom HTML tag, accepts content

Attributes

Name Value Required Description
s-tab[selected] Boolean attribute

The currently selected Tab. Defaults to the first Tab, but you can set it to any.

s-tab[disabled] Boolean attribute

Applies disabled style to a Tab

tabitems String

Stringified JSON array of tab objects. See Guidelines below.

i18n string

A stringified JSON object which defines a list of localized strings. The keys must be one of the string IDs defined below.

Properties

Name Value Required Description
tabItems Array Array of tab objects. See Guidelines below.
i18n object

A JSON object which defines a list of localized strings. The keys must be one of the string IDs defined below.

Events

Name Detail Description
s-select The selected Tab's index, isInitialSelectEvent, and id if set

Fires when the component first renders, i.e. the initial Tab selection, and on subsequent Tab selections. See the Displaying Tab content guideline below for more info. isInitialSelectEvent will return true the first time the event is dispatched (i.e., on initial render), and false every other time beyond that.

Demo

API

Other than highlighting the selected Tab, the Tabs component has no default behavior. It is left up to the implementer. See the Displaying Tab content guideline above for more info.

Tag

Name
<STabs>

Props

Name Value Required Description
tabItems Array of objects

Array of tab objects. See Guidelines above for information about the structure of these objects.

i18n object

A JSON object which defines a list of localized strings. The keys must be one of the string IDs defined below.

onS-select Function

Handles the s-select event that fires when the component first renders, i.e. the initial tab selection, and on subsequent tab selections. The selected tab's index and id (if set) are available on e.detail. See the Displaying tab content guideline above for more info.

Skylab React links

General information about using our React package

Typescript

Exported types

// Object provided (in array) to tabItems prop export type TabItem = { id: string; label: string; tabPanelId: string; selected?: boolean; disabled?: boolean; iconName?: string; badgeCount?: number | string; count?: number; badgeAriaLabel?: string; attributes?: Array<{ name: string; value: string; }>; }; // s-select event's e.detail export type STabsSelectData = { id: string; index: number; label: string; isInitialSelectEvent: boolean; };

i18n Strings

ID Description Default value
"tabs.ariaLabel.moreTabs" The aria-label used for the "More" button. More tabs
"tabs.more" The text of the "More" button. More

Guidelines

Creating Tabs

If you will be dynamically updating tabs during the s-tabs component's lifetime, including through responsive behavior, do not use <s-tab> children. You should assign a stringified JSON array of tab objects to the tabitems (note the lowercase 'i') attribute. An example:

const tabObject = [ { "id": string, // required "label": string, // required "tabPanelId": string, // required; sets aria-controls to the id of the associated tabpanel "selected": boolean, "disabled": boolean, "iconName": string, // name of s-icon to be displayed to the left of the tab text, if any "badgeCount": string, // count of s-badge to be displayed to the right of the tab text, if any. only displayed if badgeAriaLabel has a value "badgeAriaLabel": string // aria-label of s-badge, if any "count": number, // count of items related to the tab (data rows, etc.) to be displayed to the right of the tab text, if any "attributes": array of objects, // custom attributes to be defined on individual child tabs: ie; { "name": "data-ref", "value": "skylab.components.s-tabs.demo" } }, ]; const tabs = document.querySelector("s-tabs"); tabs.setAttribute("tabitems", JSON.stringify(tabObject));

Responsiveness

The s-tabs component is responsive to varying screen widths ONLY when populated with the tabitems method described immediately above. Tabs components populated with hardcoded <s-tab> children will simply wrap to a new row as the viewport shrinks.

It is highly recommended to take advantage of true responsive behavior, which includes keeping tabs in a single row at all widths, the addition of a collapsed items (More) menu when required, and a separate layout for viewports smaller than 600px wide.

Accessibility

The Tabs element will have its aria-role attribute set to tablist automatically within the component. An aria-label should be set to indicate the purpose of the set of tabs.

Each JSON <s-tab> element will have its aria-role attribute set to tab automatically. Legacy <s-tab> will need to have role="tab" manually inserted. An id and aria-controls attribute should be set on each within the HTML markup. aria-selected will be managed within the component.

Each tab panel (not managed within Skylab SDK component) should have an role attribute set to tabpanel, an id attribute set to the value of the aria-controls attribute on the corresponding tab, and an aria-labelledby attribute set to the value of theid attribute of the corresponding tab, and a tabindex attribute set to 0.

Displaying Tab content

When a Tab is selected the s-select event is fired. Listen to this event and respond accordingly, which usually means immediately displaying that Tab's corresponding content, but there can be other use cases like navigating to a new page or triggering an animation that transitions to a new view. See code sample at top for an example on implementing this logic in vanilla JavaScript. If your app utilizes a front-end JavaScript framework, we recommend using the coding patterns that framework endorses rarther than the declarative implementation above.

React Typescript Code Sample

For those using React + Typescript, below is a React Component <TabGenerator /> which accepts a tab array, and will render the <s-tab> component for you. You can copy+paste this into your codebase and be up and running without any extra fussing.

  
  import React, { ReactElement, useEffect } from 'react';
  
  type TabArray = {
    id: string; // required
    label: string; // required
    tabPanelId: string; // required; sets aria-controls to the id of the associated tabpanel
    selected?: boolean;
    disabled?: boolean;
    iconName?: string; // name of s-icon to be displayed to the left of the tab text, if any
    badgeCount?: string | number; // count of s-badge to be displayed to the right of the tab text, if any
    badgeAriaLabel?: string; // aria-label of s-badge, if any
    onClick: () => void; // required for this component, but could be refactored out
  }[];
  
  interface TabComponent extends HTMLElement {
    tabItems: TabArray;
  }
  
  type TabGeneratorProps = {
    tabArray: TabArray;
    parentId: string; //don't include the # when passing this value
  };
  
  export default function TabGenerator({
    tabArray,
    parentId = 'typescript-s-tabs',
  }: TabGeneratorProps): ReactElement {
    useEffect(() => {
      const tabsComponent = document.querySelector(
        `#${parentId}`
      ) as TabComponent;
  
      tabsComponent.tabItems = tabArray;
      tabsComponent.addEventListener('s-select', (event) => {
        const tabId = (event as CustomEvent).detail.id;
        const tabArrayItem = tabArray.find((tabObj) => tabObj.id === tabId);
        tabArrayItem?.onClick();
      });
    });
  
    return <s-tabs id={parentId} />;
  }
  
  

Design

Design resources can be found on the Skylab design documentation site: skylab.avalara.com