Markdown:
Components

ToggleGroupField

A form control for selecting one or multiple options from a set of toggle buttons.

Toggle Group Field
Select your options

Usage

Use standalone, or wrap ToggleGroup to share state.

Svelte
<ToggleGroupField {...props} />

<ToggleGroupField {...props}>
  <ToggleGroup {...props} />
</ToggleGroupField>

Anatomy

The class attribute values represent part names, and conditional indicates elements that are rendered conditionally. Other attributes represent their corresponding props.

Svelte
<div class="whole">
  <div class="top" conditional>
    <label class="label" conditional>
      {label}
      <span class="extra" conditional>{extra}</span>
    </label>
    <span class="aux" conditional>{aux}</span> Snippet args: [values, variant, element]
  </div>
  <div class="middle">
    <span class="left" conditional>{left}</span>
    <input style="display:none" aria-hidden="true" oninvalid />
    {#if name}
      {#each values as value}
        <input type="hidden" {name} {value} />
      {/each}
    {/if}
    {#if children}
      {@render children()}
    {:else if options}
      <ToggleGroup {options} {multiple} {...toggleGroup} />
    {/if}
    <span class="right" conditional>{right}</span>
  </div>
  <div class="bottom" conditional: has text, or always when reserve>{bottom}</div>
</div>

aux, left, and right snippets receive [values, variant, element]. When a declarative child is supplied, the field's multiple and toggleGroup props are ignored.

Props

NameTypeDefaultDescription
optionsSvelteMap-Display text and corresponding values; each option may be a string or a ToggleOption
labelstring-Field label
extrastring-Extra text shown with the label
auxSnippet-Auxiliary content; receives values, variant, and input element
leftSnippet-Content before the inner group; receives values, variant, and input element
rightSnippet-Content after the inner group; receives values, variant, and input element
bottomstring-Bottom message
reservebooleanfalseRenders the bottom element even without a message to prevent layout shift
valuesstring[]-Bindable selected values
multiplebooleantrueWhen true, toggle buttons behave like checkboxes; otherwise, they behave like radio buttons
validationsToggleGroupFieldValidation[]-Validation functions used to validate input values
constraintsToggleGroupFieldConstraint[]-Constraint functions used to restrict selected values
namestring-The name attribute for form submission
elementsHTMLButtonElement[]-Bindable button elements
stylingSVSClass-Styling override
variantSVSVariantNEUTRALBindable variant
toggleGroupToggleGroupProps-Props for the default inner ToggleGroup when no children are supplied; ignored if a child control is provided
childrenSnippet-Use the compound form to provide a child ToggleGroup that shares the field state

constraints run only on the add (click) path, so in single-select mode they gate each newly selected value but not removals or the final set. To forbid a specific value outright, mark that option disabled; to validate the resulting selection, use validations.

Styling

To learn more, see Styling.

Variant Management

In the initial state or when there is no input value, neutral is applied. If constraints do not allow the input, inactive is applied. After a value is entered, the variant switches to active if validation succeeds, or remains neutral temporarily if validation fails. During form validation, inactive is applied when validation fails. If the variant prop is specified, the specified value is used instead of neutral.

Default Class Name

svs-toggle-group-field

Accessibility

ARIA Roles and Attributes

The component applies these automatically:

  • Field (whole <div>): role="group" and aria-labelledby pointing to the label part when label text is present.
  • Validity proxy input: aria-hidden="true".

Provide label text for the field's accessible name; the component wires that label to the group instead of requiring an author-passed aria-label. Roving-focus keyboard behavior and toggle state ARIA for the default control are documented on ToggleGroup.

Exports

Types

TypeScript
type ToggleGroupFieldValidation = SVSFieldValidation<string[]>;
type ToggleGroupFieldConstraint = SVSFieldConstraint;
interface ToggleGroupFieldProps {
  options?: SvelteMap<string, string | ToggleOption> | Map<string, string | ToggleOption>;
  label?: string;
  extra?: string;
  aux?: Snippet<[string[], string, HTMLInputElement | undefined]>; // Snippet<[values,variant,element]>
  left?: Snippet<[string[], string, HTMLInputElement | undefined]>; // Snippet<[values,variant,element]>
  right?: Snippet<[string[], string, HTMLInputElement | undefined]>; // Snippet<[values,variant,element]>
  bottom?: string;
  reserve?: boolean; // (false)
  values?: string[]; // bindable
  multiple?: boolean; // (true)
  validations?: ToggleGroupFieldValidation[];
  constraints?: ToggleGroupFieldConstraint[];
  name?: string;
  elements?: HTMLButtonElement[]; // bindable
  styling?: SVSClass;
  variant?: SVSVariant; // bindable (VARIANT.NEUTRAL)
  toggleGroup?: Omit<
    ToggleGroupProps,
    ToggleGroupReqdProps | ToggleGroupBindProps | "ariaDescId" | "ariaErrMsgId" | "multiple" | "variant" | "events"
  >;
  children?: Snippet;
}
type ToggleGroupFieldReqdProps = never;
type ToggleGroupFieldBindProps = "values" | "variant" | "elements";

Others

No additional exports are available.

Examples

The example code uses Tailwind CSS along with SVSClass.

Basic

Toggle Group Field
Select your options

With Interactive Variant

Preset for Demo
Enter variant as you like
Favorite Fruits (optional)
Choose your favorites

values: 03