Skip to Content
Onevra

Tabs

Switches between views, as an underline, a segmented control or a highlight.

Loading the live example…

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

type tabVariants struct {
	underline, segmented, highlight int
}

func (s *tabVariants) View(c *ui.Context) {
	labels := []string{"General", "Appearance", "Advanced"}
	ui.Column(c).Gap(20).AlignItems(ui.Center).Children(func() {
		components.Tabs(c, &s.underline, components.TabsUnderline, labels...)
		components.Tabs(c, &s.segmented, components.TabsSegmented, labels...)
		components.Tabs(c, &s.highlight, components.TabsHighlight, labels...)
	})
}
Source

components.Tabs binds the selected index to an *int. TabsUnderline suits page sections, TabsSegmented replaces a segmented control, and TabsHighlight slides a soft block behind the chosen label.

Slots and menus

TabsWith takes options: Leading and Trailing build content beside each label, such as an icon or a count, and Menu gives a tab a context menu.

Loading the live example…

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

type mailboxTabs struct {
	selected int
}

func (s *mailboxTabs) View(c *ui.Context) {
	icons := []func() *ui.SVG{lucide.Inbox, lucide.Star, lucide.Archive}
	counts := []string{"12", "3", ""}
	components.TabsWith(c, &s.selected, components.TabsOptions{
		Variant: components.TabsSegmented,
		Leading: func(c *ui.Context, i int, size float32) {
			components.Icon(c, icons[i]).Size(size, size).Shrink(0)
		},
		Trailing: func(c *ui.Context, i int, _ float32) {
			if counts[i] != "" {
				components.Text(c, counts[i]).TextColor(c.Theme().TextMuted)
			}
		},
		Menu: func(i int) func(m *ui.Menu) {
			return func(m *ui.Menu) {
				if m.Item("Open").Chosen() {
					s.selected = i
				}
			}
		},
	}, "Inbox", "Starred", "Archive")
}
Source

API

Tabs

func Tabs(c *ui.Context, selected *int, variant TabsVariant, labels ...string) ui.TabsParts

Tabs creates a row of tabs showing labels, of which *selected is the index of the one chosen. A click chooses a tab, as do the arrows, Home and End while one has the keyboard focus, which follows the choice. Reduced Motion skips transitions. List's Changed reports a new choice.

TabsUnderline animates a shared accent underline to the chosen tab and keeps every label's weight, so selection never moves the labels. TabsSegmented raises the chosen tab on a shadowed thumb over a capsule track and shows its label in a heavier weight, like a macOS segmented control. TabsHighlight slides a rounded block behind the chosen tab's label.

pkg.go.dev

TabsVariant

type TabsVariant int

TabsVariant selects the look of a tab list.

const (
	// TabsUnderline marks the chosen tab with a shared accent underline that
	// animates to it. This is the default.
	TabsUnderline TabsVariant = iota
	// TabsSegmented draws the tabs as the segments of a rounded track, with
	// the chosen one raised on a shadowed, lighter thumb, as macOS draws a
	// segmented control.
	TabsSegmented
	// TabsHighlight sets the chosen tab on a softly filled, rounded block that
	// slides behind the labels, without a track.
	TabsHighlight
)
pkg.go.dev

TabsWith

func TabsWith(c *ui.Context, selected *int, options TabsOptions, labels ...string) ui.TabsParts

TabsWith is Tabs configured by options.

pkg.go.dev

TabsOptions

TabsOptions configures TabsWith.

Prop

Type

pkg.go.dev

TabsWithLeading

func TabsWithLeading(c *ui.Context, selected *int, variant TabsVariant, leading func(c *ui.Context, i int, size float32), labels ...string) ui.TabsParts

TabsWithLeading is Tabs with an application-built leading slot before each tab's label, for example an icon. The builder receives the tab's index and the theme-scaled icon size as a suggestion, and inherits the tab's text color; a nil builder leaves no leading space. Keep it non-interactive and free of I/O or background work.

pkg.go.dev
Edit this page on GitHub

On this page