-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutComponents.cs
More file actions
94 lines (81 loc) · 3.14 KB
/
LayoutComponents.cs
File metadata and controls
94 lines (81 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Code311.Tabler.Core.Mapping;
using Code311.Ui.Abstractions.Semantics;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Code311.Tabler.Components.Layout;
[HtmlTargetElement("cd311-container")]
public sealed class Cd311ContainerTagHelper(ITablerSemanticClassMapper mapper) : TagHelper
{
public UiLayout Layout { get; set; } = UiLayout.Stack;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.SetAttribute("class", $"container {mapper.MapLayout(Layout)}");
}
}
[HtmlTargetElement("cd311-section")]
public sealed class Cd311SectionTagHelper : TagHelper
{
public string? Title { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "section";
if (!string.IsNullOrWhiteSpace(Title))
{
output.PreContent.SetHtmlContent($"<h2>{Title}</h2>");
}
}
}
public sealed class Cd311PageHeaderViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string title, string? subtitle = null)
{
var html = $"<header class=\"page-header\"><h1>{title}</h1><p>{subtitle}</p></header>";
return new HtmlContentViewComponentResult(new HtmlString(html));
}
}
[HtmlTargetElement("cd311-card")]
public sealed class Cd311CardTagHelper(ITablerSemanticClassMapper mapper) : TagHelper
{
public string? Title { get; set; }
public UiTone Tone { get; set; } = UiTone.Neutral;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "article";
output.Attributes.SetAttribute("class", $"card border-{mapper.MapTone(Tone)}");
if (!string.IsNullOrWhiteSpace(Title))
{
output.PreContent.SetHtmlContent($"<div class=\"card-header\">{Title}</div>");
}
}
}
[HtmlTargetElement("cd311-grid")]
public sealed class Cd311GridTagHelper : TagHelper
{
public int Columns { get; set; } = 2;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.SetAttribute("class", $"row row-cols-{Math.Max(1, Columns)}");
}
}
[HtmlTargetElement("cd311-stack")]
public sealed class Cd311StackTagHelper(ITablerSemanticClassMapper mapper) : TagHelper
{
public UiDensity Density { get; set; } = UiDensity.Comfortable;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes.SetAttribute("class", $"d-flex flex-column {mapper.MapDensity(Density)}");
}
}
public sealed class Cd311AccordionViewComponent : ViewComponent
{
public IViewComponentResult Invoke(IReadOnlyCollection<string>? items)
{
var html = string.Join(string.Empty, (items ?? []).Select((x, i) => $"<div class=\"accordion-item\"><h2>{x}</h2><div id=\"acc-{i}\"></div></div>"));
return new HtmlContentViewComponentResult(new HtmlString($"<div class=\"accordion\">{html}</div>"));
}
}