noora
Sign in

Toggle

Switches a setting on or off with immediate effect.

States

<.toggle id="toggle-off" label="Slack reports" />
<.toggle id="toggle-on" label="Slack reports" checked />

Sizes

Large suits a setting that stands on its own, such as the main switch of a page.

<.toggle id="toggle-size-medium" label="Medium" checked />
<.toggle id="toggle-size-large" label="Large" size="large" checked />

With description

A second line explains what the setting does.

<.toggle
  id="toggle-description-default-branch"
  label="Default branch"
  description="Jobs on each repository's default branch save their changes."
  checked
/>
<.toggle
  id="toggle-description-pull-requests"
  label="Pull requests"
  description="Jobs on pull requests save their changes too."
/>

Without label

The control on its own, for rows that already show the setting's name.

<.toggle id="toggle-standalone-off" />
<.toggle id="toggle-standalone-on" checked />

Disabled

<.toggle id="toggle-disabled-off" label="Enforce Single Sign-On" disabled />
<.toggle id="toggle-disabled-on" label="Enforce Single Sign-On" checked disabled />

Guidelines

  • Use a toggle for a setting that applies as soon as it changes, such as enabling Slack reports. React to it with phx-click or with a data-on-checked-change event, which receives the new checked value.
  • Always pass an id . The toggle derives the id of its label from it and fails to render without one.
  • The toggle follows checked when the server changes it. Between changes it keeps the state people set in the browser.
  • Pass field or name to submit the toggle with a form. It submits true when on and false when off.
  • description only renders together with label . Without a label , place the setting's name next to the toggle yourself.
  • Name the setting in the label, such as Enable Single Sign-On, rather than describing the on and off states.

API reference

<.toggle>

Attribute Type Default Description
checked
boolean
false
Whether the toggle is checked.
description
string
nil
An optional description.
disabled
boolean
false
Whether the toggle is disabled.
field
Phoenix.HTML.FormField
nil
A Phoenix form field.
id
string
nil
The id of the toggle.
label
string
nil
The label of the toggle.
name
string
nil
The name of the toggle.
size
string
"medium"
The size of the toggle. "medium""large"
tabindex
integer
nil
Tabindex to add to the toggle control

Also accepts global attributes.