namespace Htmx.ApiDemo.Templates.Components;
///
/// shadcn-style Dialog using the native HTML <dialog> element.
/// Open with data-dialog-open="id" on any button; close with data-dialog-close or .dialog-close.
/// JS wiring is in components.js.
///
public sealed class Dialog : DialogBase
{
private readonly byte[] _idData;
private readonly byte[] _headerData;
private readonly byte[] _contentData;
private readonly byte[] _footerData;
public Dialog(
string id,
string content,
string title = "",
string description = "",
string footer = "")
{
_idData = id.ToUtf8Bytes();
_contentData = content.ToUtf8Bytes();
_headerData = (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description))
? []
: BuildHeader(id, title, description);
_footerData = string.IsNullOrEmpty(footer)
? []
: $"""
{footer}
""".ToUtf8Bytes();
}
private static byte[] BuildHeader(string id, string title, string description)
{
var sb = new System.Text.StringBuilder();
sb.Append("""""");
sb.Append("""
""");
if (!string.IsNullOrEmpty(title))
sb.Append($"""
{title}
""");
if (!string.IsNullOrEmpty(description))
sb.Append($"""
{description}
""");
sb.Append("
");
sb.Append("""
");
sb.Append("
");
return sb.ToString().ToUtf8Bytes();
}
protected override void RenderId(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_idData);
protected override void RenderHeader(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_headerData);
protected override void RenderContent(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_contentData);
protected override void RenderFooter(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_footerData);
}