Fragiola

API: components

Toggle

A two-state button with aria-pressed, alone, in a ToggleGroup or in a Toolbar (the APG toggle button), in each framework.

A button the user presses on and off (WAI-ARIA APG button pattern, its toggle button): a native <button> with aria-pressed, or your own element (render) with the keys a native button has. Inside a ToggleGroup it is one of the group's values; inside a Toolbar it is one of the toolbar's items. Toggle is one component, not a namespace.

Track actions
Gallery

Behaviour

  • State: pressed (controlled) or defaultPressed; every press reports the new state with reason none. The accessible name never changes with the state: aria-pressed tells it.
  • Keys: Enter and Space press and release it, as a native button's click; a non-native Toggle (nativeButton={false}) clicks on Enter's key down and Space's key up, and Space does not scroll.
  • In a ToggleGroup: it is pressed while the group's value holds its value (required there); pressed and defaultPressed are ignored, onPressedChange still reports before the group's onValueChange.
  • In a composite (a ToggleGroup, a Toolbar): it takes the composite's roving tabindex, and the arrow keys reach it; a disabled Toggle is natively disabled and skipped.
  • Disabled: it ignores presses and keys; a disabled ToggleGroup, Toolbar or Toolbar.Group disables it too. It never submits a form (type="button").

Anatomy

partelementdata-primitives-partrole
Togglebuttontoggle-roota button with aria-pressed

Usage

import { Toggle } from "@fragiola/primitives-react/toggle";

<Toggle aria-label="Bold" defaultPressed onPressedChange={(pressed) => console.log(pressed)}>
    B
</Toggle>;

Props

It also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. Vue binds them as v-model:pressed and @pressed-change; Angular as [(pressed)] and (pressedChangeDetails) ({ value, details }); in a ToggleGroup only @pressed-change and (pressedChangeDetails) report (no update:pressed, no pressedChange). Vue and Angular take the click, pointer, key and focus listeners as native events (@click, (click)).

Toggle

proptypedefaultdescription
pressedbooleanwhether it is pressed, controlled; ignored in a ToggleGroup
defaultPressedbooleanfalsethe initial state, uncontrolled; ignored in a ToggleGroup
onPressedChange(pressed: boolean, details: { reason }) => voidevery press (reason none), before a group's onValueChange
valuestringits value in a ToggleGroup (required there)
disabledbooleanfalseignores presses and keys
nativeButtonbooleantruewhether the rendered element is a native <button>; false with a render that gives another element
onClick (react)MouseEventHandlera click, after the Toggle's own; not while disabled
onMouseDown (react)MouseEventHandlernot while disabled
onPointerDown (react)PointerEventHandlernot while disabled (its default is prevented)
onKeyDown (react)KeyboardEventHandlerruns before the non-native keys; not while disabled
onKeyUp (react)KeyboardEventHandlerruns before Space's click on a non-native Toggle; not while disabled
onFocus (react)FocusEventHandlerafter the composite took note of the focus

State and part attributes

caseattributes
nativetype="button", aria-pressed ("true" / "false"), data-pressed while pressed, disabled and data-disabled while disabled
not nativerole="button", aria-pressed, data-pressed, tabindex="0" (-1 while disabled), aria-disabled="true" and data-disabled while disabled
in a compositethe same, with the roving tabindex ("0" on the tab stop, "-1" elsewhere)

Keyboard

keyaction
Enterpresses or releases it (a click)
Spacepresses or releases it (a click); a non-native Toggle's key down does not scroll
Tabmoves on; a disabled Toggle is skipped. In a composite, Tab leaves the composite

Exports

entryexports
@fragiola/primitives-react/toggleChangeDetails, ChangeReason, RenderProp, Toggle, ToggleProps, ToggleState
@fragiola/primitives-vue/toggleChangeDetails, ChangeReason, RenderSlotProps, Toggle, ToggleEmits, ToggleProps, ToggleState
@fragiola/primitives-angular/toggleChangeDetails, ChangeReason, Toggle, ToggleState, ValueChangeEvent