HoverCard
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/hover_card.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"
"time"
)
func init() {
registerSection("hover_card", "overlays", func() core.Widget { return el.Root(newHoverCardGallery()) })
}
type hoverCardGallery struct{ card *kit.HoverCardView }
func newHoverCardGallery() *hoverCardGallery {
return &hoverCardGallery{card: kit.HoverCard(
kit.Button(demoText("@Alex", "@张三"), nil).Variant(kit.ButtonGhost),
el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().Row().Gap(12).Child(kit.Avatar(demoText("Alex Chen", "张三")).Size(40).Render(cx), el.Div().Gap(4).Grow().Child(
el.Text(demoText("Alex Chen", "张三 Zhang San")).Bold(),
el.Text(demoText("East China sales representative managing 12 customers.", "华东区销售,负责 12 个客户。")).TextSize(13).TextColor(theme.Muted),
kit.Button(demoText("Send message", "发消息"), nil).Size(28).Variant(kit.ButtonSecondary).Render(cx),
))
}),
).OpenDelay(200*time.Millisecond).CloseDelay(500*time.Millisecond).Placement(el.Bottom, el.Start).Offset(8)}
}
func (g *hoverCardGallery) Render(cx *el.Context) el.Element {
return el.Div().P(24).Gap(12).Items(el.Start).Child(
el.Text(demoText("HoverCard: hover for 0.2 seconds or focus to open. Moving onto the card keeps it open.", "HoverCard:悬停 0.2 秒或聚焦打开,移到卡片上保持打开")).Bold(),
el.Div().Row().Gap(4).Child(el.Text(demoText("Owner: ", "负责人:")), g.card.Render(cx)),
)
}
When the pointer stops on a certain view, a preview card pops up, such as personnel information and link summary.
card := kit.HoverCard(nameView, profileView).Width(300).
OpenDelay(200*time.Millisecond).CloseDelay(500*time.Millisecond).
Placement(el.Right, el.Start).Offset(8)
- By default, the pointer opens after staying for 700ms and closes after leaving for 300ms;
OpenDelay/CloseDelayare configured according to the instance, and negative numbers are treated as zero. The delay for which modifications are pending will be restarted from the time of modification. - When the pointer moves from the target to the card, the card remains open, so links and buttons can be placed inside the card.
- Pressing Esc or clicking outside will close it immediately; it will be reopened only after the pointer and focus leave, to avoid stopping in place and popping up automatically.
- A focusable target opens immediately when it gains focus; it remains open while the focus is inside the target or card, and the card will not actively move the focus. The plain text target does not add a Tab stop. When keyboard access is required, pass in a Button or other focusable View.
Agent: The role of the card is dialog, and the elements inside are listed separately.
Verification: go run ./examples/components -section hover_card.
The card width and height are constrained by the window, and long content can be scrolled; non-finite widths are ignored. SetDisabled(true) Closes and disables the target area. The content can include overlays such as Menu, and the submenu is closed with a delayed pause during opening, so that the parent card will not be accidentally closed during the operation.
Placement(side, align) sets the preferred direction and alignment, Offset(dp) sets the anchor spacing (default 4dp, ignore non-finite values). The edges of the window are still automatically avoided; positioning modifications when it is open will take effect in the next frame. The delay only affects mouseover, keyboard focus is still turned on immediately.