refactor(ui): fix prime menu positioning + cleanup (#1377)

This commit is contained in:
Alex
2026-05-20 18:14:47 +01:00
committed by GitHub
parent e46305e082
commit 1e763b1e89
12 changed files with 237 additions and 432 deletions
@@ -1,18 +0,0 @@
export interface MenuItem {
readonly label: string;
readonly icon?: string;
readonly routerLink?: readonly string[];
readonly action?: () => void;
readonly disabled?: boolean;
readonly children?: readonly MenuEntry[];
}
export interface MenuSeparator {
readonly separator: true;
}
export type MenuEntry = MenuItem | MenuSeparator;
export function isMenuSeparator(entry: MenuEntry): entry is MenuSeparator {
return 'separator' in entry && entry.separator === true;
}
@@ -1,20 +0,0 @@
import type { ConnectedPosition } from '@angular/cdk/overlay';
export const ABOVE_ALIGN_LEFT: ConnectedPosition[] = [
{ originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', offsetY: -8 },
{ originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top', offsetY: 8 },
];
export const BELOW_ALIGN_LEFT: ConnectedPosition[] = [
{ originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top', offsetY: 8 },
{ originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', offsetY: -8 },
];
export const BESIDE_RIGHT_BOTTOM: ConnectedPosition[] = [
{ originX: 'end', originY: 'bottom', overlayX: 'start', overlayY: 'bottom', offsetX: 8 },
];
export const SUBMENU_RIGHT: ConnectedPosition[] = [
{ originX: 'end', originY: 'top', overlayX: 'start', overlayY: 'top', offsetX: 0 },
{ originX: 'start', originY: 'top', overlayX: 'end', overlayY: 'top', offsetX: 0 },
];
@@ -1,52 +0,0 @@
<ng-template
cdkConnectedOverlay
[cdkConnectedOverlayOrigin]="origin()"
[cdkConnectedOverlayOpen]="open()"
[cdkConnectedOverlayPositions]="positions()"
[cdkConnectedOverlayViewportMargin]="8"
[cdkConnectedOverlayPush]="true"
[cdkConnectedOverlayFlexibleDimensions]="true"
>
<div ngMenu #inner="ngMenu" class="menu-panel" (itemSelected)="onSelected($event)">
<ng-template ngMenuContent>
@for (entry of items(); track $index) {
@if (isSeparator(entry)) {
<div role="separator" class="menu-separator"></div>
} @else if (entry.children?.length) {
<button
ngMenuItem
#aria="ngMenuItem"
#subTrigger
type="button"
class="menu-item"
[value]="entry"
[submenu]="subMenu.menuRef()"
[disabled]="entry.disabled ?? false"
>
@if (entry.icon) { <i [class]="entry.icon" aria-hidden="true"></i> }
<span>{{ entry.label }}</span>
<i class="pi pi-chevron-right menu-chevron" aria-hidden="true"></i>
</button>
<app-menu
#subMenu="appMenu"
[origin]="subTrigger"
[open]="aria.expanded() ?? false"
[items]="entry.children ?? []"
[positions]="submenuPositions"
></app-menu>
} @else {
<button
ngMenuItem
type="button"
class="menu-item"
[value]="entry"
[disabled]="entry.disabled ?? false"
>
@if (entry.icon) { <i [class]="entry.icon" aria-hidden="true"></i> }
<span>{{ entry.label }}</span>
</button>
}
}
</ng-template>
</div>
</ng-template>
@@ -1,39 +0,0 @@
import { ChangeDetectionStrategy, Component, ElementRef, inject, input, viewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CdkConnectedOverlay, ConnectedPosition } from '@angular/cdk/overlay';
import { Menu as AriaMenu, MenuContent, MenuItem as AriaMenuItem } from '@angular/aria/menu';
import { MenuEntry, MenuItem, isMenuSeparator } from './menu-item.model';
import { SUBMENU_RIGHT } from './menu-positions';
type OriginInput = HTMLElement | ElementRef;
@Component({
selector: 'app-menu',
exportAs: 'appMenu',
imports: [CdkConnectedOverlay, AriaMenu, AriaMenuItem, MenuContent],
styles: `:host { display: contents; }`,
templateUrl: './menu.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MenuComponent {
protected readonly submenuPositions = SUBMENU_RIGHT;
readonly origin = input.required<OriginInput>();
readonly open = input.required<boolean>();
readonly items = input.required<readonly MenuEntry[]>();
readonly positions = input.required<ConnectedPosition[]>();
readonly menuRef = viewChild<AriaMenu<MenuItem>>('inner');
private readonly router = inject(Router);
protected readonly isSeparator = isMenuSeparator;
protected onSelected(item: MenuItem): void {
if (item.routerLink) {
this.router.navigate(item.routerLink);
return;
}
item.action?.();
}
}