Skip to Content
Onevra

Switch

Turns a setting on and off at once, with a knob that lifts into glass while held.

Loading the live example…

switch.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type wirelessSettings struct {
	wifi, bluetooth bool
}

func (s *wirelessSettings) View(c *ui.Context) {
	ui.Column(c).Gap(c.Theme().Space(3)).Children(func() {
		components.Switch(c, &s.wifi, "Wi-Fi")
		components.Switch(c, &s.bluetooth, "Bluetooth")
	})
}
Source

A tap toggles the switch; holding or dragging the knob lifts it as a glass lens that follows the pointer along the track. Space and Enter toggle it from the keyboard.

Compact

Compact draws a smaller switch for dense rows such as table cells and toolbars. HideLabel hides the text while keeping it as the accessible name.

Loading the live example…

switch_compact.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type channelList struct {
	on []bool
}

func (l *channelList) View(c *ui.Context) {
	t := c.Theme()
	compact := components.SwitchOptions{Compact: true, HideLabel: true}
	ui.Column(c).Width(280).Gap(t.Space(1)).Children(func() {
		for i, name := range []string{"Email", "Push", "SMS"} {
			ui.Row(c).Key(name).FillWidth().AlignItems(ui.Center).Padding(t.Space(1.5), 0).Children(func() {
				components.Text(c, name).Grow(1)
				// HideLabel keeps the label as the switch's accessible name.
				components.Switch(c, &l.on[i], name, compact)
			})
		}
	})
}
Source

API

Switch

func Switch(c *ui.Context, on *bool, label string, options ...SwitchOptions) ui.Element

Switch creates a switch toggling *on, with a label. The label is also the accessible name.

A tap, Space or Enter toggles it, sliding the knob across, as does a click beside the knob. Holding or dragging the knob lifts it as a glass lens; dragging moves it along the track, and releasing settles on the nearer side. Letting go of a held knob without dragging it sets the lens down and leaves the switch as it was. After a drag, the lens sets down once the knob comes to rest.

pkg.go.dev

SwitchOptions

SwitchOptions controls the visible label independently of the accessible name.

Prop

Type

pkg.go.dev
Edit this page on GitHub

On this page