Skip to Content
Onevra

Button

Starts an action, in five weights, with icons, menus or a toggled state.

Loading the live example…

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

type buttonVariants struct {
	last string
}

func (b *buttonVariants) View(c *ui.Context) {
	ui.Column(c).Gap(16).AlignItems(ui.Center).Children(func() {
		ui.Row(c).Wrap().Gap(8).Justify(ui.Center).Children(func() {
			for _, v := range []struct {
				label   string
				variant components.ButtonVariant
			}{
				{"Primary", components.ButtonPrimary},
				{"Secondary", components.ButtonSecondary},
				{"Outline", components.ButtonOutline},
				{"Ghost", components.ButtonGhost},
				{"Link", components.ButtonLink},
			} {
				ui.Box(c).Key(v.label).Children(func() {
					if components.Button(c, v.label, v.variant).Clicked() {
						b.last = v.label
					}
				})
			}
			components.Button(c, "Disabled", components.ButtonPrimary).Disabled(true)
		})
		if b.last != "" {
			components.Text(c, "Clicked "+b.last).TextColor(c.Theme().TextMuted)
		}
	})
}
Source

components.Button takes a label and a variant, and reports a click with Clicked() in the frame it happens. Use one primary button for the main action of a view, and quieter variants for the rest.

With icons

Pass an empty label to give a button its own content, and keep its accessible name with Label.

Loading the live example…

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

type buttonIcons struct{}

func (buttonIcons) View(c *ui.Context) {
	t := c.Theme()
	// A button with an empty label takes any content as its children; Label
	// keeps its accessible name.
	iconButton := func(label string, icon *ui.SVG, variant components.ButtonVariant) {
		ui.Box(c).Key(label).Children(func() {
			components.Button(c, "", variant).Label(label).Gap(t.Space(1.5)).Children(func() {
				components.Icon(c, icon).Size(t.Rem(1), t.Rem(1))
				components.Text(c, label).SingleLine()
			})
		})
	}
	ui.Row(c).Wrap().Gap(8).Justify(ui.Center).Children(func() {
		iconButton("New", lucide.Plus(), components.ButtonPrimary)
		iconButton("Download", lucide.Download(), components.ButtonSecondary)
		iconButton("Share", lucide.Share(), components.ButtonOutline)
		iconButton("Edit", lucide.Pencil(), components.ButtonGhost)
	})
}
Source

Icon buttons

components.IconButton shows only an icon, with its label as a tooltip. IconButtonPlain suits rows and headers; IconButtonToolbar raises the face to match ButtonToolbar and segmented tabs.

Loading the live example…

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

type iconButtons struct {
	starred bool
}

func (b *iconButtons) View(c *ui.Context) {
	ui.Column(c).Gap(16).AlignItems(ui.Center).Children(func() {
		// Plain buttons tint a transparent face; hover one for its tooltip.
		ui.Row(c).Gap(4).Children(func() {
			components.IconButton(c, lucide.Copy(), "Copy", components.IconButtonPlain)
			star := lucide.Star()
			if b.starred {
				star = lucide.StarOff()
			}
			if components.IconButton(c, star, "Favorite", components.IconButtonPlain).Clicked() {
				b.starred = !b.starred
			}
			components.IconButton(c, lucide.RefreshCw(), "Refresh", components.IconButtonPlain)
			components.IconButton(c, lucide.Trash(), "Delete", components.IconButtonPlain).Disabled(true)
		})
		// Toolbar buttons raise their face, as ButtonToolbar does.
		ui.Row(c).Gap(8).Children(func() {
			components.Button(c, "Filter", components.ButtonToolbar)
			components.IconButton(c, lucide.Undo(), "Undo", components.IconButtonToolbar)
			components.IconButton(c, lucide.Redo(), "Redo", components.IconButtonToolbar)
		})
	})
}
Source

Toggle buttons

components.ToggleButton turns a *bool on and off, filled with the accent while on, for formatting and view options.

Loading the live example…

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

type formatting struct {
	bold, italic, underline bool
}

func (f *formatting) View(c *ui.Context) {
	size := c.Theme().Space(4)
	ui.Row(c).Gap(8).Children(func() {
		components.ToggleButton(c, &f.bold, "Bold")
		// An empty label takes custom content; Label names it.
		components.ToggleButton(c, &f.italic, "").Label("Italic").Children(func() {
			components.Icon(c, lucide.Italic).Size(size, size)
		})
		components.ToggleButton(c, &f.underline, "").Label("Underline").Children(func() {
			components.Icon(c, lucide.Underline).Size(size, size)
		})
	})
}
Source

Menu opens a menu when the button is clicked; ContextMenu opens one on right-click. Ask each item whether it was Chosen().

Loading the live example…

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

type fileActions struct {
	last string
}

func (f *fileActions) View(c *ui.Context) {
	actions := func(m *ui.Menu) {
		for _, action := range []string{"Open", "Duplicate", "Rename"} {
			if m.Item(action).Chosen() {
				f.last = action
			}
		}
		m.Separator()
		if m.Item("Delete").Chosen() {
			f.last = "Delete"
		}
	}
	ui.Column(c).Gap(16).AlignItems(ui.Center).Children(func() {
		ui.Row(c).Gap(8).Children(func() {
			components.Button(c, "Actions", components.ButtonSecondary).Menu(actions)
			components.Button(c, "Right-click me", components.ButtonOutline).ContextMenu(actions)
		})
		text := "Choose an action"
		if f.last != "" {
			text = "Chose " + f.last
		}
		components.Text(c, text).TextColor(c.Theme().TextMuted)
	})
}
Source

API

Button

func Button(c *ui.Context, label string, variant ButtonVariant) ui.Element

Button creates a labeled button in the Onevra style with a single standard size. Ask Clicked whether it was clicked; give it other content with Children and an empty label. The label is also the accessible name. ButtonLink uses text height without padding or a background and supports RichText composition; its children must be text. For stable identity, key a parent container rather than this element: interaction styling is resolved during construction.

pkg.go.dev

ButtonVariant

type ButtonVariant int

ButtonVariant selects a button's visual weight.

const (
	// ButtonPrimary is the accent-filled main action.
	ButtonPrimary ButtonVariant = iota
	// ButtonSecondary is a bordered action with a subtle text-tinted fill.
	ButtonSecondary
	// ButtonOutline is the bordered quiet action.
	ButtonOutline
	// ButtonGhost is the borderless quiet action.
	ButtonGhost
	// ButtonLink is inline text with an underline on hover.
	ButtonLink
	// ButtonToolbar is a raised toolbar control whose capsule and shadow match
	// the chosen segment of a segmented tab list, with a hairline edge that
	// keeps it readable outside a track.
	ButtonToolbar
)
pkg.go.dev

IconButton

func IconButton(c *ui.Context, icon *ui.SVG, label string, style IconButtonStyle) ui.Element

IconButton creates an icon-only button with an accessible label and a tooltip. It uses one theme-scaled square size. Applications own the label and handle Clicked on the result; MyGo owns its pointer, keyboard and focus behavior.

IconButtonPlain keeps a transparent face and tints it on hover and press. IconButtonToolbar keeps the raised, shadowed face of ButtonToolbar, matching TabsSegmented's chosen segment. IconButtonCompact shrinks the plain face.

pkg.go.dev

IconButtonStyle

type IconButtonStyle int

IconButtonStyle selects an icon button's face.

const (
	// IconButtonPlain tints a transparent face on hover and press.
	IconButtonPlain IconButtonStyle = iota
	// IconButtonToolbar raises the face, shadowed, as the chosen segment of a
	// segmented tab list is raised.
	IconButtonToolbar
	// IconButtonCompact is IconButtonPlain at a smaller square, for dense
	// rows such as a SidebarSectionWithActions heading.
	IconButtonCompact
)
pkg.go.dev

ToggleButton

func ToggleButton(c *ui.Context, on *bool, label string) ui.Element

ToggleButton creates a button that toggles *on by click or focused Space. Changed reports user changes. The label is also the accessible name; for custom content, pass an empty label and use Label and Children. It uses one standard size, a quiet off face and an accent-filled on face. Key a parent container for stable identity, as styling is resolved here.

pkg.go.dev
Edit this page on GitHub

On this page