2026-10-03 15:26:21 +02:00
|
|
|
@inherits DComponentBase
|
2026-10-03 12:01:42 +02:00
|
|
|
|
|
|
|
|
@* daisyUI Carousel: https://daisyui.com/components/carousel/ . A scroll-snap strip of DCarouselItem children; the
|
2026-10-03 12:43:07 +02:00
|
|
|
user scrolls or swipes it. Give each item w-full for one slide at a time. ShowNavigation (previous/next on every
|
|
|
|
|
slide) and ShowIndicators (numbered buttons under the strip) are links to the slides' ids, as in daisyUI's examples:
|
|
|
|
|
they keep the current path, so Blazor treats them as same-page fragments and scrolls the slide into view. *@
|
|
|
|
|
<CascadingValue Value="this" IsFixed="true">
|
|
|
|
|
@if (ShowIndicators)
|
|
|
|
|
{
|
|
|
|
|
<div class="flex flex-col gap-2">
|
|
|
|
|
@Strip
|
|
|
|
|
<div class="flex w-full justify-center gap-2">
|
|
|
|
|
@for (var i = 0; i < items.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
<a href="@Link(i)" class="btn btn-xs" aria-label="@($"{SlideText} {i + 1}")">@(i + 1)</a>
|
|
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
@Strip
|
|
|
|
|
}
|
|
|
|
|
</CascadingValue>
|
2026-10-03 12:01:42 +02:00
|
|
|
|
|
|
|
|
@code {
|
2026-10-03 15:26:21 +02:00
|
|
|
[Inject] NavigationManager NavigationManager { get; set; }
|
2026-10-03 12:01:42 +02:00
|
|
|
[Parameter] public RenderFragment ChildContent { get; set; }
|
|
|
|
|
[Parameter] public DCarouselSnap Snap { get; set; }
|
|
|
|
|
[Parameter] public DDirection Direction { get; set; }
|
2026-10-03 12:43:07 +02:00
|
|
|
/// <summary>Previous/next buttons on every slide.</summary>
|
|
|
|
|
[Parameter] public bool ShowNavigation { get; set; }
|
|
|
|
|
/// <summary>A row of numbered buttons under the strip, one per slide.</summary>
|
|
|
|
|
[Parameter] public bool ShowIndicators { get; set; }
|
|
|
|
|
[Parameter] public string PreviousText { get; set; } = "Previous slide";
|
|
|
|
|
[Parameter] public string NextText { get; set; } = "Next slide";
|
|
|
|
|
[Parameter] public string SlideText { get; set; } = "Slide";
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
readonly List<DCarouselItem> items = [];
|
2026-10-03 12:43:07 +02:00
|
|
|
readonly string uid = "d" + Guid.NewGuid().ToString("N")[..8];
|
|
|
|
|
|
|
|
|
|
RenderFragment Strip => @<div @attributes="AdditionalAttributes" class="@CssClass">@ChildContent</div>;
|
2026-10-03 12:01:42 +02:00
|
|
|
|
|
|
|
|
string CssClass => Cx("carousel", SnapClass, Direction == DDirection.Vertical ? "carousel-vertical" : null, Class);
|
|
|
|
|
|
|
|
|
|
string SnapClass => Snap switch
|
|
|
|
|
{
|
|
|
|
|
DCarouselSnap.Center => "carousel-center",
|
|
|
|
|
DCarouselSnap.End => "carousel-end",
|
|
|
|
|
_ => null
|
|
|
|
|
};
|
2026-10-03 12:43:07 +02:00
|
|
|
|
|
|
|
|
internal int Count => items.Count;
|
|
|
|
|
|
|
|
|
|
internal int IndexOf(DCarouselItem item) => items.IndexOf(item);
|
|
|
|
|
|
|
|
|
|
internal string SlideId(int index) => $"{uid}-{index + 1}";
|
|
|
|
|
|
|
|
|
|
/// <summary>The current page plus the slide's fragment; the index wraps around.</summary>
|
|
|
|
|
internal string Link(int index)
|
|
|
|
|
{
|
|
|
|
|
var path = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
|
|
|
|
|
var hash = path.IndexOf('#');
|
|
|
|
|
if (hash >= 0)
|
|
|
|
|
path = path[..hash];
|
|
|
|
|
return $"{path}#{SlideId((index % items.Count + items.Count) % items.Count)}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Add(DCarouselItem item)
|
|
|
|
|
{
|
|
|
|
|
items.Add(item);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Remove(DCarouselItem item)
|
|
|
|
|
{
|
|
|
|
|
items.Remove(item);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
2026-10-03 12:01:42 +02:00
|
|
|
}
|