Keel v0.5.1 中文 GitHub

CopyButton

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/copy_button.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("copy_button", "general", func() core.Widget {
		cb := kit.CopyButton(func() string { return "SO-1001" })
		last := demoText("Not copied yet", "尚未复制")
		custom := kit.CopyButton(func() string { return "SO-1001" }).OnCopied(func(value string) { last = demoText("Last copied: ", "最近复制:") + value })
		custom.Content(el.ViewFunc(func(cx *el.Context) el.Element {
			label, icon := demoText("Copy order number", "复制订单号"), kit.IconCopy
			if custom.Copied() {
				label, icon = demoText("Order number copied", "订单号已复制"), kit.IconCheck
			}
			return el.Div().Row().Gap(8).Items(el.Center).Child(kit.Icon(icon).Render(cx), el.Text(label))
		}))
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(8).Items(el.Start).Child(el.Text(demoText("Order number SO-1001", "订单号 SO-1001")), cb.Render(cx), custom.Render(cx), el.Text(last))
		}))
	})
}

Copies text to the clipboard and displays "Copied" as feedback.

kit.CopyButton(func() string { return order.ID })
  • Text is only read when clicked, so the function always gets the latest content.
  • After copying, the button displays "Copied" and a check icon, and returns to "Copy" after kit.CopiedFeedback (1.5 seconds).
  • The button is Ghost style, 28dp high, supports Tab focus and Space/Enter triggering.

Agent: The role is button and the name switches between "Copied" and "Copied".

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

Continuous clicks will retain the full 1.5 seconds of feedback after each copy, regardless of the last cutoff time. SetDisabled(true) disables copying and clears feedback; ancestor disabling also prevents reading text and writing to the clipboard. When nil is passed to the value function, "Copied" will not be displayed when clicked.

OnCopied(func(string)) is called after submitting the clipboard write request. The parameter is the original text submitted this time, and the value function will not be read again. The system clipboard did not successfully acknowledge the signal, so this callback does not represent the operating system acknowledging the write. Not called when the disabled or value function is nil; passing nil removes the callback.

Content(el.View) Replaces the default icon and text, preserving the button's keyboard action and "copy"/"copied" semantic name. Content should be text, icons and other display elements, and avoid nested input boxes or buttons. The minimum height of custom content is 28dp, which can be increased with the content; pass nil to restore the default button.

copy := kit.CopyButton(func() string { return order.ID }).
    OnCopied(func(value string) { lastCopied = value })
copy.Content(el.ViewFunc(func(cx *el.Context) el.Element {
    label := "Copy order number"
    if copy.Copied() {
        label = "Order number copied"
    }
    return el.Text(label)
}))

Copied() provides the current feedback status. Custom content shares a 1.5-second timer with the default button; the timer is reset each time it is copied, and SetDisabled(true) clears feedback.