Created more components
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
namespace Htmx.ApiDemo.Templates.Components;
|
||||
|
||||
/// <summary>
|
||||
/// CSS-only Tooltip using group-hover. Wraps a trigger element.
|
||||
/// Position: "top" | "bottom" | "left" | "right" (default: top)
|
||||
/// </summary>
|
||||
public sealed class Tooltip : TooltipBase
|
||||
{
|
||||
private static readonly Dictionary<string, string> PositionClasses = new()
|
||||
{
|
||||
["top"] = "bottom-full left-1/2 -translate-x-1/2 mb-2",
|
||||
["bottom"] = "top-full left-1/2 -translate-x-1/2 mt-2",
|
||||
["left"] = "right-full top-1/2 -translate-y-1/2 mr-2",
|
||||
["right"] = "left-full top-1/2 -translate-y-1/2 ml-2",
|
||||
};
|
||||
|
||||
private readonly byte[] _triggerData;
|
||||
private readonly byte[] _textData;
|
||||
private readonly byte[] _positionData;
|
||||
|
||||
public Tooltip(string text, IHtmxComponent trigger, string position = "top")
|
||||
{
|
||||
_textData = text.ToUtf8Bytes();
|
||||
_positionData = PositionClasses.GetValueOrDefault(position, PositionClasses["top"]).ToUtf8Bytes();
|
||||
|
||||
var bufferWriter = new System.IO.Pipelines.Pipe().Writer;
|
||||
// Render trigger to bytes via a simple ArrayBufferWriter
|
||||
var writer = new System.Buffers.ArrayBufferWriter<byte>();
|
||||
trigger.Render(new HtmxRenderContext(writer));
|
||||
_triggerData = writer.WrittenSpan.ToArray();
|
||||
}
|
||||
|
||||
protected override void RenderTrigger(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_triggerData);
|
||||
protected override void RenderText(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_textData);
|
||||
protected override void RenderPosition(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_positionData);
|
||||
}
|
||||
Reference in New Issue
Block a user