Skip to content

Avatar: avatars

Build avatars with Avatar. This page includes a runnable example, all props, and behavior constraints.

Complete example

Complete environment setup first. Copy this complete main.go and run go run ..

go
package main

import (
	"github.com/dxui-org/dxui"
	"image"
	"image/color"
	"log"
)

func main() {
	pixels := image.NewRGBA(image.Rect(0, 0, 48, 48))
	for y := 0; y < 48; y++ {

		for x := 0; x < 48; x++ {
			pixels.SetRGBA(x, y, color.RGBA{
				R: 40,
				G: 160,
				B: 120,
				A: 255,
			})
		}
	}
	source := dxui.ImageFromGo(pixels)
	app := dxui.NewApp(dxui.AppOptions{
		Title:      "Avatar",
		Width:      640,
		Height:     480,
		Background: dxui.RGBA(248, 250, 252, 255),
	})
	if err := app.Run(func() dxui.View {
		return dxui.Box(
			dxui.BoxProps{
				Gap: 16,
				Style: dxui.Style{
					Padding: dxui.Padding(24),
				},
			},
			dxui.Avatar(dxui.AvatarProps{
				Source: source,
				Shape:  dxui.AvatarCircle,
				Size:   64,
			}),
		)
	}); err != nil {

		log.Fatal(err)
	}
}

Parameters and API

go
func Avatar(props AvatarProps) View
FieldTypePurpose, defaults, and constraints
KeystringStable unique identity within one parent; empty by default. Use business IDs in dynamic lists, not changing indices.
StyleStyleLocal layout, paint, and text styling; zero uses intrinsic sizes and theme defaults.
TokenComponentTokenComponent theme entry; empty selects its default.
StatesStateStylesPaint patches for actual interaction states; cannot fabricate state or change layout.
PointerPointerBehaviorDefaults to PointerAuto; PointerNone excludes the entire subtree from pointer participation, unlike Disabled.
SourceImageSourceStable image handle; reuse it rather than allocating during every build.
ShapeAvatarShapeDefaults to AvatarCircle; AvatarSquare selects a square.
Sizefloat32Logical size; 0 uses the theme default.
OnLoadfunc(Size)Success notification with decoded size; nil still loads.
OnErrorfunc(error)Resource error notification; the app may render a fallback. Nil still attempts loading.

Behavior and limitations

Shares ImageSource and OnLoad/OnError, with centered Cover fitting. Shape defaults to AvatarCircle; AvatarSquare is optional. Size=0 uses the theme default of 40; positive values are logical units, overridden by explicit Style dimensions. No URLs, initials, automatic fallback, or status dots. Render fallback content in response to OnError.

All components share identity, style, and pointer rules. See full declarations for referenced enums, structs, and comments. Running examples explains build verification.