# Toggle Group Primitive

Toggle Group is a Starwind Runtime primitive in the controlled-value-group contract family.
Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.
## Anatomy
### Astro
Use the Astro primitive adapter to render ToggleGroup anatomy with the Runtime wiring included.
```astro
---
import { ToggleGroup } from "@starwind-ui/astro/toggle-group";
---

<ToggleGroup.Root />
```

### React
Use the React primitive adapter when ToggleGroup state participates in React rendering.
```tsx
import { ToggleGroup } from "@starwind-ui/react/toggle-group";

export function Example() {
  return (
    <ToggleGroup.Root />
  );
}
```

### HTML
Render the ToggleGroup data-sw-* contract yourself, then initialize createToggleGroup.
```html
<div data-sw-toggle-group role="group"></div>

<script type="module">
  import { createToggleGroup } from "@starwind-ui/runtime/toggle-group";

  const root = document.querySelector("[data-sw-toggle-group]");
  if (root) {
    createToggleGroup(root);
  }
</script>
```

### Vue
Use the Vue 3.5 beta adapter to render ToggleGroup anatomy.
```vue
<script setup lang="ts">
import ToggleGroup from "@starwind-ui/vue/toggle-group";
</script>

<template>
  <ToggleGroup.Root />
</template>
```
## API Reference
### Root
The main element that owns the Toggle Group Runtime instance.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-toggle-group` |
| Role | `group` |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| value | `ToggleGroupValue` | - | control | Controls the current Toggle Group value. | **React:** Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.<br>**Astro:** Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.<br>**Runtime / HTML:** Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates. |
| defaultValue | `ToggleGroupValue` | - | control | Sets the initial Toggle Group value for uncontrolled usage. | **React:** Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.<br>**Astro:** Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.<br>**Runtime / HTML:** Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates. |
| disabled | `boolean` | false | option | Disables the Root part. | - |
| loopFocus | `boolean` | true | option | Lets keyboard focus wrap around the Toggle Group items. | - |
| multiple | `boolean` | false | option | Allows multiple Toggle Group values to be selected. | - |
| orientation | `"horizontal" \| "vertical"` | "horizontal" | option | Sets the Toggle Group orientation. | - |
| onValueChange | `(value: ToggleGroupValue, details: ToggleGroupValueChangeDetails) => void` | - | callback | Runs when the Toggle Group value changes. | **React:** Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.<br>**Astro:** Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.<br>**Runtime / HTML:** Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates. |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-toggle-group` | runtime | - | Marks the Root part so Starwind Runtime can find it. |
| `data-default-value` | prop | - | Reflects the default value prop on the Root part. |
| `data-disabled` | prop | - | Reflects the disabled prop on the Root part. |
| `data-loop-focus` | prop | - | Reflects the loop focus prop on the Root part. |
| `data-multiple` | prop | - | Reflects the multiple prop on the Root part. |
| `data-orientation` | prop | - | Reflects the orientation prop on the Root part. |
| `data-value` | state | - | Reflects the value state on the Root part. |
#### State
| State | Value Type | Controlled Prop | Default Prop | Initial Attribute | Runtime Getter | Runtime Setter | Description | State Control Support |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| value | `ToggleGroupValue` | value | defaultValue | `data-default-value` | `getValue` | `setValue` | Tracks the current Toggle Group value. | **React:** Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.<br>**Astro:** Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.<br>**Runtime / HTML:** Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates. |
#### Events
| Event | Callback | DOM Event | Value | Details | Timing | Cancelable | Description | Cancellation Sequence |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| valueChange | onValueChange | starwind:value-change | value: `ToggleGroupValue` | ToggleGroupValueChangeDetails | before-state-commit | Yes | Fires when the value changes for Toggle Group. | 1. Check internal eligibility and intent.<br>2. Create one details object for the proposal.<br>3. Call the Runtime callback with the details object when the controller exposes one.<br>4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.<br>5. Read details.isCanceled, including cancellation caused by preventDefault().<br>6. Apply the accepted state.<br>7. Notify Runtime subscribers and other accepted-only observers. |
#### Runtime Setters
| Method | Target | Options | Suppresses Emit | Description |
| --- | --- | --- | --- | --- |
| `setValue` | state: value | emit: false | Yes | Updates the current Toggle Group value from Runtime code. |
| `setDisabled` | prop: disabled | - | No | Updates whether Toggle Group is disabled from Runtime code. |
| `setLoopFocus` | prop: loopFocus | - | No | Updates Toggle Group keyboard focus wrapping from Runtime code. |
| `setMultiple` | prop: multiple | - | No | Updates whether Toggle Group allows multiple values from Runtime code. |
| `setOrientation` | prop: orientation | - | No | Updates the Toggle Group orientation from Runtime code. |
#### Refs
| Part | Public |
| --- | --- |
| root | Yes |
#### Context
| Name | Direction | Values |
| --- | --- | --- |
| toggle-group | provides | disabled, loopFocus, multiple, orientation, value |
#### Initial Markup
| Attributes | Reason |
| --- | --- |
| `data-sw-toggle-group`, `role`, `data-default-value`, `data-disabled`, `data-loop-focus`, `data-multiple`, `data-orientation`, `data-value` | The value group needs initial selection, orientation, and group state for styling before hydration. |
## Runtime API
| Fact | Value |
| --- | --- |
| Factory | [`createToggleGroup`](/docs/runtime/#create-toggle-group) |
| Import | `@starwind-ui/runtime/toggle-group` |
| Root part | root |
| Option props | defaultValue, disabled, loopFocus, multiple, orientation, value |
| Option lifecycles | - |
## Related Styled Components
| Component | Relationship |
| --- | --- |
| [Toggle Group](/docs/components/toggle-group/) | Composite |
## Exports
| Group | Import | Exports |
| --- | --- | --- |
| Runtime | `@starwind-ui/runtime/toggle-group` | `createToggleGroup` |
| Astro Primitive | `@starwind-ui/astro/toggle-group` | `ToggleGroup`, `ToggleGroupRoot` |
| React Primitive | `@starwind-ui/react/toggle-group` | `ToggleGroup`, `ToggleGroupRoot` |
## Canonical Names
| Kind | Name |
| --- | --- |
| namespace | `ToggleGroup` |
| runtime-factory | `createToggleGroup` |
| part | `ToggleGroup.Root` |
## Changelog
### v1.0.0
- Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.
### v0.1.2
- Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
- Kept generated React state-change callbacks and DOM events cancelable until the Runtime accepts the proposed state, then synchronized rendered state.
### v0.1.1
- Included the generated React context provider in vendored Toggle Group output so items receive current group state.
### v0.1.0
- Introduced single and multiple grouped toggle state with coordinated keyboard navigation.