carousel

A carousel with motion and swipe built using Embla.

PreviousNext
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCarouselImports } from '@/shared/components/carousel/carousel.imports';

@Component({
  imports: [ZardCarouselImports, ZardCardImports],
  template: `
    <div class="w-full max-w-[12rem] sm:max-w-xs">
      <z-carousel>
        <z-carousel-content>
          @for (slide of slides; track slide) {
            <z-carousel-item>
              <div class="p-1">
                <z-card>
                  <z-card-content class="flex aspect-square items-center justify-center p-6">
                    <span class="text-4xl font-semibold">{{ slide }}</span>
                  </z-card-content>
                </z-card>
              </div>
            </z-carousel-item>
          }
        </z-carousel-content>
      </z-carousel>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCarouselPreviewComponent {
  protected slides = ['1', '2', '3', '4', '5'];
}

About

The carousel is built using the Embla Carousel library. Embla Carousel

Installation

Copy
npx zard-cli@latest add carousel

Usage

import { ZardCarouselImports } from '@/shared/components/carousel/carousel.imports';
Copy
<z-carousel>
  <z-carousel-content>
    <z-carousel-item>Slide 1</z-carousel-item>
    <z-carousel-item>Slide 2</z-carousel-item>
    <z-carousel-item>Slide 3</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy

Examples

sizes

To set the size of the items, you can use the basis utility class on the <z-carousel-item />.

1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCarouselImports } from '@/shared/components/carousel/carousel.imports';

@Component({
  imports: [ZardCarouselImports, ZardCardImports],
  template: `
    <div class="w-full max-w-[12rem] sm:max-w-xs md:max-w-sm">
      <z-carousel [zOptions]="{ align: 'start' }">
        <z-carousel-content>
          @for (slide of slides; track slide) {
            <z-carousel-item class="basis-1/2 lg:basis-1/3">
              <div class="p-1">
                <z-card>
                  <z-card-content class="flex aspect-square items-center justify-center p-6">
                    <span class="text-3xl font-semibold">{{ slide }}</span>
                  </z-card-content>
                </z-card>
              </div>
            </z-carousel-item>
          }
        </z-carousel-content>
      </z-carousel>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCarouselSizeComponent {
  protected slides = ['1', '2', '3', '4', '5'];
}
<!-- 33% of the carousel width. -->
<z-carousel>
  <z-carousel-content>
    <z-carousel-item class="basis-1/3">...</z-carousel-item>
    <z-carousel-item class="basis-1/3">...</z-carousel-item>
    <z-carousel-item class="basis-1/3">...</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy
<!-- 50% on small screens and 33% on larger screens. -->
<z-carousel>
  <z-carousel-content>
    <z-carousel-item class="md:basis-1/2 lg:basis-1/3">...</z-carousel-item>
    <z-carousel-item class="md:basis-1/2 lg:basis-1/3">...</z-carousel-item>
    <z-carousel-item class="md:basis-1/2 lg:basis-1/3">...</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy

spacing

To set the spacing between the items, we use a pl-[VALUE] utility on the <z-carousel-item /> and a negative -ml-[VALUE] on the <z-carousel-content />.

1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCarouselImports } from '@/shared/components/carousel/carousel.imports';

@Component({
  imports: [ZardCarouselImports, ZardCardImports],
  template: `
    <div class="w-full max-w-[12rem] sm:max-w-xs md:max-w-sm">
      <z-carousel>
        <z-carousel-content class="-ml-1">
          @for (slide of slides; track slide) {
            <z-carousel-item class="basis-1/2 pl-1 lg:basis-1/3">
              <div class="p-1">
                <z-card>
                  <z-card-content class="flex aspect-square items-center justify-center p-6">
                    <span class="text-2xl font-semibold">{{ slide }}</span>
                  </z-card-content>
                </z-card>
              </div>
            </z-carousel-item>
          }
        </z-carousel-content>
      </z-carousel>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCarouselSpacingComponent {
  protected slides = ['1', '2', '3', '4', '5'];
}
<z-carousel>
  <z-carousel-content class="-ml-4">
    <z-carousel-item class="pl-4">...</z-carousel-item>
    <z-carousel-item class="pl-4">...</z-carousel-item>
    <z-carousel-item class="pl-4">...</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy
<z-carousel>
  <z-carousel-content class="-ml-2 md:-ml-4">
    <z-carousel-item class="pl-2 md:pl-4">...</z-carousel-item>
    <z-carousel-item class="pl-2 md:pl-4">...</z-carousel-item>
    <z-carousel-item class="pl-2 md:pl-4">...</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy

orientation

Use the zOrientation input to set the orientation of the carousel.

1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCarouselImports } from '@/shared/components/carousel/carousel.imports';

@Component({
  imports: [ZardCarouselImports, ZardCardImports],
  template: `
    <div class="w-full min-w-xs">
      <z-carousel [zOptions]="{ align: 'start' }" zOrientation="vertical">
        <z-carousel-content class="-mt-1 h-[270px]">
          @for (slide of slides; track slide) {
            <z-carousel-item class="basis-1/2 pt-1">
              <div class="p-1">
                <z-card>
                  <z-card-content class="flex items-center justify-center p-6">
                    <span class="text-3xl font-semibold">{{ slide }}</span>
                  </z-card-content>
                </z-card>
              </div>
            </z-carousel-item>
          }
        </z-carousel-content>
      </z-carousel>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCarouselOrientationComponent {
  protected slides = ['1', '2', '3', '4', '5'];
}
<z-carousel zOrientation="vertical | horizontal">
  <z-carousel-content>
    <z-carousel-item>...</z-carousel-item>
    <z-carousel-item>...</z-carousel-item>
    <z-carousel-item>...</z-carousel-item>
  </z-carousel-content>
</z-carousel>
Copy

API Reference

z-carouselComponent

A carousel component with slide controls and swipe gesture support.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
[zOptions] Embla Carousel configuration options EmblaOptionsType {loop: false}
[zPlugins] Embla Carousel plugins EmblaPluginType[] []
[zOrientation] Carousel orientation 'horizontal' | 'vertical' 'horizontal'
[zControls] Navigation type buttons 'button' | 'dot' | 'none' 'button'
(zInited) Emits Embla API when carousel is initialized EmblaCarouselType -
(zSelected) Emitted when a slide is selected void -

z-carousel-contentComponent

The content container for the carousel.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''

z-carousel-itemComponent

An individual carousel item.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
github iconwhatsapp icondiscord iconX icon

Made with in Brazil. Open source and available on GitHub .