Keel v0.5.1 中文 GitHub

ShimmerText

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

func init() {
	registerSection("shimmer_text", "motion", func() core.Widget {
		normal := kit.ShimmerText(demoText("Generating content; the text remains readable…", "正在生成内容,文字仍然可以阅读……")).Size(20)
		reverse := kit.ShimmerText(demoText("Reverse sweep", "反向扫光 · Reverse")).Size(24).Style(kit.ShimmerStyle{Duration: 3 * time.Second, Spread: .4, Reverse: true})
		once := kit.ShimmerText(demoText("Play once", "只播放一次 · Once")).Size(20).Once(true)
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(20).Child(normal.Render(cx), reverse.Render(cx), once.Render(cx), kit.Button(demoText("Replay", "重新播放"), once.Restart).Render(cx))
		}))
	})
}

Readable text is preserved, allowing the highlight to sweep across the glyphs; the background and white space between characters are unaffected.

loading := kit.ShimmerText("Generating content…").Duration(2*time.Second).Spread(.3)
loading.Reverse(true).Once(true)
loading.Restart()

The default cycle is from left to right, with a cycle of 2 seconds, and the highlight half-width is 30% of the width of the text box. Duration accepts positive duration; Spread accepts (0,1], illegal values are ignored. Reverse reverses; Once restores normal text after one cycle and stops requesting animation frames, Restart starts again. Adjust the duration without resetting the starting point, and explicitly Restart when needed.

Enabled(false) displays normal text, and then enables Start from Scratch; it also displays normal text under the reduced animation setting. SetText updates the text, Size sets the font size (0 is inherited), MaxLines limits the number of lines (0 is not limited); the font, font weight, line height and color are inherited from the el container by default. Color covers the text background color, Highlight covers the highlight color, and the default highlight uses the theme PrimaryText.

Colored bitmap glyphs (such as some emoji) retain their original colors and do not participate in highlight shading. The component maintains text semantics and layout size, does not undertake loading tasks, and does not provide text selection. The underlying el.Text(...).Shimmer(phase, spread, color) is only responsible for drawing, making it easy to apply custom time control.

Run the example: go run ./examples/components -section shimmer_text.

ShimmerStyle is a reusable animation configuration value, including Duration, Spread, Reverse, and Once; Style(config) replaces four configurations at a time, and the text, font size, color, etc. remain unchanged. A value of zero reverts to default; non-positive Duration, beyond (0,1], or non-finite Spread use default. Valid configuration changes are replayed from the next frame, and repeated application of the same configuration does not restart the animation. Legacy methods such as itemized Duration/Spread still retain their original behavior.

motion := kit.ShimmerStyle{Duration: 3*time.Second, Spread: .45, Reverse: true, Once: false}
text.Style(motion)
attachment.TitleShimmer(motion)