Skip to content

Commit c87d553

Browse files
author
Warren Buckley
committed
Rename file and add support for adding a CSS class if page is active to any DOM element in conjuction with our-active-href=""
1 parent f7891ea commit c87d553

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Our.Umbraco.TagHelpers/IsActivePageTagHelper.cs renamed to Our.Umbraco.TagHelpers/ActiveClassTagHelper.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ namespace Our.Umbraco.TagHelpers
1414
/// Will apply the CSS class name navi-active to the class attribute
1515
/// if the value in the href of the <a> compared to the page being rendered
1616
/// is the current page or part of an ancestor
17-
/// </summary>
17+
/// </summary>"
18+
[HtmlTargetElement("*", Attributes = tagHelperAttributes)]
1819
[HtmlTargetElement("a", Attributes = tagHelperAttributeName)]
19-
public class IsActivePageTagHelper : TagHelper
20+
public class ActiveClassTagHelper : TagHelper
2021
{
2122
private const string tagHelperAttributeName = "our-active-class";
23+
private const string tagHelperAttributeHrefName = "our-active-href";
24+
private const string tagHelperAttributes = tagHelperAttributeName + ", " + tagHelperAttributeHrefName;
25+
2226
private IUmbracoContextAccessor _umbracoContextAccessor;
2327

24-
public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
28+
public ActiveClassTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
2529
{
2630
_umbracoContextAccessor = umbracoContextAccessor;
2731
}
@@ -33,6 +37,10 @@ public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
3337
[HtmlAttributeName(tagHelperAttributeName)]
3438
public string ActiveClassName { get; set; }
3539

40+
41+
[HtmlAttributeName("our-active-href")]
42+
public string? ActiveLink { get; set; }
43+
3644
public override void Process(TagHelperContext context, TagHelperOutput output)
3745
{
3846
// Remove the attribute
@@ -41,8 +49,8 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
4149

4250
var ctx = _umbracoContextAccessor.GetRequiredUmbracoContext();
4351

44-
// On the <a> try to find the href attribute and its value
45-
var href = output.Attributes["href"]?.Value.ToString();
52+
// If we have active link prop set use that othewise try to find the href attribute on an <a> and its value
53+
var href = string.IsNullOrEmpty(ActiveLink) ? output.Attributes["href"]?.Value.ToString() : ActiveLink;
4654
if (string.IsNullOrEmpty(href))
4755
{
4856
return;

0 commit comments

Comments
 (0)