Skip to Content
Onevra

Sidebar

Grouped navigation items with icons, section actions and trees.

Loading the live example…

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

type navigation struct {
	page string
}

func (n *navigation) View(c *ui.Context) {
	t := c.Theme()
	item := func(label string, icon *ui.SVG) {
		if components.SidebarItem(c, label, icon, n.page == label).Clicked() {
			n.page = label
		}
	}
	ui.Row(c).Fill().Children(func() {
		ui.Column(c).Width(220).FillHeight().Padding(t.Space(4), 0).Background(t.Surface).Children(func() {
			components.Sidebar(c).Children(func() {
				components.SidebarSection(c, "Library", func() {
					item("Home", lucide.House())
					item("Recent", lucide.Clock())
					item("Shared", lucide.Users())
				})
				components.SidebarSectionWithActions(c, "Projects", func(c *ui.Context) {
					components.IconButton(c, lucide.Plus(), "New project", components.IconButtonCompact)
				}, func() {
					item("Website", lucide.Globe())
					item("Desktop app", lucide.AppWindow())
				})
			})
		})
		ui.Column(c).Grow(1).FillHeight().Center().Children(func() {
			components.Text(c, n.page).FontSize(t.Rem(1.4)).Bold()
		})
	})
}
Source

components.Sidebar is a column of sections, each a muted heading over application-owned items. The application owns selection and handles Clicked(). It works on its own or inside the sidebar shell, which provides the window's resizable column.

Section actions

SidebarSectionWithActions ends a heading with controls, such as an add button; IconButtonCompact fits it without raising the heading.

File trees

Put a Tree in a section for an explorer whose folders expand in place.

Loading the live example…

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

type projectFile struct {
	name     string
	children []projectFile
}

var projectFiles = []projectFile{
	{"source", []projectFile{
		{"components", []projectFile{{"button.go", nil}, {"tree.go", nil}}},
		{"main.go", nil},
		{"go.mod", nil},
	}},
	{"docs", []projectFile{{"build.md", nil}}},
	{"README.md", nil},
}

type explorer struct {
	open     map[string]*bool // by folder name
	selected string
}

// folder returns whether a folder is expanded, as a pointer the tree updates.
func (e *explorer) folder(name string) *bool {
	if e.open[name] == nil {
		e.open[name] = new(bool)
	}
	return e.open[name]
}

func (e *explorer) View(c *ui.Context) {
	t := c.Theme()
	var files func([]projectFile)
	files = func(nodes []projectFile) {
		for _, node := range nodes {
			var open *bool
			var children func()
			if node.children != nil {
				open = e.folder(node.name)
				children = func() { files(node.children) }
			}
			if components.TreeItem(c, node.name, open, e.selected == node.name, children).Clicked() {
				e.selected = node.name
			}
		}
	}
	ui.Row(c).Fill().Children(func() {
		ui.Column(c).Width(240).FillHeight().Padding(t.Space(4), 0).Background(t.Surface).Children(func() {
			components.Sidebar(c).Children(func() {
				components.SidebarSection(c, "Explorer", func() {
					components.Tree(c, func() { files(projectFiles) }).Label("Explorer")
				})
			})
		})
		ui.Column(c).Grow(1).FillHeight().Center().Children(func() {
			components.Text(c, e.selected).TextColor(t.TextMuted)
		})
	})
}
Source

Custom items

SidebarImageItem leads with an image, and SidebarItemWithSlots and SidebarItemWithContent take application-built leading, trailing and label content, such as monograms, counts or a busy indicator.

API

func Sidebar(c *ui.Context) ui.Element

Sidebar creates a transparent column of SidebarSections. Place it inside a ui.Scroll when scrolling is needed. Applications own its width, titlebar inset and selection; it can be used independently of Shell.

pkg.go.dev

SidebarSection

func SidebarSection(c *ui.Context, label string, fn func()) ui.Element

SidebarSection builds a muted heading followed by application-owned items. Sections are always expanded; fn may be nil for a heading alone.

pkg.go.dev

SidebarSectionWithActions

func SidebarSectionWithActions(c *ui.Context, label string, actions func(c *ui.Context), fn func()) ui.Element

SidebarSectionWithActions is a SidebarSection whose heading ends in application-built controls, such as an add button. IconButtonCompact fits the heading without raising it. A nil actions builder leaves the heading as SidebarSection's. Like the items, actions run on the UI thread and must not start I/O or background work.

pkg.go.dev

SidebarItem

func SidebarItem(c *ui.Context, label string, icon *ui.SVG, selected bool) ui.Element

SidebarItem creates a navigation button with a neutral selected face and an optional accent-colored icon. Hover does not change its face. The caller supplies selection and handles Clicked; MyGo provides Tab focus and activation with Enter or Space, not tree-style arrow navigation. Key can distinguish items with identical labels. A nil icon leaves no icon space.

pkg.go.dev

SidebarImageItem

func SidebarImageItem(c *ui.Context, label string, image ui.ImageSource, selected bool) ui.Element

SidebarImageItem is a SidebarItem led by an image, such as an application's project logo, in place of the icon. The image keeps its own colors, fits within the icon's square without cropping and has slightly rounded corners. A nil image leaves no icon space.

pkg.go.dev

SidebarItemWithLeading

func SidebarItemWithLeading(c *ui.Context, label string, selected bool, leading func(c *ui.Context, size float32)) ui.Element

SidebarItemWithLeading is a SidebarItem with an application-built leading slot, for example an image, monogram or status mark. A nil builder leaves no leading space. The builder receives the theme-scaled standard icon size as a suggestion and owns its content's size and appearance. Keep it non-interactive so the navigation button remains the only focus and activation target. It runs synchronously on the UI thread and must not start I/O or background work.

pkg.go.dev

SidebarItemWithSlots

func SidebarItemWithSlots(c *ui.Context, label string, selected bool, leading, trailing func(c *ui.Context, size float32)) ui.Element

SidebarItemWithSlots is a SidebarItemWithLeading with an application-built trailing slot after the label, for example counts or a status mark. The label then takes the remaining room and ends in an ellipsis when it does not fit. Nil builders leave no space. Both builders receive the standard icon size and follow the leading slot's rules: keep them non-interactive and free of I/O or background work.

pkg.go.dev

SidebarItemWithContent

func SidebarItemWithContent(c *ui.Context, label string, selected bool, leading func(c *ui.Context, size float32), content func(c *ui.Context) ui.Element, trailing func(c *ui.Context, size float32)) ui.Element

SidebarItemWithContent is a SidebarItemWithSlots whose visible label is application-built, for example animated or styled text; label still keys the item and names it for accessibility. A nil content builder shows label as plain text. With a trailing slot, the returned element takes the remaining room and may shrink below its natural width. Content follows the leading slot's rules.

pkg.go.dev
Edit this page on GitHub

On this page