Actions

Toggle

A button that turns an option on or off.

Class nameTypeDescription
btnplf:btnComponentA Button with data-plf-toggle
btn-iconplf:btn-iconPartAn SVG icon in the toggle; an icon class there doesn't need it
btn-ghostplf:btn-ghostStyleNo border until pressed
btn-outlineplf:btn-outlineStyleOutline on the surface
btn-smplf:btn-smSizeSmall
btn-lgplf:btn-lgSizeLarge
btn-squareplf:btn-squareModifierSquare, for an icon-only toggle

aria-pressed tells assistive technologies the state, so keep the same label in both states.

With icons

An icon-only toggle needs an aria-label.

JavaScript

Toggle needs JavaScript to change its state. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-toggle Marks a <button> as a toggle
aria-pressed The state: "true" starts pressed; the script adds "false" when it is left out, and updates it on change
disabled Blocks clicks and method calls
Method Behavior
PLF.Toggle.getOrCreateInstance(button) Returns the instance, creating it for valid markup
PLF.Toggle.getInstance(button) Returns the instance or null
instance.setPressed(pressed) Sets the state from a boolean; returns false when unchanged, canceled or disabled
instance.toggle() Switches the state; returns like setPressed
instance.destroy() Removes the listener; the state stays
Event When
plf:toggle:before-change Before the state changes; cancelable
plf:toggle:change After aria-pressed is updated

Each event’s detail:

Property Value
instance The Toggle instance
reason api or trigger
trigger The toggle, or null from a method
previousPressed The state before: true when pressed
pressed The new state: true when pressed

Toggle only changes its state: apply the effect, such as formatting, in your plf:toggle:change handler. It submits no form value.

Key Action
Enter, Space Toggles the focused button
Tab, Shift+Tab Move between toggles

React

Toggle from @pelatform/react renders this button with aria-pressed, which a click switches; it keeps its own state, or the application sets it with pressed.

Prop Values Default
variant ghost, outline ghost
size sm, lg The default size
shape square, for an icon-only toggle with an aria-label None
pressed true or false, which the application changes in onPressedChange Its own state
defaultPressed true starts pressed when the application does not set pressed false
onPressedChange (pressed), called when a click presses or releases the toggle None

The other props are a native button’s, className and ref included. It is type="button", so it never submits a form, and inside a ButtonGroup the toggle is joined.

CSS variables

The pressed state reads these; everything else is Button’s.

Variable Default
--plf-toggle-pressed-bg --plf-color-primary-subtle
--plf-toggle-pressed-color --plf-color-primary-text
--plf-toggle-pressed-border --plf-color-primary-border

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons