32 lines
1.3 KiB
C#
32 lines
1.3 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style Avatar component. Shows an image or falls back to initials.
|
|
/// Size: sm (h-8 w-8) | default (h-10 w-10) | lg (h-14 w-14) | xl (h-20 w-20)
|
|
/// </summary>
|
|
public sealed class Avatar : AvatarBase
|
|
{
|
|
private static readonly Dictionary<string, string> Sizes = new()
|
|
{
|
|
["sm"] = "h-8 w-8",
|
|
["default"] = "h-10 w-10",
|
|
["lg"] = "h-14 w-14",
|
|
["xl"] = "h-20 w-20",
|
|
};
|
|
|
|
private readonly byte[] _sizeClassesData;
|
|
private readonly byte[] _contentData;
|
|
|
|
public Avatar(string fallback, string? src = null, string size = "default")
|
|
{
|
|
_sizeClassesData = Sizes.GetValueOrDefault(size, Sizes["default"]).ToUtf8Bytes();
|
|
|
|
_contentData = !string.IsNullOrEmpty(src)
|
|
? $"""<img src="{src}" alt="{fallback}" class="aspect-square h-full w-full object-cover" />""".ToUtf8Bytes()
|
|
: $"""<span class="flex h-full w-full items-center justify-center rounded-full bg-muted text-muted-foreground text-sm font-medium select-none">{fallback}</span>""".ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderSizeClasses(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_sizeClassesData);
|
|
protected override void RenderContent(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_contentData);
|
|
}
|