Carousel
A carousel with motion and swipe built using Embla.
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
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
└── CarouselNextSizes
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.
<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 />.
<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.
<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.
<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,
};
}RTL
To enable RTL support in Blazix, see the RTL configuration guide.
Translated with AI for the demo, so it may have mistakes.
API Reference
See the Embla Carousel docs for more information on options and plugins.
Carousel
Other attributes are passed to the element it renders.
CarouselContent
Other attributes are passed to the element it renders.
CarouselItem
Other attributes are passed to the element it renders.
CarouselPrevious
Other attributes are passed to the element it renders.
CarouselNext
Other attributes are passed to the element it renders.