Skip to content

Instantly share code, notes, and snippets.

@DanElliott
Forked from bjcull/ActiveRouteTagHelper.cs
Last active July 13, 2023 21:10
Show Gist options
  • Save DanElliott/32787b4ae1941780d70cb085d55f8b24 to your computer and use it in GitHub Desktop.
Save DanElliott/32787b4ae1941780d70cb085d55f8b24 to your computer and use it in GitHub Desktop.
Adds an "active" class to the given element when the route parameters match. An Active Page Tag Helper for use with Razor Pages.
[HtmlTargetElement(Attributes = "is-active-page")]
public class ActivePageTagHelper : TagHelper
{
/// <summary>The name of the action method.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-page")]
public string Page { get; set; }
/// <summary>
/// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
if (ShouldBeActive())
{
MakeActive(output);
}
output.Attributes.RemoveAll("is-active-page");
}
private bool ShouldBeActive()
{
string currentPage = ViewContext.RouteData.Values["Page"].ToString();
if (!string.IsNullOrWhiteSpace(Page) && Page.ToLower() != currentPage.ToLower())
{
return false;
}
return true;
}
private void MakeActive(TagHelperOutput output)
{
var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
if (classAttr == null)
{
classAttr = new TagHelperAttribute("class", "active");
output.Attributes.Add(classAttr);
}
else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
{
output.Attributes.SetAttribute("class", classAttr.Value == null
? "active"
: classAttr.Value.ToString() + " active");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment