Button
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/button.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"fmt"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
"github.com/dyike/keel/ui/theme"
"image/color"
)
func init() { registerSection("button", "general", buttonGallery) }
type buttonDemo struct {
save *kit.ButtonView
toggle *kit.ButtonView
count int
loading bool
bold *kit.ButtonView
}
func buttonGallery() core.Widget {
v := &buttonDemo{}
v.save = kit.Button(demoText("Save 123", "保存 Save 123"), func() { v.count++ }).Icon(kit.IconPlus)
v.toggle = kit.Button(demoText("Toggle loading state", "切换加载状态"), func() { v.loading = !v.loading; v.save.SetLoading(v.loading) }).Variant(kit.ButtonSecondary)
v.bold = kit.Button(demoText("Bold", "加粗"), nil).Variant(kit.ButtonGhost).Selected(true)
return el.Embed(v)
}
func (v *buttonDemo) Render(cx *el.Context) el.Element {
row := func(views ...el.View) el.Element {
d := el.Div().Wrap().Gap(12).Items(el.Center)
for _, view := range views {
d.Child(view.Render(cx))
}
return d
}
rich := el.ViewFunc(func(*el.Context) el.Element {
return el.Div().Row().Gap(theme.SpaceSm).Child(el.Text(demoText("Export", "导出")), el.Text("CSV").Bold())
})
custom := func(a kit.ButtonAppearance) kit.ButtonAppearance {
a.Background = color.NRGBA{R: 110, G: 60, B: 170, A: 255}
a.Hover, a.Active = a.Background, a.Background
a.Foreground, a.HoverForeground, a.ActiveForeground = color.NRGBA{R: 255, G: 255, B: 255, A: 255}, color.NRGBA{R: 255, G: 255, B: 255, A: 255}, color.NRGBA{R: 255, G: 255, B: 255, A: 255}
return a
}
disabled := kit.Button(demoText("Disabled", "不可操作"), nil)
disabled.SetDisabled(true)
return el.Div().P(24).Gap(16).Child(
el.Text(demoText("Button", "按钮")).TextSize(24).Bold(),
el.Text(demoText("Tab focuses; Enter / Space activates. Loading and disabled buttons do not respond.", "Tab 聚焦;Enter / Space 激活。加载和禁用时不响应。")),
row(kit.Button(demoText("Primary", "主要"), nil), kit.Button(demoText("Secondary", "次要"), nil).Variant(kit.ButtonSecondary), kit.Button(demoText("Lightweight", "轻量"), nil).Variant(kit.ButtonGhost), kit.Button(demoText("Delete", "删除"), nil).Variant(kit.ButtonDanger)),
row(kit.Button(demoText("Small 28", "小 28"), nil).Size(28), kit.Button(demoText("Standard 32", "标准 32"), nil), kit.Button(demoText("Large 40", "大 40"), nil).Size(40)),
row(kit.Button(demoText("Link", "链接"), nil).Variant(kit.ButtonLink), kit.Button(demoText("Text", "文字"), nil).Variant(kit.ButtonText), kit.Button(demoText("Success", "成功"), nil).Variant(kit.ButtonSuccess), kit.Button(demoText("Warning", "警告"), nil).Variant(kit.ButtonWarning), kit.Button(demoText("Info", "信息"), nil).Variant(kit.ButtonInfo)),
row(kit.Button(demoText("Tinted", "着色"), nil).Outline(true), kit.Button(demoText("Danger tinted", "危险着色"), nil).Variant(kit.ButtonDanger).Outline(true), kit.Button(demoText("Compact", "紧凑"), nil).Compact(true), kit.Button("", nil).Name(demoText("Add", "添加")).Icon(kit.IconPlus)),
row(kit.Button(demoText("Export CSV", "导出 CSV"), nil).Content(rich), kit.Button(demoText("Exporting", "正在导出"), nil).Content(rich).Loading(true), kit.Button(demoText("Custom palette", "自定义配色"), nil).Appearance(custom)),
row(v.save, v.toggle),
el.Text(fmt.Sprintf(demoText("Save count: %d", "保存次数:%d"), v.count)).TextColor(theme.Muted),
row(kit.Button(demoText("Loading", "加载中"), nil).Loading(true), kit.Button(demoText("Loading icon", "加载图标"), nil).Icon(kit.IconPlus).Loading(true), disabled),
el.Div().W(el.Dp(140)).Child(kit.Button(demoText("Narrow container: English 123", "窄容器中文 English 123"), nil).Render(cx)),
el.Text(demoText("Selected state", "选中状态")).TextSize(16).Bold(),
row(kit.Button(demoText("Selected", "已选中"), nil).Selected(true), kit.Button(demoText("Secondary selected", "次要已选中"), nil).Variant(kit.ButtonSecondary).Selected(true),
kit.Button(demoText("Tinted selected", "着色已选中"), nil).Outline(true).Selected(true), v.bold),
)
}
el-based action button that supports mouse, keyboard, icon and loading state. Default Primary appearance, 32dp height, 6dp rounded corners; color reads current theme in Render.
save := kit.Button("Save", saveOrder).Icon(kit.IconPlus)
deleteButton := kit.Button("Delete", deleteOrder).Variant(kit.ButtonDanger)
Variant Accepts ButtonVariant: ButtonPrimary (zero value), ButtonSecondary, ButtonGhost, ButtonDanger, ButtonLink, ButtonText, ButtonSuccess, ButtonWarning, ButtonInfo. Only Variant is used to configure the appearance, and shortcut entries such as Danger are not provided. Size(float32) specifies the height dp, 28 / 32 / 40 is recommended; non-positive numbers and non-finite numbers are ignored. Narrow containers are limited to a single row, respecting the available width.
Outline(true) is a compatibility name for the borderless tinted style: the background is transparent, the text uses the variant color, hover uses Subtle, and active uses SubtleHover. Compact(true) reduces horizontal padding while retaining height. Link has no horizontal padding by default, and Text retains padding. Buttons with only icons and no copy are square, and use Name to set the accessible name.
Content(el.View) replaces the visible text and icon, and returns it by passing nil. The content should be a display element, without nested buttons or input boxes; the copy (or Name) when constructed should still be used as an accessible name. Hide the drawing of content while loading and preserve the layout, showing a progress ring in the center.
Appearance(func(ButtonAppearance) ButtonAppearance) modifies the current variant coloring every Render, including normal, hover, pressed, and keyboard-focus colors. ButtonAppearance.Border is deprecated and ignored. Pass nil to restore the theme color; custom color matching does not use PrimaryGradient. Success, warning, and information variants choose black or white text according to the background color. Disabled buttons remain borderless and use the theme disabled colors.
Icon(IconName) configures the front icon. Loading(bool) sets the loading state, SetLoading can be updated in the callback. Reuse the spinner's animation when loading: replace the icon when there is an icon, retain the measured size of the copy when there is no icon and draw the spinner centered on it, so the button size remains unchanged before and after switching. Shows a static progress ring when the Reduce Animation setting is on.
SetText updates the copy, SetDisabled updates the disabled state, neither triggers a click callback. Background updates must be placed in core.Update. Maintain focus and tab order when loading, ignore only activations; do not show hover, press appearance, use default cursor. When disabled, it does not participate in Tab navigation; the Disabled of the parent element also limits button interaction, and is processed as disabled when disabled and loaded at the same time. Ghost, Link, Text, and Outline maintain a transparent background when disabled.
Tab / Shift+Tab focuses; Space / Enter activates once when released. Hover, Active and FocusStyle provide hover, press and focus appearance respectively without changing the layout. Buttons draw no persistent border in default, hover, active, selected, disabled, or loading states. Keyboard/programmatic focus alone draws the inset focus ring; pointer focus does not.
The Agent role is button, and the name is text; when loading, the value is loading, and disabled is false; disabled is true when only itself or the parent element is disabled. The icon and spinner are button internal decorations and are not included in the snapshot separately.
Verify: go run ./examples/components -section button, add -theme dark to check the dark theme. Examples cover nine variants, tinted/compact configurations, custom content/colorways, three sizes, icons, disabled, loading toggles, and narrow containers.
Selected state
Selected(true) / SetSelected(on) displays the button as selected, such as the current view in the toolbar and enabled filtering; the Agent sees selected as true. Solid buttons darken; secondary, lightweight, text, and tinted buttons use the Highlight background with PrimaryText. Clicking does not toggle automatically; set it in OnClick. For mutually exclusive options, use ToggleGroup.
Button group see ButtonGroup.
Button size sets a minimum height. Text and custom content can grow the button vertically with accessibility font scaling, retaining vertical padding.