Toggle button

A button that stays pressed or not pressed, for a yes/no choice that reads as an action: follow, pin, show a layer or turn on a filter. Its value is a boolean.

  • vig-toggle-button
Import
import { ToggleButton } from '@vigdx/ui/toggle-button';

Basic

Bind [(value)] to a boolean signal. onLabel is shown while pressed and offLabel while not pressed; the pressed style uses the primary colour.

Following: false · Subscribed: true

Single label

Without offLabel the button keeps the onLabel text in both states and only its style and aria-pressed change. This suits filter chips and layer switches.

Accessible name

When the visible text is too short to make sense on its own, ariaLabel sets the accessible name. It is also the hook for translating that name.

Disabled

disabled keeps the current state visible but removes the button from the tab order; use hint to say why it cannot be changed.

Only owners can change this.

Reactive Forms

Import VigValueAccessor next to the component to use formControlName , [formControl] or ngModel .

{
  "drafts": true,
  "archived": false
}

Signal Forms

vig-toggle-button implements FormValueControl<boolean> , so [formField] binds each button to a boolean field of the model.

Visible layers: traffic

Styling hooks

Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="toggle-button"][data-part="control"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.

Scope: [data-scope="toggle-button"]

ToggleButton styling hooks
Part Element data-state
control The <button> ( aria-pressed ). on, off
hint The vig-hint (a display: contents host; style its text part). —
disabled-reason The vig-hint with the disabled reason ( display: contents ). —
error The vig-field-error (a display: contents host; style its message part). —

Accessibility

  • A native <button> with aria-pressed, following the APG toggle button pattern; Enter and Space toggle it.
  • With a single label (or a fixed ariaLabel) the name stays constant and aria-pressed carries the state. When onLabel and offLabel differ, the changing label is the state, so aria-pressed is left off rather than contradicting it.
  • Wrap related buttons in an element with role="group" and an aria-label so the set has a name.
  • Hint, disabled reason and error are linked through aria-describedby; a touched, invalid button sets aria-invalid="true". readonly keeps the button focusable but inert with aria-disabled="true" (aria-readonly is not valid on buttons).

API

ToggleButton

vig-toggle-button component

A pressed-state button for a boolean choice that reads as an action (PrimeNG p-togglebutton territory). With one label (or an ariaLabel) the name stays fixed and aria-pressed carries the state; when onLabel/offLabel differ the changing label is the state, so aria-pressed is left off (announcing both would contradict). Readonly keeps the button focusable but inert (aria-disabled).

ToggleButton props
Name Type Default Description
ariaLabelstring | undefinedundefined
—
dirtybooleanfalse
—
disabledbooleanfalse
—
disabledReasonsreadonly WithOptionalFieldTree<DisabledReason>[][]
—
errorMessagesRecord<string, string>—
—
errorsreadonly ValidationError.WithOptionalFieldTree[][]
—
hiddenbooleanfalse
—
hintstring | undefinedundefined
—
invalidbooleanfalse
—
namestring''
—
offLabelstring | undefinedundefined
—
onLabel required string—

Label while pressed; also the fallback when offLabel is not set.

pendingbooleanfalse
—
readonlybooleanfalse
—
requiredbooleanfalse
—
touchedbooleanfalse
—
value model booleanfalse
—

VigValueAccessor

directive

Bridges the vigdx form fields to Reactive Forms (formControl, formControlName) and template-driven forms (ngModel). Import it next to the component; Signal Forms ([formField]) need no accessor.

VigValueAccessor methods
Signature Description
registerOnChange(fn: (value: unknown) => void): void
—
registerOnTouched(fn: () => void): void
—
setDisabledState(disabled: boolean): void
—
writeValue(value: unknown): void
—

Are you sure?