namespace Htmx.ApiDemo.Templates.Components;
///
/// shadcn-style Pagination. Generates prev/next and page-number buttons.
/// urlPattern: format string where {0} is replaced by the page number, e.g. "/items?page={0}"
///
public sealed class Pagination : PaginationBase
{
private const string BtnBase =
"inline-flex items-center justify-center rounded-md border border-input bg-background " +
"px-3 h-9 text-sm font-medium ring-offset-background transition-colors " +
"hover:bg-accent hover:text-accent-foreground " +
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 " +
"disabled:pointer-events-none disabled:opacity-50";
private const string ActiveBtn =
"inline-flex items-center justify-center rounded-md bg-primary text-primary-foreground " +
"px-3 h-9 text-sm font-medium ring-offset-background " +
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2";
private readonly byte[] _prevData;
private readonly byte[] _pagesData;
private readonly byte[] _nextData;
public Pagination(int current, int total, string urlPattern = "?page={0}")
{
_prevData = current <= 1
? $"""""".ToUtf8Bytes()
: $"""‹""".ToUtf8Bytes();
_nextData = current >= total
? $"""""".ToUtf8Bytes()
: $"""›""".ToUtf8Bytes();
var sb = new System.Text.StringBuilder();
for (int p = 1; p <= total; p++)
{
if (p == current)
sb.Append($"""{p}""");
else
sb.Append($"""{p}""");
}
_pagesData = sb.ToString().ToUtf8Bytes();
}
protected override void RenderPrev(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_prevData);
protected override void RenderPages(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_pagesData);
protected override void RenderNext(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_nextData);
}