Carousel
Slides browsed by dragging, swiping or arrows, with optional rotation.
Loading the live example…
import (
"time"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
var tripSlides = []struct {
title, caption string
icon func() *ui.SVG
color ui.Color
}{
{"Mountains", "Three days above the clouds", lucide.Mountain, ui.RGB(88, 128, 196)},
{"Forest", "Quiet trails and cabins", lucide.Trees, ui.RGB(72, 168, 128)},
{"Desert", "Dunes at sunrise", lucide.Sun, ui.RGB(226, 150, 60)},
{"Camping", "Under the stars", lucide.TentTree, ui.RGB(196, 108, 148)},
{"Harbour", "Boats and fresh fish", lucide.Sailboat, ui.RGB(66, 160, 190)},
}
type trips struct {
state components.CarouselState
}
func (s *trips) View(c *ui.Context) {
t := c.Theme()
ui.Column(c).FillWidth().Children(func() {
components.Carousel(c, &s.state, components.CarouselOptions{
Label: "Trips",
Count: len(tripSlides),
Interval: 5 * time.Second, // adds a play control
Slide: func(c *ui.Context, i int) {
slide := tripSlides[i]
ui.Column(c).FillWidth().Height(200).Radius(t.Radius*2).Padding(t.Space(5)).Gap(t.Space(1)).
LinearGradient(ui.LinearGradient{From: slide.color.Alpha(.32), To: slide.color.Alpha(.1), Angle: 135}).
Border(1, slide.color.Alpha(.35)).Children(func() {
components.Icon(c, slide.icon).Size(t.Space(8), t.Space(8)).TextColor(slide.color)
ui.Spacer(c)
components.Text(c, slide.title).FontWeight(600).FontSize(t.Rem(1.25))
components.Text(c, slide.caption).TextColor(t.TextMuted)
})
},
})
})
}The active slide sits in the center, and its neighbors shrink and dim as a hint that there is more. Drag or flick the track, click a neighbor, or use the arrow keys. With an Interval, a play control rotates the slides, pausing on hover, focus and dragging.
API
Carousel
func Carousel(c *ui.Context, state *CarouselState, options CarouselOptions) ui.ElementCarousel shows Count slides in a centered row, with neighbors partly in view as a hint that there is more. The active slide sits in the center; the others shrink and dim with their distance from it, frame by frame as the track moves. Below the slides, a row of indicators stretches the active one into a pill that travels with the slides, beside previous and next arrows and, with an Interval, a play control whose pill drains to show the time left on the slide.
Dragging the track follows the pointer and settles with momentum, rubber banding past the ends; a flick moves at least one slide, a horizontal touchpad swipe moves it as well, and a click on a neighbor centers it. The arrows, Home and End move the slides while the slide area has the focus, and the indicators are tabs. Presses on interactive slide content stay with that content. Inactive slides are disabled, so their controls take neither focus nor clicks. Slide changes the user makes are announced. Reduced Motion moves without gliding.
Applications own the state, slide content and labels. Changed on the returned element reports an Index the user chose in this frame.
pkg.go.dev