Tag
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/tag.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 (
"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("tag", "display", func() core.Widget {
return el.Embed(&tagGallery{interactive: kit.Tag(demoText("Selectable and removable", "可选择和移除")).Selectable()})
})
}
type tagGallery struct {
interactive *kit.TagView
removed bool
}
func (v *tagGallery) Render(cx *el.Context) el.Element {
v.interactive.OnRemove(func() { v.removed = true })
custom := func(a kit.TagAppearance) kit.TagAppearance {
a.Background = color.NRGBA{R: 110, G: 60, B: 170, A: 255}
a.Foreground = color.NRGBA{R: 255, G: 255, B: 255, A: 255}
a.Border = color.NRGBA{}
return a
}
rich := el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().Row().Gap(theme.SpaceXs).Child(kit.Icon(kit.IconCheck).Size(14).Color(theme.Success).Render(cx), el.Text(demoText("Verified", "已验证")).Bold())
})
root := el.Div().P(theme.SpaceXl).Gap(16).Items(el.Start).Child(
el.Div().Row().Wrap().Gap(8).Child(kit.Tag(demoText("Default 123", "默认 123")).Render(cx), kit.Tag(demoText("Primary", "主要")).Tone(kit.ToneInfo).Render(cx), kit.Tag(demoText("Done", "完成")).Tone(kit.ToneSuccess).Render(cx), kit.Tag(demoText("To check", "待检查")).Tone(kit.ToneWarning).Render(cx)),
)
root.Child(el.Div().Wrap().Gap(theme.SpaceSm).Child(kit.Tag(demoText("Outline", "描边")).Tone(kit.ToneInfo).Outline(true).Render(cx), kit.Tag(demoText("Square corners", "直角")).Rounded(0).Render(cx), kit.Tag(demoText("Custom colors", "自定义颜色")).Appearance(custom).Rounded(4).Render(cx)),
el.Div().Wrap().Gap(theme.SpaceSm).Items(el.Center).Child(kit.Tag(demoText("Compact 20", "紧凑 20")).Size(20).Render(cx), kit.Tag(demoText("Standard 28", "标准 28")).Size(28).Render(cx), kit.Tag(demoText("Large 32", "大号 32")).Size(32).Render(cx)), kit.Tag(demoText("Verified", "已验证")).Tone(kit.ToneSuccess).Content(rich).Outline(true).Render(cx))
if !v.removed {
root.Child(v.interactive.Render(cx))
}
return root
}
kit.Tag("Urgent").Tone(kit.ToneDanger) Display dye capsule label. Tone supports ToneNeutral, ToneInfo, ToneSuccess, ToneWarning, ToneDanger, and the default is ToneNeutral; the background is mixed with the current theme Surface and grade color, and SetText updates the copy.
label := kit.Tag("Verified").Tone(kit.ToneSuccess).Outline(true).Size(20).Rounded(4)
Outline(true)uses a stroke and a transparent background; uses SelectedBackground to indicate status when selected.Size(dp)sets the minimum height of the content area, minimum 16dp; 20/28/32 suitable for compact, standard and large labels. Small reduces the font size and padding at the same time; long text still wraps, and strokes can increase the outer size. The original natural height is retained by default.Rounded(dp)specifies rounded corners, 0 is right angle; the default istheme.RadiusFullcapsule. Dimensions and rounded corners ignore illegal and non-finite values.Content(el.View)replaces the visible text, and restores it by passing nil. Transmit the display content without nesting interactive controls; the text during construction is still used as the Agent name and select/remove action name.Appearance(func(TagAppearance) TagAppearance)modifies Background, Foreground, Border, and SelectedBackground based on the theme color matching of each frame. Transparent colors are valid, non-transparent custom Borders will display borders; passing nil restores the theme. The callback is executed after Outline's default transparent background, so the background can be overridden explicitly.
Agent role tag, the name is the construction copy, and the value retains the Tone name; the custom color does not change the semantic level. The presentation-only version does not respond to the keyboard. Selectable().OnChange(fn) enables selection; Value/SetValue queries and program assignments. Programmatic assignments do not trigger callbacks. OnRemove(fn) adds an independent remove button, Space/Enter or Backspace/Delete is triggered after Tab is focused; the caller is responsible for deleting data, and the component will not hide itself. Both SetDisabled and the ancestor Disabled prohibit interaction, and the Agent reports disabled and selected. Removing does not also toggle the selected state.
Multiple tags are combined using el.Wrap. Verification: go run ./examples/components -section tag -theme dark, omit theme to see the light theme. Examples show stroke, size, angle, color, rich content, selection, and removal.