Styling
Start with style.S(). Builders return a new value; assign the result or pass it directly to an element. Properties use Optional[T] internally: missing and explicitly set to zero are different. Setting Bold(false) can override inherited boldness; omitting Bold does not.
c := demo.Default.Colors
base := style.S(). Padding(0, 1). Background(c.Surface). Foreground(c.Text). Border(style.RoundedBorder().Color(c.Border)). Reverse(false)
hover := style.S(). Background(c.AccentSoft). Foreground(style.Hex("#f8fafc")). Bold(true). Reverse(false)
focus := style.S(). Border(style.DoubleBorder().Color(c.Accent)). Background(c.AccentSoft). Reverse(false)
active := style.S(). Background(style.Hex("#1e496e")). Foreground(c.Accent). Bold(true). Reverse(false)
save := element.Button("Save").Style( base. Hover(hover). Focus(focus). Active(active). Disabled(style.S().Foreground(c.Muted)),)The input previews use a tinted fill instead of an outline. With no top or bottom border and no vertical padding, focus changes the colors without changing the field’s one-row height:
inputStyle := style.S().Border(style.EmptyBorder()).Padding(0, 1). Background(style.Hex("#1e293b")).Foreground(style.Hex("#e2e8f0")). Focus(style.S().Background(style.Hex("#25354f")).Foreground(style.Hex("#ffffff")))Which value wins?
Section titled “Which value wins?”The four cascade origins are inherited → element defaults → author → UA intrinsic. When debugging a value, first check whether that property inherits at all:
| Origin | What it supplies |
|---|---|
| Inherited | Parent foreground, bold, italic, underline, text alignment and wrapping, among others. Width, padding, and borders do not inherit. |
| Element defaults | The element’s normal presentation, such as inline display for a span. During resolution, parent text properties are copied over the element baseline, so a parent foreground can replace a default foreground. |
| Author | Properties you set with .Style(...), including matching media and interaction rules. |
| UA intrinsic | Properties the control needs to work. An input’s required overflow clipping wins even over an explicit author setting. |
Combine styles for application state
Section titled “Combine styles for application state”Merge overlays just the properties the argument sets. When performs that merge only if its condition is true. This badge keeps its padding and bold text in both states; only a failed build gets the warning color:
c := demo.Default.Colorsactive := false
label := element.Text("Build passing")base := style.S(). Padding(0, 1). Merge(style.S().Bold(true).Background(c.SuccessBg))warning := style.S(). Background(c.WarnBg). Foreground(c.Warn)badge := element.Box(label).Style(base)
toggle := element.Button("Toggle failure"). Style(demo.Default.Button). OnPress(func() { active = !active badge.Style(base.When(active, warning)) if active { label.SetData("Build failed") } else { label.SetData("Build passing") } })When tests the boolean when you build the style. Reapply the style if the data changes. For interaction, declare pseudo-states instead: the button above doesn’t need a handler to change its hover appearance. Hover requires a backend that reports mouse motion.
Set box edges and borders
Section titled “Set box edges and borders”For a section heading, leave space outside the box with Margin, inset its text with Padding, and draw only a bottom rule:
heading := element.Box("Build summary").Style(style.S(). Margin(1, 0).Padding(0, 2). Border(style.EmptyBorder()).BorderBottom(true, style.BorderSingle))c := demo.Default.Colorscard := func(name string, border style.Border) *element.BoxElement { return element.Box(name).Style( style.S(). Border(border). Background(c.CardBg). Padding(0, 1), )}
row := element.Box( card("Single", style.SingleBorder().Color(c.Accent)), card("Rounded", style.RoundedBorder().Color(c.Success)), card("Double", style.DoubleBorder().Color(c.Warn)), card("Empty", style.EmptyBorder()),).Style(demo.Default.ButtonRow)| Need | Builder or value |
|---|---|
| Space on all sides | Padding(n) or Margin(n) |
| Different vertical and horizontal space | Padding(vertical, horizontal); Margin takes the same forms |
| Four independent sides | Padding(top, right, bottom, left) or the corresponding Margin call; there is no three-value shorthand |
| Adjust one side | For example, PaddingLeft(2), MarginBottom(1), or BorderTop(false) after a shorthand |
| Draw an outline | Pass style.SingleBorder(), style.RoundedBorder(), or style.DoubleBorder() to Border; all occupy one cell per visible edge |
| Start without an outline | style.EmptyBorder() leaves every edge off; enable only the sides you need |
Choose a color representation
Section titled “Choose a color representation”| Source | Use |
|---|---|
| Channel values | style.RGB(r, g, b) is opaque; style.RGBA(r, g, b, a) also takes alpha. All channels are bytes. |
| Known hex literal | style.Hex("#fc0") accepts 3, 6, or 8 hex digits with an optional #; invalid input returns opaque black. |
| User-supplied hex string | style.ParseHex(text) returns a color and an error so you can reject a typo. |
| Numeric RGB literal | style.HexInt(0xffcc00) takes a 24-bit RGB value, not a palette index. |
| Host foreground or background | Pass style.TerminalDefault rather than guessing a contrasting RGB value. |
For a theme setting, report an invalid color instead of silently turning the text black:
func accentStyle(hex string) (style.Style, error) { accent, err := style.ParseHex(hex) if err != nil { return style.S(), err } return style.S().Foreground(accent), nil}Respond to viewport size
Section titled “Respond to viewport size”Use media rules when the condition is terminal size rather than application state. This column becomes a row when at least 80 cells are available across the viewport:
wide := style.MediaQuery{}.MinWidth(80)layout := style.S(). Display(style.DisplayFlex). FlexDirection(style.FlexColumn). Media(wide, style.S().FlexDirection(style.FlexRow))Preferred color scheme
Section titled “Preferred color scheme”ColorScheme is the host’s background preference, not a palette. Supply a base style that works when the preference is unknown. A dark or light rule matches only that preference, and every constraint in a chained query must match. The buttons simulate a change in host preference:
c := demo.Default.Colorsdark := style.S().Background(c.CardBg).Foreground(c.Text)light := style.S().Background(style.Hex("#fafafa")).Foreground(style.Hex("#222"))
label := element.Text("Panel")preferLight := style.Query().ColorScheme(style.ColorSchemeLight)panel := element.Box(label).Style( dark.Padding(0, 2).Media(preferLight, light),)
choice := func(name string, scheme style.ColorScheme) *element.ButtonElement { return element.Button(name). Style(demo.Default.Button). OnPress(func() { simulateColorScheme(panel, scheme) label.SetData(name + " panel") })}
buttons := element.Box( choice("Dark", style.ColorSchemeDark), choice("Light", style.ColorSchemeLight),).Style(demo.Default.ButtonRow)The terminal backend reads COLORFGBG as an initial hint (15;0 means dark; 0;15 means light). On interactive terminals it also queries the background with OSC 11 at startup and on resume. A reply can update the hint. In a browser, Kite uses matchMedia("(prefers-color-scheme: dark)") and listens for changes. If detection is unavailable, the scheme stays style.ColorSchemeUnknown; Kite does not guess a theme. NO_COLOR affects terminal color output, independently of this preference.
Media styles update automatically. For component logic, kitex.UseColorScheme() returns the current scheme and re-renders on changes. Imperative code can read doc.Terminal().Window().ColorScheme() and listen for event.EventColorSchemeChange on the document. In headless tests, use env.SetColorScheme(...) followed by env.Flush().
Organizing styles with a Theme struct
Section titled “Organizing styles with a Theme struct”As an app grows, keep color tokens in a Palette, derive reusable style.Style presets once in a newTheme(p Palette) constructor, and share the Theme through kitex.CreateContext. For styles used by only one component, group them in a small component-local struct instead of loose package globals.
type Palette struct { Surface color.Color Border color.Color Text color.Color Accent color.Color AccentBg color.Color}
type Theme struct { Card style.Style Button style.Style}
func newTheme(p Palette) Theme { hover := style.S(). Background(p.AccentBg). Foreground(p.Accent). Bold(true)
return Theme{ Card: style.S(). Border(style.SingleBorder().Color(p.Border)). Background(p.Surface). Foreground(p.Text). Padding(1, 2), Button: style.S(). Border(style.EmptyBorder()). Padding(0, 1). Background(p.Surface). Foreground(p.Text). Hover(hover). Focus(hover), }}
var darkTheme = newTheme(Palette{ Surface: style.Hex("#182235"), Border: style.Hex("#334866"), Text: style.Hex("#e2e8f0"), Accent: style.Hex("#38bdf8"), AccentBg: style.Hex("#163a59"),})
var themeCtx = kitex.CreateContext(darkTheme)
var statusCard = kitex.FC("StatusCard", func(kitex.NoProps) kitex.Node { theme := kitex.UseContext(themeCtx) return kitex.Box.Style( theme.Card, kitex.Button(kitex.ButtonProps{ Style: theme.Button, }, kitex.Text("Refresh")), )})Wrap the root in themeCtx.Provider(lightTheme, app) when switching themes at runtime; consumers reading kitex.UseContext(themeCtx) update automatically.