Carousel
One story at a time. Scrollable slides with explicit, accessible controls.
<div class="carousel">
<div><div><span class="badge">HTML</span><h2>Start with content.</h2><p>Semantic markup is the foundation, not a compilation target.</p></div></div>
<div><div><span class="badge">CSS</span><h2>A shared visual language.</h2><p>Design tokens keep every block consistent, with room to make it yours.</p></div></div>
<div><div><span class="badge">JavaScript</span><h2>Enhance, don't replace.</h2><p>Small modules add interactions while preserving your authored content.</p></div></div>
</div>.carousel-track { display: flex; gap: 1rem; overflow-x: auto; scroll-snap-type: x mandatory; position: relative; }
.carousel-slide { flex: 0 0 100%; min-width: 0; scroll-snap-align: start; padding: 2rem; border: 1px solid var(--border); border-radius: var(--radius); background: var(--muted); }
.carousel-slide img { width: 100%; aspect-ratio: 16 / 8; object-fit: cover; border-radius: var(--radius); }
.carousel-controls { display: flex; align-items: center; gap: 0.5rem; margin-top: 1rem; }
.carousel-controls span { margin-left: auto; font-size: 12px; color: var(--muted-foreground); }export default function decorate(block) {
if (block.querySelector('.carousel-controls')) return;
const slides = [...block.children];
if (!slides.length) return;
const track = document.createElement('div');
track.className = 'carousel-track';
track.id = `carousel-${crypto.randomUUID()}`;
track.tabIndex = 0;
track.setAttribute('role', 'region');
track.setAttribute('aria-label', block.dataset.label || 'Slides');
slides.forEach((slide, index) => {
slide.classList.add('carousel-slide');
slide.setAttribute('role', 'group');
slide.setAttribute('aria-label', `${index + 1} of ${slides.length}`);
track.append(slide);
});
const controls = document.createElement('div');
controls.className = 'carousel-controls';
const status = document.createElement('span');
status.setAttribute('aria-live', 'polite');
let current = 0;
const events = [];
['Previous', 'Next'].forEach((label, index) => {
const button = document.createElement('button');
button.type = 'button';
button.className = 'button-secondary';
button.textContent = label;
button.setAttribute('aria-controls', track.id);
const handler = () => {
current = (current + (index ? 1 : -1) + slides.length) % slides.length;
track.scrollTo({ left: slides[current].offsetLeft - track.offsetLeft, behavior: matchMedia('(prefers-reduced-motion: reduce)').matches ? 'instant' : 'smooth' });
status.textContent = `Slide ${current + 1} of ${slides.length}`;
};
button.addEventListener('click', handler);
events.push([button, handler]);
controls.append(button);
});
controls.append(status);
block.replaceChildren(track, controls);
return () => { events.forEach(([button, handler]) => button.removeEventListener('click', handler)); block.replaceChildren(...slides); };
}Installation
Run the source CLI from a Blocks UI checkout. This package is not published to npm.
node packages/cli/index.js add carouselOr copy the source files above. Include tokens.css and base.css once in your project.
Usage
Use the authored row-and-column structure, then let the standalone runtime discover and enhance the block.
<div class="carousel">
<div><div><span class="badge">HTML</span><h2>Start with content.</h2><p>Semantic markup is the foundation, not a compilation target.</p></div></div>
<div><div><span class="badge">CSS</span><h2>A shared visual language.</h2><p>Design tokens keep every block consistent, with room to make it yours.</p></div></div>
<div><div><span class="badge">JavaScript</span><h2>Enhance, don't replace.</h2><p>Small modules add interactions while preserving your authored content.</p></div></div>
</div>import decorate from './blocks/carousel/carousel.js';
document.querySelectorAll('.carousel').forEach(decorate);Variants
Choose a variant in the live preview. Add its class to the authored block.
defaultAccessibility & behavior
No autoplay. Previous and Next buttons announce the selected slide. The track is keyboard scrollable and respects reduced-motion preferences.
Multiple instances are supported. Layouts adapt to smaller viewports. No npm dependencies are required at runtime.
AEM usage
Copy the canonical CSS and JavaScript into your EDS block folder. Use the same row-and-column authoring structure shown above and let your existing AEM loader call the default decorator.
Standalone verified. EDS markup compatible. Live AEM integration has not been verified.