Markdown:
Components

ToggleGroup

A collection of toggle buttons that allows users to select one or multiple options.

Usage

Use standalone, or inside ToggleGroupField.

Svelte
<ToggleGroup {...props} />

Anatomy

The class attribute values represent part names. Other attributes represent their corresponding props.

Svelte
<span class="whole" {...rest} role aria-labelledby aria-describedby={ariaDescId} aria-errormessage={ariaErrMsgId}>
  {#each options as { value, text, ...attrs }}
    <button class="main" {...attrs} type="button" role aria-checked>
      {text} or {children}
    </button>
  {/each}
</span>

Props

NameTypeDefaultDescription
options*SvelteMap-Display text and corresponding values; each option may be a string or a ToggleOption with per-button attributes and text
childrenSnippet-Custom option content; receives value, text, and variant
valuesstring[]-Bindable selected values
multiplebooleantrueWhen true, toggle buttons behave like checkboxes; otherwise, they behave like radio buttons
eventsToggleGroupEvents-Functions that can filter which added or removed values are committed
ariaDescIdstring-Description element ID for aria-describedby
ariaErrMsgIdstring-Bindable error-message element ID for aria-errormessage
attachAttachment-Svelte attachment on each button
elementsHTMLButtonElement[]-Bindable button elements
stylingSVSClass-Styling override
variantSVSVariantNEUTRALVariant

* required. Other <span> attributes are passed via rest props; class is merged onto the group.

Styling

To learn more, see Styling.

Variant Management

Selected buttons will have the active variant applied. Others are not automatically switched.

Default Class Name

svs-toggle-group

Accessibility

Keyboard Interactions

KeyDescription
ArrowRight / ArrowDownWhen multiple is false, moves focus to the next enabled option and selects it
ArrowLeft / ArrowUpWhen multiple is false, moves focus to the previous enabled option and selects it
HomeWhen multiple is false, moves focus to the first enabled option and selects it
EndWhen multiple is false, moves focus to the last enabled option and selects it
Space / EnterActivates the focused button using native button behavior

When multiple is true, arrow-key roving focus is not applied; each enabled toggle remains a normal button in the tab order.

ARIA Roles and Attributes

The component applies these automatically:

  • Group (whole): role="group" when multiple is true, or role="radiogroup" when multiple is false; also aria-describedby, aria-invalid, and aria-errormessage from props or ToggleGroupField context.
  • Button (main): role="checkbox" when multiple is true, or role="radio" when multiple is false; also aria-checked and managed tabindex for single-select roving focus.

Each toggle button takes its accessible name from its option text or the children content. Name the standalone group with aria-label or aria-labelledby through rest props. ToggleGroupField with label also names the inner group through its ariaLabelId context.

Exports

Types

TypeScript
interface ToggleOption extends Omit<
  HTMLButtonAttributes,
  "class" | "type" | "role" | "aria-checked" | "aria-invalid" | "aria-errormessage" | "onclick"
> {
  text: string;
}
interface ToggleGroupEvents extends CollectionEvents<string> {}
interface ToggleGroupProps extends Omit<HTMLAttributes<HTMLSpanElement>, "children" | "role" | "aria-describedby" | "aria-invalid" | "aria-errormessage"> {
  options: SvelteMap<string, string | ToggleOption> | Map<string, string | ToggleOption>;
  children?: Snippet<[string, string, string]>; // Snippet<[value,text,variant]>
  values?: string[]; // bindable
  multiple?: boolean; // (true)
  events?: ToggleGroupEvents;
  ariaDescId?: string;
  ariaErrMsgId?: string; // bindable
  attach?: Attachment<HTMLButtonElement>;
  elements?: HTMLButtonElement[]; // bindable
  styling?: SVSClass;
  variant?: SVSVariant; // (VARIANT.NEUTRAL)
  // other span attributes are passed to the group <span> via ...rest; `class` is merged
  // aria-label / aria-labelledby name the group
}
type ToggleGroupReqdProps = "options";
type ToggleGroupBindProps = "values" | "elements";

Others

No additional exports are available.

Examples

The example code uses Tailwind CSS along with SVSClass.

Basic

With Interactive Variant

Preset for Demo
Enter variant as you like