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
Each direct child is a slide; label is the carousel's name for screen readers.
<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>
| Prop | Values |
|---|---|
label | Name for screen readers; default "Images" |
Dots and arrows
pagination (dots) and arrows are on by default; counter shows the position, such as "3 / 8", in a corner.
<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>
| Prop | Values |
|---|---|
pagination | true | false (dots) |
arrows | true · 'top-end' · 'bottom-center' · false (arrows and where) |
counter | true | false ("3 / 8" counter) |
Autoplay
interval is the time between slides in milliseconds; without it, the carousel does not advance by itself.
<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>
| Prop | Values |
|---|---|
interval | Milliseconds, e.g. 4000; off when not given |
Product strip
per-view is the number of slides in view: a number, or one value per breakpoint (base, sm, md, lg, xl).
<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>
| Prop | Values |
|---|---|
per-view | 1–6 or ['base' => 2, 'md' => 3, …] |
gap | 0 · 2 · 3 · 4 · 6 · 8 (spacing step) |
loop | true · 'infinite' · false |
Arrows in the heading
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.
<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>
| Prop | Values |
|---|---|
arrows | true · 'top-end' · 'bottom-center' · false |
carousel.controls: for | The carousel's id |
carousel.controls: counter | A "1 / 3" counter |
carousel.controls: mobile | false: hidden on phones |
Thumbnails and zoom
Write the thumbnails in <zb:slot:thumbnails> as zb:carousel.thumb, in the slides' order; zoom lets you zoom into the slide picture.
<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>
| Prop | Values |
|---|---|
thumbs | 'horizontal' (default) · 'vertical' |
zoom | true | false |
carousel.thumb: label | The thumbnail's name for screen readers |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
interval | — | null | Autoplay interval (ms); null = off |
label | — | Images | Screen reader label |
per-view | number | 1 | Slides in view (1–6), or one per breakpoint: ['base' => 2, 'md' => 3] |
gap | — | 0 / 4 | Space between slides, a spacing step: 0 · 2 · 3 · 4 · 6 · 8 (0 with one in view, 4 in a strip) |
loop | boolean | true | true: rewinds from the last to the first · 'infinite': seamless loop · false: stops at the ends |
centered | boolean | false | The shown slide in the middle |
draggable | boolean | true | Dragging with the mouse (a finger always swipes) |
snap | boolean | true | Stops at a slide; false: free scrolling |
auto-height | boolean | false | Height of the shown slide |
arrows | boolean | true | Previous / 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) |
pagination | boolean | true | Dots |
counter | boolean | false | "3 / 8" counter |
thumbs | — | horizontal | Where the thumbnails go (with the thumbnails slot): horizontal (under) · vertical (beside) · vertical-wide (on the left from 1024px, under on a phone) |
hover-zoom | boolean | false | On 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 | — | null | One width for the horizontal thumbnails ('4rem', '64px'): as many as fit stand in a row, the rest slide; null: four to a row |
zoom | boolean | false | Zooming the slide's picture (zb:carousel.item) |
fullscreen | boolean | false | A full-screen button; a tap on the picture opens it too (modal, zoom, arrows, counter) |
set | — | null | The set name of the carousel's own slides (with zb:carousel.set) |
carousel.item: label | — | null | The slide's name for screen readers; without it, its position |
carousel.item: interval | — | null | How long this slide stays when it changes by itself (ms): a video slide longer; null: the carousel's interval |
carousel.thumb: label | — | null | The 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 / true | The 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.
composer update zubazar/ui
npm install
npm run build
Advanced
Labelled slides
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.
<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
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.
<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 puts the shown slide in the middle; with several in view its neighbours show at both sides.
<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>
| Prop | Values |
|---|---|
centered | true | false |
Dragging and free scrolling
:draggable="false" turns off only the mouse drag; a finger still swipes. :snap="false" scrolls freely with momentum instead of stopping at a slide.
<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>
| Prop | Values |
|---|---|
draggable | true | false (mouse drag) |
snap | true | false (false: free scrolling) |
Auto height
auto-height makes the carousel as tall as the shown slide; short and long slides leave no gap.
<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>
| Prop | Values |
|---|---|
auto-height | true | false |
Vertical thumbnails
thumbs="vertical" puts the thumbnails in a column to the left of the slides; Up and Down move too.
<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
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.
<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>
| Prop | Values |
|---|---|
fullscreen | true | false |
Picture sets (colour options)
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.
<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>
| Prop | Values |
|---|---|
set | The shown set's name |
carousel.set: name | The set's name |
carousel.set: label | Said to screen readers when it is shown |
With images
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.
<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
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.
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
- 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-viewarray 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
labelis 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 givesnext(),previous(),goTo(i),pause(),play(),update()and the Swiper instance (swiper).
Events
| Event | detail | Description |
|---|---|---|
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"witharia-roledescription="carousel", named bylabel. Each slide getsrole="group"andaria-roledescription="slide"; its name islabelforzb: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
intervalthe 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 thelabelofzb:carousel.thumb.