namespace Htmx.ApiDemo.Templates.Components; /// /// shadcn-style Accordion. Items collapse/expand client-side via components.js. /// Pass a list of (Title, Content) tuples; set openIndex to expand one by default (-1 = all closed). /// public sealed class Accordion : AccordionBase { private const string ChevronSvg = """"""; private readonly byte[] _idData; private readonly byte[] _itemsData; public Accordion(string id, IEnumerable<(string Title, string Content)> items, int openIndex = -1) { _idData = id.ToUtf8Bytes(); var list = items.ToList(); var sb = new System.Text.StringBuilder(); for (int i = 0; i < list.Count; i++) { var (title, content) = list[i]; var expanded = i == openIndex; var height = expanded ? "auto" : "0"; var opacity = expanded ? "1" : "0"; sb.Append($"""

{content}
"""); } _itemsData = sb.ToString().ToUtf8Bytes(); } protected override void RenderId(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_idData); protected override void RenderItems(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_itemsData); }