Empty
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/empty.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"
)
func init() {
registerSection("empty", "display", func() core.Widget { return el.Embed(emptyGallery{}) })
}
type emptyGallery struct{}
func (emptyGallery) Render(cx *el.Context) el.Element {
return el.Div().W(el.Full).Gap(16).Child(
kit.Empty(demoText("No orders yet", "暂无订单")).Description(demoText("New orders will appear here.", "创建订单后将在这里显示。")).Icon(kit.IconInbox).Action(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(8).Name(demoText("New order", "新建订单")).OnClick(func() {}).Child(el.Text(demoText("New order", "新建订单")))
})).Render(cx),
el.Div().W(el.Dp(180)).Child(kit.Empty(demoText("No search results 0", "没有搜索结果 0")).Description(demoText("Try another search query or adjust the filters.", "请尝试其他关键词 Search again,或调整筛选条件。")).Render(cx)),
kit.Empty(demoText("No notifications", "暂无通知")).Icon(kit.IconNone).Heading(kit.Tag(demoText("No notifications", "暂无通知"))).DescriptionContent(el.ViewFunc(func(*el.Context) el.Element {
return el.Text(demoText("Notifications appear here", "通知将显示在这里")).Bold()
})).Footer(kit.Button(demoText("About notification settings", "了解通知设置"), func() {}).Variant(kit.ButtonLink)).PartStyle(kit.EmptyPartRoot, func(e *el.DivEl) { e.Bg(theme.Subtle).Rounded(theme.RadiusLg) }).Render(cx),
kit.Empty(demoText("Alex has not joined yet", "Alex 尚未加入")).Media(kit.Avatar("Alex").Size(56)).Description(demoText("Invite members to collaborate.", "邀请成员一起协作。")).Action(kit.Button(demoText("Invite members", "邀请成员"), func() {})).Render(cx),
kit.Empty(demoText("Drop files here", "拖动文件到这里")).Description(demoText("Outline style, suitable for upload areas.", "描边样式,适合上传区域。")).Icon(kit.IconFile).Variant(kit.EmptyOutline).Render(cx),
kit.Empty(demoText("Nothing scheduled this week", "这一周没有安排")).Description(demoText("Soft background style for cards and panels.", "浅底样式,放在卡片或面板里。")).Icon(kit.IconCalendar).Variant(kit.EmptyMuted).Render(cx),
)
}
kit.Empty("No orders yet").Description("Create an order to get started").Icon(kit.IconInbox).Action(action) displays the icon, title, description and operating area vertically centered. Action accepts an el.View and its Render is called every frame. The operation area usually uses kit.Button, and a custom view can also be passed in.
SetTitle, SetDescription update copy. Empty does not add new semantic roles, the title and description are used as text respectively, and the operation elements retain their own semantics and keyboard behavior. Narrow container text wraps. Verify: go run ./examples/components -section empty, add -theme dark to check the dark theme.
kit.Empty("No orders yet").Action(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().OnClick(createOrder).Child(el.Text("New order").TextColor(theme.Primary))
}))
Media(view) Place an avatar, image, avatar group, or any custom content above the title, preserving the content's own size, semantics, and interactivity. The last call replaces the previous content; passing nil restores the Icon configuration, and Icon(kit.IconNone) hides the default icon. Media, title, and description changes do not re-establish the Action's input state or focus. The media still needs to fit the parent container width; scrolling is provided by the parent container.
kit.Empty("No members yet").Media(kit.Avatar("Alex").Size(48)).Action(kit.Button("Invite", invite))
Heading(view) and DescriptionContent(view) replace the string display of the title and description, and pass nil to restore the string; at this time, SetTitle / SetDescription updates the fallback copy. Footer(view) Add independent auxiliary content below Action, pass nil to remove. The semantics of rich content are provided by the content itself, without repeated declaration of replaced strings.
PartStyle(part, func(*el.DivEl)) adjusts the partition after the default style every frame, supporting EmptyPartRoot, EmptyPartHeader, EmptyPartMedia, EmptyPartTitle, EmptyPartDescription, EmptyPartContent (Action), EmptyPartFooter. Background, border, rounded corners, width, spacing, alignment and inherited font size/color can be set; explicit styles of sub-content take precedence. Pass nil to restore the default style of the partition, and illegal parts are ignored. Callbacks do not retain element references; subpartition IDs are fixed by the component to preserve operation area state.
kit.Empty("No results").
Heading(kit.Tag("No results")).
Footer(kit.Button("View help", help).Variant(kit.ButtonLink)).
PartStyle(kit.EmptyPartRoot, func(e *el.DivEl) {
e.Bg(theme.Subtle).Rounded(theme.RadiusLg)
})
The default layout still retains Keel's Surface background and spacing; Empty does not add automatic announcements or focus targets, and the content buttons and input boxes maintain their own behaviors.
Default style
Variant(kit.EmptyOutline) displays a dotted rounded border and a transparent bottom, which is suitable for uploading and dragging areas; Variant(kit.EmptyMuted) uses theme.Subtle with a shallow bottom and rounded corners, which is suitable for placing in cards and panels. The default EmptyPlain maintains the original Surface background color. Further adjustments can still be made later using PartStyle(kit.EmptyPartRoot, ...).