Tabs
Tabs component allows users to organize content into multiple sections which allow users to navigate between them. Each content under the tabs component should be related to a coherent unit.
install | version | usage | style |
---|---|---|---|
yarn add @ciceksepeti/cui-tabs | 1.0.0 | import {Tabs, TabList, Tab, TabPanelList, TabPanel} from '@ciceksepeti/cui-tabs' | import '@ciceksepeti/cui-tabs/style.css' |
Content​
The Tabs component consists of clickable tabs, that are aligned side by side. Tabs make it easy to explore and switch between different views. Tabs organize and allow navigation between groups of content that are related and at the same level of hierarchy.
Examples​
Default​
Example of a default behavior of Tabs component. Renders as 'div' by default
import {
Tabs,
TabList,
Tab,
TabPanelList,
TabPanel,
} from '@ciceksepeti/cui-tabs';
import '@ciceksepeti/cui-tabs/styles.css';
const Example = () => {
return (
<Tabs>
<TabList>
<Tab>Tab1</Tab>
<Tab disabled>Tab2</Tab>
<Tab disabled>Tab3</Tab>
<Tab>Tab4</Tab>
<Tab disabled>Tab5</Tab>
</TabList>
<TabPanelList>
<TabPanel>Panel1</TabPanel>
<TabPanel>Panel2</TabPanel>
<TabPanel>Panel3</TabPanel>
<TabPanel>Panel4</TabPanel>
<TabPanel>Panel5</TabPanel>
</TabPanelList>
</Tabs>
);
};
- Horizontal
- Horizontal - Manual
- Vertical
- Vertical - Manual
{' '}
- Choose any tab button by hitting keyboard tab or clicking over to select tab widget
- Switch between tabs through Left or Right Arrow Keys
- Hit Enter or Spacebar to activate related tab panel
- Choose any tab button by hitting keyboard tab or clicking over to select tab widget
- Switch between tabs through Up or Down Arrow Keys
- Hit Enter or Spacebar to activate related tab panel
Controlled​
caution
However, be sure that you have removed defaultIndex prop while you are using controlled component.
import React, { useState } from 'react';
import {
Tabs,
TabList,
Tab,
TabPanelList,
TabPanel,
} from '@ciceksepeti/cui-tabs';
import '@ciceksepeti/cui-tabs/styles.css';
const Example = () => {
const [key, setKey] = useState(0);
return (
<Tabs index={key} onChange={(k) => setKey(k)}>
<TabList>
<Tab>Tab1</Tab>
<Tab disabled>Tab2</Tab>
<Tab disabled>Tab3</Tab>
<Tab>Tab4</Tab>
<Tab disabled>Tab5</Tab>
</TabList>
<TabPanelList>
<TabPanel>Panel1</TabPanel>
<TabPanel>Panel2</TabPanel>
<TabPanel>Panel3</TabPanel>
<TabPanel>Panel4</TabPanel>
<TabPanel>Panel5</TabPanel>
</TabPanelList>
</Tabs>
);
};
- Controlled - Horizontal
- Controlled - Vertical
Render Props​
Example of a default behavior of Tabs component with render props pattern. Renders as 'div' by default.
import {
Tabs,
TabList,
Tab,
TabPanelList,
TabPanel,
} from '@ciceksepeti/cui-tabs';
import '@ciceksepeti/cui-tabs/styles.css';
const Example = () => {
return (
<Tabs>
{({ selectedTabIndex, focusedTabIndex }) => {
return (
<>
<TabList>
<Tab>Tab1</Tab>
<Tab disabled>
{focusedTabIndex === 0 ? 'focus on left' : 'focus on right'}
</Tab>
<Tab>{selectedTabIndex === 2 ? 'selected' : 'not selected'}</Tab>
</TabList>
<TabPanelList>
<TabPanel>Panel1</TabPanel>
<TabPanel>Panel2</TabPanel>
<TabPanel>Panel3</TabPanel>
</TabPanelList>
</>
);
}}
</Tabs>
);
};
- Horizontal
- Vertical
Props​
Tabs Props​
Name | Type | Default | Required | Description |
as | string | div | - | Changes html tag of tabs component which renders to DOM |
defaultIndex | number | 0 | - | Default selected tab |
orientation | horizontal | vertical | horizontal | - | Orientation of tabs |
activationType | auto | manual | auto | - | Activation type of tabs |
index | number | - | - | Allows a user to turn uncontrolled Tabs component into a controlled component |
children | React.ReactNode | - | required | The element which shows Tabs option. Tabs accept TabList, Tab, TabPanelList and TabPanel as children. |
Tabs Events​
Name | Type | Description |
onChange | (index,id) => void | Gets called when tab changes |
TabList Props​
Name | Type | Default | Required | Description |
children | React.ReactNode | - | required | The element which shows TabList option. TabList accepts Tab as children. |
TabList Events​
Name | Type | Description |
onKeyDown | () => void | Gets called when accessibility keyboard actions are used. |
Keyboard Actions​
Name | Type | Description |
Enter, Spacebar | Enter | Spacebar | A tabs widget where users activate a tab and display its panel by pressing Space or Enter when activationType setteled as 'manual'. |
ArrowRight | ArrowRight | Moves focus to the next tab, If focus is on the last tab, moves focus to the first tab when orientation is horizontal. |
ArrowLeft | ArrowLeft | Moves focus to the previous tab, If focus is on the first tab, moves focus to the last tab when orientation is horizontal. |
ArrowUp | ArrowUp | Moves focus to the previous tab, If focus is on the first tab, moves focus to the last tab when orientation is vertical. |
ArrowDown | ArrowDown | Moves focus to the next tab, If focus is on the last tab, moves focus to the first tab when orientation is vertical. |
Home | Home | Moves focus to the first tab. |
End | Home | Moves focus to the last tab. |
Tab Props​
Name | Type | Default | Required | Description |
disabled | boolean | false | - | Switches active state of tab |
children | React.ReactNode | - | required | Element shows content of Tab |
TabPanelList Props​
Name | Type | Default | Required | Description |
children | React.ReactNode | - | required | The element which shows TabPanelList option.TabPanelList accepts TabPanel as children. |
TabPanel Props​
Name | Type | Default | Required | Description |
children | React.ReactNode | - | required | Element shows content of TabPanel |
TabPanel Accessibility​
info
Name | Type | Default | Description |
aria-labelledby | string | - | Refers to the ( tab ) element that controls the panel and provides an accessible name for the tab panel. |
Styling​
Element Selectors​
tip
Name | Type |
[data-cui-tabs][data-orientation='vertical'] | Element Selector |
[data-cui-tab-list] | Element Selector |
[data-cui-tab-list][aria-orientation='vertical'] | Element Selector |
[data-cui-tab] | Element Selector |
[data-cui-tab][aria-selected='true'] | Element Selector |
[data-cui-tab][aria-selected='true'][data-orientation='vertical'] | Element Selector |
[data-cui-tab][aria-disabled='true'] | Element Selector |
Default Selector Values​
[data-cui-tabs][data-orientation='vertical'] {
display: flex;
}
[data-cui-tab-list] {
display: flex;
background-color: #cce5ff;
}
[data-cui-tab-list][aria-orientation='vertical'] {
flex-direction: column;
}
[data-cui-tab] {
border: none;
cursor: pointer;
padding: 5px 10px;
border: 1px solid transparent;
background-color: transparent;
}
[data-cui-tab][aria-selected='true'] {
color: #004085;
border-bottom: 1px solid currentColor;
}
[data-cui-tab][aria-selected='true'][data-orientation='vertical'] {
border-bottom: none;
border-right: 1px solid currentColor;
}
[data-cui-tab][aria-disabled='true'] {
opacity: 0.4;
cursor: default;
}