- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input Group
- Input OTP
- Input
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner (Toast)
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Tooltip
- Trace
Carousel
A carousel with motion and swipe built using Embla.
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-preview',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs">
<hlm-carousel-content>
@for (item of items; track item) {
<hlm-carousel-item>
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-4xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselPreview {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
}About
Carousel is built on top of Embla Carousel library and the embla-carousel-angular wrapper.
Installation
ng g @spartan-ng/cli:ui carouselnx g @spartan-ng/cli:ui carouselimport { OVERLAY_DEFAULT_CONFIG } from '@angular/cdk/overlay';
import { brnClasses, type BrnClassesOptions } from '@spartan-ng/brain/core';
import { cn, type ClassValue } from 'cn';
import { createCn } from 'cn/config';
import { makeEnvironmentProviders, type EnvironmentProviders } from '@angular/core';
import { provideSpartanHlm } from '@spartan-ng/helm/utils';
/**
* Merges class values and resolves Tailwind CSS conflicts.
*
* To customize the merge (e.g. register custom theme values), create your own instance:
*
* @example
* ```ts
*
*
* export const hlm = createCn({
* extend: { classGroups: { 'font-size': [{ text: ['hero'] }] } },
* });
* ```
*/
export const hlm = cn;
/**
* This function dynamically adds and removes classes for a given element without requiring
* the a class binding (e.g. `[class]="..."`) which may interfere with other class bindings.
* The classes are merged with `hlm`.
*/
export function classes(computed: () => ClassValue[] | string, options: Omit<BrnClassesOptions, 'merge'> = {}) {
brnClasses(computed, { ...options, merge: hlm });
}
/**
* Provides default configuration for Spartan Helm components.
*
* This utility configures the Angular CDK overlay to disable the `usePopover`
* behavior introduced in Angular 21, which causes CDK overlay-based components
* (sheets, dialogs, tooltips, etc.) to render above `position: fixed` elements
* like `<hlm-toaster>`.
*
* @returns {EnvironmentProviders} Environment providers to be added to the application config.
*
* @example
* ```ts
* // app.config.ts
*
*
* export const appConfig: ApplicationConfig = {
* providers: [
* provideSpartanHlm(),
* // ... other providers
* ],
* };
* ```
*/
export function provideSpartanHlm(): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: OVERLAY_DEFAULT_CONFIG,
useValue: { usePopover: false },
},
]);
}import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;import { ChangeDetectionStrategy, Component, Directive, computed, effect, inject, input, signal, untracked, viewChild, type InputSignal, type Signal } from '@angular/core';
import { Directionality } from '@angular/cdk/bidi';
import { EmblaCarouselDirective } from 'embla-carousel-angular';
import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { lucideArrowLeft, lucideArrowRight } from '@ng-icons/lucide';
import { type ClassValue } from 'cn';
import { type EmblaEventType, type EmblaOptionsType, type EmblaPluginType } from 'embla-carousel';
@Directive({
selector: '[hlmCarouselContent],hlm-carousel-content',
exportAs: 'hlmCarouselContent',
host: {
'data-slot': 'carousel-content',
},
})
export class HlmCarouselContent {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['flex', this._orientation() === 'horizontal' ? '-ml-4' : '-mt-4 flex-col']);
}
}
@Directive({
selector: '[hlmCarouselItem],hlm-carousel-item',
exportAs: 'hlmCarouselItem',
host: {
'data-slot': 'carousel-item',
role: 'group',
'aria-roledescription': 'slide',
},
})
export class HlmCarouselItem {
private readonly _orientation = inject(HlmCarousel).orientation;
constructor() {
classes(() => ['min-w-0 shrink-0 grow-0 basis-full', this._orientation() === 'horizontal' ? 'pl-4' : 'pt-4']);
}
}
@Component({
selector: 'button[hlm-carousel-next], button[hlmCarouselNext]',
exportAs: 'hlmCarouselNext',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowRight }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-next',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollNext()',
},
template: `
<ng-icon name="lucideArrowRight" class="rtl:rotate-180" />
<span class="sr-only">Next slide</span>
`,
})
export class HlmCarouselNext {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-end-12 top-1/2 -translate-y-1/2'
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollNext();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'button[hlm-carousel-previous], button[hlmCarouselPrevious]',
exportAs: 'hlmCarouselPrevious',
imports: [NgIcon],
providers: [provideIcons({ lucideArrowLeft }), provideBrnButtonConfig({ variant: 'outline', size: 'icon-sm' })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [{ directive: HlmButton, inputs: ['variant', 'size'] }],
host: {
'data-slot': 'carousel-previous',
'[disabled]': 'isDisabled()',
'(click)': '_carousel.scrollPrev()',
},
template: `
<ng-icon name="lucideArrowLeft" class="rtl:rotate-180" />
<span class="sr-only">Previous slide</span>
`,
})
export class HlmCarouselPrevious {
private readonly _button = inject(HlmButton);
protected readonly _carousel = inject(HlmCarousel);
private readonly _computedClass = computed(() =>
hlm(
'rounded-full absolute h-8 w-8',
this._carousel.orientation() === 'horizontal'
? '-start-12 top-1/2 -translate-y-1/2'
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
),
);
protected readonly isDisabled = () => !this._carousel.canScrollPrev();
constructor() {
effect(() => {
const computedClass = this._computedClass();
untracked(() => this._button.setClass(computedClass));
});
}
}
@Component({
selector: 'hlm-carousel-slide-display',
exportAs: 'hlmCarouselSlideDisplay',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel-slide-display',
},
template: `
<span class="sr-only">{{ _labelContent() }}</span>
<div aria-hidden="true" [class]="slideClass()">{{ _currentSlide() }} / {{ _carousel.slideCount() }}</div>
`,
})
export class HlmCarouselSlideDisplay {
protected readonly _carousel = inject(HlmCarousel);
protected readonly _currentSlide = computed(() => this._carousel.currentSlide() + 1);
public readonly slideClass = input<ClassValue>('text-muted-foreground text-sm');
/** Screen reader only text for the slide display */
public readonly label = input<string>('Slide');
protected readonly _labelContent = computed(() => {
const currentSlide = this._currentSlide();
const slideCount = this._carousel.slideCount();
return `${this.label()} ${currentSlide} of ${slideCount} is displayed`;
});
}
@Component({
selector: 'hlm-carousel',
exportAs: 'hlmCarousel',
imports: [EmblaCarouselDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'carousel',
role: 'region',
'aria-roledescription': 'carousel',
'(keydown)': 'onKeydown($event)',
},
template: `
<div
emblaCarousel
class="overflow-hidden"
[plugins]="plugins()"
[options]="_emblaOptions()"
[subscribeToEvents]="['init', 'select', 'reInit']"
(emblaChange)="onEmblaEvent($event)"
>
<ng-content select="[hlmCarouselContent],hlm-carousel-content" />
</div>
<ng-content />
`,
})
export class HlmCarousel {
protected readonly _emblaCarousel = viewChild.required(EmblaCarouselDirective);
private readonly _dir = inject(Directionality);
public readonly orientation = input<'horizontal' | 'vertical'>('horizontal');
public readonly options: InputSignal<Omit<EmblaOptionsType, 'axis'> | undefined> =
input<Omit<EmblaOptionsType, 'axis'>>();
public readonly plugins: InputSignal<EmblaPluginType[]> = input<EmblaPluginType[]>([]);
protected readonly _emblaOptions: Signal<EmblaOptionsType> = computed(() => ({
// Default embla's direction to the ambient layout direction so the carousel scrolls RTL
// automatically; an explicit `direction` in `options` still wins.
direction: this._dir.valueSignal(),
...this.options(),
axis: this.orientation() === 'horizontal' ? 'x' : 'y',
}));
private readonly _canScrollPrev = signal(false);
public readonly canScrollPrev = this._canScrollPrev.asReadonly();
private readonly _canScrollNext = signal(false);
public readonly canScrollNext = this._canScrollNext.asReadonly();
private readonly _currentSlide = signal(0);
public readonly currentSlide = this._currentSlide.asReadonly();
private readonly _slideCount = signal(0);
public readonly slideCount = this._slideCount.asReadonly();
constructor() {
classes(() => 'relative');
}
protected onEmblaEvent(event: EmblaEventType) {
const emblaApi = this._emblaCarousel().emblaApi;
if (!emblaApi) {
return;
}
if (event === 'select' || event === 'init' || event === 'reInit') {
this._canScrollPrev.set(emblaApi.canScrollPrev());
this._canScrollNext.set(emblaApi.canScrollNext());
this._currentSlide.set(emblaApi.selectedScrollSnap());
this._slideCount.set(emblaApi.scrollSnapList().length);
}
}
protected onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
this._emblaCarousel().scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
this._emblaCarousel().scrollNext();
}
}
scrollPrev() {
this._emblaCarousel().scrollPrev();
}
scrollNext() {
this._emblaCarousel().scrollNext();
}
}
export const HlmCarouselImports = [
HlmCarousel,
HlmCarouselContent,
HlmCarouselItem,
HlmCarouselPrevious,
HlmCarouselNext,
HlmCarouselSlideDisplay,
] as const;Usage
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';<hlm-carousel>
<hlm-carousel-content>
<hlm-carousel-item>
Item Content
</hlm-carousel-item>
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>Examples
Sizes
The size of the items can be set by using the basis utility class on the hlm-carousel-item .
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-sizes',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs md:max-w-sm">
<hlm-carousel-content>
@for (item of items; track item) {
<hlm-carousel-item class="md:basis-1/2 lg:basis-1/3">
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-3xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselSizes {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
}Spacing
The spacing between the items can be set by using a pl-[VALUE] utility on the hlm-carousel-item and a negative -ml-[VALUE] on the hlm-carousel-content .
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-spacing',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs md:max-w-sm">
<hlm-carousel-content class="-ml-1">
@for (item of items; track item) {
<hlm-carousel-item class="basis-1/2 pl-1 lg:basis-1/3">
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-2xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselSpacing {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
}Orientation
The orientation prop can be used to set the orientation of the carousel.
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-orientation',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-xs" orientation="vertical">
<hlm-carousel-content class="-mt-1 h-[270px]">
@for (item of items; track item) {
<hlm-carousel-item class="basis-1/2 pt-1">
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex items-center justify-center p-6">
<span class="text-3xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselOrientation {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
}Slide Count
Use hlm-carousel-slide-display to display the current and total slides.
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-slide-count',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs">
<hlm-carousel-content>
@for (item of items; track item) {
<hlm-carousel-item>
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-4xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
<hlm-carousel-slide-display class="mt-1 flex justify-end" />
</hlm-carousel>
`,
})
export class CarouselSlideCount {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
}Plugins
You can use the plugins plugins prop to add plugins to the carousel.
1
2
3
4
5
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
import Autoplay from 'embla-carousel-autoplay';
@Component({
selector: 'spartan-carousel-plugins',
imports: [HlmCarouselImports, HlmCardImports],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: 'flex w-full items-center justify-center p-4' },
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs" [plugins]="plugins">
<hlm-carousel-content>
@for (item of items; track item) {
<hlm-carousel-item>
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-4xl font-semibold">{{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselPlugins {
public items = Array.from({ length: 5 }, (_, i) => i + 1);
public plugins = [Autoplay({ delay: 3000 })];
}See the Embla Carousel docs for more information on using plugins.
RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.
شريحة 1
شريحة 2
شريحة 3
شريحة 4
شريحة 5
import { Directionality } from '@angular/cdk/bidi';
import { ChangeDetectionStrategy, Component, computed, effect, inject, untracked } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmCardImports } from '@spartan-ng/helm/card';
import { HlmCarouselImports } from '@spartan-ng/helm/carousel';
@Component({
selector: 'spartan-carousel-rtl-preview',
imports: [HlmCarouselImports, HlmCardImports],
providers: [Directionality],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex w-full items-center justify-center p-4',
'[dir]': '_dir()',
},
template: `
<hlm-carousel class="w-full max-w-48 sm:max-w-xs">
<hlm-carousel-content>
@for (item of items; track item) {
<hlm-carousel-item>
<div class="p-1">
<section hlmCard>
<p hlmCardContent class="flex aspect-square items-center justify-center p-6">
<span class="text-4xl font-semibold">{{ _t()['slide'] }} {{ item }}</span>
</p>
</section>
</div>
</hlm-carousel-item>
}
</hlm-carousel-content>
<button hlm-carousel-previous></button>
<button hlm-carousel-next></button>
</hlm-carousel>
`,
})
export class CarouselRtlPreview {
private readonly _directionality = inject(Directionality);
private readonly _language = inject(TranslateService).language;
public items = Array.from({ length: 5 }, (_, i) => i + 1);
private readonly _translations: Translations = {
en: { dir: 'ltr', values: { slide: 'Slide' } },
ar: { dir: 'rtl', values: { slide: 'شريحة' } },
he: { dir: 'rtl', values: { slide: 'שקופית' } },
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
constructor() {
effect(() => {
const dir = this._dir();
untracked(() => this._directionality.valueSignal.set(dir));
});
}
}Helm API
HlmCarouselContent
Selector: [hlmCarouselContent],hlm-carousel-content
ExportAs: hlmCarouselContent
HlmCarouselItem
Selector: [hlmCarouselItem],hlm-carousel-item
ExportAs: hlmCarouselItem
HlmCarouselNext
Selector: button[hlm-carousel-next], button[hlmCarouselNext]
ExportAs: hlmCarouselNext
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | ButtonVariants['variant'] | this._config.variant | - |
| size | ButtonVariants['size'] | this._config.size | - |
HlmCarouselPrevious
Selector: button[hlm-carousel-previous], button[hlmCarouselPrevious]
ExportAs: hlmCarouselPrevious
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | ButtonVariants['variant'] | this._config.variant | - |
| size | ButtonVariants['size'] | this._config.size | - |
HlmCarouselSlideDisplay
Selector: hlm-carousel-slide-display
ExportAs: hlmCarouselSlideDisplay
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| slideClass | ClassValue | 'text-muted-foreground text-sm' | - |
| label | string | 'Slide' | Screen reader only text for the slide display |
HlmCarousel
Selector: hlm-carousel
ExportAs: hlmCarousel
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| orientation | 'horizontal' | 'vertical' | 'horizontal' | - |
| options | Omit<EmblaOptionsType, 'axis'> | - | - |
| plugins | EmblaPluginType[] | [] | - |
On This Page