Composite
ContentCarousel
A flexible carousel component supporting images, custom content blocks, drag navigation, autoplay, and responsive sizing.
Basic usage (items prop)
Pass an array of items to quickly create an image carousel. Each item uses the Image block and supports all ImageProps (src, alt, width, height, aspectRatio, layout, etc.).
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
{
src: 'https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=800&h=600&fit=crop',
alt: 'Desert dunes',
},
];
---
<ContentCarousel
items={sampleImages}
autoplay={{ enabled: true, delay: 3000, pauseOnHover: true }}
loop
debug
/>Slot children with interactive blocks
Use the default slot to include any custom content, including interactive components. The carousel intelligently distinguishes between drag gestures and clicks.
Interactive Content
Clicking buttons works seamlessly — drag to navigate, click to interact. The carousel uses a movement threshold to distinguish between the two.
Third Slide
You can place any Astro component here — Images, Videos, Buttons, even nested layouts or other composite blocks.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
import Button from '@alfadormx/blocks/primitives/Button.astro';
import ContentCarouselItem from '@alfadormx/blocks/primitives/ContentCarouselItem.astro';
import Image from '@alfadormx/blocks/primitives/Image.astro';
---
<ContentCarousel>
<ContentCarouselItem basis="basis-full" background={{ class: 'bg-surface' }}>
<Image
src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop"
alt="Mountain"
width={400}
height={300}
class="rounded-lg"
/>
<h3 class="text-xl font-bold">Slide with Image Block</h3>
<Button
href="https://unsplash.com"
target="_blank"
label="View on Unsplash"
icon={{ name: 'heroicons:arrow-top-right-on-square' }}
intent="primary"
/>
</ContentCarouselItem>
<ContentCarouselItem basis="basis-full" background={{ class: 'bg-surface' }}>
<h3 class="text-xl font-bold">Interactive Content</h3>
<p class="text-center max-w-md">
Clicking buttons works seamlessly — drag to navigate, click to interact. The carousel uses a
movement threshold to distinguish between the two.
</p>
<div class="flex gap-3">
<Button label="Action 1" icon={{ name: 'heroicons:star' }} intent="primary" />
<Button label="Action 2" icon={{ name: 'heroicons:heart' }} intent="secondary" />
</div>
</ContentCarouselItem>
<ContentCarouselItem basis="basis-full" background={{ class: 'bg-surface' }}>
<h3 class="text-xl font-bold">Third Slide</h3>
<p class="text-center max-w-md">
You can place any Astro component here — Images, Videos, Buttons, even nested layouts or other
composite blocks.
</p>
</ContentCarouselItem>
</ContentCarousel>Autoplay and responsive sizing
Enable autoplay with configurable delay and pause behavior. Use itemClass with Tailwind basis utilities for responsive layouts (e.g., one slide on mobile, multiple on desktop).
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
{
src: 'https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=800&h=600&fit=crop',
alt: 'Desert dunes',
},
];
---
<ContentCarousel
items={sampleImages}
autoplay={{ enabled: true, delay: 3000, pauseOnHover: true }}
loop={true}
itemClass="basis-full md:basis-1/2 lg:basis-1/3"
/>Outside controls
Position controls and indicators outside the carousel for a cleaner content area.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel items={sampleImages} controlsPosition="outside" />Loop mode
Enable loop for continuous infinite scrolling using cloned-slide technique for seamless transitions.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel
items={[{ ...sampleImages[0], id: 'loop-mountain' }, ...sampleImages.slice(1)]}
loop={true}
/>Continuous autoplay
autoplay: { mode: 'continuous', speed } drifts the track at speed pixels per second (default 40) instead of stepping. It requires loop, pauses on hover, focus and the pause button, and does not start with reduced motion. Prev and next snap to the nearest slide in that direction.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
{
src: 'https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=800&h=600&fit=crop',
alt: 'Desert dunes',
},
];
---
<ContentCarousel
items={sampleImages}
loop
itemClass="basis-1/2 md:basis-1/3"
showIndicators={false}
controlsPosition="outside"
autoplay={{ mode: 'continuous', speed: 60 }}
/>Button config
defaultIndicatorButtonConfig, pauseButton and playButton reach their buttons, and the autoplay toggle keeps the labels you set.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel
items={sampleImages}
controlsPosition="outside"
autoplay={{ enabled: true, delay: 3000, pauseOnHover: false }}
defaultIndicatorButtonConfig={{ shape: 'square', 'data-test': 'indicator' }}
pauseButton={{ 'aria-label': 'Stop slideshow' }}
playButton={{ 'aria-label': 'Start slideshow' }}
/>Container
container wraps the carousel in a Container; carousel attributes stay on the inner root. Without it no wrapper is rendered.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel
items={sampleImages}
container={{ background: { class: 'bg-surface' }, spacing: { padding: 'p-6' } }}
/>Indicator class
defaultIndicatorButtonConfig.class can recolour the selected indicator.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel
items={sampleImages}
defaultIndicatorButtonConfig={{ class: 'aria-selected:bg-accent' }}
/>Image config
defaultImageConfig reaches every slide image; a slide’s class stays on its wrapper. The image optimiser writes object-fit inline: cover, or contain with layout: 'contained'. Change it through layout, not a class.
---
import ContentCarousel from '@alfadormx/blocks/composite/ContentCarousel.astro';
const sampleImages = [
{
src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=600&fit=crop',
alt: 'Mountain landscape',
},
{
src: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=800&h=600&fit=crop',
alt: 'Forest path',
},
{
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800&h=600&fit=crop',
alt: 'Ocean waves',
},
];
---
<ContentCarousel items={sampleImages} defaultImageConfig={{ class: 'rounded-xl' }} />Usage notes
Props
items: Array of CarouselItem objects (optional if using slot)itemClass: Tailwind classes for slide sizing (default:basis-full)controlsPosition:'inside'(overlay) or'outside'(adjacent)- Hide prev/next with
prevButton={false}/nextButton={false}; indicators withshowIndicators={false} draggable: Enable/disable pointer drag navigation (default:true)loop: Enable infinite looping (default:false)autoplay: Boolean, number (delay in ms), or config object; setmode: 'continuous'withloopfor a smooth drift atspeedpx/ssnapFallback:'transform'(default) or'scroll-snap'
Accessibility
- Screen reader announcements for slide changes
- ARIA labels and roles for carousel structure
- Respects
prefers-reduced-motionfor autoplay
Keyboard
- The indicators are one Tab stop. Left/Right arrows move to the previous or next slide, and Home/End to the first or last; focus follows the active indicator.
- The same keys work while the previous or next button has focus. When a button hides at the first or last slide, focus moves to the active indicator.
- In RTL, Left and Right swap.
- With
controlsPosition: 'inside', the overlay controls show while anything inside the carousel has focus, as they do on hover.
Responsive sizing
Use Tailwind’s basis-* utilities in itemClass for responsive layouts:
basis-full: One slide at a time (default)basis-1/2 md:basis-1/3: Two slides on mobile, three on tablet+basis-full sm:basis-1/2 lg:basis-1/4: Progressively show more slides
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| items | CarouselItem[] | [] | Images rendered as slides when no default slot content is provided (default: []). |
| initialIndex | number | 0 | Slide index shown on load (default: 0). |
| itemClass | string | 'basis-full' | Tailwind classes applied to each rendered slide, e.g. for basis sizing (default: 'basis-full'). |
| controlsPosition | 'inside' | 'outside' | 'inside' | Whether controls/indicators overlay the viewport or sit adjacent to it (default: 'inside'). |
| showIndicators | boolean | true | Whether the slide indicator dots are rendered (default: true). |
| draggable | boolean | true | Whether pointer/touch drag navigation is enabled (default: true). |
| loop | boolean | false | Whether the carousel loops infinitely using cloned slides (default: false). |
| autoplay | AutoplayConfig | boolean | number | false | Autoplay: boolean to enable or disable, a number as step delay in ms, or a step or continuous config. Continuous requires `loop` (default: false). |
| snapFallback | 'transform' | 'scroll-snap' | 'transform' | Positioning strategy used when moving between slides (default: 'transform'). |
| ariaLabel | string | — | Accessible label for the carousel region (falls back to 'Content carousel' when unset). |
| debug | boolean | false | Shows a debug overlay with live drag/index state (default: false). |
| prevButton | ButtonProps | false | — | Previous-slide button merged over the default; `false` hides it (default: rendered). |
| nextButton | ButtonProps | false | — | Next-slide button merged over the default; `false` hides it (default: rendered). |
| defaultIndicatorButtonConfig | LayerConfig<ButtonProps> | — | Button config merged under every slide indicator; `aria-label` is a prefix, and the slide number is appended. |
| defaultImageConfig | LayerConfig<ImageProps> | — | Image config merged under every slide in `items`, over the defaults (default: { class: 'w-full h-auto', layout: 'responsive', aspectRatio: 16 / 9 }). Named after the block it reaches, since a slide's `class` stays on its wrapper (contract A14). `object-fit` is written inline by the image optimiser (`cover`; `contain` with `layout: 'contained'`); change it through `layout`, not a class. |
| pauseButton | ButtonProps | — | Autoplay pause button merged over the default. |
| playButton | ButtonProps | — | Autoplay resume button merged over the default. |
| container | ContainerProps | — | Container wrapping the carousel; rendered only when set. Carousel attributes stay on the inner root. |
| class | string | '' | Additional class names applied to the root carousel element (default: ''). |