Skip to content

Commit

Permalink
fix tieredMenuSub not positioning itself within viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
Cihan Polat committed May 3, 2024
1 parent 3365c74 commit 0cd14ad
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/app/components/tieredmenu/tieredmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
booleanAttribute,
effect,
forwardRef,
input,
numberAttribute,
signal
} from '@angular/core';
Expand Down Expand Up @@ -176,7 +177,7 @@ import { ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primeng/utils';
[itemTemplate]="itemTemplate"
[autoDisplay]="autoDisplay"
[menuId]="menuId"
[activeItemPath]="activeItemPath"
[activeItemPath]="activeItemPath()"
[focusedItemId]="focusedItemId"
[ariaLabelledBy]="getItemId(processedItem)"
[level]="level + 1"
Expand Down Expand Up @@ -217,7 +218,7 @@ export class TieredMenuSub {

@Input() focusedItemId: string | undefined;

@Input() activeItemPath: any[];
activeItemPath = input<any[]>([]);

@Input({ transform: numberAttribute }) tabindex: number = 0;

Expand All @@ -233,16 +234,23 @@ export class TieredMenuSub {

@ViewChild('sublist', { static: true }) sublistViewChild: ElementRef;

constructor(public el: ElementRef, public renderer: Renderer2, @Inject(forwardRef(() => TieredMenu)) public tieredMenu: TieredMenu) {}
constructor(public el: ElementRef, public renderer: Renderer2, @Inject(forwardRef(() => TieredMenu)) public tieredMenu: TieredMenu) {
effect(() => {
const path = this.activeItemPath();
if (ObjectUtils.isNotEmpty(path)) {
this.positionSubmenu();
}
})
}

positionSubmenu() {
const sublist = this.sublistViewChild && this.sublistViewChild.nativeElement;

if (sublist) {
if (sublist && !DomHandler.hasClass(sublist, 'p-submenu-list-flipped')) {
const parentItem = sublist.parentElement.parentElement;
const containerOffset = DomHandler.getOffset(parentItem);
const viewport = DomHandler.getViewport();
const sublistWidth = sublist.offsetParent ? sublist.offsetWidth : DomHandler.getHiddenElementOuterWidth(sublist);
const sublistWidth = sublist.offsetParent ? sublist.offsetWidth : DomHandler.getOuterWidth(sublist);
const itemOuterWidth = DomHandler.getOuterWidth(parentItem.children[0]);

if (parseInt(containerOffset.left, 10) + itemOuterWidth + sublistWidth > viewport.width - DomHandler.calculateScrollbarWidth()) {
Expand Down Expand Up @@ -306,8 +314,8 @@ export class TieredMenuSub {
}

isItemActive(processedItem: any): boolean {
if (this.activeItemPath) {
return this.activeItemPath.some((path) => path.key === processedItem.key);
if (this.activeItemPath()) {
return this.activeItemPath().some((path) => path.key === processedItem.key);
}
}

Expand Down

0 comments on commit 0cd14ad

Please sign in to comment.