본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

메뉴

원문 보기 위키 갱신

메뉴 (Menu)

메뉴(Menu)는 탐색과 명령 실행을 위한 내비게이션 컴포넌트예요. 정적·동적 위치 지정을 모두 지원해서 페이지에 바로 배치할 수도 있고 팝업으로 띄울 수도 있어요. 기본 사용법부터 커맨드 처리, 그룹·팝업·라우터 연동, 템플릿 커스터마이징, 접근성과 테마까지 메뉴를 활용하는 데 필요한 내용을 모두 다루고 있어요

출처: 문서

본문

접근성

스크린 리더 Menu 컴포넌트는 menu role을 사용해요. 메뉴를 설명하는 값은 aria-labelledby 또는 aria-label 프롭으로 제공할 수 있어요. 각 리스트 아이템은 presentation role을 가지는 반면, 앵커 요소는 menuitem role을 가지며 aria-label은 아이템의 라벨을 참조하고, 아이템이 비활성화된 경우에는 aria-disabled가 정의돼요. Menu 내부의 서브메뉴는 group role을 사용하며, aria-labelledby는 서브메뉴 루트 menuitem 라벨의 id로 정의돼요. 팝업 모드에서는 컴포넌트가 대상 요소의 aria-expanded, aria-haspopup, aria-controls 속성을 암묵적으로 관리해서 대상과 팝업 사이의 관계를 정의해요. 키보드 지원 키 기능 tab 메뉴 안으로 포커스가 들어오면 첫 번째 아이템에 포커스를 추가해요. 포커스가 이미 메뉴 안에 있다면, 페이지 탭 시퀀스에서 포커스 가능한 다음 아이템으로 포커스가 이동해요. shift + tab 메뉴 안으로 포커스가 들어오면 마지막 아이템에 포커스를 추가해요. 포커스가 이미 메뉴 안에 있다면, 페이지 탭 시퀀스에서 포커스 가능한 이전 아이템으로 포커스가 이동해요. enter 포커스된 menuitem을 활성화해요. 메뉴가 오버레이 모드라면 팝업이 닫히고 포커스가 대상으로 이동해요. space 포커스된 menuitem을 활성화해요. 메뉴가 오버레이 모드라면 팝업이 닫히고 포커스가 대상으로 이동해요. escape 메뉴가 오버레이 모드라면 팝업이 닫히고 포커스가 대상으로 이동해요. down arrow 다음 menuitem으로 포커스를 이동해요. up arrow 이전 menuitem으로 포커스를 이동해요. home 첫 번째 menuitem으로 포커스를 이동해요. end 마지막 menuitem으로 포커스를 이동해요.

기본

Menu는 menuitems 컬렉션을 model로 필요로 해요.

import { Component, OnInit } from '@angular/core';
import { MenuModule } from 'primeng/menu';
import { MenuItem } from 'primeng/api';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-menu [model]="items" />
        </div>
    `,
    standalone: true,
    imports: [MenuModule]
})
export class MenuBasicDemo implements OnInit {
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            { label: 'New', icon: 'pi pi-plus' },
            { label: 'Search', icon: 'pi pi-search' }
        ];
    }
}

커맨드

아이템을 클릭했을 때 호출할 함수는 command 프로퍼티로 정의해요.

import { Component, OnInit, inject } from '@angular/core';
import { MenuModule } from 'primeng/menu';
import { ToastModule } from 'primeng/toast';
import { MenuItem, MessageService } from 'primeng/api';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-toast />
            <p-menu [model]="items" />
        </div>
    `,
    standalone: true,
    imports: [MenuModule, ToastModule],
    providers: [MessageService]
})
export class MenuCommandDemo implements OnInit {
    private messageService = inject(MessageService);
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            {
                label: 'New',
                icon: 'pi pi-plus',
                command: () => {
                    this.update();
                }
            },
            {
                label: 'Search',
                icon: 'pi pi-search',
                command: () => {
                    this.delete();
                }
            }
        ];
    }

    update() {
        this.messageService.add({ severity: 'success', summary: 'Success', detail: 'File created', life: 3000 });
    }

    delete() {
        this.messageService.add({ severity: 'warn', summary: 'Search Completed', detail: 'No results found', life: 3000 });
    }
}

그룹

Menu는 items 프로퍼티로 children을 정의하는 방식으로 한 단계 중첩을 지원해요.

import { Component, OnInit, inject } from '@angular/core';
import { MenuModule } from 'primeng/menu';
import { MenuItem, MessageService } from 'primeng/api';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-menu [model]="items" />
        </div>
    `,
    standalone: true,
    imports: [MenuModule],
    providers: [MessageService]
})
export class MenuGroupDemo implements OnInit {
    private messageService = inject(MessageService);
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            {
                label: 'Documents',
                items: [
                    {
                        label: 'New',
                        icon: 'pi pi-plus'
                    },
                    {
                        label: 'Search',
                        icon: 'pi pi-search'
                    }
                ]
            },
            {
                label: 'Profile',
                items: [
                    {
                        label: 'Settings',
                        icon: 'pi pi-cog'
                    },
                    {
                        label: 'Logout',
                        icon: 'pi pi-sign-out'
                    }
                ]
            }
        ];
    }
}

팝업

popup 프로퍼티를 true로 설정하고 대상의 이벤트와 함께 toggle 메서드를 호출하면 팝업 모드가 활성화돼요.

import { Component, OnInit, inject } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { MenuModule } from 'primeng/menu';
import { MenuItem, MessageService } from 'primeng/api';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-menu #menu [model]="items" [popup]="true" />
            <p-button (click)="menu.toggle($event)" icon="pi pi-ellipsis-v" />
        </div>
    `,
    standalone: true,
    imports: [ButtonModule, MenuModule],
    providers: [MessageService]
})
export class MenuPopupDemo implements OnInit {
    private messageService = inject(MessageService);
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            {
                label: 'Options',
                items: [
                    {
                        label: 'Refresh',
                        icon: 'pi pi-refresh'
                    },
                    {
                        label: 'Export',
                        icon: 'pi pi-upload'
                    }
                ]
            }
        ];
    }
}

라우터

메뉴 아이템은 routerLink를 통한 네비게이션, commands를 사용한 프로그래밍 방식 라우팅, 또는 외부 URL을 통한 이동을 지원해요.

import { Component, OnInit } from '@angular/core';
import { MenuModule } from 'primeng/menu';
import { MenuItem } from 'primeng/api';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-menu [model]="items" />
        </div>
    `,
    standalone: true,
    imports: [MenuModule]
})
export class MenuRouterDemo implements OnInit {
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            {
                label: 'Navigate',
                items: [
                    {
                        label: 'Router Link',
                        icon: 'pi pi-palette',
                        routerLink: '/theming'
                    },
                    {
                        label: 'Programmatic',
                        icon: 'pi pi-link',
                        command: () => {
                            this.router.navigate(['/installation']);
                        }
                    },
                    {
                        label: 'External',
                        icon: 'pi pi-home',
                        url: 'https://angular.io//'
                    }
                ]
            }
        ];
    }
}

템플릿

Menu는 모델에서 menuitem 인스턴스를 파라미터로 받는 item 템플릿을 통해 아이템 커스터마이징을 제공해요. 서브메뉴 헤더에는 자체적인 submenuheader 템플릿이 있고, start와 end라는 이름의 추가 슬롯이 제공돼서 메뉴 앞이나 뒤에 콘텐츠를 넣을 수 있어요.

import { Component, OnInit } from '@angular/core';
import { AvatarModule } from 'primeng/avatar';
import { BadgeModule } from 'primeng/badge';
import { Menu, MenuModule } from 'primeng/menu';
import { RippleModule } from 'primeng/ripple';
import { MenuItem } from 'primeng/api';

@Component({
    template: `
        <div class="card">
            <p-menu [model]="items" class="flex justify-center" styleClass="w-full md:w-60">
                <ng-template #start>
                    <span class="inline-flex items-center gap-1 px-2 py-2">
                        <svg width="31" height="33" viewBox="0 0 31 33" fill="none" xmlns="http://www.w3.org/2000/svg" class="block mx-auto">
                            <path d="M15.1934 0V0V0L0.0391235 5.38288L2.35052 25.3417L15.1934 32.427V32.427V32.427L28.0364 25.3417L30.3478 5.38288L15.1934 0Z" fill="var(--p-primary-color)" />
                            <mask id="mask0_1_52" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="0" y="0" width="31" height="33">
                                <path d="M15.1934 0V0V0L0.0391235 5.38288L2.35052 25.3417L15.1934 32.427V32.427V32.427L28.0364 25.3417L30.3478 5.38288L15.1934 0Z" fill="var(--ground-background)" />
                            </mask>
                            <g mask="url(#mask0_1_52)">
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M15.1935 0V3.5994V3.58318V20.0075V20.0075V32.427V32.427L28.0364 25.3417L30.3478 5.38288L15.1935 0Z" fill="var(--p-primary-color)" />
                            </g>
                            <path d="M19.6399 15.3776L18.1861 15.0547L19.3169 16.6695V21.6755L23.1938 18.4458V12.9554L21.4169 13.6013L19.6399 15.3776Z" fill="var(--ground-background)" />
                            <path d="M10.5936 15.3776L12.0474 15.0547L10.9166 16.6695V21.6755L7.03966 18.4458V12.9554L8.81661 13.6013L10.5936 15.3776Z" fill="var(--ground-background)" />
                            <path
                                fill-rule="evenodd"
                                clip-rule="evenodd"
                                d="M11.3853 16.9726L12.6739 15.0309L13.4793 15.5163H16.7008L17.5061 15.0309L18.7947 16.9726V24.254L17.8283 25.7103L16.7008 26.843H13.4793L12.3518 25.7103L11.3853 24.254V16.9726Z"
                                fill="var(--ground-background)"
                            />
                            <path d="M19.3168 24.7437L21.4168 22.6444V20.5451L19.3168 22.3214V24.7437Z" fill="var(--ground-background)" />
                            <path d="M10.9166 24.7437L8.81662 22.6444V20.5451L10.9166 22.3214V24.7437Z" fill="var(--ground-background)" />
                            <path
                                fill-rule="evenodd"
                                clip-rule="evenodd"
                                d="M13.0167 5.68861L11.7244 8.7568L13.8244 14.8932H14.7936V5.68861H13.0167ZM15.4397 5.68861V14.8932H16.5706L18.5091 8.7568L17.2167 5.68861H15.4397Z"
                                fill="var(--ground-background)"
                            />
                            <path d="M13.8244 14.8932L6.87813 12.3094L5.90888 8.27235L11.8859 8.7568L13.9859 14.8932H13.8244Z" fill="var(--ground-background)" />
                            <path d="M16.5706 14.8932L23.5169 12.3094L24.4861 8.27235L18.3476 8.7568L16.4091 14.8932H16.5706Z" fill="var(--ground-background)" />
                            <path d="M18.8321 8.27235L22.2245 7.94938L19.9629 5.68861H17.7013L18.8321 8.27235Z" fill="var(--ground-background)" />
                            <path d="M11.4013 8.27235L8.00893 7.94938L10.2705 5.68861H12.5321L11.4013 8.27235Z" fill="var(--ground-background)" />
                        </svg>
                        <span class="text-xl font-semibold">PRIME<span class="text-primary">APP</span></span>
                    </span>
                </ng-template>
                <ng-template #submenuheader let-item>
                    <span class="text-primary font-bold">{{ item.label }}</span>
                </ng-template>
                <ng-template #item let-item>
                    <a pRipple class="flex items-center px-3 py-2 cursor-pointer" [class]="item.linkClass">
                        <span [class]="item.icon"></span>
                        <span class="ms-2">{{ item.label }}</span>
                        <p-badge *ngIf="item.badge" class="ms-auto" [value]="item.badge" />
                        <span *ngIf="item.shortcut" class="ms-auto border border-surface rounded bg-emphasis text-muted-color text-xs p-1">{{ item.shortcut }}</span>
                    </a>
                </ng-template>
                <ng-template #end>
                    <button pRipple class="relative overflow-hidden w-full border-0 bg-transparent flex items-start p-2 pl-4 hover:bg-surface-100 dark:hover:bg-surface-800 rounded-none cursor-pointer transition-colors duration-200">
                        <p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" class="mr-2" shape="circle" />
                        <span class="inline-flex flex-col items-start">
                            <span class="font-bold">Amy Elsner</span>
                            <span class="text-sm">Admin</span>
                        </span>
                    </button>
                </ng-template>
            </p-menu>
        </div>
    `,
    standalone: true,
    imports: [AvatarModule, BadgeModule, MenuModule, RippleModule]
})
export class MenuTemplateDemo implements OnInit {
    items: MenuItem[] | undefined;

    ngOnInit() {
        this.items = [
            {
                separator: true
            },
            {
                label: 'Documents',
                items: [
                    {
                        label: 'New',
                        icon: 'pi pi-plus',
                        shortcut: '⌘+N'
                    },
                    {
                        label: 'Search',
                        icon: 'pi pi-search',
                        shortcut: '⌘+S'
                    }
                ]
            },
            {
                label: 'Profile',
                items: [
                    {
                        label: 'Settings',
                        icon: 'pi pi-cog',
                        shortcut: '⌘+O'
                    },
                    {
                        label: 'Messages',
                        icon: 'pi pi-inbox',
                        badge: '2'
                    },
                    {
                        label: 'Logout',
                        icon: 'pi pi-sign-out',
                        shortcut: '⌘+Q',
                        linkClass: '!text-red-500 dark:!text-red-400'
                    }
                ]
            },
            {
                separator: true
            }
        ];
    }
}

Menu는 동적 및 정적 위치 지정을 지원하는 내비게이션 / 커맨드 컴포넌트예요.

프롭스

이름 타입 기본값 설명
dt InputSignal undefined 컴포넌트의 스코프 디자인 토큰을 정의해요.
unstyled InputSignal undefined 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요.
pt InputSignal undefined 컴포넌트 내부의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
ptOptions InputSignal undefined 컴포넌트의 passthrough(pt) 옵션을 구성하는 데 사용돼요.
model MenuItem[] - menuitem 배열이에요.
popup boolean false 메뉴를 팝업으로 표시할지 정의해요.
style { [klass: string]: any } - 컴포넌트의 인라인 스타일이에요.
styleClass string - 컴포넌트의 스타일 클래스예요.
autoZIndex boolean true 레이어링을 자동으로 관리할지 여부예요.
baseZIndex number 0 레이어링에서 사용할 기본 zIndex 값이에요.
showTransitionOptions string .12s cubic-bezier(0, 0, 0.2, 1) show 애니메이션의 트랜지션 옵션이에요. (지원 중단됨)
hideTransitionOptions string .1s linear hide 애니메이션의 트랜지션 옵션이에요. (지원 중단됨)
ariaLabel string - 인터랙티브 엘리먼트에 라벨을 지정하는 문자열 값을 정의해요.
ariaLabelledBy string - 기반이 되는 input 엘리먼트의 식별자예요.
id string - 문자열 형태의 현재 id 상태예요.
tabindex number 0 탭 순서에서 엘리먼트의 인덱스예요.
appendTo InputSignal 'self' 오버레이를 어태치할 대상 엘리먼트예요. 유효한 값은 "body" 또는 다른 엘리먼트의 로컬 ng-template 변수예요 (참고: 템플릿 변수에는 대괄호를 사용한 바인딩을 사용하세요. 예를 들어 #mydiv를 변수 이름으로 가지는 div 엘리먼트라면 [appendTo]="mydiv"처럼 지정해요).
motionOptions InputSignal ... 모션 옵션이에요.

이벤트

이름 파라미터 설명
onShow value: any 오버레이 메뉴가 표시될 때 호출되는 콜백이에요.
onHide value: any 오버레이 메뉴가 숨겨질 때 호출되는 콜백이에요.
onBlur event: Event 리스트가 포커스를 잃을 때 호출되는 콜백이에요.
onFocus event: Event 리스트가 포커스를 받을 때 호출되는 콜백이에요.

템플릿

이름 타입 설명
start TemplateRef start에 대한 템플릿 옵션을 정의해요.
end TemplateRef end에 대한 템플릿 옵션을 정의해요.
header TemplateRef header에 대한 템플릿 옵션을 정의해요.
item TemplateRef 커스텀 아이템 템플릿이에요.
submenuheader TemplateRef 커스텀 서브메뉴 헤더 템플릿이에요.

메서드

이름 파라미터 반환 타입 설명
toggle event: Event void 팝업 메뉴의 표시 여부를 토글해요.
show event: any void 팝업 메뉴를 표시해요.
hide void 팝업 메뉴를 숨겨요.

패스 스루 옵션

이름 타입 설명
root PassThroughOption<HTMLDivElement, I> root의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
start PassThroughOption<HTMLDivElement, I> start의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
list PassThroughOption<HTMLUListElement, I> list의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
submenuLabel PassThroughOption<HTMLLIElement, I> 서브메뉴 라벨의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
separator PassThroughOption<HTMLLIElement, I> separator의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
item PassThroughOption<HTMLLIElement, I> item의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
itemContent PassThroughOption<HTMLDivElement, I> item content의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
itemLink PassThroughOption<HTMLAnchorElement, I> item link의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
itemIcon PassThroughOption<HTMLSpanElement, I> item icon의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
itemLabel PassThroughOption<HTMLSpanElement, I> item label의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
end PassThroughOption<HTMLDivElement, I> end의 DOM 엘리먼트에 속성을 전달하는 데 사용돼요.
motion MotionOptions motion 컴포넌트/디렉티브에 옵션을 전달하는 데 사용돼요.

테마

CSS 클래스

클래스 설명
p-menu 루트 엘리먼트의 클래스 이름이에요
p-menu-start start 엘리먼트의 클래스 이름이에요
p-menu-list list 엘리먼트의 클래스 이름이에요
p-menu-submenu-item 서브메뉴 아이템 엘리먼트의 클래스 이름이에요
p-menu-separator separator 엘리먼트의 클래스 이름이에요
p-menu-end end 엘리먼트의 클래스 이름이에요
p-menu-item item 엘리먼트의 클래스 이름이에요
p-menu-item-content item content 엘리먼트의 클래스 이름이에요
p-menu-item-link item link 엘리먼트의 클래스 이름이에요
p-menu-item-icon item icon 엘리먼트의 클래스 이름이에요
p-menu-item-label item label 엘리먼트의 클래스 이름이에요

디자인 토큰

토큰 CSS 변수 설명
menu.background --p-menu-background 루트의 배경
menu.border.color --p-menu-border-color 루트의 테두리 색상
menu.color --p-menu-color 루트의 색상
menu.border.radius --p-menu-border-radius 루트의 테두리 반경
menu.shadow --p-menu-shadow 루트의 그림자
menu.transition.duration --p-menu-transition-duration 루트의 전환 지속 시간
menu.list.padding --p-menu-list-padding 리스트의 패딩
menu.list.gap --p-menu-list-gap 리스트의 간격
menu.item.focus.background --p-menu-item-focus-background 항목의 포커스 배경
menu.item.color --p-menu-item-color 항목의 색상
menu.item.focus.color --p-menu-item-focus-color 항목의 포커스 색상
menu.item.padding --p-menu-item-padding 항목의 패딩
menu.item.border.radius --p-menu-item-border-radius 항목의 테두리 반경
menu.item.gap --p-menu-item-gap 항목의 간격
menu.item.icon.color --p-menu-item-icon-color 항목의 아이콘 색상
menu.item.icon.focus.color --p-menu-item-icon-focus-color 항목의 아이콘 포커스 색상
menu.submenu.label.padding --p-menu-submenu-label-padding 서브메뉴 라벨의 패딩
menu.submenu.label.font.weight --p-menu-submenu-label-font-weight 서브메뉴 라벨의 글꼴 두께
menu.submenu.label.background --p-menu-submenu-label-background 서브메뉴 라벨의 배경
menu.submenu.label.color --p-menu-submenu-label-color 서브메뉴 라벨의 색상
menu.separator.border.color --p-menu-separator-border-color 구분선의 테두리 색상

더 알아보기 (Learn more)

이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.