CardStack
A deck reviewed one card at a time, swiped left or right, with undo.
Loading the live example…
import (
"strings"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type destination struct {
id, name, country string
icon func() *ui.SVG
tint ui.Color
}
var destinations = []destination{
{"lisbon", "Lisbon", "Portugal", lucide.TramFront, ui.RGB(226, 132, 72)},
{"kyoto", "Kyoto", "Japan", lucide.Landmark, ui.RGB(206, 86, 110)},
{"reykjavik", "Reykjavík", "Iceland", lucide.Snowflake, ui.RGB(72, 150, 214)},
{"cape-town", "Cape Town", "South Africa", lucide.Mountain, ui.RGB(72, 168, 128)},
}
type shortlist struct {
cards components.CardStackState
}
func (s *shortlist) View(c *ui.Context) {
t := c.Theme()
items := make([]components.CardStackItem, len(destinations))
for i, d := range destinations {
items[i] = components.CardStackItem{ID: d.id, Label: d.name, Content: func(c *ui.Context, radius float32) {
ui.Column(c).Fill().Children(func() {
ui.Box(c).FillWidth().Height(t.Space(28)).Center().Radius(radius, radius, 0, 0).
LinearGradient(ui.LinearGradient{From: d.tint.Alpha(.32), To: d.tint.Alpha(.08), Angle: 135}).
Children(func() {
components.Icon(c, d.icon).Size(t.Space(12), t.Space(12)).TextColor(d.tint)
})
ui.Column(c).FillWidth().Padding(t.Space(4)).Gap(t.Space(1)).Children(func() {
components.Text(c, d.name).FontSize(t.Rem(1.2)).FontWeight(600)
components.Text(c, d.country).TextColor(t.TextMuted)
})
})
}}
}
ui.Column(c).Width(360).MaxWidthPercent(100).Gap(t.Space(4)).Children(func() {
components.CardStack(c, &s.cards, components.CardStackOptions{
Items: items,
Label: "Destinations",
LeftLabel: "Skip", RightLabel: "Shortlist",
})
var kept []string
for _, d := range s.cards.Decisions {
if d.Decision == components.CardRight {
for _, dest := range destinations {
if dest.id == d.ID {
kept = append(kept, dest.name)
}
}
}
}
summary := "Drag a card left or right, or use the arrow keys."
if len(kept) > 0 {
summary = "Shortlisted: " + strings.Join(kept, ", ")
}
components.Text(c, summary).TextColor(t.TextMuted).TextAlign(ui.Center).FillWidth()
})
}Drag or flick the top card, use the buttons, or press the arrow keys. A throw carries the card off with the pointer's velocity while the cards behind rise. Undo brings the last card back, and once every card is decided, Start over deals them again.
The decisions live in application state, which the application may change itself.
API
CardStack
func CardStack(c *ui.Context, state *CardStackState, options CardStackOptions) CardStackEventCardStack shows a deck reviewed one card at a time: drag or flick the top card left or right, or use the buttons or the arrow keys, to decide it. The card follows the pointer with a tilt and a stamp naming the action, and a throw carries it off with the pointer's velocity while the cards behind rise. Undo, by its button, Backspace or Command/Ctrl+Z, flies the last card back from where it left; once all are decided, Start over deals them back.
state holds the decisions, which applications own and may change; the stack returns the change its own input made this frame. Applications supply the cards' stable IDs, localized labels and content, and act on decisions. Reduced Motion moves cards at once.
pkg.go.devCardStackOptions
CardStackItem
CardStackState
CardStackEvent
CardDecision
type CardDecision intCardDecision is the side a card of a CardStack is sent to.
const (
// CardLeft passes on a card, by its left action.
CardLeft CardDecision = iota + 1
// CardRight keeps it, by its right, primary action.
CardRight
)