Keel v0.5.1 中文 GitHub

Tooltip

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/tooltip.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"
)

func init() {
	registerSection("tooltip", "overlays", func() core.Widget { return el.Root(newTooltipGallery()) })
}

type tooltipGallery struct{ copy, save *kit.TooltipView }

func newTooltipGallery() *tooltipGallery {
	core.Bind("gallery.save", "mod+s")
	return &tooltipGallery{
		copy: kit.WithTooltip(kit.Button("", nil).Name(demoText("Copy", "复制")).Icon(kit.IconCopy).Variant(kit.ButtonGhost), demoText("Copy (⌘C)", "复制 Copy(⌘C)")),
		save: kit.WithTooltip(kit.Button(demoText("Save", "保存"), nil), demoText("Save locally", "保存到本地")).Action("gallery.save").Placement(el.Bottom, el.Start).Offset(8).
			Content(el.ViewFunc(func(*el.Context) el.Element {
				return el.Div().Gap(4).Child(el.Text(demoText("Save", "保存")).Bold(), el.Text(demoText("Keep up to 30 versions", "最多保留 30 个版本")))
			})),
	}
}

func (g *tooltipGallery) Render(cx *el.Context) el.Element {
	return el.Div().P(48).Gap(16).Items(el.Start).Child(
		el.Text(demoText("Tooltip: hover for 0.5 seconds to show, focus with Tab to show immediately, or press Esc to hide.", "Tooltip:悬停 0.5 秒显示,Tab 聚焦立即显示,Esc 隐藏")).Bold(),
		el.Div().Row().Gap(12).Child(g.copy.Render(cx), g.save.Render(cx)),
	)
}

Add a short reminder to any view.

copy := kit.WithTooltip(kit.Button("", doCopy).Icon(kit.IconCopy), "Copy (⌘C)")
  • When to display: Displayed after the pointer stays at kit.TooltipDelay (500ms); displayed immediately when the keyboard focus enters the wrapped view.
  • When to hide: pointer moves away, focus leaves, press Esc. After pressing Esc to hide it, it will not be displayed again until the pointer and focus leave once.
  • Display position: Centered at the top by default, flip to the bottom if it cannot fit.
  • The prompt itself cannot receive focus and does not respond to clicks.
  • SetText is used to update the prompt text, such as changing it to "Copied" after copying.

Agent: The role is tooltip, the name is the prompt text; rich content and action keys are listed as child elements.

Verification: go run ./examples/components -section tooltip.

The hover delay is bound to the actual visible and enabled target, and will stop waiting when removed, disabled, or modally blocked; resume waiting for the full delay again. SetDisabled(true) Disables both the prompt and the target area. The prompt width will be limited by the current window. The delays of multiple Tooltips are calculated independently and the waiting time of the previous target is not used.

Content(view) replaces the display content and supports multi-line text, icons, etc.; the original text is retained as a semantic name and must be non-empty. Pass nil to restore plain text. Controls in rich content are disabled and will not gain focus or perform clicks; please use HoverCard for previews that require interaction.

Action(name) displays the first shortcut key for the action in core.Bind, and is automatically bound accordingly; it is not displayed when it is not bound, and is not responsible for registering or executing actions. Placement(side, align) sets direction and alignment, Offset(dp) sets spacing (default 4dp, ignore non-finite values); positioning is still constrained by window edge avoidance.