Skip to content

Commit 15a8c0e

Browse files
author
Warren Buckley
committed
Adds IsActivePage Taghelper our-is-active-page="selected"
1 parent 3b93d8c commit 15a8c0e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using Umbraco.Cms.Core.Web;
3+
using Umbraco.Extensions;
4+
5+
namespace Our.Umbraco.TagHelpers
6+
{
7+
/// <summary>
8+
/// An attribute TagHelper that only works with <a> tags
9+
///
10+
/// Applying the attribute our-is-active-page="navi-active"
11+
/// Will apply the CSS class name navi-active to the class attribute
12+
/// if the value in the href of the <a> compared to the page being rendered
13+
/// is the current page or part of an ancestor
14+
/// </summary>
15+
[HtmlTargetElement("a", Attributes = "our-is-active-page")]
16+
public class IsActivePageTagHelper : TagHelper
17+
{
18+
private IUmbracoContextAccessor _umbracoContextAccessor;
19+
20+
public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
21+
{
22+
_umbracoContextAccessor = umbracoContextAccessor;
23+
}
24+
25+
/// <summary>
26+
/// The CSS class name you wish to append to the class attribute
27+
/// on an <a> tag when the page is active/selected
28+
/// </summary>
29+
[HtmlAttributeName("our-is-active-page")]
30+
public string ActiveClassName { get; set; }
31+
32+
public override void Process(TagHelperContext context, TagHelperOutput output)
33+
{
34+
// Remove the attribute
35+
// We don't want it in the markup we send down to the page
36+
if(output.Attributes.TryGetAttribute("our-is-active-page", out TagHelperAttribute tagHelperAttr))
37+
{
38+
output.Attributes.Remove(tagHelperAttr);
39+
}
40+
41+
var ctx = _umbracoContextAccessor.GetRequiredUmbracoContext();
42+
43+
// On the <a> try to find the href attribute and its value
44+
var href = output.Attributes["href"]?.Value.ToString();
45+
if (string.IsNullOrEmpty(href))
46+
{
47+
return;
48+
}
49+
50+
// Clean href values of any querystrings
51+
// As we won't be able to query GetByRoute for an Umbraco node with them
52+
href = href.Substring(0, href.IndexOf("?") > 0 ? href.IndexOf("?") : href.Length);
53+
54+
// Get the node based of the value in the HREF
55+
var nodeOfLink = ctx.Content.GetByRoute(href);
56+
if(nodeOfLink == null)
57+
{
58+
return;
59+
}
60+
61+
// Get the current node of the page that is rendering
62+
var currentPageRendering = ctx.PublishedRequest.PublishedContent;
63+
64+
// Check if thelink we are rendering is current page or an ancestor
65+
if(nodeOfLink.IsAncestorOrSelf(currentPageRendering))
66+
{
67+
// Is active page
68+
// Check if the <a> has a class attribute already
69+
if(output.Attributes.TryGetAttribute("class", out TagHelperAttribute classAttribute))
70+
{
71+
var existingClasses = classAttribute.Value.ToString();
72+
output.Attributes.SetAttribute("class", existingClasses + " nav-link--active");
73+
}
74+
else
75+
{
76+
// No class attribute exists so add a new one
77+
output.Attributes.SetAttribute("class", "nav-link--active");
78+
}
79+
}
80+
}
81+
}
82+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ If you want the edit link to open in a new tab, just add the `target="_blank"` a
292292
<our-edit-link target="_blank">Edit</our-edit-link>
293293
```
294294

295+
## `our-is-active-page`
296+
This is a tag helper attribute that can be applied to `<a>` element in the razor template or partial. It will use the value inside the attribute and append it to the class attribute of the `<a>`.
297+
If the link inside the href attribute can be found by its route as a content node, if so then it will check with the current page being rendered if its the same node or an ancestor.
298+
299+
This allows for the navigation item to still have the class added to it when a child or grandchildren page is the currently page being rendered.
300+
301+
### Simple Example
302+
```cshtml
303+
@foreach (var item in Model.Root().Children)
304+
{
305+
<a href="@item.Url()" class="nav-link" our-is-active-page="navi-link--active">@item.Name</a>
306+
}
307+
```
308+
309+
295310
## Video 📺
296311
[![How to create ASP.NET TagHelpers for Umbraco](https://user-images.githubusercontent.com/1389894/138666925-15475216-239f-439d-b989-c67995e5df71.png)](https://www.youtube.com/watch?v=3fkDs0NwIE8)
297312

0 commit comments

Comments
 (0)