TabMenu: active route no longer highlighted with PrimeNG 10.0.0
I'm submitting a ...
[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Stackblitz Case
https://stackblitz.com/edit/primeng-tabmenu-demo-xuauks?file=src/app/app.component.ts
Current behavior TabMenu does not highlight the active route. It only activates a tab by clicking on it, but not by navigating to a route.
Expected behavior As described in the documentation:
By default item that matches the active route is highlighted, [...]
This was also the behavior with PrimeNG 9
Minimal reproduction of the problem with instructions Open Stackblitz example and notice how the active route does not get reflected in the TabMenu when using the navigation links.
What is the motivation / use case for changing the behavior? Looks like a regression bug from PrimeNG 9 to PrimeNG 10
-
Angular version: 10
-
PrimeNG version: 10.0.0
-
Browser: Chrome 85
-
Language: TypeScript 3.9
Same problem! I suspect this is due to the change ChangeDetectionStrategy
This seems to be a duplicate of this task https://github.com/primefaces/primeng/issues/9367 But in 10.0.3 the bug is still present.
<div [ngClass]="'p-tabmenu p-component'" [ngStyle]="style" [class]="styleClass">
<ul #navbar class="p-tabmenu-nav p-reset" role="tablist">
<li *ngFor="let item of model; let i = index" role="tab" [attr.aria-selected]="activeItem==item" [attr.aria-expanded]="activeItem==item"
[ngClass]="{'p-tabmenuitem':true,'p-disabled':item.disabled,'p-highlight':activeItem==item,'p-hidden': item.visible === false}">
<a *ngIf="!item.routerLink" [attr.href]="item.url" class="p-menuitem-link" role="presentation" (click)="itemClick($event,item)" [attr.tabindex]="item.disabled ? null : '0'"
[attr.target]="item.target" [attr.title]="item.title" [attr.id]="item.id" pRipple>
<ng-container *ngIf="!itemTemplate">
<span class="p-menuitem-icon" [ngClass]="item.icon" *ngIf="item.icon"></span>
<span class="p-menuitem-text" *ngIf="item.escape !== false; else htmlLabel">{{item.label}}</span>
<ng-template #htmlLabel><span class="p-menuitem-text" [innerHTML]="item.label"></span></ng-template>
</ng-container>
<ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item, index: i}"></ng-container>
</a>
<a *ngIf="item.routerLink" [routerLink]="item.routerLink" [queryParams]="item.queryParams" [routerLinkActive]="'p-menuitem-link-active'" [routerLinkActiveOptions]="item.routerLinkActiveOptions||{exact:false}"
role="presentation" class="p-menuitem-link" (click)="itemClick($event,item)" [attr.tabindex]="item.disabled ? null : '0'"
[attr.target]="item.target" [attr.title]="item.title" [attr.id]="item.id"
[fragment]="item.fragment" [queryParamsHandling]="item.queryParamsHandling" [preserveFragment]="item.preserveFragment" [skipLocationChange]="item.skipLocationChange" [replaceUrl]="item.replaceUrl" [state]="item.state" pRipple>
<ng-container *ngIf="!itemTemplate">
<span class="p-menuitem-icon" [ngClass]="item.icon" *ngIf="item.icon"></span>
<span class="p-menuitem-text" *ngIf="item.escape !== false; else htmlRouteLabel">{{item.label}}</span>
<ng-template #htmlRouteLabel><span class="p-menuitem-text" [innerHTML]="item.label"></span></ng-template>
</ng-container>
<ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item, index: i}"></ng-container>
</a>
</li>
Maybe add [routerLinkActive]="p-highlight" to li element will fix this issue?
Same problem here on an active project. I'm using 11.0.0-rc.1 If you need a tester, just get in touch.
Any updates on this?
HI, I am also seeing this issue since upgrading (last re-tested on 11.2). and raised same issue on MenuBar #9473
@yigitfindikli can you update on this issue? I can't use latest version due to this bug.
Could we get an update on this? Facing the exact same issue.
The current active styles are applied based on .p-highlight but the active route link has class .p-menuitem-link-active. So basically, adding the following styles has fixed this for me
::ng-deep {
.p-tabmenu .p-tabmenu-nav .p-tabmenuitem .p-menuitem-link-active {
background: #ffffff;
border-color: #2196F3;
color: #2196F3;
}
}
Any updates here? menubar active not working as well
any updates ? same issue with PrimeNG 11
Same issue in PrimeNG 12.
I've solved using: :host ::ng-deep .p-menuitem-link-active { // Your style here }
@yigitfindikli I sent a PR with a possible fix for this.
Hi,
I hate doing that, but in the meantime, I'll use this directive as temporary workaround (version 12.2.2):
import { AfterViewInit, Directive } from '@angular/core';
import { Router } from '@angular/router';
import { TabMenu } from 'primeng/tabmenu';
/**
* Temporary workaround for bugs with `TabMenu` active item.
*
* @see https://github.com/primefaces/primeng/issues/9306
*/
@Directive({
selector: '[activeItemWorkaround]',
})
export class ActiveItemWorkaroundDirective implements AfterViewInit {
constructor(private readonly menu: TabMenu, private readonly router: Router) {}
public ngAfterViewInit(): void {
this.fixActiveItemWithRouterLink();
this.fixActiveItemWithoutRouterLink();
}
/**
* Problem: Items with `routerLink` are not correctly activated.
* Solution: Manually set `TabMenu.activeItem`
*/
private fixActiveItemWithRouterLink(): void {
const url = this.router.routerState.snapshot.url;
const activeItem = this.menu.model.find(tab => url.startsWith(tab.routerLink));
if (activeItem) {
this.menu.activeItem = activeItem;
}
}
/**
* Problem: Items without `routerLink` are activated, but not correctly highlighted.
* Solution: Manually add/remove the `p-highlight` class to `.p-tabmenuitem`
*/
private fixActiveItemWithoutRouterLink(): void {
const activeIndex = this.menu.model.findIndex(tab => tab.id === this.menu.activeItem.id);
const list = this.menu.navbar.nativeElement as HTMLUListElement;
const item = list.children[activeIndex];
if (!item) {
return;
}
// Add class
item.classList.add('p-highlight');
// Remove class on first change
list.addEventListener(
'click',
function (event: Event) {
const targetItem = (event.target as HTMLElement).closest('.p-tabmenuitem');
if (targetItem !== item) {
item.classList.remove('p-highlight');
}
},
{ once: true }
);
}
}
Works with [activeItem] or with items with routerLink:
<p-tabMenu [model]="tabs$ | async" [activeItem]="activeTab$ | async" activeItemWorkaround></p-tabMenu>
<p-tabMenu [model]="tabs$ | async" activeItemWorkaround></p-tabMenu>
Hi,
We couldn't replicate the issue with the latest PrimeNG (v13). If the issue still exists with the latest PrimeNG version, please provide a StackBlitz example by using PrimeNG Issue Template and re-open the issue.
Regards
It does not seem to work with the latest PrimeNG (v13.3.2) either.
Here a simplistic example of using the tab-menu: https://stackblitz.com/edit/github-8dcahm?file=src/app/app.component.html https://github-8dcahm.stackblitz.io
Nothing happens when changing the URL manually. I expected the selection of the tab-menu changes automatically.
What am i missing?
UPDATE:
I just make it work when using routerLink: 'home' instead of command: () =>this.router.navigate(['home'])
with primeng 20.2 I did this to fix it
:host ::ng-deep .p-menu-item-link-active {
background: var(--p-menu-item-focus-background);
}