diff options
| author | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-10-28 19:48:59 +1000 |
|---|---|---|
| committer | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-10-28 19:48:59 +1000 |
| commit | 38a050c8151b9894c62b2d49ef482807412ea98c (patch) | |
| tree | b26d90c19acac2dfcb8d9a62d9503dc52789db79 /src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs | |
| parent | e566edc0442330c11906da384d589c47f836de37 (diff) | |
feat: ui changes; ci: add tests
Diffstat (limited to 'src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs')
| -rw-r--r-- | src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs b/src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs new file mode 100644 index 0000000..759b701 --- /dev/null +++ b/src/marlin.web/TagHelpers/ActiveLinkTagHelper.cs @@ -0,0 +1,44 @@ +namespace marlin.web.TagHelpers; + +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.AspNetCore.Razor.TagHelpers; + +[HtmlTargetElement("a", Attributes = "active-when")] +public class ActiveLinkTagHelper : TagHelper +{ + [HtmlAttributeName("asp-controller")] + public string Controller { get; set; } + + [HtmlAttributeName("asp-action")] + public string Action { get; set; } + + [HtmlAttributeName("active-when")] + public ActiveLinkWhen ActiveWhen { get; set; } + + [ViewContext] + [HtmlAttributeNotBound] + public ViewContext ViewContext { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + var currentController = ViewContext.RouteData.Values["controller"]?.ToString(); + var currentAction = ViewContext.RouteData.Values["action"]?.ToString(); + + if (!string.Equals(currentController, Controller, StringComparison.OrdinalIgnoreCase)) + return; + + if (!string.Equals(currentAction, Action, StringComparison.OrdinalIgnoreCase) + && this.ActiveWhen == ActiveLinkWhen.ControllerAndAction) + return; + + var existingClasses = output.Attributes["class"]?.Value?.ToString() ?? ""; + output.Attributes.SetAttribute("class", $"{existingClasses} active".Trim()); + } +} + +public enum ActiveLinkWhen +{ + Controller, + ControllerAndAction +}
\ No newline at end of file |
