Skip to Content
Onevra

HoldToConfirm

A button that acts only after it is held, for actions a stray click must not trigger.

Loading the live example…

hold_to_confirm.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type deleteProject struct {
	deleted bool
}

func (d *deleteProject) View(c *ui.Context) {
	t := c.Theme()
	ui.Row(c).Gap(12).AlignItems(ui.Center).Children(func() {
		hold := components.HoldToConfirm(c.Key("delete"), &d.deleted, "Hold to delete", components.HoldToConfirmOptions{
			ConfirmedLabel: "Deleted",
			Tone:           components.HoldDanger,
		})
		switch {
		case hold.Holding():
			components.Text(c, "Keep holding…").TextColor(t.TextMuted)
		case d.deleted:
			if components.Button(c, "Undo", components.ButtonGhost).Clicked() {
				d.deleted = false
			}
		}
	})
}
Source

Press and hold the button: a fill tracks the hold, and letting go early rewinds it. When the hold completes, the button sets its *bool and morphs into its confirmed label. Space and Enter hold it from the keyboard.

Prefer an alert dialog when people need to read the consequences first. Set the value back to false to reset the button.

API

HoldToConfirm

func HoldToConfirm(c *ui.Context, confirmed *bool, label string, options ...HoldToConfirmOptions) *HoldToConfirmControl

HoldToConfirm creates a button that sets *confirmed only after it is held, for destructive or hard-to-undo actions a stray click must not trigger. A fill tracks the hold linearly from the leading edge; letting go early, or sliding well off the button, rewinds it, and completing morphs the label and icon into a done state. Holding Space or Enter works too; moving the focus away cancels. Set *confirmed back to false to reset the button.

h := HoldToConfirm(c, &deleted, "Hold to delete", HoldToConfirmOptions{ConfirmedLabel: "Deleted", Tone: HoldDanger})
if h.Changed() { /* delete */ }

Prefer AlertDialog when people need to read the consequences first. The label is the accessible name; a localized description explains the hold, and completion is announced. Applications own both labels. Key a parent container, or pass c.Key, for stable identity: the hold is element state.

pkg.go.dev

HoldToConfirmOptions

HoldToConfirmOptions customizes a hold; the zero value is a 1.2-second accent hold with a trash icon and the catalog's done label.

Prop

Type

pkg.go.dev

HoldToConfirmTone

type HoldToConfirmTone int

HoldToConfirmTone selects the color a hold fills with.

const (
	// HoldAccent fills with the theme accent.
	HoldAccent HoldToConfirmTone = iota
	// HoldDanger tints the face and fills with the danger color, for an
	// irreversible action.
	HoldDanger
	// HoldNeutral fills with the text color.
	HoldNeutral
)
pkg.go.dev

HoldToConfirmControl

HoldToConfirmControl exposes the button element. Keep this handle to query Changed and Holding after applying Element methods, which return ui.Element.

Prop

Type

Methods
func (h *HoldToConfirmControl) Changed() bool

Changed reports the frame in which a hold completed and set *confirmed.

func (h *HoldToConfirmControl) Holding() bool

Holding reports a hold in progress, for nearby hints such as "Keep holding".

pkg.go.dev
Edit this page on GitHub

On this page