Files
mixtape/Finch/TagHelpers/ClassTagHelper.cs
T

29 lines
823 B
C#
Raw Normal View History

using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
2022-12-12 16:11:49 +01:00
2026-04-07 14:23:29 +02:00
namespace Finch.TagHelpers;
2022-12-12 16:11:49 +01:00
[HtmlTargetElement(Attributes = Prefix + "*")]
2022-12-12 16:11:49 +01:00
public class ClassTagHelper : TagHelper
{
private const string Prefix = "app-class:";
2022-12-12 16:11:49 +01:00
private IDictionary<string, bool> _classValues;
[HtmlAttributeName("", DictionaryAttributePrefix = Prefix)]
2022-12-12 16:11:49 +01:00
public IDictionary<string, bool> ClassValues
{
get => _classValues ??= new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
2022-12-12 16:11:49 +01:00
set => _classValues = value;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
foreach (KeyValuePair<string, bool> item in _classValues.Where(e => e.Value))
2022-12-12 16:11:49 +01:00
{
output.AddClass(item.Key, HtmlEncoder.Default);
2022-12-12 16:11:49 +01:00
}
}
}