Skip to content
Kite

Layout, Focus & Animation

← All examples · Run any demo natively with go run ./examples/<name>.

Run go run ./examples/flex to inspect inline-flex, space-between, column alignment, row-reverse, order, and flex-wrap.

type Styles struct {
Header st.Style
InlineFlexItem st.Style
ColumnFlexItem st.Style
InlineFlexContainer st.Style
InlineFlexWrapper st.Style
FlexRowContainer st.Style
FlexColContainer st.Style
ReverseItemRed st.Style
ReverseItemGreen st.Style
ReverseItemBlue st.Style
FlexRowReverseContainer st.Style
FlexOrderContainer st.Style
FlexWrapContainer st.Style
FlexWrapItem st.Style
Root st.Style
}
var styles = Styles{
Header: st.S().
Margin(0, 0, 1, 0).
Foreground(st.RGB(255, 255, 0)),
InlineFlexItem: st.S().
Padding(0, 1).
Background(st.RGB(150, 0, 0)),
ColumnFlexItem: st.S().
Width(st.Auto).
Padding(0, 2).
Background(st.RGB(180, 80, 0)),
InlineFlexContainer: st.S().
Display(st.DisplayInlineFlex).
Gap(0, 1).
Padding(0, 1).
Border(st.SingleBorder()).
Background(st.RGB(0, 80, 150)),
InlineFlexWrapper: st.S().
Margin(0, 0, 2, 0),
FlexRowContainer: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
JustifyContent(st.JustifyBetween).
AlignItems(st.AlignCenter).
Height(st.Cells(5)).
Margin(0, 0, 2, 0).
Padding(0, 2).
Background(st.RGB(40, 40, 40)),
FlexColContainer: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
AlignItems(st.AlignEnd).
Gap(1, 0).
Width(st.Percent(50)).
Margin(0, 0, 2, 0).
Padding(1, 2).
Background(st.RGB(30, 30, 60)),
ReverseItemRed: st.S().
Padding(0, 1).
Background(st.RGB(200, 0, 0)),
ReverseItemGreen: st.S().
Padding(0, 1).
Background(st.RGB(0, 200, 0)),
ReverseItemBlue: st.S().
Padding(0, 1).
Background(st.RGB(0, 0, 200)),
FlexRowReverseContainer: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRowReverse).
Gap(0, 2).
Margin(0, 0, 2, 0).
Padding(0, 2).
Background(st.RGB(60, 30, 30)),
FlexOrderContainer: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Gap(2).
Width(st.Percent(100)).
Padding(0, 2).
Border(st.SingleBorder()).
Background(st.RGB(30, 60, 30)),
FlexWrapContainer: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
FlexWrap(st.FlexWrapOn).
Gap(1, 1).
Width(st.Percent(100)).
Margin(0, 0, 2, 0).
Padding(1, 2).
Background(st.RGB(80, 30, 80)),
FlexWrapItem: st.S().
Padding(0, 2).
Border(st.SingleBorder()).
Background(st.RGB(120, 60, 180)),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Width(st.Percent(100)).
Height(st.Percent(100)).
OverflowY(st.OverflowAuto).
Padding(1, 2).
Background(st.RGB(15, 15, 15)),
}
func orderedItemStyle(base st.Style, order int) st.Style {
return base.
Flex(1).
Order(order).
Border(st.SingleBorder())
}
func rowItemStyle(i int) st.Style {
return st.S().
Height(st.Cells(1+i%2)).
Padding(0, 2).
Background(st.RGB(0, 120, 0))
}
// App constructs the portable Flexbox layout showcase UI tree.
func App() dom.Element {
return el.Box(styles.Root,
el.Box(styles.Header, "1. Inline Flex (Shrink-wrap content)"),
el.Box(styles.InlineFlexWrapper,
"Text before -> ",
el.Box(styles.InlineFlexContainer,
el.Box(styles.InlineFlexItem, "Item 1"),
el.Box(styles.InlineFlexItem, "Item 2"),
el.Box(styles.InlineFlexItem, "Item 3"),
),
" <- Text after",
),
el.Box(styles.Header, "2. Flex Row (Justify: Space-Between, Align: Center)"),
el.Box(styles.FlexRowContainer,
el.Box(rowItemStyle(1), "Row Item 1"),
el.Box(rowItemStyle(2), "Row Item 2"),
el.Box(rowItemStyle(3), "Row Item 3"),
el.Box(rowItemStyle(4), "Row Item 4"),
),
el.Box(styles.Header, "3. Flex Column (Align: End)"),
el.Box(styles.FlexColContainer,
el.Box(styles.ColumnFlexItem, "Column Item 1 (Stays Right)"),
el.Box(styles.ColumnFlexItem, "Column Item 2 (Stays Right)"),
el.Box(styles.ColumnFlexItem, "Column Item 3 (Stays Right)"),
),
el.Box(styles.Header, "4. Flex Row Reverse"),
el.Box(styles.FlexRowReverseContainer,
el.Box(styles.ReverseItemRed, "Reverse Item 1"),
el.Box(styles.ReverseItemGreen, "Reverse Item 2"),
el.Box(styles.ReverseItemBlue, "Reverse Item 3"),
),
el.Box(styles.Header, "5. Flex Order Property"),
el.Box(styles.FlexOrderContainer,
el.Box(orderedItemStyle(styles.ReverseItemRed, 3), "First in DOM (Order 3)"),
el.Box(orderedItemStyle(styles.ReverseItemGreen, 1), "Second in DOM (Order 1)"),
el.Box(orderedItemStyle(styles.ReverseItemBlue, 2), "Third in DOM (Order 2)"),
),
el.Box(styles.Header, "6. Flex Wrap (Wrap items onto multiple lines)"),
el.Box(styles.FlexWrapContainer,
el.Box(styles.FlexWrapItem, "Long Item 1"),
el.Box(styles.FlexWrapItem, "Long Item 2"),
el.Box(styles.FlexWrapItem, "Long Item 3"),
el.Box(styles.FlexWrapItem, "Long Item 4"),
el.Box(styles.FlexWrapItem, "Long Item 5"),
el.Box(styles.FlexWrapItem, "Long Item 6"),
),
)
}

Run go run ./examples/grid to cycle through six CSS Grid examples from 1fr tracks to a Holy Grail layout.

type Styles struct {
GridContainer st.Style
Title st.Style
Hint st.Style
Toolbar st.Style
NavButton st.Style
BasicGrid st.Style
SpanningItem st.Style
SpanningGrid st.Style
AutoPlacementGrid st.Style
MixedTracksGrid st.Style
ExplicitCell1 st.Style
ExplicitCell2 st.Style
ExplicitCell3 st.Style
ExplicitGrid st.Style
HolyGrailHeaderFooter st.Style
HolyGrailSidebar st.Style
HolyGrailContent st.Style
HolyGrailGrid st.Style
Root st.Style
ViewHost st.Style
}
var styles = Styles{
GridContainer: st.S().
Width(st.Percent(100)).
Height(st.Percent(100)),
Title: st.S().
Margin(0, 0, 1, 0).
Foreground(st.RGB(255, 255, 0)),
Hint: st.S().
Foreground(st.RGB(148, 163, 184)).
Margin(0, 0, 1, 0),
Toolbar: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
FlexWrap(st.FlexWrapOn).
Gap(1, 1).
Margin(0, 0, 1, 0),
NavButton: st.S().
Border(st.EmptyBorder()).
Padding(0, 1).
Background(st.RGB(50, 60, 85)).
Foreground(color.White).
Focus(st.S().
Background(st.RGB(80, 95, 130)).
Bold(true)),
BasicGrid: st.S().
Display(st.DisplayGrid).
GridTemplateColumns(st.Repeat(2, st.Fr(1))...).
GridTemplateRows(st.Repeat(2, st.Fr(1))...).
Height(st.Auto).
Border(st.SingleBorder()),
SpanningItem: st.S().
GridColumn(st.Span(2)).
Border(st.SingleBorder()).
Background(st.RGB(150, 0, 150)),
SpanningGrid: st.S().
Display(st.DisplayGrid).
Gap(1).
GridTemplateColumns(st.Repeat(2, st.Fr(1))...).
GridTemplateRows(st.Repeat(2, st.Fr(1))...).
Height(st.Auto).
Border(st.SingleBorder()),
AutoPlacementGrid: st.S().
Display(st.DisplayGrid).
GridTemplateColumns(st.Repeat(3, st.Fr(1))...).
GridColumnGap(4).
GridRowGap(1).
Height(st.Auto).
Padding(1).
Border(st.SingleBorder()),
MixedTracksGrid: st.S().
Display(st.DisplayGrid).
GridTemplateColumns(st.Cells(10), st.Auto, st.Fr(1)).
Height(st.Auto).
Border(st.SingleBorder()),
ExplicitCell1: st.S().
GridColumn(st.GridLine(1)).
GridRow(st.GridLine(1)).
Border(st.SingleBorder()).
Background(st.RGB(100, 0, 0)),
ExplicitCell2: st.S().
GridColumn(st.GridLine(2)).
GridRow(st.GridLine(2)).
Border(st.SingleBorder()).
Background(st.RGB(0, 100, 0)),
ExplicitCell3: st.S().
GridColumn(st.GridLine(3)).
GridRow(st.GridLine(3)).
Border(st.SingleBorder()).
Background(st.RGB(0, 0, 100)),
ExplicitGrid: st.S().
Display(st.DisplayGrid).
GridTemplateColumns(st.Repeat(3, st.Fr(1))...).
GridTemplateRows(st.Repeat(3, st.Cells(3))...).
Height(st.Auto).
Border(st.SingleBorder()),
HolyGrailHeaderFooter: st.S().
GridColumn(st.Span(3)).
Border(st.SingleBorder()).
Background(st.RGB(150, 150, 150)).
Foreground(st.RGB(0, 0, 0)),
HolyGrailSidebar: st.S().
Border(st.SingleBorder()).
Background(st.RGB(100, 100, 100)),
HolyGrailContent: st.S().
Border(st.SingleBorder()).
Background(st.RGB(50, 50, 50)),
HolyGrailGrid: st.S().
Display(st.DisplayGrid).
GridTemplateColumns(
st.Cells(12),
st.Fr(1),
st.Cells(10),
).
GridTemplateRows(
st.Cells(3),
st.Fr(1),
st.Cells(3),
).
Width(st.Percent(100)).
Height(st.Cells(15)).
Border(st.SingleBorder()),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Width(st.Percent(100)).
Height(st.Percent(100)).
Padding(1, 2).
Background(st.RGB(15, 15, 15)),
ViewHost: st.S().
Width(st.Percent(100)).
Flex(1),
}
func presetButtonStyle(active bool) st.Style {
if active {
return st.S().
Border(st.EmptyBorder()).
Padding(0, 1).
Background(st.RGB(0, 200, 150)).
Foreground(color.Black).
Bold(true)
}
return st.S().
Border(st.EmptyBorder()).
Padding(0, 1).
Background(st.RGB(35, 45, 65)).
Foreground(color.White).
Focus(st.S().
Background(st.RGB(55, 70, 100)).
Bold(true))
}
func itemStyle(bg color.Color) st.Style {
return st.S().
Width(st.Percent(100)).
Height(st.Percent(100)).
Border(st.SingleBorder()).
Background(bg)
}
func viewBasicGrid() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "1. Basic 2x2 Grid (1fr tracks)"),
el.Box(styles.BasicGrid,
el.Box(itemStyle(st.RGB(150, 0, 0)), "1"),
el.Box(itemStyle(st.RGB(0, 150, 0)), "2"),
el.Box(itemStyle(st.RGB(0, 0, 150)), "3"),
el.Box(itemStyle(st.RGB(150, 150, 0)), "4"),
),
)
}
func viewSpanningGrid() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "2. Spanning Grid"),
el.Box(styles.SpanningGrid,
el.Box(styles.SpanningItem, "Spans 2 Cols"),
el.Box(itemStyle(st.RGB(0, 150, 150)), "3"),
el.Box(itemStyle(st.RGB(150, 150, 150)), "4"),
),
)
}
func viewAutoPlacement() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "3. Auto Placement (with Gaps)"),
el.Box(styles.AutoPlacementGrid,
el.Box(itemStyle(st.RGB(100, 50, 0)), "A"),
el.Box(itemStyle(st.RGB(0, 100, 50)), "B"),
el.Box(itemStyle(st.RGB(50, 0, 100)), "C"),
el.Box(itemStyle(st.RGB(100, 100, 100)), "D"),
el.Box(itemStyle(st.RGB(50, 50, 50)), "E"),
el.Box(itemStyle(st.RGB(0, 0, 0)), "F"),
),
)
}
func viewMixedTracks() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "4. Mixed Tracks (Fixed, Auto, Fr)"),
el.Box(styles.MixedTracksGrid,
el.Box(itemStyle(st.RGB(150, 0, 0)), "Fixed 10"),
el.Box(itemStyle(st.RGB(0, 150, 0)), "Auto (Shrink)"),
el.Box(itemStyle(st.RGB(0, 0, 150)), "1fr (Rest)"),
),
)
}
func viewExplicitCoordinates() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "5. Explicit Coordinates"),
el.Box(styles.ExplicitGrid,
el.Box(styles.ExplicitCell1, "1,1"),
el.Box(styles.ExplicitCell2, "2,2"),
el.Box(styles.ExplicitCell3, "3,3"),
),
)
}
func viewHolyGrail() dom.Element {
return el.Box(styles.GridContainer,
el.Box(styles.Title, "6. Holy Grail Layout"),
el.Box(styles.HolyGrailGrid,
el.Box(styles.HolyGrailHeaderFooter, "HEADER"),
el.Box(styles.HolyGrailSidebar, "SIDEBAR"),
el.Box(styles.HolyGrailContent, "CONTENT"),
el.Box(styles.HolyGrailSidebar, "RIGHT"),
el.Box(styles.HolyGrailHeaderFooter, "FOOTER"),
),
)
}
// App constructs the portable CSS Grid layout showcase UI tree.
func App() dom.Element {
var (
hint *el.BoxElement
viewHost *el.BoxElement
prevBtn *el.ButtonElement
nextBtn *el.ButtonElement
)
presets := []struct {
label string
build func() dom.Element
}{
{"[1] Basic", viewBasicGrid},
{"[2] Span", viewSpanningGrid},
{"[3] Auto", viewAutoPlacement},
{"[4] Mixed", viewMixedTracks},
{"[5] Coords", viewExplicitCoordinates},
{"[6] Grail", viewHolyGrail},
}
const hintFormat = "View %d/%d · Click a preset or press [1–6], [Space / n] next, [p] prev"
activeView := presets[0].build()
presetBtns := make([]*el.ButtonElement, len(presets))
presetChildren := make([]dom.Node, len(presets))
for i, preset := range presets {
presetChildren[i] = el.Button(presetButtonStyle(i == 0), preset.label).Ref(&presetBtns[i])
}
root := el.Box(styles.Root,
el.Box(styles.Hint, fmt.Sprintf(hintFormat, 1, len(presets))).Ref(&hint),
el.Box(styles.Toolbar,
el.Button(styles.NavButton, "< Prev").Ref(&prevBtn),
presetChildren,
el.Button(styles.NavButton, "Next >").Ref(&nextBtn),
),
el.Box(styles.ViewHost, activeView).Ref(&viewHost),
)
currentView := 0
selectView := func(idx int) {
if idx < 0 {
idx = len(presets) - 1
}
idx = idx % len(presets)
if idx == currentView {
return
}
presetBtns[currentView].Style(presetButtonStyle(false))
currentView = idx
viewHost.RemoveChild(activeView)
activeView = presets[currentView].build()
viewHost.AppendChild(activeView)
presetBtns[currentView].Style(presetButtonStyle(true))
hint.SetTextf(hintFormat, currentView+1, len(presets))
}
prevBtn.OnPress(func() { selectView(currentView - 1) })
nextBtn.OnPress(func() { selectView(currentView + 1) })
for i, btn := range presetBtns {
btn.OnPress(func() { selectView(i) })
}
var keySub event.Subscription
root.OnConnect(func() {
doc := root.OwnerDocument()
if doc == nil {
return
}
keySub = doc.AddEventListener(event.EventKeyDown, func(e event.Event) {
ke, ok := e.(*event.KeyEvent)
if !ok {
return
}
switch {
case ke.MatchString("n") || ke.MatchString(" "):
selectView(currentView + 1)
case ke.MatchString("p"):
selectView(currentView - 1)
case ke.MatchString("1"):
selectView(0)
case ke.MatchString("2"):
selectView(1)
case ke.MatchString("3"):
selectView(2)
case ke.MatchString("4"):
selectView(3)
case ke.MatchString("5"):
selectView(4)
case ke.MatchString("6"):
selectView(5)
}
})
})
root.OnDisconnect(func() {
if keySub != nil {
keySub.Cancel()
keySub = nil
}
})
return root
}

Run go run ./examples/table to compare well-formed tables, implicit cell rows, and thead/tbody/tfoot border collapse.

type Styles struct {
DefaultCell st.Style
TableGrid st.Style
Row st.Style
NameColumn st.Style
RoleColumn st.Style
WellFormedTable st.Style
SectionHeader st.Style
Col1Width st.Style
Col2Width st.Style
MalformedTable st.Style
TableHeaderBorder st.Style
TableFooterBorder st.Style
Root st.Style
}
var styles = Styles{
DefaultCell: st.S().
Width(st.Percent(100)).
Border(cellBorder),
TableGrid: st.S().
Width(st.Percent(100)).
Border(st.SingleBorder().Color(st.RGB(100, 255, 100))),
Row: st.S().
Width(st.Percent(50)).
Border(cellBorder),
NameColumn: st.S().
Width(st.Cells(15)).
Border(cellBorder),
RoleColumn: st.S().
Width(st.Cells(20)).
Border(cellBorder),
WellFormedTable: st.S().
Width(st.Percent(100)).
Border(st.SingleBorder().Color(st.RGB(100, 100, 255))),
SectionHeader: st.S().
Margin(1, 0),
Col1Width: st.S().
Width(st.Cells(15)),
Col2Width: st.S().
Width(st.Cells(20)),
MalformedTable: st.S().
Width(st.Percent(100)).
Border(st.SingleBorder().Color(st.RGB(255, 100, 100))),
TableHeaderBorder: st.S().
Border(st.SingleBorder().
Top(false).
Right(false).
Left(false)),
TableFooterBorder: st.S().
Border(st.SingleBorder().
Bottom(false).
Right(false).
Left(false)),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Gap(2, 0).
Width(st.Percent(100)).
Height(st.Percent(100)).
OverflowY(st.OverflowAuto).
Padding(2, 4).
Background(st.RGB(30, 30, 30)),
}
var cellBorder = st.SingleBorder().Color(st.RGB(255, 255, 100))
func App() dom.Element {
return el.Box(styles.Root,
el.Box(styles.SectionHeader, "Well-formed Table"),
el.Table(styles.WellFormedTable,
el.TR(styles.Row,
el.TD(styles.NameColumn, "Name"),
el.TD(styles.RoleColumn, "Role"),
),
el.TR(styles.Row,
el.TD(styles.DefaultCell, "Alice"),
el.TD(styles.DefaultCell, "Developer"),
),
el.TR(styles.Row,
el.TD(styles.DefaultCell, "Total Users: 1 (Spanning)").SetColSpan(2),
),
),
el.Box(styles.SectionHeader, "Malformed Table (Cells without Rows)"),
el.Table(styles.MalformedTable,
el.TD(styles.Col1Width, "Direct Cell 1"),
el.TD(styles.Col2Width, "Direct Cell 2"),
),
el.Box(styles.SectionHeader, "Grouped Table (thead, tbody, tfoot)"),
el.Table(styles.TableGrid,
el.THead(styles.TableHeaderBorder,
el.TR(
el.TD(styles.Col1Width, "Header Col 1"),
el.TD(styles.Col2Width, "Header Col 2"),
),
),
el.TBody(
el.TR(
el.TD("Body Row 1, C1"),
el.TD("Body Row 1, C2"),
),
el.TR(
el.TD("Body Row 2, C1"),
el.TD("Body Row 2, C2"),
),
),
el.TFoot(styles.TableFooterBorder,
el.TR(
el.TD("Footer 1"),
el.TD("Footer 2"),
),
),
),
)
}

Run go run ./examples/list to inspect unordered, ordered, square-marker, and nested lists with wrapped markers.

type Styles struct {
ListHeader st.Style
Title st.Style
SquareList st.Style
ContentContainer st.Style
Root st.Style
}
var styles = Styles{
ListHeader: st.S().
Margin(1, 0, 0, 0).
Underline(true),
Title: st.S().
Margin(0, 0, 1, 0).
Bold(true).
TextAlign(st.TextAlignCenter),
SquareList: st.S().
ListStyleType(st.ListStyleSquare),
ContentContainer: st.S().
Width(st.Percent(90)).
Margin(1, 0).
Padding(1, 2).
Border(st.SingleBorder()),
Root: st.S().
Width(st.Percent(100)).
Height(st.Percent(100)).
OverflowY(st.OverflowAuto).
Padding(1, 2).
Background(st.RGB(20, 20, 20)),
}
// App constructs the portable list components demo UI tree.
func App() dom.Element {
return el.Box(styles.Root,
el.Box(styles.ContentContainer,
el.Box(styles.Title, "Kite List Components Demonstration"),
el.Box(styles.ListHeader, "Unordered List (Default: Disc)"),
el.UL(
el.LI("First item"),
el.LI(
"Second item with long text that should wrap around "+
"the marker correctly if the container is narrow enough.",
),
el.LI("Third item"),
),
el.Box(styles.ListHeader, "Ordered List (Default: Decimal)"),
el.OL(
el.LI("Initialize engine"),
el.LI("Build DOM tree"),
el.LI("Run frame loop"),
),
el.Box(styles.ListHeader, "Custom Markers (Square)"),
el.UL(styles.SquareList,
el.LI("Customized UL"),
el.LI("Uses Square markers via inheritance"),
),
el.Box(styles.ListHeader, "Nested Lists"),
el.UL(
el.LI(
"Item with nested list:",
el.OL(
el.LI("Nested Step A"),
el.LI("Nested Step B"),
),
),
el.LI("Another parent item"),
),
),
)
}

Run go run ./examples/responsive to see @media MinWidth breakpoints, viewport size hooks, and live progress updates.

type Styles struct {
Container st.Style
Card st.Style
Title st.Style
BellButton st.Style
Root st.Style
AppMount st.Style
}
var styles = Styles{
Container: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Percent(100)).
Height(st.Percent(100)).
Background(st.RGB(20, 20, 255)).
Media(
st.Query().MinWidth(80),
st.S().Background(st.RGB(20, 255, 20)),
),
Card: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Padding(1).
Border(st.SingleBorder().Color(st.RGB(255, 255, 255))).
Background(st.RGB(30, 30, 30)),
Title: st.S().
Foreground(st.RGB(255, 255, 255)).
Bold(true),
BellButton: st.S().
Padding(0, 1).
Border(st.SingleBorder().Color(st.RGB(255, 255, 255))).
Background(st.RGB(50, 50, 50)),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
AlignItems(st.AlignCenter).
Width(st.Percent(100)).
Height(st.Percent(100)),
AppMount: st.S().
Flex(1).
Width(st.Percent(100)),
}
// App is the portable Kitex root component for the responsive hooks demo.
var App = kx.FC("App", func(_ kx.NoProps) kx.Node {
viewport := kx.UseViewportSize()
_, setTitle := kx.UseTitle("Kite Responsive Demo")
ringBell := kx.UseBell()
windowFocused := kx.UseWindowFocus()
updateProgress := kx.UseProgressBar()
progressVal, setProgressVal := kx.UseState(0)
kx.UseInterval(func() {
nextVal := progressVal() + 2
if nextVal > 100 {
nextVal = 0
}
setProgressVal(nextVal)
updateProgress(terminal.ProgressBarIndeterminate, nextVal)
}, 100*time.Millisecond, progressVal())
kx.UseKeyboard(func(e event.KeyEvent) {
if e.MatchString("q", "ctrl+c", "esc") {
updateProgress(terminal.ProgressBarHide, 0)
}
})
var infoText string
if viewport.Width >= 80 {
infoText = fmt.Sprintf(
"🖥️ WIDE SCREEN DETECTED (%d cells wide) - Background is GREEN",
viewport.Width,
)
} else {
infoText = fmt.Sprintf(
"📱 SMALL SCREEN DETECTED (%d cells wide) - Background is BLUE",
viewport.Width,
)
}
setTitle(fmt.Sprintf("Responsive: %d x %d", viewport.Width, viewport.Height))
focusText := "🔴 Inactive (Blurred)"
if windowFocused {
focusText = "🟢 Active (Focused)"
}
return kx.Box.Style(styles.Container,
kx.Box.Style(styles.Card,
kx.Span.Style(styles.Title,
kx.Text("⚡ Responsive Styling & Hook Demo ⚡"),
),
kx.Text(""),
kx.Text(infoText),
kx.Textf("Current viewport dimensions: %d x %d", viewport.Width, viewport.Height),
kx.Textf("Terminal window focus: %s", focusText),
kx.Textf("Dock/Tab progress bar: %d%%", progressVal()),
kx.Text(""),
kx.Button(kx.ButtonProps{
OnClick: func(*event.MouseEvent) {
ringBell()
},
Style: styles.BellButton,
}, kx.Text("🔔 Ring Terminal Bell")),
kx.Text(""),
kx.Text(
"Resize your terminal to see the background color, "+
"text, and window title change!",
),
kx.Text(
"Unfocus the terminal window (switch window) "+
"to see the focus status change.",
),
kx.Text(
"Look at your terminal tab/dock header "+
"to see the native progress bar updating!",
),
),
)
})

Run go run ./examples/opacity to test RGBA alpha blending on hover buttons and an adjustable translucent overlay.

type Styles struct {
HoverButton st.Style
Title st.Style
Hint st.Style
RedBand st.Style
GreenBand st.Style
BlueBand st.Style
YellowBand st.Style
BandsContainer st.Style
ButtonSectionHeader st.Style
ButtonRow st.Style
Root st.Style
OverlayTitle st.Style
OverlayHint st.Style
OverlayWrapper st.Style
}
var styles = Styles{
HoverButton: st.S().
Padding(0, 2).
Background(color.Transparent).
Foreground(color.White).
Hover(st.S().
Background(st.RGBA(255, 255, 255, 60))),
Title: st.S().
Padding(1).
Background(st.RGB(50, 80, 200)).
Bold(true).
TextAlign(st.TextAlignCenter),
Hint: st.S().
Margin(1, 0, 0, 0).
Foreground(st.RGB(148, 163, 184)).
TextAlign(st.TextAlignCenter),
RedBand: st.S().
Padding(1, 2).
Background(st.RGB(200, 50, 50)),
GreenBand: st.S().
Padding(1, 2).
Background(st.RGB(50, 180, 50)),
BlueBand: st.S().
Padding(1, 2).
Background(st.RGB(50, 50, 200)),
YellowBand: st.S().
Padding(1, 2).
Background(st.RGB(200, 180, 50)),
BandsContainer: st.S().
Margin(1, 0),
ButtonSectionHeader: st.S().
Margin(1, 0, 0, 0).
Bold(true),
ButtonRow: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Margin(0, 0, 1, 0),
Root: st.S().
Width(st.Percent(100)).
Height(st.Percent(100)).
Padding(2).
Background(st.RGB(20, 20, 30)),
OverlayTitle: st.S().
Margin(0, 0, 1, 0).
Bold(true).
TextAlign(st.TextAlignCenter),
OverlayHint: st.S().
Margin(1, 0, 0, 0),
OverlayWrapper: st.S().
Display(st.DisplayFlex).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Percent(100)).
Height(st.Percent(100)),
}
func overlayCardStyle(alpha int) st.Style {
return st.S().
Width(st.Cells(45)).
Height(st.Cells(10)).
Padding(1, 2).
Border(st.DoubleBorder().Color(color.White)).
Background(st.RGBA(255, 255, 255, uint8(alpha))).
Foreground(color.Black)
}
func App() dom.Element {
var (
alphaText *el.SpanElement
dialogCard *el.BoxElement
)
const initialAlpha = 128
overlay := el.Box(styles.OverlayWrapper,
el.Box(overlayCardStyle(initialAlpha),
el.Box(styles.OverlayTitle, "Semi-Transparent Dialog"),
el.Span(fmt.Sprintf("Current Alpha Opacity: %d / 255", initialAlpha)).Ref(&alphaText),
"\nNotice how the colored bands behind me\nblend through the dialog background!",
el.Box(styles.OverlayHint,
el.Span(st.S().Bold(true), "[+ / -]"),
" adjust opacity",
),
).Ref(&dialogCard),
)
root := el.Box(styles.Root,
el.Box(styles.Title, "Opacity & Color Blending Demonstration"),
el.Box(styles.Hint,
"Press [o] to toggle overlay · [+ / -] to adjust overlay opacity",
),
el.Box(styles.BandsContainer,
el.Box(styles.RedBand, "RED BAND (Solid Background)"),
el.Box(styles.GreenBand, "GREEN BAND (Solid Background)"),
el.Box(styles.BlueBand, "BLUE BAND (Solid Background)"),
el.Box(styles.YellowBand, "YELLOW BAND (Solid Background)"),
),
el.Box(styles.ButtonSectionHeader,
"Hover Buttons (Blends semi-transparent white hover state on base color)",
),
el.Box(styles.ButtonRow,
createHoverButton("Blue Button", st.RGB(30, 60, 180)),
createHoverButton("Green Button", st.RGB(30, 150, 60)),
createHoverButton("Purple Button", st.RGB(120, 30, 150)),
),
)
alpha := initialAlpha
visible := false
var keySub event.Subscription
root.OnConnect(func() {
doc := root.OwnerDocument()
keySub = doc.AddEventListener(event.EventKeyDown, func(e event.Event) {
key, ok := e.(*event.KeyEvent)
if !ok {
return
}
switch {
case key.MatchString("o"):
visible = !visible
if visible {
doc.ShowOverlay(overlay, 100)
} else {
doc.HideOverlay(overlay)
}
return
case !visible:
return
case key.MatchString("+") || key.MatchString("="):
alpha = min(255, alpha+20)
case key.MatchString("-"):
alpha = max(0, alpha-20)
default:
return
}
alphaText.SetTextf("Current Alpha Opacity: %d / 255", alpha)
dialogCard.Style(overlayCardStyle(alpha))
})
})
root.OnDisconnect(func() {
keySub.Cancel()
if visible {
root.OwnerDocument().HideOverlay(overlay)
visible = false
}
})
return root
}
func createHoverButton(label string, baseBg color.Color) *el.BoxElement {
return el.Box(st.S().
Margin(0, 1).
Padding(0, 1).
Border(st.SingleBorder().Color(st.RGBA(255, 255, 255, 80))).
Background(baseBg), el.Button(styles.HoverButton, label))
}

Spatial Navigation (examples/spatial_navigation)

Section titled “Spatial Navigation (examples/spatial_navigation)”

Run go run ./examples/spatial_navigation to move focus geometrically across sidebar links, header tabs, and cards.

type Styles struct {
Button st.Style
Root st.Style
Sidebar st.Style
SidebarTitle st.Style
Content st.Style
Header st.Style
GridTitle st.Style
NavHint st.Style
GridRow st.Style
LogPanel st.Style
LogBox st.Style
LogTitle st.Style
VirtualArrowPad st.Style
ArrowRow st.Style
ArrowNormal st.Style
ArrowActive st.Style
}
var styles = Styles{
Button: st.S().
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Height(st.Cells(3)).
Padding(0, 2).
Border(st.SingleBorder().Color(st.RGB(70, 70, 80))).
Background(st.RGB(30, 30, 35)).
Foreground(st.RGB(200, 200, 200)).
Hover(st.S().
Border(st.SingleBorder().Color(st.RGB(0, 220, 255))).
Background(st.RGB(40, 40, 50)).
Foreground(color.White)).
Focus(st.S().
Border(st.DoubleBorder().Color(st.RGB(0, 220, 255))).
Background(st.RGB(0, 50, 100)).
Foreground(color.White).
Bold(true)),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Width(st.Percent(100)).
Height(st.Percent(100)).
OverflowY(st.OverflowAuto).
Padding(1, 2).
Background(st.RGB(18, 18, 20)),
Sidebar: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Gap(1, 0).
Width(st.Cells(22)).
Margin(0, 2, 0, 0).
Padding(1, 1).
Border(st.SingleBorder().Color(st.RGB(60, 60, 70))),
SidebarTitle: st.S().
Padding(0, 2).
Margin(0, 0, 1, 0).
Foreground(st.RGB(130, 130, 140)).
Bold(true),
Content: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Gap(1, 0).
Flex(1),
Header: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Gap(0, 2).
Margin(0, 0, 1, 0),
GridTitle: st.S().
Margin(0, 0, 1, 0).
Foreground(st.RGB(160, 160, 170)).
Bold(true),
NavHint: st.S().
Foreground(st.RGB(148, 163, 184)),
GridRow: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Gap(0, 4).
Margin(0, 0, 1, 0),
LogPanel: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
Height(st.Cells(8)).
Margin(1, 0, 0, 0).
Padding(1, 2).
Border(st.SingleBorder().Color(st.RGB(60, 60, 70))),
LogBox: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Flex(1),
LogTitle: st.S().
Margin(0, 0, 1, 0).
Foreground(st.RGB(130, 130, 140)).
Bold(true),
VirtualArrowPad: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Cells(14)).
Margin(0, 0, 0, 4).
Border(st.SingleBorder().Color(st.RGB(45, 45, 50))),
ArrowRow: st.S().
Display(st.DisplayFlex).
Gap(0, 1),
ArrowNormal: st.S().
Foreground(st.RGB(90, 90, 100)),
ArrowActive: st.S().
Foreground(st.RGB(0, 220, 255)).
Bold(true),
}
// App constructs the portable spatial focus navigation demo UI tree.
func App() dom.Element {
var (
focusLog, keyLog *el.BoxElement
arrowUp, arrowLeft, arrowDown, arrowRight *el.SpanElement
)
root := el.Box(styles.Root,
el.Box(styles.Sidebar,
el.Box(styles.SidebarTitle, "SIDEBAR MENU"),
el.Button(styles.Button, "Home").WithID("menu-home"),
el.Button(styles.Button, "Analytics").WithID("menu-analytics"),
el.Button(styles.Button, "Profile").WithID("menu-profile"),
el.Button(styles.Button, "Settings").WithID("menu-settings"),
),
el.Box(styles.Content,
el.Box(styles.NavHint,
"Arrow keys (↑↓←→): move focus · Tab: cycle · Space/Enter",
),
el.Box(styles.Header,
el.Button(styles.Button, "Tab One").WithID("tab-1").CursorNavigable(true),
el.Button(styles.Button, "Tab Two").WithID("tab-2").CursorNavigable(true),
el.Button(styles.Button, "Tab Three").WithID("tab-3").CursorNavigable(true),
),
el.Box(styles.GridTitle,
"Action Cards Grid (Move cursor with Left/Right arrows):",
),
el.Box(styles.GridRow,
el.Button(styles.Button, "Card A").WithID("card-a").CursorNavigable(true),
el.Button(styles.Button, "Card B").WithID("card-b").CursorNavigable(true),
),
el.Box(styles.GridRow,
el.Button(styles.Button, "Card C").WithID("card-c").CursorNavigable(true),
el.Button(styles.Button, "Card D").WithID("card-d").CursorNavigable(true),
),
el.Box(styles.LogPanel,
el.Box(styles.LogBox,
el.Box(styles.LogTitle, "LOG CONSOLE"),
el.Box("Focused: None").Ref(&focusLog),
el.Box("Last Key: None").Ref(&keyLog),
),
el.Box(styles.VirtualArrowPad,
el.Box(el.Span(styles.ArrowNormal, "▲").Ref(&arrowUp)),
el.Box(styles.ArrowRow,
el.Span(styles.ArrowNormal, "◄").Ref(&arrowLeft),
el.Span(styles.ArrowNormal, "▼").Ref(&arrowDown),
el.Span(styles.ArrowNormal, "►").Ref(&arrowRight),
),
),
),
),
)
var focusSub, keyDownSub, keyUpSub event.Subscription
root.OnConnect(func() {
doc := root.OwnerDocument()
if doc == nil {
return
}
focusSub = doc.AddEventListener(event.EventFocus, func(e event.Event) {
if target, ok := e.Target().EventTarget().(dom.Element); ok {
focusLog.SetText(formatFocus(doc, target))
}
}, event.Capture())
keyDownSub = doc.AddEventListener(event.EventKeyDown, func(e event.Event) {
ke, ok := e.(*event.KeyEvent)
if !ok {
return
}
keyLog.SetTextf("Last Key: %s", keyToString(ke.Key))
switch {
case ke.MatchString("up"):
arrowUp.Style(styles.ArrowActive)
case ke.MatchString("down"):
arrowDown.Style(styles.ArrowActive)
case ke.MatchString("left"):
arrowLeft.Style(styles.ArrowActive)
case ke.MatchString("right"):
arrowRight.Style(styles.ArrowActive)
}
if target := doc.CurrentFocus(); target != nil {
focusLog.SetText(formatFocus(doc, target))
}
})
keyUpSub = doc.AddEventListener(event.EventKeyUp, func(e event.Event) {
ke, ok := e.(*event.KeyEvent)
if !ok {
return
}
switch {
case ke.MatchString("up"):
arrowUp.Style(styles.ArrowNormal)
case ke.MatchString("down"):
arrowDown.Style(styles.ArrowNormal)
case ke.MatchString("left"):
arrowLeft.Style(styles.ArrowNormal)
case ke.MatchString("right"):
arrowRight.Style(styles.ArrowNormal)
}
})
})
root.OnDisconnect(func() {
if focusSub != nil {
focusSub.Cancel()
focusSub = nil
}
if keyDownSub != nil {
keyDownSub.Cancel()
keyDownSub = nil
}
if keyUpSub != nil {
keyUpSub.Cancel()
keyUpSub = nil
}
})
return root
}
func formatFocus(doc dom.Document, target dom.Element) string {
var caretStr string
if sel := doc.Selection(); sel.RangeCount() > 0 {
r := sel.GetRangeAt(0)
caretStr = fmt.Sprintf(" [Caret: %d]", r.EndOffset())
}
return fmt.Sprintf("Focused: %s#%s%s", target.TagName(), target.ID(), caretStr)
}
func keyToString(k key.Key) string {
if k.Text != "" {
return k.Text
}
switch k.Code {
case key.KeyUp:
return "up"
case key.KeyDown:
return "down"
case key.KeyLeft:
return "left"
case key.KeyRight:
return "right"
case key.KeyTab:
return "tab"
case key.KeyEscape:
return "escape"
case key.KeyEnter:
return "enter"
case key.KeySpace:
return "space"
case key.KeyBackspace:
return "backspace"
default:
return fmt.Sprintf("Code(%d)", k.Code)
}
}

Run go run ./examples/animation to mix easing curves, durations, and width/height/color tweens on a stage box.

type Styles struct {
TargetBox st.Style
Stage st.Style
Title st.Style
Hint st.Style
SectionLabel st.Style
ButtonRow st.Style
Option st.Style
OptionActive st.Style
Trigger st.Style
TriggerBusy st.Style
Spacer st.Style
StatusText st.Style
Sidebar st.Style
Separator st.Style
StageContainer st.Style
MainLayout st.Style
Root st.Style
}
var styles = Styles{
TargetBox: st.S().
Padding(0, 2).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Cells(20)).
Height(st.Cells(3)).
Border(st.DoubleBorder().Color(st.RGB(220, 220, 220))).
Background(st.RGB(138, 43, 226)).
Foreground(color.White).
Bold(true),
Stage: st.S().
Display(st.DisplayFlex).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Percent(100)).
Height(st.Percent(100)).
Border(st.SingleBorder().Color(st.RGB(60, 60, 70))).
Background(st.RGB(20, 20, 25)),
Title: st.S().
Foreground(st.RGB(0, 255, 200)).
Bold(true).
TextAlign(st.TextAlignCenter),
Hint: st.S().
Margin(0, 0, 1, 0).
Foreground(st.RGB(148, 163, 184)).
TextAlign(st.TextAlignCenter),
SectionLabel: st.S().
Margin(0, 0, 0, 1).
Bold(true),
ButtonRow: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
FlexWrap(st.FlexWrapOn).
Gap(1, 1).
Margin(0, 0, 1, 0),
Option: st.S().
Padding(0, 1).
Border(st.SingleBorder().Color(st.RGB(70, 70, 80))).
Background(st.RGB(35, 35, 45)).
Foreground(st.RGB(200, 200, 200)).
Focus(st.S().
Background(st.RGB(50, 50, 65)).
Foreground(color.White).
Border(st.SingleBorder().Color(st.RGB(255, 165, 0)))),
OptionActive: st.S().
Padding(0, 1).
Border(st.SingleBorder().Color(st.RGB(0, 255, 200))).
Background(st.RGB(0, 200, 150)).
Foreground(color.Black),
Trigger: st.S().
Margin(1, 0, 1, 0).
Padding(0, 3).
Border(st.DoubleBorder().Color(st.RGB(255, 120, 0))).
Background(st.RGB(255, 69, 0)).
Foreground(color.White).
Bold(true).
TextAlign(st.TextAlignCenter).
Focus(st.S().
Background(st.RGB(255, 99, 71)).
Border(st.DoubleBorder().Color(st.RGB(255, 215, 0)))),
TriggerBusy: st.S().
Margin(1, 0, 1, 0).
Padding(0, 3).
Border(st.DoubleBorder().Color(st.RGB(90, 90, 100))).
Background(st.RGB(60, 60, 70)).
Foreground(st.RGB(150, 150, 150)).
Bold(true).
TextAlign(st.TextAlignCenter),
Spacer: st.S().
Flex(1),
StatusText: st.S().
Foreground(st.RGB(255, 200, 0)).
Bold(true),
Sidebar: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Width(st.Cells(50)).
Height(st.Percent(100)).
Padding(1, 2).
Border(st.SingleBorder().Color(st.RGB(80, 80, 90))).
Background(st.RGB(25, 25, 35)),
Separator: st.S().
Width(st.Cells(2)),
StageContainer: st.S().
Display(st.DisplayFlex).
Flex(1).
Height(st.Percent(100)),
MainLayout: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexRow).
JustifyContent(st.JustifyBetween).
Flex(1).
Width(st.Percent(100)),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Width(st.Percent(100)).
Height(st.Percent(100)).
Padding(1, 2).
Background(st.RGB(15, 15, 20)),
}
func App() dom.Element {
var (
target *el.BoxElement
runBtn *el.ButtonElement
status *el.BoxElement
)
easing := newChoiceGroup("EaseInOutCubic", "Linear", "EaseInQuad", "EaseOutQuad", "EaseInOutCubic")
duration := newChoiceGroup("1s", "500ms", "1s", "2s", "3s")
property := newChoiceGroup("All", "Width", "Height", "Color", "All")
forward := true
root := el.Box(styles.Root,
el.Box(styles.Title, "⚡ KITE INTERACTIVE ANIMATION SHOWCASE ⚡"),
el.Box(styles.Hint,
"Choose easing, duration & property, then click ▶ Run Animation (or Tab + Space/Enter)",
),
el.Box(styles.MainLayout,
el.Box(styles.Sidebar,
el.Box(styles.SectionLabel, "Easing Function:"),
easing.row,
el.Box(styles.SectionLabel, "Duration:"),
duration.row,
el.Box(styles.SectionLabel, "Animate Property:"),
property.row,
el.Button(styles.Trigger, triggerLabel(forward)).Ref(&runBtn),
el.Box(styles.Spacer),
el.Box(styles.StatusText, "Status: Idle (At Start)").Ref(&status),
),
el.Box(styles.Separator),
el.Box(styles.StageContainer,
el.Box(styles.Stage,
el.Box(styles.TargetBox, "Kite Engine").Ref(&target),
),
),
),
)
var activePlayback *animation.Playback
setRunning := func(running bool) {
easing.locked, duration.locked, property.locked = running, running, running
if running {
runBtn.SetText("Animating...").Style(styles.TriggerBusy)
status.SetTextf("Status: Animating (%s, %s, %s)", property.value, easing.value, duration.value)
} else {
runBtn.SetText(triggerLabel(forward)).Style(styles.Trigger)
status.SetText("Status: Idle (At " + targetStateName(forward) + ")")
}
}
runBtn.OnPress(func() {
if activePlayback != nil {
return
}
setRunning(true)
from, to := animationStyles(property.value, forward)
activePlayback = target.Animate(from, to, animation.Options{
Duration: durations[duration.value],
Easing: easings[easing.value],
OnComplete: func() {
activePlayback = nil
forward = !forward
setRunning(false)
},
})
})
root.OnDisconnect(func() {
if activePlayback != nil {
activePlayback.Cancel()
activePlayback = nil
setRunning(false)
}
})
return root
}
var (
easings = map[string]animation.EasingFunction{
"Linear": animation.Linear,
"EaseInQuad": animation.EaseInQuad,
"EaseOutQuad": animation.EaseOutQuad,
"EaseInOutCubic": animation.EaseInOutCubic,
}
durations = map[string]time.Duration{
"500ms": 500 * time.Millisecond,
"1s": time.Second,
"2s": 2 * time.Second,
"3s": 3 * time.Second,
}
)
type choiceGroup struct {
row *el.BoxElement
value string
locked bool
}
func newChoiceGroup(initial string, names ...string) *choiceGroup {
group := &choiceGroup{row: el.Box(styles.ButtonRow), value: initial}
var active *el.ButtonElement
for _, name := range names {
optionStyle := styles.Option
if name == initial {
optionStyle = styles.OptionActive
}
btn := el.Button(optionStyle, name)
if name == initial {
active = btn
}
btn.OnPress(func() {
if group.locked || btn == active {
return
}
if active != nil {
active.Style(styles.Option)
}
active = btn.Style(styles.OptionActive)
group.value = name
})
group.row.AddChild(btn)
}
return group
}
func animationStyles(property string, forward bool) (st.Style, st.Style) {
from, to := st.S(), st.S()
if property == "Width" || property == "All" {
from, to = from.Width(st.Cells(20)), to.Width(st.Cells(60))
}
if property == "Height" || property == "All" {
from, to = from.Height(st.Cells(3)), to.Height(st.Cells(10))
}
if property == "Color" || property == "All" {
from, to = from.Background(st.RGB(138, 43, 226)), to.Background(st.RGB(0, 206, 209))
}
if !forward {
return to, from
}
return from, to
}
func triggerLabel(forward bool) string {
if forward {
return "▶ Run Animation (Forward)"
}
return "◀ Run Animation (Reverse)"
}
func targetStateName(forward bool) string {
if forward {
return "Start"
}
return "End"
}

Run go run ./examples/grid_animation to interpolate CSS Grid column widths between 10 and 25 cells.

type Styles struct {
GridContainer st.Style
Button st.Style
Title st.Style
ButtonWrapper st.Style
Root st.Style
}
var styles = Styles{
GridContainer: st.S().
Display(st.DisplayGrid).
Gap(1).
GridTemplateColumns(st.Repeat(3, st.Fr(1))...).
GridTemplateRows(st.Repeat(2, st.Fr(1))...).
Height(st.Cells(10)).
Padding(1).
Border(st.SingleBorder()),
Button: st.S().
Padding(0, 2).
Border(st.SingleBorder()).
Background(st.RGB(255, 69, 0)).
Foreground(color.White).
Bold(true).
Focus(st.S().
Border(st.SingleBorder().Color(st.RGB(255, 215, 0))).
Background(st.RGB(255, 99, 71)),
),
Title: st.S().
Margin(0, 0, 2, 0).
Foreground(st.RGB(0, 255, 200)).
Bold(true).
TextAlign(st.TextAlignCenter),
ButtonWrapper: st.S().
Margin(2, 0, 0, 0),
Root: st.S().
Display(st.DisplayFlex).
FlexDirection(st.FlexColumn).
Width(st.Percent(100)).
Height(st.Percent(100)).
Padding(2, 4).
Background(st.RGB(20, 20, 25)),
}
func itemStyle(bg color.Color) st.Style {
return st.S().
Display(st.DisplayFlex).
JustifyContent(st.JustifyCenter).
AlignItems(st.AlignCenter).
Width(st.Percent(100)).
Height(st.Percent(100)).
Border(st.SingleBorder()).
Background(bg).
Bold(true)
}
func App() dom.Element {
var (
gridContainer *el.BoxElement
triggerBtn *el.ButtonElement
)
const buttonLabel = "Animate Grid Tracks"
root := el.Box(styles.Root,
el.Box(styles.Title, "⚡ Grid Animation Interpolator Showcase ⚡"),
el.Box(styles.GridContainer,
el.Box(itemStyle(st.RGB(150, 50, 50)), "Item 1"),
el.Box(itemStyle(st.RGB(50, 150, 50)), "Item 2"),
el.Box(itemStyle(st.RGB(50, 50, 150)), "Item 3"),
el.Box(itemStyle(st.RGB(150, 150, 50)), "Item 4"),
el.Box(itemStyle(st.RGB(150, 50, 150)), "Item 5"),
el.Box(itemStyle(st.RGB(50, 150, 150)), "Item 6"),
).Ref(&gridContainer),
el.Box(styles.ButtonWrapper,
el.Button(styles.Button, buttonLabel).Ref(&triggerBtn),
),
)
startCols := styles.GridContainer.GridTemplateColumnsOpt().Value()
endCols := []st.GridTrackSize{st.Fr(3), st.Fr(1), st.Cells(10)}
var playback *animation.Playback
forward := true
triggerBtn.OnPress(func() {
if !playback.IsDone() {
return
}
triggerBtn.SetText("Animating...")
start, end := startCols, endCols
if !forward {
start, end = end, start
}
from := st.S().
GridTemplateColumns(start...)
to := st.S().
GridTemplateColumns(end...)
opts := animation.Options{
Duration: time.Second,
Easing: animation.EaseInOutCubic,
OnComplete: func() {
forward = !forward
triggerBtn.SetText(buttonLabel)
},
}
playback = gridContainer.Animate(from, to, opts)
})
root.OnDisconnect(func() {
playback.Cancel()
triggerBtn.SetText(buttonLabel)
})
return root
}