2.2.1 • Published 4 months ago

@georapbox/a-tab-group v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

npm version npm license

<a-tab-group>

A custom element to create a group of tabs and tab panels.

API documentation Demo

Install

$ npm install --save @georapbox/a-tab-group

Usage

Script

import './node_modules/@georapbox/a-tab-group/dist/a-tab-group.js';

Markup

All children of <a-tab-group> should be either <a-tab> or <a-tab-panel>. Every <a-tab> should have a corresponding <a-tab-panel> as a sibling, otherwise the component will log an error.

<a-tab-group>
  <a-tab role="heading">Tab 1</a-tab>
  <a-tab-panel>Panel 1</a-tab-panel>

  <a-tab role="heading">Tab 2</a-tab>
  <a-tab-panel>Panel 2</a-tab-panel>

  <a-tab role="heading" disabled>Tab 3 (disabled)</a-tab>
  <a-tab-panel>Panel 3</a-tab-panel>

  <a-tab role="heading" closable>Tab 4</a-tab>
  <a-tab-panel>Panel 4</a-tab-panel>
</a-tab-group>

Style

By default, the component comes with the bare minimum styling. However, you can customise the styles of the various elements of the component using either CSS Parts or CSS Custom Properties. You can also check the demo for a working example.

API

Properties

<a-tab-group> properties

NameReflectsTypeRequiredDefaultDescription
placement'top' \| 'bottom' \| 'start' \| 'end'-'top'The placement of the tabs. The placement is achieved purely with CSS, therefore when stylng the component, you should take into account the placement attribute if you plan to support more than one placement, eg a-tab-group[placement="bottom"] { ... }.
noScrollControlsno-scroll-controlsboolean-falseDisables the scroll buttons that appear when tabs overflow.
scrollDistancescroll-distancenumber-200The distance to scroll when the scroll buttons are clicked. It fallsback to the default value if not provided, or its value is 0.
activation'auto' \| 'manual'-'auto'If set to 'auto', navigating the tabs using the keyboard (Left, Right, Up, Down, Home, End arrow keys) will automatically select the tab. If set to 'manual', the tab will receive focus but will not be selected until the user presses the Enter or Space key.
noTabCyclingno-tab-cyclingboolean-falseDisables tab cycling when the user reaches the first or last tab using the keyboard.

<a-tab> properties

NameReflectsTypeRequiredDefaultDescription
selectedboolean-falseDetermines if a tab is selected. Only one tab is selected at a time. If more than one tab is marked as selected, only the first one will be actually selected. If no tab is marked as selected, the first non-disabled tab will be selected by default. If a disabled tab is marked as selected, it will be ignored and the first non-disabled tab will be selected. It is highly recommended to set this property manually only during the initial render. If you need to programmatically select a tab, use the selectTab, selectTabByIndex or selectTabById methods instead, that will automatically update the selected property and link the tab to its corresponding tab panel.
disabledboolean-falseDetermines if a tab is disabled. Disabled tabs cannot be selected.
closableboolean-falseDetermines if a tab is closable.

Slots

NameDescription
tabUsed for groupping tabs in the tab group. Must be <a-tab> elements. It will be added automatically if not provided.
panelUsed for groupping tab panels in the tab group. Must be <a-tab-panel> elements. It will be added automatically if not provided.

CSS Parts

<a-tab-group> CSS Parts

NameDescription
baseThe tab groups's base wrapper.
navThe tab group's navigation container.
nav--has-scroll-controlsThe tab group's navigation container when scroll controls are enabled and visible.
scroll-buttonThe scroll buttons that show when tabs are scrollable.
scroll-button--startThe scroll button for scrolling towards the start.
scroll-button--endThe scroll button for scrolling towards the end.
scroll-button-iconThe scroll button icon.
tabsThe container that wraps the tabs.
panelsThe container that wraps the tab panels.

<a-tab> CSS Parts

NameDescription
baseThe tab's base wrapper.
close-tabThe close tab button.
close-tab-iconThe close tab icon.

<a-tab-panel> CSS Parts

NameDescription
baseThe tab panel's base wrapper.

CSS Custom Properties

NameDescriptionDefault
--selected-tab-colorThe color of the selected tab.#005fcc
--selected-tab-bg-colorThe background color of the selected tab.transparent
--tabs-scroll-behaviorThe scroll behavior of the tabs. If reduce motion is enabled, the scroll behavior will be set to auto.smooth
--scroll-button-widthThe width of the scroll buttons.2.125em
--scroll-button-heightThe height of the scroll buttons.2.125em
--scroll-button-inline-offsetThis is the value of the left property for the start scroll button and the right property for the end scroll button.0rem

Methods

NameTypeDescriptionArguments
selectTab1InstanceSelects the given tab. If the given tab is disabled or already selected, this method does nothing.tab: HTMLElement
selectTabByIndex1InstanceSelects the tab at the given index. If the tab at the given index is disabled or already selected, this method does nothing.index: number
selectTabById1InstanceSelects the tab with the given id. If the tab with the given id is disabled or already selected, this method does nothing. This is mostly useful if you provide your own ids to the tabs.id: string

1 Instance methods are only available after the component has been defined. To ensure that the components have been defined, you can use the whenDefined method of the CustomElementRegistry interface. For example:

Promise.all([
  customElements.whenDefined('a-tab-group'),
  customElements.whenDefined('a-tab'),
  customElements.whenDefined('a-tab-panel')
]).then(() => {
  /* call methods here */
});

Events

NameDescriptionEvent Detail
a-tab-showEmitted when a tab is shown (not in the initial render).{tabId: string}
a-tab-hideEmitted when a tab is hidden. It is also emitted if the user closes a closable tab which is currently selected.{tabId: string}
a-tab-closeEmitted when a tab is closed by the user (if the tab is closable).{tabId: string}

Changelog

For API updates and breaking changes, check the CHANGELOG.

License

The MIT License (MIT)

2.2.1

4 months ago

2.2.0

4 months ago

2.1.0

5 months ago

1.2.0

7 months ago

2.0.0

5 months ago

1.1.0

1 year ago

1.0.0

1 year ago