2026-03-11 13:19:31 +01:00
|
|
|
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
|
|
|
|
2026-03-11 13:19:31 +01:00
|
|
|
[HtmlTargetElement(Attributes = Prefix + "*")]
|
2022-12-12 16:11:49 +01:00
|
|
|
public class ClassTagHelper : TagHelper
|
2026-03-11 13:19:31 +01:00
|
|
|
{
|
|
|
|
|
private const string Prefix = "app-class:";
|
2022-12-12 16:11:49 +01:00
|
|
|
|
|
|
|
|
private IDictionary<string, bool> _classValues;
|
|
|
|
|
|
2026-03-11 13:19:31 +01:00
|
|
|
[HtmlAttributeName("", DictionaryAttributePrefix = Prefix)]
|
2022-12-12 16:11:49 +01:00
|
|
|
public IDictionary<string, bool> ClassValues
|
|
|
|
|
{
|
2024-02-06 12:28:32 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2026-03-11 13:19:31 +01:00
|
|
|
foreach (KeyValuePair<string, bool> item in _classValues.Where(e => e.Value))
|
2022-12-12 16:11:49 +01:00
|
|
|
{
|
2026-03-11 13:19:31 +01:00
|
|
|
output.AddClass(item.Key, HtmlEncoder.Default);
|
2022-12-12 16:11:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|