ToggleGroupField
A form control for selecting one or multiple options from a set of toggle buttons.
Usage
Use standalone, or wrap ToggleGroup to share state.
<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.
<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
| Name | Type | Default | Description |
|---|---|---|---|
options | SvelteMap | - | Display text and corresponding values; each option may be a string or a ToggleOption |
label | string | - | Field label |
extra | string | - | Extra text shown with the label |
aux | Snippet | - | Auxiliary content; receives values, variant, and input element |
left | Snippet | - | Content before the inner group; receives values, variant, and input element |
right | Snippet | - | Content after the inner group; receives values, variant, and input element |
bottom | string | - | Bottom message |
reserve | boolean | false | Renders the bottom element even without a message to prevent layout shift |
values | string[] | - | Bindable selected values |
multiple | boolean | true | When true, toggle buttons behave like checkboxes; otherwise, they behave like radio buttons |
validations | ToggleGroupFieldValidation[] | - | Validation functions used to validate input values |
constraints | ToggleGroupFieldConstraint[] | - | Constraint functions used to restrict selected values |
name | string | - | The name attribute for form submission |
elements | HTMLButtonElement[] | - | Bindable button elements |
styling | SVSClass | - | Styling override |
variant | SVSVariant | NEUTRAL | Bindable variant |
toggleGroup | ToggleGroupProps | - | Props for the default inner ToggleGroup when no children are supplied; ignored if a child control is provided |
children | Snippet | - | 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"andaria-labelledbypointing 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
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.