@@ -14,14 +14,18 @@ namespace Our.Umbraco.TagHelpers
14
14
/// Will apply the CSS class name navi-active to the class attribute
15
15
/// if the value in the href of the <a> compared to the page being rendered
16
16
/// is the current page or part of an ancestor
17
- /// </summary>
17
+ /// </summary>"
18
+ [ HtmlTargetElement ( "*" , Attributes = tagHelperAttributes ) ]
18
19
[ HtmlTargetElement ( "a" , Attributes = tagHelperAttributeName ) ]
19
- public class IsActivePageTagHelper : TagHelper
20
+ public class ActiveClassTagHelper : TagHelper
20
21
{
21
22
private const string tagHelperAttributeName = "our-active-class" ;
23
+ private const string tagHelperAttributeHrefName = "our-active-href" ;
24
+ private const string tagHelperAttributes = tagHelperAttributeName + ", " + tagHelperAttributeHrefName ;
25
+
22
26
private IUmbracoContextAccessor _umbracoContextAccessor ;
23
27
24
- public IsActivePageTagHelper ( IUmbracoContextAccessor umbracoContextAccessor )
28
+ public ActiveClassTagHelper ( IUmbracoContextAccessor umbracoContextAccessor )
25
29
{
26
30
_umbracoContextAccessor = umbracoContextAccessor ;
27
31
}
@@ -33,6 +37,10 @@ public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
33
37
[ HtmlAttributeName ( tagHelperAttributeName ) ]
34
38
public string ActiveClassName { get ; set ; }
35
39
40
+
41
+ [ HtmlAttributeName ( "our-active-href" ) ]
42
+ public string ? ActiveLink { get ; set ; }
43
+
36
44
public override void Process ( TagHelperContext context , TagHelperOutput output )
37
45
{
38
46
// Remove the attribute
@@ -41,8 +49,8 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
41
49
42
50
var ctx = _umbracoContextAccessor . GetRequiredUmbracoContext ( ) ;
43
51
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 ;
46
54
if ( string . IsNullOrEmpty ( href ) )
47
55
{
48
56
return ;
0 commit comments