Toggle Group
A toggle group is used to toggle either one option or multiple options.
Features
Install
Install the component from your command line.
Anatomy
Import all parts and piece them together.
<script setup lang="ts">import * as toggle from '@destyler/toggle'import { normalizeProps, useMachine } from '@destyler/vue'import { computed, useId } from 'vue'
const [state, send] = useMachine(toggle.machine({ id: useId(),}))const api = computed(() => toggle.connect(state.value, send, normalizeProps))</script>
<template> <div v-bind="api.getRootProps()"> <button v-bind="api.getItemProps({ value: '1' })" > </button> <button v-bind="api.getItemProps({ value: '2' })" > </button> <button v-bind="api.getItemProps({ value: '3' })" > </button> </div></template>
import { normalizeProps, useMachine } from '@destyler/react'import * as toggle from '@destyler/toggle'import { useId } from 'react'
export default function TogglePage() { const [state, send] = useMachine(toggle.machine({ id: useId(), }))
const api = toggle.connect(state, send, normalizeProps)
return ( <div {...api.getRootProps()} > <button {...api.getItemProps({ value: '0' })}></button> <button {...api.getItemProps({ value: '1' })}></button> <button {...api.getItemProps({ value: '2' })}></button> </div> )}
<script lang="ts"> import * as toggle from "@destyler/toggle"; import { normalizeProps, useMachine } from "@destyler/svelte";
const id = $props.id(); const [state, send] = useMachine(toggle.machine({ id, }));
const api = $derived(toggle.connect(state, send, normalizeProps));</script>
<div {...api.getRootProps()} > <button {...api.getItemProps({ value: '0' })}></button> <button {...api.getItemProps({ value: '1' })}></button> <button {...api.getItemProps({ value: '2' })}></button></div>
import { normalizeProps, useMachine } from '@destyler/solid'import * as toggle from '@destyler/toggle'import { createMemo, createUniqueId } from 'solid-js'
export default function TogglePage() { const [state, send] = useMachine(toggle.machine({ id: createUniqueId(), }))
const api = createMemo(() => toggle.connect(state, send, normalizeProps))
return ( <div {...api().getRootProps()} > <button {...api().getItemProps({ value: '0' })} ></button> <button {...api().getItemProps({ value: '1' })} ></button> <button {...api().getItemProps({ value: '2' })} ></button> </div> )}
Change the orientation
By default, the toggle group is assumed to be horizontal.
To change the orientation to vertical,
set the orientation
property in the destyler’s context to vertical
.
const [state, send] = useMachine( toggle.machine({ orientation: "vertical", }),)
Listening for value changes
When the pressed toggle in the group changes, onValueChange
callback is invoked.
const [state, send] = useMachine( toggle.machine({ onValueChange(details) { // details => { value: string[] } console.log(details.value) }, }),)
Allowing multiple selection
Set the multiple
property in the destyler’s context to true
to allow multiple options to be toggled.
const [state, send] = useMachine( toggle.machine({ multiple: true, }),)
Disabling the toggle group
Set the disabled
property in the destyler’s context to true
to disable the toggle group.
const [state, send] = useMachine( toggle.machine({ disabled: true, }),)
Disabling a toggle
Set the disabled
property in the getToggleProps
function to true
to disable a toggle.
<!-- ... --><template> <div v-bind="api.getRootProps()"> <button v-bind="api.getItemProps({ value: '1', disabled: true })" > </button> <!-- ... --> </div></template>
export default function TogglePage() { // ... return ( <div {...api.getRootProps()} > <button {...api.getItemProps({ value: '0', disabled: true })}></button> // ... </div> )}
<!-- ... --><div {...api.getRootProps()} > <button {...api.getItemProps({ value: '0', disabled: true })}></button> <!-- ... --></div><!-- ... -->
export default function TogglePage() { // ... return ( <div {...api().getRootProps()} > <button {...api().getItemProps({ value: '0', disabled: true })} ></button> // ... </div> )}
Disabling focus loop
The toggle group uses roving focus management by default.
To disable this, set the rovingFocus
property in the destyler’s context to false
.
const [state, send] = useMachine( toggle.machine({ rovingFocus: false, }),)
Styling guide
Earlier, we mentioned that each Toggle Group part has a
data-part
attribute added to them to select and style them in the DOM.
Pressed State
The toggle is pressed, the data-state
attribute is applied to the toggle button with on
or off
values.
[data-part="toggle"][data-state="on"] { /* styles for toggle button */}
Focused State
When a toggle button is focused, the data-focus
is applied to the root and matching toggle button.
[data-part="root"][data-focus] { /* styles for the root */}
[data-part="toggle"][data-focus] { /* styles for the toggle */}
Disabled State
When a toggle button is disabled, the data-disabled
is applied to the root and matching toggle button.
[data-part="root"][data-disabled] { /* styles for the root */}
[data-part="toggle"][data-disabled] { /* styles for the toggle */}
Methods and Properties
Machine Context
The toggle machine exposes the following context properties:
Partial<{ root: string; item(value: string): string; }>
boolean
string[]
(details: ValueChangeDetails) => void
boolean
boolean
Orientation
boolean
"ltr" | "rtl"
string
() => ShadowRoot | Node | Document
Machine API
The toggle api
exposes the following methods:
string[]
(values: string[]) => void
(props: ItemProps) => ItemState
Data Attributes
Root
data-scope
data-part
data-disabled
data-orientation
data-focus
Item
data-scope
data-part
data-focus
data-disabled
data-orientation
data-state
Accessibility
Keyboard Interaction
Tab
Space
Enter
ArrowDown
ArrowRight
ArrowUp
ArrowLeft
Home
End