Renders a button that toggles a state.
Use this component to switch a feature or setting on or off, for example to toggle dark mode or mute/unmute sound.
See also button/1
, button_link/1
, and disclosure_button/1
.
With a Phoenix.LiveView.JS
command:
<.toggle_button on_click={JS.push("toggle-mute")} pressed={@muted}>
Mute
</.toggle_button>
The button state is conveyed via the aria-pressed
attribute and the button
styling. The button text should not change depending on the state. You may
however include an icon that changes depending on the state.
A toggle button can be identified with an attribute selector for the
aria-pressed
attribute.
// any toggle button regardless of state
button[aria-pressed] {}
// unpressed toggle buttons button[aria-pressed=”false”] {}
// pressed toggle buttons button[aria-pressed=”true”] {}