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

메뉴

원문 보기 위키 갱신

메뉴 (Menu)

인라인 및 팝업 탐색을 위한 컴파운드 구성 방식의 스타일 없는 접근성 좋은 메뉴 컴포넌트예요.

출처: 문서

본문

Features (기능)

  • 하위 컴포넌트(Root, List, Item, CheckboxItem, RadioItemGroup, RadioItem, Sub, SubTrigger, Indicator, Label, Separator, Trigger, Portal, Positioner, Popup, Arrow)를 갖춘 컴파운드 API예요. 서브메뉴는 Menu.Submenu 안에서 Portal/Positioner/Popup을 재사용해요. 같은 컴포넌트들이 서브 컨텍스트를 자동으로 감지해요.
  • 인라인 및 portal 기반 팝업 렌더링 모드예요.
  • 열기/닫기 조정이 가능한 중첩 서브메뉴예요.
  • 표시기 매칭이 있는 체크박스 및 라디오 항목 유형이에요.
  • 타이핑으로 항목으로 점프하는 내장 문자 검색이에요.
  • 호버로 열리는 동작을 가진 복잡한 중첩 메뉴를 위한 컴포지트(composite) 모드예요.

Usage (사용법)

import { Menu } from 'primereact/menu';
<Menu.Root>
    <Menu.Trigger />
    <Menu.Portal>
        <Menu.Positioner>
            <Menu.Popup>
                <Menu.List>
                    <Menu.Item />
                    <Menu.Submenu>
                        <Menu.SubmenuTrigger>
                            <Menu.Indicator />
                        </Menu.SubmenuTrigger>
                        <Menu.Portal>
                            <Menu.Positioner>
                                <Menu.Popup>
                                    <Menu.List>
                                        <Menu.Item />
                                        <Menu.Item />
                                    </Menu.List>
                                </Menu.Popup>
                            </Menu.Positioner>
                        </Menu.Portal>
                    </Menu.Submenu>
                </Menu.List>
            </Menu.Popup>
        </Menu.Positioner>
    </Menu.Portal>
</Menu.Root>

Behavior (동작)

체크박스 항목 (Checkbox Items)

토글 가능한 항목을 만들려면 Menu.CheckboxItem을 사용해요. useCheckbox로 관리돼요. 항목이 활성 상태일 때 체크 표시를 보여주려면 match="checked"를 가진 Menu.Indicator를 사용해요.

<Menu.CheckboxItem defaultChecked={true}>
    <Menu.Indicator match="checked">
        <Check />
    </Menu.Indicator>
    Enable Notifications
</Menu.CheckboxItem>

라디오 항목 (Radio Items)

Menu.RadioItem 요소들을 Menu.RadioItemGroup 안에 감싸요. 그룹은 useRadioButtonGroup으로 관리되고 각 항목은 useRadioButton으로 관리돼요. 선택된 옵션에 점이나 아이콘을 표시하려면 match="checked"를 가진 Menu.Indicator를 사용해요.

<Menu.RadioItemGroup defaultValue="light">
    <Menu.RadioItem value="light">
        <Menu.Indicator match="checked">
            <Dot />
        </Menu.Indicator>
        Light Mode
    </Menu.RadioItem>
    <Menu.RadioItem value="dark">
        <Menu.Indicator match="checked">
            <Dot />
        </Menu.Indicator>
        Dark Mode
    </Menu.RadioItem>
</Menu.RadioItemGroup>

모션 애니메이션 (Motion Animation)

<Menu.Popup motionProps={{ name: 'p-popover' }}></Menu.Popup>

애니메이션 단계, CSS 변수, 숨김 전략에 대한 자세한 내용은 Motion을 참조하세요.

다형성 렌더링 (Polymorphic Rendering)

어떤 하위 컴포넌트든 as를 써서 렌더링되는 HTML 요소를 바꿀 수 있어요.

<Menu.Item as="a" href="/dashboard">
    Dashboard
</Menu.Item>

기본 요소: Root=div, List=ul, Item=li, CheckboxItem=li, RadioItem=li, Sub=li, SubTrigger=div, Trigger=button, Label=li, Separator=li, Indicator=span, Popup=div, Positioner=div, Arrow=div, RadioItemGroup=div, Portal=portal이에요.

렌더 함수 children (Render Function Children)

Item과 SubTrigger는 children으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요.

<Menu.Item>{(instance) => <span>{instance.focused ? 'Focused' : 'Idle'}</span>}</Menu.Item>

API

Attribute Value
data-scope "menu"
data-part "root"
Label Type Description
root MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
list MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the list's DOM element.
item MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the item's DOM element.
label MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the label's DOM element.
sub MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the submenu's DOM element.
trigger MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the trigger's DOM element.
subtrigger MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the subtrigger's DOM element.
checkboxItemIndicator MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the checkbox item indicator's DOM element.
radioItemIndicator MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the radio item indicator's DOM element.
submenuIndicator MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the submenu indicator's DOM element.
separator MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the separator's DOM element.
popup MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the popup's DOM element.
arrow MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the arrow's DOM element.
positioner MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the positioner's DOM element.
subpositioner MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the subpositioner's DOM element.
subpopup MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the subpopup's DOM element.
subportal MenuRootPassThroughType<HTMLAttributes> Used to pass attributes to the subportal's DOM element.
Attribute Value
data-scope "menu"
data-part "trigger"
data-open Present when menu is open
Label Type Description
root MenuTriggerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "list"
Label Type Description
root MenuListPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
content MenuListPassThroughType<HTMLAttributes> Used to pass attributes to the content's DOM element.
Attribute Value
data-scope "menu"
data-part "item"
data-value The item value
data-focused Present when item is focused
data-disabled Present when item is disabled
data-checked Present when item is checked
Label Type Description
root MenuItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "sub"
data-open Present when submenu is open
Label Type Description
root MenuSubmenuPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "subtrigger"
data-value The subtrigger value
data-focused Present when subtrigger is focused
data-disabled Present when subtrigger is disabled
data-open Present when submenu is open
Label Type Description
root MenuSubmenuTriggerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "indicator"
data-open Present when submenu is open
data-closed Present when submenu is closed
data-checked Present when item is checked
data-unchecked Present when item is unchecked
Attribute Value
data-scope "menu"
data-part "label"
Label Type Description
root MenuLabelPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "separator"
Label Type Description
root MenuSeparatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "item"
Label Type Description
root MenuCheckboxItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "radiogroup"
Attribute Value
data-scope "menu"
data-part "item"
Label Type Description
root MenuRadioItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "popup"
Label Type Description
root MenuPopupPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "positioner"

CSS 커스텀 프로퍼티

positioner 요소는 레이아웃과 변환 제어를 위한 CSS 커스텀 프로퍼티를 노출해요.

CSS Variable Description
--available-height Available vertical space in pixels
--available-width Available horizontal space in pixels
--transform-origin Computed transform origin for animations
--positioner-anchor-width Width of the anchor/trigger element
Label Type Description
root MenuPositionerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Attribute Value
data-scope "menu"
data-part "arrow"

CSS 커스텀 프로퍼티

arrow 요소는 동적 위치 지정을 위한 CSS 커스텀 프로퍼티를 노출해요:

CSS Variable Description
--placer-arrow-x Horizontal offset of arrow
--placer-arrow-y Vertical offset of arrow
--placer-arrow-left Left position of arrow element
--placer-arrow-top Top position of arrow element
Label Type Description
root MenuArrowPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Label Type Description
root MenuPortalPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Label Type Description
root MenuCheckboxItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
Label Type Description
root MenuRadioItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

Accessibility (접근성)

스크린 리더 (Screen Reader)

Menu 컴포넌트는 menu role을 사용하며, 메뉴를 설명하는 값은 aria-labelledby 또는 aria-label props로 제공할 수 있어요. 각 목록 항목은 menuitem role을 가지며 항목의 라벨을 가리키는 aria-label과, 항목이 비활성화된 경우 aria-disabled를 가져요. 체크박스 항목은 menuitemcheckbox, 라디오 항목은 menuitemradio role을 사용하고 선택 상태를 나타내는 aria-checked를 가져요.

팝업 모드에서는 컴포넌트가 대상 요소의 aria-expanded, aria-haspopup, aria-controls 속성을 암시적으로 관리해 대상과 팝업 사이의 관계를 정의해요.

키보드 지원 (Keyboard Support)

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 first 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 Activates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target.
space Activates the focused menuitem. If menu is in overlay mode, popup gets closes and focus moves to target.
escape If menu is in overlay mode, popup gets closes and focus moves to target.
down arrow Moves focus to the next menuitem.
up arrow Moves focus to the previous menuitem.
right arrow Opens submenu if focused item has one. Moves focus to the first item of the submenu.
left arrow Closes current submenu and moves focus to parent submenu trigger.
alt + up arrow If menu is in overlay mode, popup gets closes and focus moves to the target.
home Moves focus to the first menuitem.
end Moves focus to the last menuitem.

더 알아보기 (Learn more)