I've started building Blazor server app with Tailwind CSS. I created horizontal menu nav bar with new styles. But one thing I missed. In default Blazor template when I click on menu item, it changes background color, till click to other menu element or changing route component page - then that page menu element changes color, other elements return to default background. I know it is not doing by default bootstrap CSS or site.css. And in code I can't find it. Is it done in <script src="_framework/blazor.server.js"></script> in _Host.cshtml?
I've tried a few solutions as this two NavLinks:
...
<ul class="@($"{menuVisibleClass} flex flex-col bg-gray-400 px-4 py-2 md:flex md:flex-row md:bg-transparent")">
<NavLink @onclick="ToggleActive" class="@($"nav-item {activeClass}" )" href="/">Home</NavLink>
<NavLink class="@($"nav-item focus:bg-purple-400")" tabindex="-1" href="/fetchdata">Counter</NavLink>
</ul>
</div>
@code
{
...
private bool _active = false;
string activeClass = "";
private void ToggleActive()
{
_active = !_active;
activeClass = _active ? " bg-purple-400" : "";
}
...
}
But of course it is not it. I think it should be something like this in JavaScript (w3schools link):
<div id="myDIV">
<button class="btn">1</button>
<button class="btn active">2</button>
<button class="btn">3</button>
</div>
<script>
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
</script>
Is there something simmilar to achieve that? Should I pass manually id from onclick event of every NAvLink or maybe other solution would be proper?
edit: I found info (link) that with Bootstrap NavLink toggles an active CSS class based on whether its href matches the current URL. But with active class generated by Tailwind CSS I can't do it.
@tailwind base;
@tailwind components;
.nav-item {
@apply bg-gray-100 m-1 font-bold text-gray-700 px-4 py-2 rounded-sm shadow-lg;
}
.nav-item:hover {
@apply bg-purple-300;
}
.nav-item:focus {
@apply outline-none;
}
.nav-item:active {
@apply bg-purple-500;
}