Carousel

A slideshow with motion and swipe, built on Embla.

MediaExperimental

Installation

It has its own entry point, @semantic-components/ui/carousel, built on embla-carousel — an optional peer of ui: install it where you use the component.

ts
import { ScCarousel, ScCarouselViewport, ScCarouselTrack, ScCarouselItem } from '@semantic-components/ui/carousel';

Usage

import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';import {  SiChevronLeftIcon,  SiChevronRightIcon,} from '@semantic-icons/lucide-icons';
/** shadcn's demo: five cards, a slide each; the arrows (or ← →) move between them. */@Component({  selector: 'app-carousel-usage-demo',  imports: [    ScCarousel,    ScCarouselItem,    ScCarouselNext,    ScCarouselPrevious,    ScCarouselTrack,    ScCarouselViewport,    ScCard,    ScCardBody,    SiChevronLeftIcon,    SiChevronRightIcon,  ],  template: `    <div      scCarousel      aria-roledescription="carousel"      aria-label="Numbers"      class="w-full max-w-[12rem] sm:max-w-xs"    >      <div scCarouselViewport>        <div scCarouselTrack>          @for (n of slides; track n) {            <div              scCarouselItem              #slide="scCarouselItem"              aria-roledescription="slide"              [attr.aria-label]="slide.index() + 1 + ' of ' + slide.count()"            >              <div class="p-1">                <div scCard>                  <div                    scCardBody                    class="flex aspect-square items-center justify-center p-6"                  >                    <span class="text-4xl font-semibold">{{ n }}</span>                  </div>                </div>              </div>            </div>          }        </div>      </div>      <button scCarouselPrevious>        <svg siChevronLeftIcon></svg>        <span class="sr-only">Previous slide</span>      </button>      <button scCarouselNext>        <svg siChevronRightIcon></svg>        <span class="sr-only">Next slide</span>      </button>    </div>  `,  host: { class: 'flex w-full justify-center' },  encapsulation: ViewEncapsulation.None,})export class CarouselUsageDemo {  private readonly slides = [1, 2, 3, 4, 5];}

Anatomy

A region holding a viewport, its track of slides, and the previous and next buttons. You write the slides, the icons and every name.

import {  ScCarousel, ScCarouselViewport, ScCarouselTrack,  ScCarouselItem, ScCarouselPrevious, ScCarouselNext,  ScCarouselIndicators, ScCarouselIndicator,} from '@semantic-components/ui/carousel';
<div scCarousel aria-roledescription="…" aria-label="…">  <div scCarouselViewport>    <div scCarouselTrack>      <div scCarouselItem #slide="scCarouselItem"        aria-roledescription="…" [attr.aria-label]="…">…</div>    </div>  </div>  <button scCarouselPrevious>    <svg siChevronLeftIcon></svg><span class="sr-only">…</span>  </button>  <button scCarouselNext>    <svg siChevronRightIcon></svg><span class="sr-only">…</span>  </button>  <div scCarouselIndicators aria-label="…">    @for (i of carousel.positions(); track i) {      <button scCarouselIndicator [index]="i"><span class="sr-only">…</span></button>    }  </div></div>

Accessibility

  • The root is a region: name it (aria-label) and give it your word for carousel (aria-roledescription).
  • Each slide is a group: your word for slide (aria-roledescription) and its place (aria-label, from index() and count(): "2 of 5").
  • With focus inside, the arrow keys move: ← and → (mirrored right to left), ↑ and ↓ when vertical. The viewport is a tab stop, and takes focus when a slide is clicked, so the keys work from there too.
  • The indicators are a tablist (name it), one tab stop: a tab per position, the current one selected; ← and → move between them and show each one's slide, Home and End the first and last.
  • The buttons are disabled at either end (unless it loops); name them (a span with sr-only) and write their icons — mirrored right to left by the buttons.
  • Moving by itself (autoplay)? Give a pause button, and stop on hover and focus (WCAG 2.2.2): the Plugin example.

Examples

Sizes

1
2
3
4
5
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

Spacing

1
2
3
4
5
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

Orientation

1
2
3
4
5
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

API

1
2
3
4
5

Slide 1 of 0

import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

Indicators

1
2
3
4
5
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselIndicator,  ScCarouselIndicators,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,

Align

Mountains under a clear sky
A rocky coast
A forest path
A river in a gorge
A street cafe
A snowy summit
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScAspectRatio,  ScField,  ScFrame,  ScLabel,  ScSelect,  ScSelectIcon,  ScSelectItem,

Pages

1
2
3
4
5
6
7
8
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselIndicator,  ScCarouselIndicators,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,

Thumbnails

Mountains under a clear sky
A rocky coast
A forest path
A river in a gorge
A street cafe
A snowy summit
A shed in a field
A waterfall
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,

Drag free

1
2
3
4
5
6
7
8
9
10
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

Progress

1
2
3
4
5
6
7
8
import {  Component,  ViewEncapsulation,  effect,  signal,  viewChild,} from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,

Controlled

1
2
3
4
5

Slide 1 of 5

import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton, ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';
/** Controlled from outside: [(activeIndex)] both ways, plain buttons calling scrollPrev() and scrollNext() from exportAs; drag off (Embla’s watchDrag), so only they and the keys move it. */

Plugin

Embla's autoplay plugin, a package of its own: install it where you use it.

1
2
3
4
5
import {  Component,  ViewEncapsulation,  effect,  signal,  viewChild,} from '@angular/core';import { ScButton, ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,

Multiple

1
2
3
4
5
import { Component, ViewEncapsulation } from '@angular/core';import { ScCard, ScCardBody } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,} from '@semantic-components/ui/carousel';

Images

Mountains under a clear sky
A rocky coast
A forest path
A river in a gorge
A street cafe
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,  ScCarouselNext,  ScCarouselPrevious,  ScCarouselTrack,  ScCarouselViewport,

RTL

1
2
3
4
5
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScCard,  ScCardBody,  injectFormatNumber,} from '@semantic-components/ui';import {  ScCarousel,  ScCarouselItem,

API reference

Each part takes class, merged over the style (cn: a later class wins).

PartSelectorDescription
ScCarouseldiv[scCarousel] The region. orientation ('horizontal', 'vertical'); options and plugins (Embla's: a change starts it again with them; its direction from the page unless given); [(activeIndex)]. exportAs scCarousel: api() (Embla's), count() (positions), positions() (0 … count − 1, for the dots), canScrollPrev(), canScrollNext(), scrollPrev(), scrollNext().
ScCarouselViewportdiv[scCarouselViewport] Embla's viewport: the window the slides move in. A tab stop, focused by a click on a slide, so the arrows work from there.
ScCarouselTrackdiv[scCarouselTrack] The row of slides (a column, vertical); its negative margin matches the slides’ gap.
ScCarouselItemdiv[scCarouselItem] A slide, a group: a full basis by default (basis-1/2… for more in view), its gap as start padding. exportAs scCarouselItem: index() and count(), for its name.
ScCarouselPrevious, ScCarouselNextbutton[…] The style's button (variant 'outline', size 'icon-sm', the style's corners) outside either end; disabled when it can't move; your icon, mirrored right to left.
ScCarouselIndicatorsdiv[scCarouselIndicators] A tablist (name it), inside the carousel: one tab stop; ← → (mirrored right to left), Home and End move between its indicators and show their slides.
ScCarouselIndicatorbutton[scCarouselIndicator] A position’s tab (index): a click goes there; the current one is selected (aria-selected) and wider. A 24px target that draws its mark, in the style’s corners. Write its name.