primeng icon indicating copy to clipboard operation
primeng copied to clipboard

TabMenu: active route no longer highlighted with PrimeNG 10.0.0

Open rhegner opened this issue 6 years ago • 18 comments

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

rhegner avatar Sep 23 '20 07:09 rhegner

Same problem! I suspect this is due to the change ChangeDetectionStrategy

ivanmem avatar Oct 09 '20 12:10 ivanmem

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.

ivanmem avatar Oct 20 '20 11:10 ivanmem

    <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?

siehc avatar Oct 31 '20 05:10 siehc

Same problem here on an active project. I'm using 11.0.0-rc.1 If you need a tester, just get in touch.

RichardMcElroy avatar Nov 27 '20 17:11 RichardMcElroy

Any updates on this?

wederer avatar Jan 18 '21 08:01 wederer

HI, I am also seeing this issue since upgrading (last re-tested on 11.2). and raised same issue on MenuBar #9473

chrizy avatar Feb 05 '21 14:02 chrizy

@yigitfindikli can you update on this issue? I can't use latest version due to this bug.

KonstantinsS avatar Feb 12 '21 15:02 KonstantinsS

Could we get an update on this? Facing the exact same issue.

paul-solomon avatar Mar 22 '21 07:03 paul-solomon

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;
    }
} 

vivek201 avatar Mar 22 '21 13:03 vivek201

Any updates here? menubar active not working as well

GuanyiX avatar Apr 18 '21 10:04 GuanyiX

any updates ? same issue with PrimeNG 11

etay-schur-apolicy avatar May 26 '21 11:05 etay-schur-apolicy

Same issue in PrimeNG 12.

andi-dev avatar Jul 09 '21 11:07 andi-dev

I've solved using: :host ::ng-deep .p-menuitem-link-active { // Your style here }

marlonfabricio avatar Oct 15 '21 15:10 marlonfabricio

@yigitfindikli I sent a PR with a possible fix for this.

vivek201 avatar Oct 18 '21 04:10 vivek201

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>

lionelgaillard avatar Nov 08 '21 15:11 lionelgaillard

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

cetincakiroglu avatar Mar 28 '22 13:03 cetincakiroglu

@cetincakiroglu

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'])

flawesomesoft avatar Apr 19 '22 11:04 flawesomesoft

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);
    }

cyb456 avatar Oct 07 '25 20:10 cyb456