Components Forms

Switch

Toggle an on/off setting with a native checkbox exposed as a switch.

Live previewGo + HTML

Usage

Use Checked for the initial state. Space changes the setting. Read checked or listen for the native change event to save a preference.

Register switch with Head and pass its properties to Render. Go setup guide

Go usage
Go usage
err := mono.UI.Render(w, "switch", mono.Props{
	"Checked": true,
	"Label":   "Show layout guides",
	"Name":    "layout_guides",
})
Rendered HTML
Rendered HTML
<label id="demo-switch" data-mono="switch" class="mu-row">
  <input role="switch" type="checkbox" name="layout_guides" class="mu-sr-only" checked >
  <span class="mu-switch-track" aria-hidden="true"></span><span class="mu-label">Show layout guides</span>
</label>
Go template
Go template
<label id="{{.ID}}" data-mono="switch" class="mu-row">
  <input role="switch" type="checkbox" name="{{or .Name .ID}}" class="mu-sr-only" {{if .Checked}}checked{{end}} {{if .Disabled}}disabled{{end}}>
  <span class="mu-switch-track" aria-hidden="true"></span><span class="mu-label">{{.Label}}</span>
</label>
Component CSS
Component CSS
[data-mono="switch"] { position: relative; cursor: pointer; }
[data-mono="switch"] .mu-switch-track { display: inline-flex; align-items: center; flex: none; width: 2.35rem; height: 1.35rem; padding: .13rem; border: 1px solid #777; border-radius: 2rem; background: #e9e9e9; box-sizing: border-box; }
[data-mono="switch"] .mu-switch-track::after { content: ""; width: .9rem; height: .9rem; border-radius: 50%; background: #333; }
[data-mono="switch"] input:checked + .mu-switch-track { background: #171717; border-color: #171717; justify-content: end; }
[data-mono="switch"] input:checked + .mu-switch-track::after { background: white; }
[data-mono="switch"] input:focus-visible + .mu-switch-track { outline: 2px solid currentColor; outline-offset: 3px; }
[data-mono="switch"] input:disabled ~ span { opacity: .5; }
@media (forced-colors: active) { [data-mono="switch"] .mu-switch-track::after { background: CanvasText; } [data-mono="switch"] input:checked + .mu-switch-track::after { background: Highlight; } }
Use the HTML in a static site

Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.

Shared CSS Component CSS

HTML head
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./switch.css">

Copy into your project

Run this command from the Mono checkout. Set -out to the component directory in your application.

Terminal
go run ./cmd/mono-ui add -out ./components switch
Example properties (JSON)
example.json
{
  "Checked": true,
  "Label": "Show layout guides",
  "Name": "layout_guides"
}