60 lines
2.6 KiB
C#
60 lines
2.6 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style Button component.
|
|
/// Variant: default | destructive | outline | secondary | ghost | link
|
|
/// Size: default | sm | lg | icon
|
|
/// </summary>
|
|
public sealed class Button : ButtonBase
|
|
{
|
|
private static readonly Dictionary<string, string> VariantClasses = new()
|
|
{
|
|
["default"] = "bg-primary text-primary-foreground hover:bg-primary/90",
|
|
["destructive"] = "bg-destructive text-destructive-foreground hover:bg-destructive/90",
|
|
["outline"] = "border border-input bg-transparent hover:bg-accent hover:text-accent-foreground",
|
|
["secondary"] = "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
["ghost"] = "hover:bg-accent hover:text-accent-foreground",
|
|
["link"] = "text-primary underline-offset-4 hover:underline",
|
|
};
|
|
|
|
private static readonly Dictionary<string, string> SizeClasses = new()
|
|
{
|
|
["default"] = "h-10 px-4 py-2 text-sm",
|
|
["sm"] = "h-9 rounded-md px-3 text-xs",
|
|
["lg"] = "h-11 rounded-md px-8 text-base",
|
|
["icon"] = "h-10 w-10",
|
|
};
|
|
|
|
private const string BaseClasses =
|
|
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md font-medium " +
|
|
"ring-offset-background transition-colors " +
|
|
"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 readonly byte[] _labelData;
|
|
private readonly byte[] _classesData;
|
|
private readonly byte[] _typeData;
|
|
private readonly byte[] _hxAttrsData;
|
|
|
|
public Button(
|
|
string label,
|
|
string variant = "default",
|
|
string size = "default",
|
|
string type = "button",
|
|
string hxAttrs = "")
|
|
{
|
|
_labelData = label.ToUtf8Bytes();
|
|
_typeData = type.ToUtf8Bytes();
|
|
_hxAttrsData = hxAttrs.ToUtf8Bytes();
|
|
|
|
var v = VariantClasses.GetValueOrDefault(variant, VariantClasses["default"]);
|
|
var s = SizeClasses.GetValueOrDefault(size, SizeClasses["default"]);
|
|
_classesData = $"{BaseClasses} {s} {v}".ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderLabel(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_labelData);
|
|
protected override void RenderClasses(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_classesData);
|
|
protected override void RenderType(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_typeData);
|
|
protected override void RenderHxAttrs(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_hxAttrsData);
|
|
}
|