Keel v0.5.1 English GitHub

Empty

在线示例:这是真实的 Keel 程序编译成 WebAssembly,可以直接操作。首次加载约 10 MB。全屏打开
示例代码Go · 可复制

这是上方组件库展示案例的完整源码。examples/components/empty.go

在脚手架项目中复用组件构造和 Render 写法;示例的 registerSection 与共享辅助函数由组件库提供。

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("暂无订单").Description("新建订单开始").Icon(kit.IconInbox).Action(action) 垂直居中显示图标、标题、说明与操作区。Action 接受 el.View,每帧调用其 Render。操作区通常使用 kit.Button,也可以传入自定义视图。

SetTitle、SetDescription 更新文案。Empty 不新增语义角色,标题和说明分别作为 text,操作元素保留自身语义与键盘行为。窄容器文字换行。验证:go run ./examples/components -section empty,加 -theme dark 检查深色。

kit.Empty("暂无订单").Action(el.ViewFunc(func(cx *el.Context) el.Element {
    return el.Div().OnClick(createOrder).Child(el.Text("新建订单").TextColor(theme.Primary))
}))

Media(view) 在标题上方放置头像、图片、头像组或任意自定义内容,保留内容自身的尺寸、语义和交互。后一次调用替换前一次内容;传 nil 恢复 Icon 配置,Icon(kit.IconNone) 隐藏默认图标。媒体、标题和说明变化不会重建 Action 的输入状态或焦点。媒体仍需适配父容器宽度;滚动由父容器提供。

kit.Empty("暂无成员").Media(kit.Avatar("Alex").Size(48)).Action(kit.Button("邀请", invite))

Heading(view) 和 DescriptionContent(view) 替换标题、描述的字符串展示,传 nil 恢复字符串;此时 SetTitle / SetDescription 更新的是回退文案。Footer(view) 在 Action 下方添加独立辅助内容,传 nil 移除。富内容的语义由内容自身提供,不重复宣布被替换的字符串。

PartStyle(part, func(*el.DivEl)) 每帧在默认样式之后调整分区,支持 EmptyPartRoot、EmptyPartHeader、EmptyPartMedia、EmptyPartTitle、EmptyPartDescription、EmptyPartContent(Action)、EmptyPartFooter。可设置背景、边框、圆角、宽度、间距、对齐和继承字号/颜色;子内容显式样式优先。传 nil 恢复该分区默认样式,非法 part 忽略。回调不要保留元素引用;子分区 ID 由组件固定,以保留操作区状态。

kit.Empty("没有结果").
    Heading(kit.Tag("没有结果")).
    Footer(kit.Button("查看帮助", help).Variant(kit.ButtonLink)).
    PartStyle(kit.EmptyPartRoot, func(e *el.DivEl) {
        e.Bg(theme.Subtle).Rounded(theme.RadiusLg)
    })

默认布局仍保留 Keel 的 Surface 背景和间距;Empty 不新增自动播报或焦点目标,内容的按钮与输入框保持自身行为。

预设样式

Variant(kit.EmptyOutline) 显示虚线圆角边框、透明底,适合上传、拖放区域;Variant(kit.EmptyMuted) 用 theme.Subtle 浅底和圆角,适合放在卡片、面板里。默认 EmptyPlain 保持原来的 Surface 底色。之后仍可以用 PartStyle(kit.EmptyPartRoot, ...) 进一步调整。