namespace Htmx.ApiDemo.Templates.Components; /// /// shadcn-style Toast notification. Typically created dynamically via window.showToast(), /// but can also be server-rendered and injected via htmx. /// public sealed class Toast : ToastBase { private static readonly Dictionary VariantClasses = new() { ["default"] = "", ["destructive"] = "border-destructive text-destructive", }; private readonly byte[] _titleData; private readonly byte[] _descriptionData; private readonly byte[] _extraClassesData; public Toast( string title, string description = "", string variant = "default") { _titleData = $"""
{title}
""".ToUtf8Bytes(); _descriptionData = string.IsNullOrEmpty(description) ? [] : $"""
{description}
""".ToUtf8Bytes(); _extraClassesData = VariantClasses.GetValueOrDefault(variant, "").ToUtf8Bytes(); } protected override void RenderTitle(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_titleData); protected override void RenderDescription(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_descriptionData); protected override void RenderExtraClasses(HtmxRenderContext ctx)=> ctx.Writer.WriteUtf8(_extraClassesData); }