Toolbar
Displays frequently used actions or tools in a compact, easily accessible bar.
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 item) - Allows external control (e.g., toggling an item 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 theToolbar.Group
component. - Provide a
value
prop toToolbar.Group
, 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.
API Reference
The root component which contains the toolbar.
Property | Type | Description |
---|---|---|
loop | boolean | Whether or not the toolbar should loop when navigating. Default: true |
orientation | enum | The orientation of the toolbar. 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 component. |
data-toolbar-root | '' | Present on the root element. |
A button in the toolbar.
Property | Type | Description |
---|---|---|
disabled | boolean | Whether or not the button 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-toolbar-button | '' | Present on the button element. |
A link in the toolbar.
Property | Type | Description |
---|---|---|
ref $bindable | HTMLAnchorElement | 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-toolbar-link | '' | Present on the link element. |
A group of toggle items in the toolbar.
Property | Type | Description |
---|---|---|
type required | enum | The type of toggle group. Default: 'single' |
value $bindable | union | The value of the toggle group. If the type is multiple, this will be an array of strings, otherwise it will be a string. Default: undefined |
onValueChange | function | A callback function called when the value changes. Default: undefined |
controlledValue | boolean | Whether or not the Default: false |
disabled | boolean | Whether or not the switch is disabled. Default: false |
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-toolbar-group | '' | Present on the group element. |
A toggle item in the toolbar toggle group.
Property | Type | Description |
---|---|---|
value required | string | The value of the toolbar toggle group item. When the toolbar toggle group item is selected, toolbar the toggle group's value will be set to this value if in single mode, or this value will be pushed to the toggle group's array value if in multiple mode. Default: undefined |
disabled | boolean | Whether or not the item 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 | Whether the toolbar toggle item is in the on or off state. |
data-value | '' | The value of the toolbar toggle item. |
data-disabled | '' | Present when the toolbar toggle item is disabled. |
data-toolbar-item | '' | Present on the toolbar toggle item. |