Skip to content

Commit 7c9f7ad

Browse files
docs(menu): improve visual distinction for current page
1 parent 6c4264e commit 7c9f7ad

File tree

2 files changed

+70
-56
lines changed

2 files changed

+70
-56
lines changed
-318 Bytes
Loading

components/menu/templates.md

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -100,97 +100,111 @@ You can use the template to render arbitrary content according to your applicati
100100
}
101101
````
102102

103-
>caption Use templates to visually distinguish the current page as an item that is styled differently
103+
>caption Use templates to visually distinguish the current page as an item that is styled differently, and to open external links in new tabs
104104
105105
````CSHTML
106-
@inject IUriHelper urlHelper
107106
@using Telerik.Blazor.Components.Menu
107+
@inject IUriHelper urlHelper
108108
109-
<TelerikMenu Data="@MenuItems"
110-
ItemsField="@nameof(MenuItem.SubSectionList)">
111-
<ItemTemplate Context="item">
112-
@{
113-
bool isCurrentPage = CompareCurrentPageUrl(item.Page);
114-
string itemStyling = isCurrentPage ? "color: cyan; cursor: not-allowed;" : "color: blue";
115-
if (isCurrentPage)
116-
{
117-
<span style="@itemStyling">@item.Section</span>
118-
}
119-
else
109+
<TelerikMenu Data="@MenuItems" OnSelect="@((MenuItem item) => OnSelect(item))">
110+
<ItemTemplate Context="item">
111+
@{
112+
if (EqualityComparer<MenuItem>.Default.Equals(item, SelectedMenuItem))
113+
{
114+
<span style="color: black; font-weight: bold">@item.Text</span>
115+
}
116+
else
117+
{
118+
string target = "";
119+
if (!IsInternalPage(item.Url))
120120
{
121-
<a style="@itemStyling" href="@item.Page">@item.Section</a>
121+
target = "_blank";
122122
}
123+
<NavLink target="@target" href="@item.Url" class="k-link k-menu-link">@item.Text</NavLink>
123124
}
124-
</ItemTemplate>
125-
</TelerikMenu>
126-
125+
}
126+
</ItemTemplate>
127+
</TelerikMenu>
127128
@code {
128129
public List<MenuItem> MenuItems { get; set; }
129130
130-
public class MenuItem
131-
{
132-
public string Section { get; set; }
133-
public string Page { get; set; }
134-
public List<MenuItem> SubSectionList { get; set; }
135-
}
136-
137-
bool CompareCurrentPageUrl(string urlToCopmare)
138-
{
139-
return urlHelper.GetAbsoluteUri().Substring(urlHelper.GetBaseUri().Length).Equals(urlToCopmare);
140-
}
131+
public MenuItem SelectedMenuItem { get; set; }
141132
142133
protected override void OnInitialized()
143134
{
144135
MenuItems = new List<MenuItem>()
145-
{
136+
{
146137
new MenuItem()
147138
{
148-
Section = "Components",
149-
Page = "index",
150-
SubSectionList = new List<MenuItem>()
151-
{
152-
new MenuItem()
153-
{
154-
Section = "Menu",
155-
Page = "menu/index"
156-
},
157-
new MenuItem()
158-
{
159-
Section = "Grid",
160-
Page = "grid/index"
161-
},
162-
new MenuItem()
163-
{
164-
Section = "Tree",
165-
Page = "treeview/index"
166-
}
167-
}
139+
Text = "Home",
140+
Url = "/",
168141
},
169142
new MenuItem()
170143
{
171-
Section = "Services",
172-
SubSectionList = new List<MenuItem>()
144+
Text = "Fetch Data",
145+
Url = "/fetchdata"
146+
},
147+
new MenuItem()
148+
{
149+
Text = "Counter",
150+
Url = "/counter"
151+
},
152+
new MenuItem()
173153
{
154+
Text = "Telerik UI for Blazor",
155+
Items = new List<MenuItem>()
156+
{
174157
new MenuItem()
175158
{
176-
Section = "Consulting",
177-
Page = "consultingservices"
159+
Text = "Documentation",
160+
Url = "https://docs.telerik.com/blazor-ui/introduction"
178161
},
179162
new MenuItem()
180163
{
181-
Section = "Education",
182-
Page = "education"
164+
Text = "Live Demos",
165+
Url = "https://demos.telerik.com/blazor-ui"
183166
}
184167
}
185168
}
186169
};
187170
171+
SelectedMenuItem = MenuItems.Find(item => CompareCurrentPageUrl(item.Url));
172+
188173
base.OnInitialized();
189174
}
175+
176+
private void OnSelect(MenuItem item)
177+
{
178+
if (IsInternalPage(item.Url))
179+
{
180+
SelectedMenuItem = item;
181+
}
182+
}
183+
184+
private bool CompareCurrentPageUrl(string urlToCopmare)
185+
{
186+
return urlHelper.GetAbsoluteUri().Substring(urlHelper.GetBaseUri().Length - 1).Equals(urlToCopmare);
187+
}
188+
189+
private bool IsInternalPage(string url)
190+
{
191+
if (string.IsNullOrEmpty(url))
192+
{
193+
return false;
194+
}
195+
return !(url.StartsWith("https://") || url.StartsWith("http://"));
196+
}
197+
198+
public class MenuItem
199+
{
200+
public string Text { get; set; }
201+
public string Url { get; set; }
202+
public List<MenuItem> Items { get; set; }
203+
}
190204
}
191205
````
192206

193-
>caption The result from the snippet above, asuming the current page URL is `menu/index` and we hovered the "Components" item
207+
>caption The result from the snippet above, asuming the current page URL is `/counter`
194208
195209
![](images/menu-template-distinguish-item.png)
196210

0 commit comments

Comments
 (0)