@ngheadlessui/tabs
v0.0.3
Published
Easily create accessible, fully customizable tab interfaces, with robust focus management and coming keyboard navigation support.
Downloads
4
Readme
ui-tabs
Easily create accessible, fully customizable tab interfaces, with robust focus management and coming keyboard navigation support.
Demo
Installation
# npm
npm install @ngheadlessui/tabs
# Yarn
yarn add @ngheadlessui/tabs
# import the lib into module you want to use it in
import { UiTabsModule } from '@ngheadlessui/tabs';
Basic Example
Tabs are built using the headless-tab-group, headless-tab, and headlessTabPanel components. By default the first tab is selected, and clicking on any tab will activate the corresponding panel.
<headless-tab-group>
<div>
<headless-tab>Tab 1</headless-tab>
<headless-tab>Tab 2</headless-tab>
<headless-tab>Tab 3</headless-tab>
</div>
<!-- You can use the headlessTabPanel as a component -->
<headlessTabPanel>Content 1</headlessTabPanel>
<headlessTabPanel>Content 2</headlessTabPanel>
<headlessTabPanel>Content 3</headlessTabPanel>
<!--or as a directive
<div headlessTabPanel>Content 1</div>
<div headlessTabPanel>Content 2</div>
<div headlessTabPanel>Content 3</div>
-->
</headless-tab-group>
Styling the selected tab
This is a headless component so there are no styles included by default. Instead, the components expose useful information via bound classes that you can use to apply styles yourself.
To style the selected Tab, use the selected class selected-headless-tab
, which tells you whether or not that tab is currently selected. For flexiblity, the clas unselected-headless-tab
is also available for use.
.selected-headless-tab {
@apply bg-white shadow;
}
.unselected-headless-tab {
@apply text-blue-100 hover:bg-white/[0.12] hover:text-white;
}
Disabling a tab
To disable a tab, use the disabled input property on the headlessTabPanel
component. Disabled tabs cannot be selected with the mouse.
<headless-tab-group>
<div>
<headless-tab [disabled]="true">Tab 1</headless-tab>
<headless-tab>Tab 2</headless-tab>
<headless-tab>Tab 3</headless-tab>
</div>
<headlessTabPanel>Content 1</headlessTabPanel>
<headlessTabPanel>Content 2</headlessTabPanel>
<headlessTabPanel>Content 3</headlessTabPanel>
</headless-tab-group>
<!-- If you are creating tabs from component data -->
<headless-tab-group>
<div>
<headless-tab
*ngFor="let tabName of ['Recent', 'Popular', 'Trending']"
[disabled]="tabName === 'Recent'"
>
{{ tabName }}
</headless-tab>
</div>
<headlessTabPanel>Content 1</headlessTabPanel>
<headlessTabPanel>Content 2</headlessTabPanel>
<headlessTabPanel>Content 3</headlessTabPanel>
</headless-tab-group>
To style disabled tabs, use the disabled class disabled-headless-tab
, which tells you whether or not that tab is currently disabled.
.disabled-headless-tab {
@apply text-red-300 bg-yellow-700 hover:bg-white/[0.12] hover:text-white;
}
Specifying the default tab
To change which tab is selected by default, use the defaultTabIndex="number" property on the TabGroup component.
<headless-tab-group [defaultTabIndex]="2">
<div>
<headless-tab>Tab 1</headless-tab>
<headless-tab>Tab 2</headless-tab>
<headless-tab>Tab 3</headless-tab>
</div>
<headlessTabPanel>Content 1</headlessTabPanel>
<headlessTabPanel>Content 2</headlessTabPanel>
<headlessTabPanel>Content 3</headlessTabPanel>
</headless-tab-group>
Listening for changes
To run a function whenever the selected tab changes, listen to emissions from the (changeTab) EventEmitter on the TabGroup component. (changeTab) emits the index ($event) of the active tab.
<headless-tab-group (changeTab)="listenToChangeInTabs($event)">
<div>
<headless-tab>Tab 1</headless-tab>
<headless-tab>Tab 2</headless-tab>
<headless-tab>Tab 3</headless-tab>
</div>
<headlessTabPanel>Content 1</headlessTabPanel>
<headlessTabPanel>Content 2</headlessTabPanel>
<headlessTabPanel>Content 3</headlessTabPanel>
</headless-tab-group>
Component APIs
headless-tab-group
The main TabGroup component.
| Props | Default | Description |
| ------------ | ------- | -------------------------------------------------------------------------------- |
| defaultIndex | 0 | Number
The default selected index |
| (changeTab) | | EventEmitter This is fired when there is a change in selected tab |
headless-tab
The Tab component.
| Props | Default | Description |
| -------- | ------- | ------------------------------------------------------------- |
| disabled | false | Boolean
Whether or not the Tab is currently disabled. |
headlessTabPanel
The Tab component.
| Props | Default | Description |
| ----- | --------- | ----------------------------------------------------- |
| id | undefined | String
unique identifier of the tab component |