UI Toolbox logoUI Toolbox
Support

Carousel

Carousel in UIToolbox shows its items in a row. The selected item stands in the middle at full size. Each step away shrinks and fades an item, and the nearer items draw on top. It is a cover flow for a character select, a level select or a gallery.

A Carousel of hero portraits with the selected hero in front and a thumbnail strip belowA Carousel of hero portraits with the selected hero in front and a thumbnail strip below

<tb:Carousel
    name="heroes"
    selected-index="1"
    visible-items="2"
    loop="false"
    show-strip="false"
    transition-duration="300"
    style="height: 190px;">
    <ui:VisualElement class="hero-card" />
    <ui:VisualElement class="hero-card" />
    <ui:VisualElement class="hero-card" />
    <ui:VisualElement class="hero-card" />
</tb:Carousel>
var heroes = root.Q<Carousel>("heroes");
heroes.SelectionChanged += (old, now) => ShowHero(now);
heroes.ItemChosen += index => Choose(index);   // A, Enter
heroes.Next();
heroes.Previous();

// Items from a list, like a ListView
heroes.MakeItem = () => new VisualElement();
heroes.BindItem = (item, i) =>
    item.style.backgroundImage = Picture(i);
heroes.ItemsSource = party;
Member Description
SelectedIndex (selected-index), SelectedItem The selected item. Without Loop the index stays between 0 and the last item; with Loop it wraps around.
Next(), Previous() Select the next or the previous item. At an end without Loop they do nothing.
SetSelectedIndexWithoutNotify(index) Selects an item and sends no event.
Items, ItemCount The items in index order.
ItemsSource, MakeItem, BindItem Fill the carousel from a list, like a ListView: MakeItem creates an item, and BindItem(item, index) fills it. A new list reuses the items that exist and binds them again.
Rebuild(), Refresh() Rebuild makes the items again from ItemsSource. Call Refresh after you add or remove children with Add or Remove.
VisibleItems (visible-items) The number of items shown on each side of the selected one. Default 2. Items farther away are invisible.
ItemScale (item-scale), MinScale (min-scale) How much each step away takes off the scale (default 0.2), and the smallest scale (default 0.4).
ItemOpacity (item-opacity), MinOpacity (min-opacity) How much each step away takes off the opacity (default 0.25), and the smallest opacity of a visible item (default 0.2).
ItemWidth (item-width), Spacing (spacing) The width of an item in pixels (default 160), and the distance between the centers of two neighbors (default 110). A smaller Spacing makes the items overlap.
Loop (loop) Next on the last item selects the first, and Previous on the first selects the last. The items on both sides wrap, so the row has no empty end.
ShowStrip (show-strip), BindThumbnail A strip of thumbnails under the row that tracks the selection. A thumbnail shows the background image of its item; set BindThumbnail(thumbnail, index) to fill it another way.
TransitionDuration (transition-duration) The length of the slide in milliseconds. Default 300.
ReduceMotion (reduce-motion) The items jump to their places with no slide. The carousel also does this when the Workspace turns animations off.
WheelNavigation (wheel-navigation) A tick of the mouse wheel selects the next item (wheel down or right) or the previous one (wheel up or left). On by default. The carousel takes one step per 200 ms, so a fast trackpad flick does not skip items.
SelectionChanged(old, now), ItemChosen(index) The first event is sent when the selected index changes. The second is sent when the user presses A or Enter, or clicks the selected item.

Tips

Styling

Class Element
tb-carousel The carousel (flex column, focusable).
tb-carousel__stage The area that holds the items.
tb-carousel__item, --selected, --hidden Each item: absolute, centered, with a transition on translate, scale and opacity. The selected item has --selected (the focus ring shows on it while the carousel has focus). An item beyond VisibleItems has --hidden and no pointer.
tb-carousel__strip, tb-carousel__thumb, --selected The strip of thumbnails, a thumbnail (36 px) and the thumbnail of the selected item.

The CarouselDemo scene shows a character select with six heroes and a Card with the stats of the selected hero under the row, with toggles for Loop and the thumbnail strip, and sliders for the number of visible items, the scale and the fade per step, and the length of the slide.