Carousel
Slides in a scroll-snap viewport with previous and next buttons, a slide picker and optional autoplay that can be paused.
Examples
Loop with buttons
Arrow keys move between slides while the slides have focus.
Slide: 1
Razor
<div class="w-full max-w-md">
<KlyCarousel Items="Items" @bind-Index="_index" Loop AriaLabel="Featured places" data-testid="places" />
<p class="mt-2 text-sm text-(--klyroo-text-muted)" data-testid="places-state">Slide: @(_index + 1)</p>
</div>
@code {
private int _index;
private static RenderFragment Slide(string title, string text) =>
@<div class="flex h-40 flex-col items-center justify-center gap-1 bg-(--klyroo-bg-muted) p-4 text-center">
<p class="font-semibold text-(--klyroo-text-highlighted)">@title</p>
<p class="text-sm text-(--klyroo-text-muted)">@text</p>
</div>;
private static readonly KlyCarouselItem[] Items =
[
new(Slide("Lisbon", "Hills, trams and tiles.")) { Label = "Lisbon" },
new(Slide("Kyoto", "Temples and tea houses.")) { Label = "Kyoto" },
new(Slide("Cape Town", "Between the mountain and the sea.")) { Label = "Cape Town" },
new(Slide("Montreal", "Murals and bagels.")) { Label = "Montreal" },
];
}Not bound
Without a binding the browser keeps the current slide, even when other parameters change.
Razor
<div class="w-full max-w-md">
<KlyCarousel Items="Items" Loop="_loop" AriaLabel="Unbound slides" data-testid="unbound-carousel" />
<KlyButton Variant="ButtonVariant.Outline" Label="Toggle looping" OnClick="Toggle" class="mt-2" />
</div>
@code {
private bool _loop;
private void Toggle() => _loop = !_loop;
private static RenderFragment Slide(string text) =>
@<div class="flex h-24 items-center justify-center bg-(--klyroo-bg-muted) p-4 text-center text-sm text-(--klyroo-text-highlighted)">@text</div>;
private static readonly KlyCarouselItem[] Items =
[
new(Slide("First")) { Label = "First" },
new(Slide("Second")) { Label = "Second" },
new(Slide("Third")) { Label = "Third" },
];
}Autoplay
Autoplay pauses on hover and focus and has a pause button; it stays off for visitors who prefer reduced motion.
Razor
<div class="w-full max-w-md">
<KlyCarousel Items="Items" AutoPlay="2000" Loop Arrows="false" AriaLabel="News" data-testid="news" />
</div>
@code {
private static RenderFragment Slide(string text) =>
@<div class="flex h-32 items-center justify-center bg-(--klyroo-bg-muted) p-4 text-center text-sm text-(--klyroo-text-highlighted)">@text</div>;
private static readonly KlyCarouselItem[] Items =
[
new(Slide("A new release is out.")),
new(Slide("The docs have a new section.")),
new(Slide("The next meetup is in March.")),
];
}API
KlyCarousel
Scroll-snap carousel with previous and next buttons, slide dots and optional autoplay, following the WAI-ARIA carousel pattern.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
AriaLabel (required) |
string |
Accessible name of the carousel. | |
Arrows |
bool |
true |
Show the previous and next buttons. |
AutoPlay |
int |
0 |
Milliseconds between automatic slide changes. Zero (the default) turns autoplay off. Autoplay stops while the pointer or focus is inside and has a pause button; it does not start for visitors who prefer reduced motion. |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Color |
KlyColor |
"primary" |
Semantic color of the carousel: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else the default of the component. Values: primary, secondary, success, info, warning, error, neutral. |
Dots |
bool |
true |
Show a button for each slide. |
Index (@bind-Index) |
int |
0 |
Zero-based index of the current slide. Use @bind-Index to follow swipes and button presses. |
Items (required) |
IReadOnlyList |
The slides, as KlyCarouselItem . | |
Loop |
bool |
false |
Wrap from the last slide to the first and back with the buttons and autoplay. |
Ui |
CarouselUi |
Per-instance class overrides of the slots of the carousel, see CarouselUi . They are merged after the theme and the Class parameter wins on the root slot. |
Ui slots
Ui takes a CarouselUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyCarousel , after the theme and the app configuration. |
Viewport |
Classes merged into the viewport slot of KlyCarousel , after the theme and the app configuration. |
Slide |
Classes merged into the slide slot of KlyCarousel , after the theme and the app configuration. |
Controls |
Classes merged into the controls slot of KlyCarousel , after the theme and the app configuration. |
Control |
Classes merged into the control slot of KlyCarousel , after the theme and the app configuration. |
Dots |
Classes merged into the dots slot of KlyCarousel , after the theme and the app configuration. |
Dot |
Classes merged into the dot slot of KlyCarousel , after the theme and the app configuration. |
KlyCarouselItem
A slide of a KlyCarousel .
Parameters
| Name | Type | Description |
|---|---|---|
Label |
string |
Accessible name of the slide. Defaults to "1 of 3". |
Slots
| Name | Type | Description |
|---|---|---|
Content |
RenderFragment |
Content of the slide. |