What Lobe MyGo gets right about zero-value props, frame-scoped results, composable styling, and API documentation generated from Go source.
· 6 min read

API design lessons from Lobe MyGo


ActionIconGroup in lobehub/lobe-mygo returns the key of the icon or menu item selected during the current frame, or "". That small signature says a lot about the immediate-mode API: interaction is reported while the UI is being rebuilt, rather than through a callback stored on a retained widget.

Lobe MyGo brings the Lobe UI design language to MyGo (github.com/egoist/mygo/ui). Its component APIs contain several patterns that also apply outside UI code.

Context in, props struct out: the component signature

Many components put *ui.Context first, required data next, and a props struct last. From pkg/action/button.go:

func Button(c *ui.Context, label string, p ButtonProps) ui.Element {
	return StyleButton(c, ui.ButtonBase(c), label, p)
}

And in pkg/action/actionicon.go:

func ActionIcon(c *ui.Context, svg *ui.SVG, p ActionIconProps) ui.Element {
	return StyleActionIcon(c, ui.ButtonBase(c), svg, p)
}

In these functions the context comes first, required content such as the label or SVG is positional, and optional presentation settings live in a props struct. Other components add state pointers, callbacks, or return values as their behaviour requires.

Go has no named or default arguments, so a props struct keeps a growing list of options readable. The important detail is that its zero value must still be useful.

Zero values picked the defaults for you

Look at the variant enums:

type ButtonVariant int

const (
	Default ButtonVariant = iota
	Primary
	Dashed
	Filled
	Text
	Link
)
type ActionIconVariant int

const (
	ActionIconBorderless ActionIconVariant = iota
	ActionIconFilled
	ActionIconOutlined
)

In both enums the iota zero slot is the default appearance: Default for buttons and ActionIconBorderless for icon buttons. The bools on ActionIconGroupProps follow the same rule. Vertical, Disabled, and Shadow all mean “off” when unset.

That makes action.Button(c, "Save", action.ButtonProps{}) a complete call. If an enum’s zero value is invalid, every caller must set it and the supposedly optional props value becomes mandatory configuration.

Sizes resolve the same way through theme.Size, with a switch that falls through to the middle option:

func buttonSize(tk *theme.Tokens, s theme.Size) (buttonMetrics, float32) {
	switch s {
	case theme.Small:
		return buttonMetrics{24, 8, 12}, tk.BorderRadiusSM
	case theme.Large:
		return buttonMetrics{40, 16, 14}, tk.BorderRadius
	default:
		return buttonMetrics{32, 14, 13}, tk.BorderRadiusSM
	}
}

theme.Middle is the zero value of theme.Size. It deliberately uses the default branch, which also gives unknown values the middle metrics.

Style* functions let you restyle an element you didn’t create

Button and ActionIcon are two-liners. Their implementation lives in exported StyleButton and StyleActionIcon functions, which decorate an existing ui.Element instead of constructing one.

pkg/action/actiongroup.go uses that split when the group has a menu. It passes a styling callback to overlay.DropdownMenu:

if k := overlay.DropdownMenu(c, open, overlay.DropdownMenuProps{Items: p.Menu}, func(t ui.Element) {
	StyleActionIcon(c, t.Disabled(p.Disabled), lucide.Ellipsis(), mp)
}); k != "" {
	chosen = k
}

The dropdown owns the trigger’s behaviour while the action package owns its appearance. Separating construction from decoration lets one package style an element created by another. The lucide.Ellipsis() glyph comes from the kit’s pkg/lucide package and the Lucide icon set.

Frame-scoped return values and per-element state

Back to that string return. ActionIconGroup builds the row inside root.Children(func() { ... }) and writes to a variable the closure captures:

chosen := ""
open := ui.Local(root, "more", func() bool { return false })
root.Children(func() {
	for _, it := range p.Items {
		ip := icon
		ip.Danger, ip.Title = it.Danger, it.Label
		b := ActionIcon(c.Key(it.Key), it.Icon, ip).Disabled(p.Disabled || it.Disabled)
		if b.Clicked() {
			chosen = it.Key
		}
	}
	// ...
})
return chosen

ip := icon makes a shallow copy of ActionIconProps before overwriting two scalar fields. Those per-item changes therefore cannot leak into the next iteration. This is not a general deep-copy guarantee for props structs: fields such as slices, maps, or pointers can still refer to shared data.

c.Key(it.Key) assigns the identity of the next control. Because MyGo rebuilds the view each frame, the stable caller-supplied key lets the runtime find the right element state after items reorder. An index would instead attach that state to a position.

ui.Local(root, "more", func() bool { return false }) returns a pointer to state that root keeps across frames under the key "more". The initializer runs when that local state is first created, and the code can update it directly with *open = false.

There’s a smaller trick in button.go worth noting, the color resolution closure:

pick := func(rest, hover, active ui.Color) ui.Color {
	switch s {
	case buttonActive:
		return active
	case buttonHover:
		return hover
	}
	return rest
}

s is captured once, so each variant can select its rest, hover, and active colours in one expression: pick(tk.Primary, tk.PrimaryHover, tk.PrimaryActive). Without the helper, the state switch would be repeated across the variant branches.

Docs generated from props structs with go/ast

The props convention also powers cmd/gen-api/main.go. The generator uses go/ast, go/parser, go/token, go/types, and go/constant to parse package directories. By default it scans pkg/*, filters out internal, and writes api.json into each matching catalog page directory. Its field record is:

type apiField struct {
	Name    string   `json:"name"`
	Type    string   `json:"type"`
	Default string   `json:"default"`
	Doc     string   `json:"doc"`
	Values  []string `json:"values,omitempty"`
}

The comments above fields such as ButtonProps.Variant therefore become the published documentation. The parser records exported integer constants by their named type; go/types evaluates them and constant.Int64Val extracts exact values. That supplies both the ordered allowed-value list and the name of the zero-valued default.

Two small helpers handle page placement. pageOf trims the Props suffix and converts a type name to a lowercase, acronym-aware slug. pageDir repeatedly removes the final -segment until it finds a catalog directory, so ToastHostProps can land on the toast page. Every API write goes through writeIfChanged:

func writeIfChanged(file string, data []byte) error {
	if old, err := os.ReadFile(file); err == nil && bytes.Equal(old, data) {
		return nil
	}
	return os.WriteFile(file, data, 0o644)
}

This avoids rewriting unchanged generated files and needlessly changing their modification times.

The generator also scans each page’s index.mdx for <GoApi ... name="...">, allowing a page to include a struct that belongs to another package.

Screenshots rendered headlessly, with an alpha-recovery trick

cmd/gen-shots/main.go renders the docs images from the real components rather than from mockups. The core is ui.NewTester with a view function. Condensed from shotAt:

tt := ui.NewTester(func(c *ui.Context) { /* theme + frame + view */ }, int(w), int(h))
tt.SetScale(scale)
tt.SetDark(dark)
tt.Frame()
tt.Advance(settle)
tt.Frame()
return tt.Image()

Advance(settle) moves the tester’s virtual clock and runs frames that become due during that interval. That lets animated views reach a deterministic settled state before the final frame is captured. For example, buttonTransition lasts 160ms with a ui.CubicBezier(0.32, 0.72, 0, 1) easing curve.

The unmatte helper is another useful detail. The tiles need transparent backgrounds, but each source render has an opaque background. tile renders the same view over black and white, then recovers coverage from the difference:

k, w := black.RGBAAt(x, y), white.RGBAAt(x, y)
through := max(int(w.R)-int(k.R), int(w.G)-int(k.G), int(w.B)-int(k.B), 0)
a := 255 - min(through, 255)
if a == 0 {
	continue
}
un := func(v uint8) uint8 { return uint8(min(int(v)*255/a, 255)) }
out.SetNRGBA(x, y, color.NRGBA{un(k.R), un(k.G), un(k.B), uint8(a)})

Where the two renders agree, the pixel is fully opaque. Where they differ by 255, the foreground contributed nothing. The black render’s colour channels are already scaled by coverage, so dividing by the recovered alpha produces straight-alpha channels for image.NRGBA. Go’s predeclared max performs the four-way comparison; unlike an ordinary variadic function, it requires explicit arguments.

If you’re comparing native Go UI approaches, the Gova and Wails write-ups cover declarative structs and an embedded webview. Lobe MyGo takes an immediate-mode approach built from ordinary structs, pointers, and closures. That makes identity and frame timing part of the component contract, but it also keeps APIs such as ActionIconGroup small and makes annotated props structs useful as a single source for generated documentation. Neither lesson is limited to UI libraries.