v0.0.2
Demos (demo panel, opens in a new tab)

Carousel

A slideshow you move through by dragging, with arrows or with dots; used for a home page slider, a product strip and a product gallery.

zb:carousel zubazar/ui · free (MIT)

Basic usage

Basic usage: view
BLADE

Each direct child is a slide; label is the carousel's name for screen readers.

blade
<zb:carousel label="Announcements">
    <div class="flex h-56 flex-col justify-end bg-gradient-to-br from-brand-600 to-brand-800 p-8 text-white">
        <p class="text-xl font-semibold">The October campaign starts on 7 October</p>
    </div>
    <div class="flex h-56 flex-col justify-end bg-gradient-to-br from-emerald-600 to-emerald-800 p-8 text-white">
        <p class="text-xl font-semibold">Version 2.4 of the mobile app is out</p>
    </div>
</zb:carousel>
PropValues
labelName for screen readers; default "Images"

Dots and arrows

Dots and arrows: view
BLADE

pagination (dots) and arrows are on by default; counter shows the position, such as "3 / 8", in a corner.

blade
<zb:carousel label="Campaigns" :arrows="false" counter>
    <div class="flex h-48 items-end bg-gradient-to-br from-sky-600 to-sky-800 p-8 text-white"><p class="text-xl font-semibold">Free shipping</p></div>
    <div class="flex h-48 items-end bg-gradient-to-br from-brand-600 to-brand-800 p-8 text-white"><p class="text-xl font-semibold">New season</p></div>
    <div class="flex h-48 items-end bg-gradient-to-br from-amber-500 to-amber-700 p-8 text-white"><p class="text-xl font-semibold">Weekend sale</p></div>
</zb:carousel>
PropValues
paginationtrue | false (dots)
arrowstrue · 'top-end' · 'bottom-center' · false (arrows and where)
countertrue | false ("3 / 8" counter)

Autoplay

Autoplay: view
BLADE

interval is the time between slides in milliseconds; without it, the carousel does not advance by itself.

blade
<zb:carousel :interval="4000" label="Store news">
    <div class="flex h-48 flex-col justify-end bg-gradient-to-br from-sky-600 to-sky-800 p-8 text-white">
        <p class="text-xs font-semibold tracking-wider uppercase opacity-80">Customer satisfaction</p>
        <p class="mt-1 text-xl font-semibold">Reached 96% in September</p>
    </div>
    <div class="flex h-48 flex-col justify-end bg-gradient-to-br from-brand-600 to-brand-800 p-8 text-white">
        <p class="text-xs font-semibold tracking-wider uppercase opacity-80">Logistics</p>
        <p class="mt-1 text-xl font-semibold">A new warehouse opened in Ankara</p>
    </div>
</zb:carousel>
PropValues
intervalMilliseconds, e.g. 4000; off when not given

Product strip

Product strip: view
BLADE

per-view is the number of slides in view: a number, or one value per breakpoint (base, sm, md, lg, xl).

blade
<zb:carousel :per-view="['base' => 2, 'md' => 3, 'lg' => 4]" :loop="false" label="Recommended products">
    @foreach (['Wireless headphones', 'Mechanical keyboard', 'Wireless mouse', 'Portable speaker', 'Webcam', 'Fast charger'] as $product)
        <zb:carousel.item :label="$product">
            <a href="#" class="block overflow-hidden rounded-sm border border-line bg-surface">
                <div class="aspect-[4/3] bg-gradient-to-br from-brand-400 to-brand-600"></div>
                <p class="truncate p-3 text-sm font-medium text-ink">{{ $product }}</p>
            </a>
        </zb:carousel.item>
    @endforeach
</zb:carousel>
PropValues
per-view1–6 or ['base' => 2, 'md' => 3, …]
gap0 · 2 · 3 · 4 · 6 · 8 (spacing step)
looptrue · 'infinite' · false

Arrows in the heading

Arrows in the heading: view
BLADE

zb:carousel.controls puts the arrows outside the carousel (linked by its id, disabled at an end, hidden when everything is in view); arrows="top-end" puts them above the carousel, at the end.

blade
<div class="mb-3 flex items-end justify-between gap-4">
    <h3 class="text-lg font-semibold text-ink">Best sellers</h3>
    <zb:carousel.controls for="best-sellers" counter />
</div>
<zb:carousel id="best-sellers" :arrows="false" :per-view="['base' => 2, 'md' => 3]" :loop="false" :pagination="false" label="Best sellers">
    @foreach (['Wireless headphones', 'Mechanical keyboard', 'Wireless mouse', 'Portable speaker', 'Webcam'] as $product)
        <zb:carousel.item :label="$product">
            <a href="#" class="block overflow-hidden rounded-sm border border-line bg-surface">
                <div class="aspect-[4/3] bg-gradient-to-br from-brand-400 to-brand-600"></div>
                <p class="truncate p-3 text-sm font-medium text-ink">{{ $product }}</p>
            </a>
        </zb:carousel.item>
    @endforeach
</zb:carousel>
PropValues
arrowstrue · 'top-end' · 'bottom-center' · false
carousel.controls: forThe carousel's id
carousel.controls: counterA "1 / 3" counter
carousel.controls: mobilefalse: hidden on phones

Thumbnails and zoom

Thumbnails and zoom: view
BLADE

Write the thumbnails in <zb:slot:thumbnails> as zb:carousel.thumb, in the slides' order; zoom lets you zoom into the slide picture.

blade
<zb:carousel label="Wireless headphones" zoom :pagination="false">
    @foreach (['from-brand-400 to-brand-700', 'from-sky-400 to-sky-700', 'from-emerald-400 to-emerald-700'] as $colour)
        <zb:carousel.item>
            <div class="swiper-zoom-target aspect-square w-full bg-gradient-to-br {{ $colour }}" role="img" aria-label="Headphones, picture {{ $loop->iteration }}"></div>
        </zb:carousel.item>
    @endforeach
    <zb:slot:thumbnails>
        @foreach (['from-brand-400 to-brand-700', 'from-sky-400 to-sky-700', 'from-emerald-400 to-emerald-700'] as $colour)
            <zb:carousel.thumb :label="'Picture '.$loop->iteration"><div class="aspect-square bg-gradient-to-br {{ $colour }}"></div></zb:carousel.thumb>
        @endforeach
    </zb:slot:thumbnails>
</zb:carousel>
PropValues
thumbs'horizontal' (default) · 'vertical'
zoomtrue | false
carousel.thumb: labelThe thumbnail's name for screen readers

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
interval—nullAutoplay interval (ms); null = off
label—ImagesScreen reader label
per-viewnumber1Slides in view (1–6), or one per breakpoint: ['base' => 2, 'md' => 3]
gap—0 / 4Space between slides, a spacing step: 0 · 2 · 3 · 4 · 6 · 8 (0 with one in view, 4 in a strip)
loopbooleantruetrue: rewinds from the last to the first · 'infinite': seamless loop · false: stops at the ends
centeredbooleanfalseThe shown slide in the middle
draggablebooleantrueDragging with the mouse (a finger always swipes)
snapbooleantrueStops at a slide; false: free scrolling
auto-heightbooleanfalseHeight of the shown slide
arrowsbooleantruePrevious / next buttons and where: true (on both sides) · 'sm' (on both sides, from 640px only: they don't cover a heading on a phone) · 'top-end' (above, side by side at the end) · 'bottom-center' (below, around the dots) · false (none; zb:carousel.controls puts them elsewhere)
paginationbooleantrueDots
counterbooleanfalse"3 / 8" counter
thumbs—horizontalWhere the thumbnails go (with the thumbnails slot): horizontal (under) · vertical (beside) · vertical-wide (on the left from 1024px, under on a phone)
hover-zoombooleanfalseOn a wide screen with a mouse, a lens over the photo and the part larger beside the carousel (a product gallery). A photo with a large copy, <img data-zoom-src="…">: the magnifier and the full screen show it
thumb-width—nullOne width for the horizontal thumbnails ('4rem', '64px'): as many as fit stand in a row, the rest slide; null: four to a row
zoombooleanfalseZooming the slide's picture (zb:carousel.item)
fullscreenbooleanfalseA full-screen button; a tap on the picture opens it too (modal, zoom, arrows, counter)
set—nullThe set name of the carousel's own slides (with zb:carousel.set)
carousel.item: label—nullThe slide's name for screen readers; without it, its position
carousel.item: interval—nullHow long this slide stays when it changes by itself (ms): a video slide longer; null: the carousel's interval
carousel.thumb: label—nullThe thumbnail's name for screen readers
carousel.set: name / label——The set's name / said to screen readers when it is shown
carousel.controls: for / counter / mobile—— / false / trueThe id of the carousel it moves / a "1 / 3" counter between the arrows / false: hidden on phones (a swipe is enough)

Slots: default content: each direct child is a slide; zb:carousel.item for a labelled slide · thumbnails: zb:carousel.thumb thumbnails, in the slides' order · zb:carousel.set: another set of pictures (a colour)

Installation

Swiper is an npm dependency of Zubazar UI: run npm install after installing or updating the package; Swiper is downloaded only on a page with a carousel.

bash
composer update zubazar/ui
npm install
npm run build

Advanced

10 examples

Labelled slides

Labelled slides: view
BLADE

Write a slide with zb:carousel.item and screen readers read its label (without one, its position such as "2 of 3"). Hidden slides cannot be reached.

blade
<zb:carousel label="Product images">
    <zb:carousel.item label="Front view">
        <div class="flex h-48 items-end bg-gradient-to-br from-brand-600 to-brand-800 p-6 text-white">
            <p class="text-lg font-semibold">Wireless headphones · Front view</p>
        </div>
    </zb:carousel.item>
    <zb:carousel.item label="In the box">
        <div class="flex h-48 items-end bg-gradient-to-br from-emerald-500 to-emerald-700 p-6 text-white">
            <p class="text-lg font-semibold">Wireless headphones · In the box</p>
        </div>
    </zb:carousel.item>
</zb:carousel>

Infinite loop

Infinite loop: view
BLADE

loop="infinite" brings the first slide after the last without a break; the default true rewinds from the last to the first. For slides a Livewire update changes, use true or false.

blade
<zb:carousel loop="infinite" counter label="Brands">
    @foreach (['Aura', 'Nova', 'Pixel', 'Echo'] as $brand)
        <div class="flex h-40 items-center justify-center bg-gradient-to-br from-zinc-600 to-zinc-800 text-2xl font-semibold text-white">{{ $brand }}</div>
    @endforeach
</zb:carousel>

Centered slide

Centered slide: view
BLADE

centered puts the shown slide in the middle; with several in view its neighbours show at both sides.

blade
<zb:carousel centered :per-view="['base' => 2, 'md' => 3]" :gap="4" label="Collections">
    @foreach (['Summer', 'Autumn', 'Winter', 'Spring', 'Outlet'] as $collection)
        <div class="flex h-40 items-center justify-center rounded-sm bg-gradient-to-br from-brand-500 to-brand-700 text-xl font-semibold text-white">{{ $collection }}</div>
    @endforeach
</zb:carousel>
PropValues
centeredtrue | false

Dragging and free scrolling

Dragging and free scrolling: view
BLADE

:draggable="false" turns off only the mouse drag; a finger still swipes. :snap="false" scrolls freely with momentum instead of stopping at a slide.

blade
<zb:carousel :per-view="['base' => 3, 'md' => 5]" :snap="false" :pagination="false" :loop="false" label="Tags">
    @foreach (['Headphones', 'Keyboard', 'Mouse', 'Speaker', 'Camera', 'Charger', 'Cable', 'Bag'] as $tag)
        <a href="#" class="block rounded-full border border-line bg-surface px-4 py-2 text-center text-sm font-medium text-ink">{{ $tag }}</a>
    @endforeach
</zb:carousel>
PropValues
draggabletrue | false (mouse drag)
snaptrue | false (false: free scrolling)

Auto height

Auto height: view
BLADE

auto-height makes the carousel as tall as the shown slide; short and long slides leave no gap.

blade
<zb:carousel auto-height label="Customer reviews">
    <div class="bg-gradient-to-br from-sky-600 to-sky-800 p-8 text-white">"Fast shipping."</div>
    <div class="bg-gradient-to-br from-brand-600 to-brand-800 p-8 text-white">"Exactly as described; the box was packed with care and I never needed the returns. The headphones sound even better than I expected."</div>
</zb:carousel>
PropValues
auto-heighttrue | false

Vertical thumbnails

Vertical thumbnails: view
BLADE

thumbs="vertical" puts the thumbnails in a column to the left of the slides; Up and Down move too.

blade
<zb:carousel thumbs="vertical" :pagination="false" label="Mechanical keyboard">
    @foreach (['from-zinc-500 to-zinc-800', 'from-sky-400 to-sky-700', 'from-amber-400 to-amber-700', 'from-rose-400 to-rose-700'] as $colour)
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br {{ $colour }}"></div></zb:carousel.item>
    @endforeach
    <zb:slot:thumbnails>
        @foreach (['from-zinc-500 to-zinc-800', 'from-sky-400 to-sky-700', 'from-amber-400 to-amber-700', 'from-rose-400 to-rose-700'] as $colour)
            <zb:carousel.thumb :label="'Picture '.$loop->iteration"><div class="aspect-square bg-gradient-to-br {{ $colour }}"></div></zb:carousel.thumb>
        @endforeach
    </zb:slot:thumbnails>
</zb:carousel>

Full screen

Full screen: view
BLADE

fullscreen puts a full-screen button in the corner; a tap or a click on a slide picture opens it too. Full screen has zoom, the arrows, the counter and, on wide screens, the thumbnails; Escape first leaves the zoom, then closes.

blade
<zb:carousel fullscreen zoom :pagination="false" label="Wireless headphones">
    @foreach (['from-brand-400 to-brand-700', 'from-sky-400 to-sky-700', 'from-emerald-400 to-emerald-700'] as $colour)
        <zb:carousel.item>
            <div class="swiper-zoom-target aspect-square w-full bg-gradient-to-br {{ $colour }}" role="img" aria-label="Headphones, picture {{ $loop->iteration }}"></div>
        </zb:carousel.item>
    @endforeach
</zb:carousel>
PropValues
fullscreentrue | false

Picture sets (colour options)

Picture sets (colour options): view
BLADE

Each color of a product is an zb:carousel.set; set says which set the carousel's own slides are. The zb:carousel:set event on the root ({ name }) or showSet(name) shows that set from its first slide.

blade
<zb:carousel id="product-gallery-demo" set="purple" :pagination="false" counter label="Mug">
    <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-brand-400 to-brand-700"></div></zb:carousel.item>
    <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-brand-500 to-brand-800"></div></zb:carousel.item>
    <zb:carousel.set name="purple" label="Purple colour images are shown">
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-brand-400 to-brand-700"></div></zb:carousel.item>
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-brand-500 to-brand-800"></div></zb:carousel.item>
    </zb:carousel.set>
    <zb:carousel.set name="green" label="Green colour images are shown">
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-emerald-400 to-emerald-700"></div></zb:carousel.item>
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-emerald-500 to-emerald-800"></div></zb:carousel.item>
        <zb:carousel.item><div class="aspect-square bg-gradient-to-br from-sky-400 to-sky-700"></div></zb:carousel.item>
    </zb:carousel.set>
</zb:carousel>
<div class="mt-3 flex gap-2">
    <button type="button" class="rounded-sm border border-line px-3 py-1.5 text-sm" onclick="document.getElementById('product-gallery-demo').dispatchEvent(new CustomEvent('zb:carousel:set', { detail: { name: 'purple' } }))">Purple</button>
    <button type="button" class="rounded-sm border border-line px-3 py-1.5 text-sm" onclick="document.getElementById('product-gallery-demo').dispatchEvent(new CustomEvent('zb:carousel:set', { detail: { name: 'green' } }))">Green</button>
</div>
PropValues
setThe shown set's name
carousel.set: nameThe set's name
carousel.set: labelSaid to screen readers when it is shown

With images

BLADE

For image slides, fix the height and use object-cover; photos of different ratios are cropped to the same size. Give every image a meaningful alt text.

blade
<zb:carousel :interval="5000" label="Product photos">
    @foreach ($photos as $photo)
        <img src="{{ $photo->url }}" alt="{{ $photo->description }}" class="h-72 w-full object-cover">
    @endforeach
</zb:carousel>

Destroy and start again

JS

A carousel removed by a Livewire re-render stops by itself, and one added starts. To do it by hand, use ZubazarUI.destroy(el) to remove it and ZubazarUI.init(el) to start it again.

js
const showcase = document.querySelector('#showcase');

ZubazarUI.destroy(showcase);        // Swiper is off, the slides scroll side by side
ZubazarUI.init(showcase);           // and it starts again

const carousel = ZubazarUI.plugins.get(showcase, 'carousel');
carousel.goTo(2);
showcase.addEventListener('zb:carousel:change', (e) => console.log(e.detail.index, e.detail.count));

Developer notes

Events, hooks and details
  • The carousel runs on Swiper with the kit's look; Swiper is included in the package.
  • Slides are dragged with the mouse or swiped with a finger; after the last slide it goes back to the first. The dots and arrows show only when there is something to slide to.
  • A breakpoint left out of the per-view array takes the value of the one before it; a strip with several slides in view sits on the page background.
  • A pressed thumbnail shows its slide, and the strip follows the shown slide (Swiper Thumbs).
  • zoom: a double tap or a pinch on a touch screen, a double click or the button in the corner with a mouse; zoomed in, the picture follows the mouse, and Escape zooms out.
  • In an infinite loop, Swiper moves the slides around itself; it needs at least one slide more than are in view.
  • Full screen is the kit's modal: the focus stays inside and the page does not scroll; when it closes, the focus goes back to the button and the carousel on the page moves to the last slide viewed.
  • With fullscreen, a double tap does not zoom but opens full screen; a pinch and the magnifier button zoom.
  • When the set changes, the thumbnails and full screen show that set too, and the set's label is said to screen readers; the page does not reload and Livewire does not re-render.
  • ZubazarUI.destroy(el) takes Swiper off and leaves the server's markup; the plugin instance gives next(), previous(), goTo(i), pause(), play(), update() and the Swiper instance (swiper).

Events

EventdetailDescription
zb:carousel:change{ index, count }Sent from the root when the shown slide (in a strip, the first position) or the number of slides changes.
zb:carousel:set{ name }A command sent to the root: shows that set (zb:carousel.set) from its first slide.

Accessibility

ARIA and roles
The root is a role="region" with aria-roledescription="carousel", named by label. Each slide gets role="group" and aria-roledescription="slide"; its name is label for zb:carousel.item, else its position, such as "2 of 5". Image slides (img) keep their own role.
Keyboard
With the focus inside, Left and Right go to the previous and next slide; with vertical thumbnails Up and Down do too. The arrows, dots, thumbnails and the pause button are real buttons. Escape zooms a zoomed picture out.
Focus
With one slide in view the hidden slides are inert: Tab skips their links. In a strip (several in view) Tab goes through the cards and the focused card slides into view.
Motion
With interval the carousel stops while the pointer is over it and while the focus is inside, has a pause button (WCAG 2.2.2) and never advances when the operating system asks for reduced motion; the transitions are not animated then either. Automatic turns are not announced to screen readers; the user's are.
What you provide
Give every picture a meaningful alt; a thumbnail's picture is decorative (alt=""), its name is the label of zb:carousel.thumb.
Zubazar UI · ZubazarDesigned for people.