Keel v0.5.1 中文 GitHub

Badge

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/badge.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"
	"image/color"
)

func init() {
	registerSection("badge", "display", func() core.Widget {
		unread := kit.Badge(9)
		unread.Child(kit.Button(demoText("Notifications", "通知"), func() { unread.SetValue(0) }).Variant(kit.ButtonSecondary))
		online := kit.Badge(1).Dot().Tone(kit.ToneSuccess).Child(kit.Avatar(demoText("Alex Chen", "张三")))
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Wrap().Gap(24).Items(el.Center).Child(
				unread.Render(cx),
				kit.Button(demoText("Add unread message", "增加未读"), func() { unread.SetValue(unread.Value() + 1) }).Variant(kit.ButtonGhost).Render(cx),
				kit.Badge(120).Render(cx), online.Render(cx),
				kit.Badge(0).Icon(kit.IconCheck).Name(demoText("Verified", "已验证")).Tone(kit.ToneSuccess).Child(kit.Avatar("Ada")).Render(cx),
				kit.Badge(0).Icon(kit.IconStar).Name(demoText("Star", "收藏")).Size(24).Color(color.NRGBA{R: 255, G: 210, A: 255}).Child(kit.Avatar(demoText("Lin", "林"))).Render(cx),
				kit.Badge(3).Size(12).Render(cx), kit.Badge(12).Size(18).Render(cx), kit.Badge(999).Size(24).Max(999).Render(cx))
		}))
	})
}

A number, dot or icon logo that can be displayed alone or hung on a subcomponent corner.

unread := kit.Badge(3).Child(kit.Button("Notifications", open))
kit.Badge(1).Dot().Tone(kit.ToneSuccess).Child(avatar)
  • In number and dot modes, hide when the count is less than or equal to 0. Displays "99+" when exceeding Max (default 99).
  • When hanging on a sub-component, the corner mark is drawn on the corner without changing the size and position of the sub-component, and the count changes will not cause the layout to jump.
  • Icon(IconName) switches to icon mode regardless of count and is displayed even if count is 0. Icon(IconNone) Restores numeric mode; Dot switches to dot and clears icon. Icon mode is in the lower right corner with a Surface-colored border; numbers and dots are in the upper right corner.
  • Size(dp) Set number/icon height, default 18, accepts 12–128; recommended 12/18/24. The dots are scaled 8/18, and the width grows with the text when the numbers are too long. Illegal values are ignored.
  • Color(color.NRGBA) specifies the background color, and the foreground automatically selects black and white according to the background color. Tone Sets the theme semantic color (default ToneDanger) while clearing the Color override. Fixed custom colors not automatically changing with the theme.
  • Name(string) Sets the accessible name, appropriate for the icon state, such as "verified"; an empty string restores the original count name. Value() / SetValue operation count, does not change icon mode.

Agent: role badge, the default name is the original count, which can be overridden by Name; value is the displayed number ("99+"), dot or icon.

Verify: go run ./examples/components -section badge, add -theme dark to check the dark theme.