Tabs
Organizes content into distinct sections, allowing users to switch between them.
Prague
06:05
3h 30m
Malaga
06:05
Malaga
07:25
3h 20m
Prague
10:45
Structure
Managing Value State
Bits UI offers several approaches to manage and synchronize the component's value state, catering to different levels of control and integration needs.
1. Two-Way Binding
For seamless state synchronization, use Svelte's bind:value
directive. This method automatically keeps your local state in sync with the component's internal state.
Key Benefits
- Simplifies state management
- Automatically updates
myValue
when the internal state changes (e.g., via clicking on an tab's trigger) - Allows external control (e.g., switching tabs via a separate button)
2. Change Handler
For more granular control or to perform additional logic on state changes, use the onValueChange
prop. This approach is useful when you need to execute custom logic alongside state updates.
Use Cases
- Implementing custom behaviors on value change
- Integrating with external state management solutions
- Triggering side effects (e.g., logging, data fetching)
3. Fully Controlled
For complete control over the component's value state, use the controlledValue
prop. This approach requires you to manually manage the value state, giving you full control over when and how the component responds to value change events.
To implement controlled state:
- Set the
controlledValue
prop totrue
on theTabs.Root
component. - Provide a
value
prop toTabs.Root
, which should be a variable holding the current state. - Implement an
onValueChange
handler to update the state when the internal state changes.
When to Use
- Implementing complex logic
- Coordinating multiple UI elements
- Debugging state-related issues
Note
While powerful, fully controlled state should be used judiciously as it increases complexity and can cause unexpected behaviors if not handled carefully.
For more in-depth information on controlled components and advanced state management techniques, refer to our Controlled State documentation.
Orientation
The orientation
prop is used to determine the orientation of the Tabs
component, which influences how keyboard navigation will work.
When the orientation
is set to 'horizontal'
, the ArrowLeft
and ArrowRight
keys will move the focus to the previous and next tab, respectively. When the orientation
is set to 'vertical'
, the ArrowUp
and ArrowDown
keys will move the focus to the previous and next tab, respectively.
Activation Mode
By default, the Tabs
component will automatically activate the tab associated with a trigger when that trigger is focused. This behavior can be disabled by setting the activationMode
prop to 'manual'
.
When set to 'manual'
, the user will need to activate the tab by pressing the trigger.
API Reference
The root tabs component which contains the other tab components.
Property | Type | Description |
---|---|---|
value $bindable | string | The active tab value. Default: undefined |
onValueChange | function | A callback function called when the active tab value changes. Default: undefined |
controlledValue | boolean | Whether or not the value is controlled or not. If Default: false |
activationMode | enum | How the activation of tabs should be handled. If set to Default: 'automatic' |
disabled | boolean | Whether or not the tabs are disabled. Default: false |
loop | boolean | Whether or not the tabs should loop when navigating with the keyboard. Default: true |
orientation | enum | The orientation of the tabs. Default: horizontal |
ref $bindable | HTMLDivElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-orientation | enum | The orientation of the tabs. |
data-tabs-root | '' | Present on the root element. |
The component containing the tab triggers.
Property | Type | Description |
---|---|---|
ref $bindable | HTMLDivElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-orientation | enum | The orientation of the tabs. |
data-tabs-list | '' | Present on the list element. |
The trigger for a tab.
Property | Type | Description |
---|---|---|
value required | string | The value of the tab this trigger represents. Default: undefined |
disabled | boolean | Whether or not the tab is disabled. Default: false |
ref $bindable | HTMLButtonElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-state | enum | The state of the tab trigger. |
data-value | '' | The value of the tab this trigger represents. |
data-orientation | enum | The orientation of the tabs. |
data-disabled | '' | Present when the tab trigger is disabled. |
data-tabs-trigger | '' | Present on the trigger elements. |
The panel containing the contents of a tab.
Property | Type | Description |
---|---|---|
value required | string | The value of the tab this content represents. Default: undefined |
ref $bindable | HTMLDivElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-tabs-content | '' | Present on the content elements. |