Skip to Content
bloom

Boxes and borders

Draw Lip Gloss borders around nodes, put titles, tabs, and buttons on them, join lines to them, and animate them.

bloom.Box draws a Lip Gloss frame around a node and gives the node the space inside. You can put children on the border itself, such as a title, tabs, a counter, buttons, or a scrollbar, join lines drawn inside the box to it, and animate it.

Draw a box around a node

bloom.Box(style, child) draws a Lip Gloss style's frame around a child: its margins, border, padding, and background. The child gets the space inside.

panel := lipgloss.NewStyle().
	Border(lipgloss.RoundedBorder()).
	BorderForeground(lipgloss.Color("#6e6a86")).
	Padding(0, 1)

bloom.Box(panel, bloom.Column(title, body))

Use Box rather than calling panel.Render(...) yourself. A pre-rendered string has a fixed size, but a box knows its frame and gives the right amount of space to its child at any size. A box given less space than its own border and padding is left out, rather than drawn broken.

Put titles and more on a border

Edge(side, align, child) draws a child over one side of a box's border, such as a title, a counter, or help text, so it fits more on screen without taking space from what's inside:

bloom.Box(panel, list).
	Edge(bloom.Top, bloom.Start, bloom.Text("secrets").Style(title)).
	Edge(bloom.Top, bloom.End, bloom.Text(fmt.Sprintf("%d/%d", m.selected+1, len(m.items)))).
	Edge(bloom.Bottom, bloom.Center, bloom.Text("? help").Style(muted))
╭─secrets───────────3/120─╮
│kv/app/db                │
│kv/app/redis             │
╰─────────? help──────────╯

On the top and bottom, a child is one row tall and stays a cell away from the corners. On the left and right, it's one column wide, and a Text runs down the side, a character to a row.

Children at Start and End go from that end inward, in the order you add them, one cell apart. Children at Center share the middle of what's left, and a child at Stretch takes all of it. A child gets no more room than is left, so a Text is clipped, a Truncated one is cut short with "…", and a child with no room isn't shown. A side with no border shows nothing.

Edge children are nodes like any other, so they can have IDs, mouse handlers, and hover styles. Tabs on a border can be clicked:

tabs := bloom.Row(
	bloom.Text("all").Style(tab).HoverStyle(onTab).On(on.Click(m.showAll)),
	bloom.Text("kv").Style(tab).HoverStyle(onTab).On(on.Click(m.showKV)),
).Gap(1)

bloom.Box(panel, list).Edge(bloom.Top, bloom.End, tabs)

Single characters make good buttons on any side. Several at Start go down the left side one after another, a cell apart:

for _, a := range actions { // such as + for new, ✎ for edit, ✕ for delete
	box = box.Edge(bloom.Left, bloom.Start, bloom.Text(a.glyph).Style(icon).HoverStyle(hot).On(on.Click(a.run)))
}
box = box.Edge(bloom.Right, bloom.Center, bloom.Text("LOGS").Style(label)) // L, O, G, S down the right side
╭─secrets────────╮
+kv/app/db       L
│kv/app/redis    O
✎kv/app/smtp     G
│kv/infra/vault  S
✕kv/infra/pki    │
╰────────────────╯

A scrollbar can go on a border too, where it shows only while there's more to scroll to, and can be dragged.

Join lines to the border

EdgeAt(side, at, child) puts a child at an exact cell along a side, counted from the top or left end, where 0 is the corner. A negative at counts back from the other end, so -1 is the far corner. It's for joining lines drawn inside a box to its border, such as where column separators meet it, or for swapping a corner for a ├ where two boxes stack:

box := bloom.Box(panel, table)
for _, x := range separators { // the column of each │ inside, from the box's left edge
	box = box.
		EdgeAt(bloom.Top, x, bloom.Text("┬").Style(border)).
		EdgeAt(bloom.Bottom, x, bloom.Text("┴").Style(border))
}
box = box.Edge(bloom.Top, bloom.Start, bloom.Text("jobs").Style(title))
╭─jobs─┬──────┬─────╮
│   12 │ api  │ ok  │
│   13 │ db   │ ok  │
╰──────┴──────┴─────╯

Children placed with EdgeAt take no room from those placed with Edge, so a title or a counter stays where it was. Edge children are drawn in the order you add them, so add the junctions first to let a title cover any under it. A child gets the room from its cell to the end of the side, and a cell past the end, or a side with no border, shows nothing.

Animate a border

BorderFx(t, effects...) applies effects to a box's frame, its border, padding, and background, leaving what's inside it and on its edges alone. The border effects in fx loop: t is the phase, such as from anim.NewLoop, and they look the same at 0 and 1, so the loop is seamless:

type model struct {
	ui    *bloom.Root
	phase anim.Sequence // anim.NewLoop(4 * time.Second), started with m.phase.Loop() in Init
	// ...
}

bloom.Box(panel, list).BorderFx(m.phase.Float(), fx.Chase{From: accent, To: pink})

The borders example puts it all together:

The borders example: a list with a title, clickable filter tabs, its position, and icon buttons on its border, its scrollbar lighting up under the pointer, then dragged and clicked, a log scrolled sideways with a horizontal scrollbar and its name down its side, next to boxes looping each border effect

To draw a static gradient, use Lip Gloss's own BorderForegroundBlend in the box's style. fx.Chase is for a gradient that moves, which would otherwise mean rendering the whole frame again on every frame.

Last updated on

On this page