Keel v0.5.1 中文 GitHub

Alert

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/alert.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/markdown"
)

func init() {
	registerSection("alert", "feedback", func() core.Widget {
		return el.Embed(&alertGallery{closable: kit.Alert(demoText("Dismissible alert", "可关闭提示")).Description(demoText("Can be restored after closing", "关闭后可以恢复")).OnClose(func() {})})
	})
}

type alertGallery struct{ closable *kit.AlertView }

func (v *alertGallery) Render(cx *el.Context) el.Element {
	return el.Div().W(el.Full).Gap(12).Child(
		kit.Alert(demoText("Maintenance banner", "维护横幅")).Banner(true).Size(kit.AlertSizeSmall).Icon(kit.IconCalendar).Description(demoText("Maintenance at 22:00 tonight. Save your work beforehand.", "今晚 22:00 维护,请提前保存工作。")).Render(cx),
		kit.Alert(demoText("Rich content", "富内容")).Content(markdown.New(demoText("**Payment incomplete**. Check:\n- Card details\n- Account balance", "**支付未完成**,请检查:\n- 卡片信息\n- 账户余额"))).Icon(kit.IconReceipt).Render(cx),
		kit.Alert(demoText("Compact alert", "紧凑提示")).Size(kit.AlertSizeXSmall).Icon(kit.IconNone).Description(demoText("Severity remains identifiable without an icon.", "无图标也保留等级标识。")).Render(cx),
		kit.Alert(demoText("Large alert", "大号提示")).Size(kit.AlertSizeLarge).Tone(kit.ToneSuccess).Render(cx), v.closable.Render(cx), el.Div().OnClick(func() { v.closable.SetVisible(true) }).P(8).Child(el.Text(demoText("Restore alert", "恢复提示"))),
		kit.Alert(demoText("Info", "提示")).Description(demoText("3 new messages. You can view them later.", "有 3 条新消息,稍后也可以查看。")).Render(cx),
		kit.Alert(demoText("Success", "成功")).Description(demoText("Order SO-123 saved.", "订单 SO-123 已保存。")).Tone(kit.ToneSuccess).Render(cx),
		kit.Alert(demoText("Warning", "警告")).Description(demoText("Unstable connection. Check your network and try again.", "连接不稳定,请检查网络后重试。")).Tone(kit.ToneWarning).Render(cx),
		kit.Alert(demoText("Danger", "失败")).Description(demoText("Save failed. Your input has been preserved.", "保存失败,输入内容已保留。")).Tone(kit.ToneDanger).Render(cx),
		el.Div().W(el.Dp(180)).Child(kit.Alert(demoText("Narrow container 123", "窄容器 123")).Description(demoText("Mixed text should wrap naturally without clipping.", "中英文 Mixed text should wrap naturally without clipping.")).Render(cx)),
	)
}

kit.Alert("Save failed").Tone(kit.ToneDanger).Description("Network unavailable").OnClose(fn) Displays inline hints. The default is ToneInfo; Tone supports ToneNeutral, ToneInfo, ToneSuccess, ToneWarning, and ToneDanger. SetTone, SetTitle, SetDescription can be updated programmatically.

The left level bar and icons are colored according to the theme, the title is bold, and the default description is 13sp Muted.

  • Size(AlertSizeXSmall/AlertSizeSmall/AlertSizeMedium/AlertSizeLarge) sets four levels of font size, icon and padding. The default is Medium to retain the original layout; illegal values are ignored.
  • Icon(IconName) replaces the level icon, IconNone hides the icon and its spacing.
  • Content(el.View) replaces the description, and can pass Markdown document, rich text or action button; pass nil to restore Description. The text control retains its own keyboard and click behavior, and inherits Alert's disabled state.
  • Banner(true) uses a full-width, right-angled, borderless dyed banner, without displaying a separate title line; displays Content or Description, and uses the title as the message when there is no body text. The Close button and Agent name still use the title. Pass false to restore inline hints.
notice := kit.Alert("Maintenance notice").Banner(true).
    Description("Scheduled maintenance tonight").Icon(kit.IconCalendar).
    Size(kit.AlertSizeSmall)
// Rich text is composed by the application: notice.Content(markdown.New("**NOTE**: Please save your work first"))

The close button is displayed only when OnClose is set; Space/Enter is closed after click or Tab focus, and is hidden first and then called back. Visible queries the status, SetVisible does not call the callback when restoring or hiding.

Agent container role alert, the name is the title, and the value is the level. The description and close button are individually readable. Narrow container text wraps. Run go run ./examples/components -section alert -theme dark; omit theme to see the light themes.

SetDisabled(true) disables closing and passes disabled semantics to the button; it can be refocused and closed after being restored, and the visible and hidden state will not be lost due to measurement or theme switching.