ContextMenu는 마우스 우클릭 동작을 위한 compound composition 방식의, styled가 없는 접근성 높은 컨텍스트 메뉴 컴포넌트예요. 다양한 하위 컴포넌트를 조합해 완전히 커스텀한 우클릭 메뉴를 만들 수 있어요.
출처: 문서
본문
기능 (Features)
- 하위 컴포넌트(
Root, Trigger, Portal, Positioner, Popup, Arrow, List, Item, Sub, SubTrigger, SubPortal, SubPositioner, SubPopup, Indicator, Label, Separator, RadioGroup)로 구성된 compound 컴포넌트 API
- 커서 위치 기반 자동 팝업 위치 지정을 사용한 우클릭 활성화
- 컨텍스트 메뉴를 문서 전체에 붙이는 글로벌 모드
- 열림/닫힘을 조율하는 중첩 서브메뉴
- 표시기(indicator) 매칭이 되는 체크박스 및 라디오 아이템 타입
- 타이핑으로 아이템으로 이동하는 내장 문자 검색
사용법 (Usage)
import { ContextMenu } from 'primereact/contextmenu';
<ContextMenu.Root>
<ContextMenu.Trigger></ContextMenu.Trigger>
<ContextMenu.Portal>
<ContextMenu.Positioner>
<ContextMenu.Popup>
<ContextMenu.Arrow />
<ContextMenu.List>
<ContextMenu.Label></ContextMenu.Label>
<ContextMenu.Item></ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Submenu>
<ContextMenu.SubmenuTrigger>
<ContextMenu.Indicator />
</ContextMenu.SubmenuTrigger>
<ContextMenu.SubmenuPortal>
<ContextMenu.SubmenuPositioner>
<ContextMenu.SubmenuPopup>
<ContextMenu.List>
<ContextMenu.Item></ContextMenu.Item>
</ContextMenu.List>
</ContextMenu.SubmenuPopup>
</ContextMenu.SubmenuPositioner>
</ContextMenu.SubmenuPortal>
</ContextMenu.Submenu>
</ContextMenu.List>
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
동작 (Behavior)
체크박스 아이템 (Checkbox Items)
토글 가능한 아이템을 만들려면 ContextMenu.Item에 type="checkbox"를 설정하세요. 아이템이 활성 상태일 때 체크 표시를 보여 주려면 match="checked"를 가진 ContextMenu.Indicator를 사용해요.
<ContextMenu.Item type="checkbox" defaultChecked={true}>
<ContextMenu.Indicator match="checked">
<Check />
</ContextMenu.Indicator>
Enable Notifications
</ContextMenu.Item>
라디오 아이템 (Radio Items)
type="radio"를 가진 ContextMenu.Item 요소들을 ContextMenu.RadioGroup 안에 감싸세요. 선택된 옵션에 점이나 아이콘을 표시하려면 match="checked"를 가진 ContextMenu.Indicator를 사용해요.
<ContextMenu.RadioGroup defaultValue="light">
<ContextMenu.Item type="radio" value="light">
<ContextMenu.Indicator match="checked">
<Dot />
</ContextMenu.Indicator>
Light Mode
</ContextMenu.Item>
<ContextMenu.Item type="radio" value="dark">
<ContextMenu.Indicator match="checked">
<Dot />
</ContextMenu.Indicator>
Dark Mode
</ContextMenu.Item>
</ContextMenu.RadioGroup>
모션 애니메이션 (Motion Animation)
<ContextMenu.Popup motionProps={{ name: 'p-anchored-overlay' }}></ContextMenu.Popup>
애니메이션 단계(phases), CSS 변수, hide 전략에 대해서는 Motion 문서를 참고하세요.
다형적 렌더링 (Polymorphic Rendering)
아무 하위 컴포넌트에 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.
<ContextMenu.Item as="a" href="/dashboard">
Dashboard
</ContextMenu.Item>
기본 요소: Root=div, Trigger=div, List=ul, Item=li, Sub=li, SubTrigger=div, SubPositioner=div, SubPopup=div, Label=li, Separator=li, Indicator=span, Popup=div, Positioner=div, Arrow=div, RadioGroup=div, Portal=portal, SubPortal=portal.
렌더 함수 자식 (Render Function Children)
Item과 SubTrigger는 렌더 함수를 자식으로 받아 컴포넌트 인스턴스에 접근할 수 있게 해 줘요.
<ContextMenu.Item>{(instance) => <span>{instance.focused ? 'Focused' : 'Idle'}</span>}</ContextMenu.Item>
패스 스루 (Pass Through)
<DocPTViewer name="contextmenu-pt" components={['ContextMenuRoot']} />
API
| Attribute |
Value |
data-scope |
"contextmenu" |
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 |
"contextmenu" |
data-part |
"trigger" |
| Label |
Type |
Description |
| root |
ContextMenuTriggerPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"contextmenu" |
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 |
"contextmenu" |
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 |
ContextMenuItemPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"contextmenu" |
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 |
"contextmenu" |
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 |
"contextmenu" |
data-part |
"subpositioner" |
CSS 커스텀 속성 (CSS Custom Properties)
positioner 요소는 레이아웃과 transform 제어를 위한 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 |
| Attribute |
Value |
data-scope |
"contextmenu" |
data-part |
"subpopup" |
| Attribute |
Value |
data-scope |
"contextmenu" |
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 |
"contextmenu" |
data-part |
"label" |
| Label |
Type |
Description |
| root |
MenuLabelPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"contextmenu" |
data-part |
"separator" |
| Label |
Type |
Description |
| root |
MenuSeparatorPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"contextmenu" |
data-part |
"radiogroup" |
| Attribute |
Value |
data-scope |
"contextmenu" |
data-part |
"popup" |
| Label |
Type |
Description |
| root |
MenuPopupPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"contextmenu" |
data-part |
"positioner" |
CSS 커스텀 속성 (CSS Custom Properties)
positioner 요소는 레이아웃과 transform 제어를 위한 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 |
"contextmenu" |
data-part |
"arrow" |
CSS 커스텀 속성 (CSS Custom Properties)
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 |
ContextMenuCheckboxItemPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
ContextMenuRadioItemPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
ContextMenu 컴포넌트는 menu 역할을 사용하며, 메뉴를 설명하는 값은 aria-labelledby 또는 aria-label props로 제공할 수 있어요. 각 목록 아이템은 menuitem 역할을 가지며, aria-label으로 아이템의 라벨을, 아이템이 비활성화되어 있으면 aria-disabled를 나타내요. 체크박스 아이템은 menuitemcheckbox, 라디오 아이템은 menuitemradio 역할을 사용하고, 선택 상태를 나타내기 위해 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. Popup closes and focus moves to target. |
space |
Activates the focused menuitem. Popup closes and focus moves to target. |
escape |
Closes the popup 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 |
Closes the popup and focus moves to the target. |
home |
Moves focus to the first menuitem. |
end |
Moves focus to the last menuitem. |
더 알아보기 (Learn more)