MegaMenu is navigation component that displays submenus together.
import { MegaMenu } from 'primeng/megamenu';
MegaMenu requires a collection of menuitems as its model.
<p-megamenu [model]="items" />
Layout of the MegaMenu is changed with the orientation property that accepts horizontal and vertical as options.
<p-megamenu [model]="items" orientation="vertical" />
Custom content can be placed between p-megamenu tags. Megamenu should be horizontal for custom content.
<p-megamenu [model]="items" [style]="{ 'border-radius': '3rem', display: 'flex' }" styleClass="p-4 bg-surface-0 dark:bg-surface-900">
<ng-template #start>
<svg width="33" height="35" viewBox="0 0 33 35" fill="none" xmlns="http://www.w3.org/2000/svg" class="h-8">
<path d="..." fill="var(--primary-color)" />
</svg>
</ng-template>
<ng-template #item let-item>
<a *ngIf="item.root" pRipple class="flex items-center cursor-pointer px-4 py-2 overflow-hidden relative font-semibold text-lg uppercase" style="border-radius: 2rem">
<i [ngClass]="item.icon"></i>
<span class="ml-2">{{ item.label }}</span>
</a>
<a *ngIf="!item.root && !item.image" class="flex items-center p-4 cursor-pointer mb-2 gap-2">
<span class="inline-flex items-center justify-center rounded-full bg-primary text-primary-contrast w-12 h-12">
<i [ngClass]="item.icon + ' text-lg'"></i>
</span>
<span class="inline-flex flex-col gap-1">
<span class="font-medium text-lg text-surface-900 dark:text-surface-0">{{ item.label }}</span>
<span class="whitespace-nowrap">{{ item.subtext }}</span>
</span>
</a>
<div *ngIf="item.image" class="flex flex-col items-start gap-4">
<img [src]="item.image" alt="megamenu-demo" class="w-full" />
<span>{{ item.subtext }}</span>
<p-button [label]="item.label" [outlined]="true"></p-button>
</div>
</ng-template>
<ng-template #end>
<p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" shape="circle" />
</ng-template>
</p-megamenu>
The command property of a menuitem defines the callback to run when an item is activated by click or a key event.
{
label: 'Log out',
icon: 'pi pi-signout',
command: () => {
// Callback to run
}
}
Items with navigation are defined with templating to be able to use a routerLink directive, an external link or programmatic navigation.
<p-megamenu [model]="items">
<ng-template #item let-item>
<ng-container *ngIf="item.route; else elseBlock">
<a [routerLink]="item.route" class="p-menuitem-link">
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
</a>
</ng-container>
<ng-template #elseBlock>
<a [href]="item.url" class="p-menuitem-link">
<span [class]="item.icon"></span>
<span class="ml-2">{{ item.label }}</span>
</a>
</ng-template>
</ng-template>
</p-megamenu>
MegaMenu component uses the menubar role along with aria-orientation and the value to describe the component can either be provided with aria-labelledby or aria-label props. Each list item has a presentation role whereas anchor elements have a menuitem role with aria-label referring to the label of the item and aria-disabled defined if the item is disabled. A submenu within a MegaMenu uses the menu role with an aria-labelledby defined as the id of the submenu root menuitem label. In addition, root menuitems that open a submenu have aria-haspopup, aria-expanded and aria-controls to define the relation between the item and the submenu.
Key | Function |
---|---|
tab | Add focus to the first item if focus moves in to the menu. If the focus is already within the menu, focus moves to the next focusable item in the page tab sequence. |
shift + tab | Add focus to the last item if focus moves in to the menu. If the focus is already within the menu, focus moves to the previous focusable item in the page tab sequence. |
enter | If menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays. |
space | If menuitem has a submenu, toggles the visibility of the submenu otherwise activates the menuitem and closes all open overlays. |
escape | If focus is inside a popup submenu, closes the submenu and moves focus to the root item of the closed submenu. |
down arrow | If focus is on a root element, open a submenu and moves focus to the first element in the submenu otherwise moves focus to the next menuitem within the submenu. |
up arrow | If focus is on a root element, opens a submenu and moves focus to the last element in the submenu otherwise moves focus to the previous menuitem within the submenu. |
right arrow | If focus is on a root element, moves focus to the next menuitem. If the focus in inside a submenu, moves focus to the first menuitem of the next menu group. |
left arrow | If focus is on a root element, moves focus to the previous menuitem. If the focus in inside a submenu, moves focus to the first menuitem of the previous menu group. |
home | Moves focus to the first menuitem within the submenu. |
end | Moves focus to the last menuitem within the submenu. |