34 lines
1.4 KiB
C#
34 lines
1.4 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style Badge component.
|
|
/// Variant: default | secondary | destructive | outline
|
|
/// </summary>
|
|
public sealed class Badge : BadgeBase
|
|
{
|
|
private static readonly Dictionary<string, string> VariantClasses = new()
|
|
{
|
|
["default"] = "bg-primary text-primary-foreground hover:bg-primary/80",
|
|
["secondary"] = "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
["destructive"] = "bg-destructive text-destructive-foreground hover:bg-destructive/80",
|
|
["outline"] = "text-foreground border border-input hover:bg-accent",
|
|
};
|
|
|
|
private const string BaseClasses =
|
|
"inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold transition-colors " +
|
|
"focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2";
|
|
|
|
private readonly byte[] _textData;
|
|
private readonly byte[] _classesData;
|
|
|
|
public Badge(string text, string variant = "default")
|
|
{
|
|
_textData = text.ToUtf8Bytes();
|
|
var v = VariantClasses.GetValueOrDefault(variant, VariantClasses["default"]);
|
|
_classesData = $"{BaseClasses} {v}".ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderText(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_textData);
|
|
protected override void RenderClasses(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_classesData);
|
|
}
|