Toggle
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/toggle.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
)
func init() {
registerSection("toggle", "general", func() core.Widget {
pin, star := kit.Toggle(demoText("Pin", "固定"), false).Variant(kit.ToggleGhost).Size(kit.ToggleSizeSmall), kit.Toggle(demoText("Star", "收藏"), true).Icon(kit.IconStar).Variant(kit.ToggleOutline).Size(kit.ToggleSizeLarge)
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Row().Gap(8).Child(pin.Render(cx), star.Render(cx))
}))
})
}
Buttons that remain pressed, such as "Bold" in the toolbar.
bold := kit.Toggle("Bold", false).Icon(kit.IconStar).OnChange(func(on bool) { … })
- Click, Space, Enter to switch;
Value()/SetValue(bool);SetDisabled.
Agent: Role toggle, selected indicates whether to press or not.
Verify: go run ./examples/components -section toggle, add -theme dark to check the dark theme.
All variants are borderless. ToggleGhost and ToggleOutline use a transparent background when not selected; ToggleDefault uses a Subtle fill. Selected toggles use Highlight, and keyboard/programmatic focus draws the inset focus ring without changing layout.
Size(...) accepts ToggleSizeXSmall, ToggleSizeSmall, ToggleSizeMedium, ToggleSizeLarge, and adjusts the height, font size, icon and horizontal blank synchronously. The heights are 24/28/32/40dp respectively, and the default is Medium. Switching styles during operation retains the focus and selection status and does not trigger OnChange. Illegal enumeration values are ignored.