Data display

Carousel

A row of slides shown one at a time (or a few side by side), moved with arrows, dots, thumbnails, swipes or the keyboard, and optionally rotating on its own. Use it for galleries, featured content and product strips where only part of the set needs to be visible at once.

tk:carousel.arrows, tk:carousel.indicators and tk:carousel.thumbnails are rendered by the carousel itself; their attributes are reached through prefixes (see Customization).

Blade

The first image fills the carousel, with previous and next arrows over it and a row of dots below. The arrows, the dots and the arrow keys move between the images, and going past the last one returns to the first.

Slides with content #

The first slide shows the title and the link in white over a dark gradient at the bottom of the image; the second shows its caption under the image; the third is centered text. With label, the carousel is a landmark region named "Highlights".

Blade

Several per view, from data #

Three products side by side with a gap between them. Each arrow click moves a whole page of three, and the dots count pages instead of slides. Without wrap the previous arrow is marked disabled on the first page and the next arrow on the last. A Livewire update that adds products updates the slide and page count without moving the carousel.

Blade

Binding the current slide #

The bound value is the zero-based index of the first visible slide: clicking the second thumbnail sets $index to 1, and setting $index to 0 on the server moves the carousel back to the first slide.

Index: 0
Blade

Autoplay #

The slides cross-fade every 4 seconds, with a thin bar along the bottom of the image filling up until the next one. A pause button sits under the dots. Rotation stops while the mouse is over the carousel (a tap or swipe does not count), while the page or the carousel is out of view, and for good once the user moves keyboard focus into it, until the play button is pressed. With :wrap="false" it pauses on the last slide, and the play button starts again from the first.

Blade

The carousel renders only its slides; the arrows and dots render wherever tk:carousel.controls is placed, and drive the carousel with the same name.

Blade

Driving it from Alpine #

Inside the carousel, and on its element through Alpine.$data(), these methods move it and control the autoplay:

Method What it does
next(), prev() Moves by one step (a slide, or a page with advance="page").
goTo(index), goToPage(page) Moves to that zero-based slide or page.
pause(), resume() Stops the autoplay until resumed, like the pause button; resuming on the last slide without wrap starts from the first.
isPaused(), isRotating() Whether the user paused it, and whether it is rotating right now (not paused, hovered, hidden or out of view).
Blade

Arrows and indicators #

arrows-position puts the arrows half outside (overlap) or in a row under the slides (outside); indicators='counter' shows a 2 / 3 counter; color paints the active dot.

Blade

Changed event #

changed fires on the tag with $event.detail.index when the user moves the carousel; here it calls a Livewire action.

Last viewed: —
Blade

Customization #

Classes go to the viewport; prefixes reach the inner parts: container: the root, slide:image: each image, arrows:prev: and arrows:next: the buttons.

Blade

Reference

tk:carousel
└── tk:carousel.slide

tk:carousel.controls (outside the carousel, linked by name)
Prop Type Default Description
slides array null Slides to render from data, after the ones in the default slot. Each item is an image URL or an array with src, alt, content, position, thumbnail and name (see tk:carousel.slide); any other key becomes an attribute of the slide.
label string null The accessible name, translated. With a label the carousel is a region landmark; without, a group.
perView int 1 How many slides are visible side by side. Ignored with fade.
gap int | string null The space between slides: a number is a Tailwind spacing step (4 is 1rem), a string any CSS length. Ignored with fade.
advance string slide slide moves one slide per step; page moves a whole view, and the dots count pages.
wrap bool true Going past the last slide returns to the first, and back from the first to the last. false stops at the ends.
fade bool false Cross-fades the slides in place instead of sliding them.
arrows bool true false removes the previous and next buttons.
arrowsPosition string over the slides overlap puts the arrows half outside the edges; outside puts them in a row under the slides.
indicators bool | string true false removes the dots; counter replaces them with a 2 / 5 counter. Off by default when thumbnails is on.
thumbnails bool false A scrollable strip of thumbnails under the slides, one per slide: the slide's thumbnail, else its first image, else its number.
autoplay bool false Moves to the next slide on its own and adds a pause button.
interval int 5000 With autoplay, the milliseconds each slide stays.
progress bool | string false With autoplay, a bar showing the time until the next slide, under the slides; overlay lays it over the bottom edge of the slides.
color string the accent color The color of the active dot, the active thumbnail ring and the progress bar.
name string a generated id Links the carousel to a tk:carousel.controls with the same name.
pauseLabel string Pause slides The accessible name of the pause button while playing, translated.
playLabel string Play slides The accessible name of the pause button while paused, translated.

wire:model and x-model bind the zero-based index of the first visible slide, as a number.

Slots

Slot Description
default tk:carousel.slide tags, rendered before the slides items. A carousel with no slides here and an empty slides renders nothing.

Events

Event Detail When
changed { index }, the new zero-based index The current slide changed by an arrow, a dot, a thumbnail, a key, a swipe or autoplay. Not fired when the index changes through wire:model or x-model.
Prop Type Default Description
src string null An image that fills the slide.
alt string null The image's alternative text, translated. Without it the image is decorative (alt="").
content string | slot null Text or markup for the slide, rendered before the default slot. A string is translated.
position string center Where the content goes when the slide has an image: center, top or bottom over the image, on a dark backdrop; above or below the image, outside it.
thumbnail string null The image shown for this slide in the thumbnails strip, in place of the slide's own image.
name string a generated id The slide's wire:key inside Livewire. An explicit wire:key takes precedence.

fade, arrows and arrowsPosition are inherited from the carousel.

Slots

Slot Description
default The slide content. With an image it is placed according to position.
Prop Type Default Description
name string null The name of the carousel to control.
arrows bool true false removes the previous and next buttons.
indicators bool | string true false removes the dots; counter replaces them with a counter.
position string over the nearest positioned ancestor The position of the arrows, as arrowsPosition on the carousel. Use outside to keep them in the flow.
color string the accent color The color of the active dot.

Events

Events are dispatched on the component's own element and do not bubble: listen on the tag itself.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes on tk:carousel go to the viewport, the element that clips the slides, and that is also where changed is dispatched. wire:model and x-model go to the root. On the other tags they go to the root element. To reach an inner element, prefix the attribute:

Prefix Tag Goes to
container: tk:carousel The root element, holding the slides and the controls.
area: tk:carousel The element around the viewport, the overlaid arrows and the overlay progress bar.
track: tk:carousel The element that moves, holding the slides.
slide: tk:carousel Each slide rendered from slides, including its own prefixes (slide:image:class). Not the slides in the slot.
arrows: tk:carousel, tk:carousel.controls The element holding the two arrows.
prev: / next: tk:carousel.arrows The previous / next button. From the carousel: arrows:prev: / arrows:next:.
indicators: tk:carousel, tk:carousel.controls The element holding the dots or the counter.
indicator: tk:carousel.indicators Each dot. From the carousel: indicators:indicator:.
counter: tk:carousel.indicators The counter. From the carousel: indicators:counter:.
thumbnails: tk:carousel The thumbnail strip.
thumbnail: tk:carousel.thumbnails Each thumbnail button. From the carousel: thumbnails:thumbnail:.
progress: tk:carousel The progress bar, with the prefixes of tk:progress.
pause-area: tk:carousel The element around the pause button.
pause: tk:carousel The pause button.
image: tk:carousel.slide The image.
content: tk:carousel.slide The content block of a slide that has an image.
blade

Accessibility

Follows the WAI-ARIA carousel pattern. The root has role="region" when it has a label, otherwise role="group", and aria-roledescription="carousel". Each slide is a group with aria-roledescription="slide" and a name such as "Slide 2 of 5"; slides out of view are aria-hidden and inert, so their links cannot be tabbed to. The dots are buttons named "Go to slide 3" ("Go to page 2" with advance="page") and the thumbnails "Go to slide 3", each set in a group named "Slides", with aria-current on the active one. The arrows are named by their tooltips ("Previous slide", "Next slide") and get aria-disabled at the ends when wrap is off. The slides are announced politely when they change, except while autoplay is rotating. The progress bar is hidden from screen readers.

Key Action
Arrow Left / Arrow Right With focus anywhere inside the carousel, moves to the previous / next slide (or page with advance="page"). Reversed in right-to-left pages. Ignored while typing in a field.

On touch and pen screens a horizontal swipe of at least 50px moves to the next or previous slide; a mostly vertical move scrolls the page. Mouse drags do not swipe.

Behavior notes

  • Autoplay stops for good when focus enters the carousel from the keyboard, until the play button is pressed; hovering, hiding the browser tab or scrolling the carousel out of view only suspends it. With reduced motion requested by the system, autoplay does not run and the pause button and the progress bar are not shown.
  • Moving to another slide from the carousel (arrows, dots, keys, swipes) restarts the autoplay countdown.
  • With advance="page" the last page is aligned to the end, so it may repeat slides of the previous page: five slides two per view show 1-2, 3-4 and 4-5.
  • With perView and advance="slide" the dots count the positions the carousel can stop at, not the slides: four slides two per view have three dots.
  • The arrows and dots are hidden when everything fits in one view.
  • A value set through wire:model or x-model is not checked against the number of slides: an index past the last one leaves no slide visible.
  • Slides added or removed by a Livewire update are picked up; when the current index no longer exists, the carousel moves to the last valid one.
  • The first image of the slides items loads with high priority and the others lazily; the images of the slides next to the current one are loaded ahead. Images of slides in the slot load as usual.
  • tk:carousel.controls placed before its carousel works too: it finds the carousel by name once both are on the page.