Skip to content
GitHub11 stars

Carousel

A carousel with motion and swipe built using Embla.

1
2
3
4
5
<Carousel aria-label="Numbered slides" Class="w-[calc(100%-6rem)] max-w-[12rem] sm:max-w-xs">
    <CarouselContent>
        @foreach (var index in Enumerable.Range(0, 5))

About

The carousel component is a port of the Embla Carousel library. It ships as a JavaScript module, so there is nothing to install from npm.

Installation

dnx Blazix.Cli add carousel

Usage

<Carousel>
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
    <CarouselPrevious />
    <CarouselNext />
</Carousel>

Composition

Use the following composition to build a Carousel:

Carousel
├── CarouselContent
│   ├── CarouselItem
│   └── CarouselItem
├── CarouselPrevious
└── CarouselNext

Sizes

To set the size of the items, you can use the basis utility class on the <CarouselItem />. Here basis-1/3 makes each item a third of the carousel's width; the second example makes them half its width on medium screens and a third on large ones.

1
2
3
4
5
<Carousel Opts="@Opts" aria-label="Slides a third wide" Class="w-[calc(100%-6rem)] max-w-[12rem] sm:max-w-xs md:max-w-sm">
    <CarouselContent>
        @foreach (var index in Enumerable.Range(0, 5))
<Carousel>
    <CarouselContent>
        <CarouselItem Class="basis-1/3">...</CarouselItem>
        <CarouselItem Class="basis-1/3">...</CarouselItem>
        <CarouselItem Class="basis-1/3">...</CarouselItem>
    </CarouselContent>
</Carousel>
<Carousel>
    <CarouselContent>
        <CarouselItem Class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
        <CarouselItem Class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
        <CarouselItem Class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
    </CarouselContent>
</Carousel>

Spacing

To set the spacing between the items, we use a ps-[VALUE] utility on the <CarouselItem /> and a negative -ms-[VALUE] on the <CarouselContent />.

1
2
3
4
5
<Carousel aria-label="Slides with spacing" Class="w-[calc(100%-6rem)] max-w-[12rem] sm:max-w-xs md:max-w-sm">
    <CarouselContent Class="-ms-1">
        @foreach (var index in Enumerable.Range(0, 5))
<Carousel>
    <CarouselContent Class="-ms-4">
        <CarouselItem Class="ps-4">...</CarouselItem>
        <CarouselItem Class="ps-4">...</CarouselItem>
        <CarouselItem Class="ps-4">...</CarouselItem>
    </CarouselContent>
</Carousel>
<Carousel>
    <CarouselContent Class="-ms-2 md:-ms-4">
        <CarouselItem Class="ps-2 md:ps-4">...</CarouselItem>
        <CarouselItem Class="ps-2 md:ps-4">...</CarouselItem>
        <CarouselItem Class="ps-2 md:ps-4">...</CarouselItem>
    </CarouselContent>
</Carousel>

Orientation

Use the Orientation parameter to set the orientation of the carousel.

1
2
3
4
5
<Carousel Opts="@Opts" Orientation="Orientation.Vertical" aria-label="Vertical slides" Class="w-full max-w-xs">
    <CarouselContent Class="-mt-1 h-[270px]">
        @foreach (var index in Enumerable.Range(0, 5))
<Carousel Orientation="Orientation.Vertical">
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
</Carousel>

Options

You can pass options to the carousel using the Opts parameter. CarouselOptions mirrors Embla's options. See the Embla Carousel docs for more information.

<Carousel Opts="@Opts">
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
</Carousel>

@code {
    private static readonly CarouselOptions Opts = new()
    {
        Align = Align.Start,
        Loop = true,
    };
}

API

Use the SetApi callback to get an instance of the carousel API. Its properties are a snapshot that the carousel refreshes before every event.

1
2
3
4
5
Slide 0 of 0
<div class="mx-auto max-w-[10rem] sm:max-w-xs">
    <Carousel SetApi="SetApi" aria-label="Slides with a counter" Class="w-full max-w-xs">
        <CarouselContent>
<Carousel SetApi="SetApi">
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
</Carousel>

@code {
    private int _current;
    private int _count;

    private void SetApi(CarouselApi api)
    {
        _count = api.ScrollSnapList.Count;
        _current = api.SelectedScrollSnap + 1;

        api.On("select", _ =>
        {
            _current = api.SelectedScrollSnap + 1;
            StateHasChanged();
        });
    }
}

Events

You can listen to events using the API instance from SetApi. Handlers run outside the component's own event pipeline, so call StateHasChanged when a handler changes what the component renders.

<Carousel SetApi="SetApi">
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
</Carousel>

@code {
    private void SetApi(CarouselApi api)
    {
        api.On("select", _ => { });
    }
}

See the Embla Carousel docs for more information on using events. Use Off to remove a handler.

Plugins

You can use the Plugins parameter to add plugins to the carousel. CarouselAutoplay is a port of embla-carousel-autoplay.

<Carousel Plugins="@([_autoplay])">
    <CarouselContent>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
        <CarouselItem>...</CarouselItem>
    </CarouselContent>
</Carousel>

@code {
    private readonly CarouselAutoplay _autoplay = new()
    {
        Delay = 2000,
    };
}
1
2
3
4
5
@* Slides that move by themselves need a way to stop them: the button, besides hover and focus. *@
<div class="flex w-full flex-col items-center gap-2">
    <Carousel Plugins="@([_plugin])" aria-label="Autoplaying slides" Class="w-full max-w-[10rem] sm:max-w-xs"

RTL

To enable RTL support in Blazix, see the RTL configuration guide.

Translated with AI for the demo, so it may have mistakes.

١
٢
٣
٤
٥
@* The carousel scrolls right to left on its own: it follows the text direction around it. *@
<Carousel aria-label="@Texts[Language]" Class="w-[calc(100%-6rem)] max-w-[12rem] sm:max-w-xs">
    <CarouselContent>

API Reference

See the Embla Carousel docs for more information on options and plugins.

ParameterTypeDefault
Orientation Orientation Orientation.Horizontal
Opts CarouselOptions? -
Plugins IReadOnlyList<CarouselPlugin>? -
SetApi EventCallback<CarouselApi> -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

CarouselContent

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

CarouselItem

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

CarouselPrevious

ParameterTypeDefault
Variant ButtonVariant ButtonVariant.Outline
Size ButtonSize ButtonSize.IconSm
Type ButtonType ButtonType.Button
Class string? -

Other attributes are passed to the element it renders.

CarouselNext

ParameterTypeDefault
Variant ButtonVariant ButtonVariant.Outline
Size ButtonSize ButtonSize.IconSm
Type ButtonType ButtonType.Button
Class string? -

Other attributes are passed to the element it renders.