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

Menubar (메뉴바)

원문 보기 위키 갱신

데스크톱 애플리케이션에서 흔히 볼 수 있는, 시각적으로 지속되는 메뉴예요. 일관된 명령 집합에 빠르게 접근할 수 있게 해주는 컴포넌트예요. 서브메뉴, 라디오·체크박스 항목, 그룹, 분리선 등 풍부한 메뉴 구조를 지원해요.

출처: 문서

본문

Import (불러오기)

import { Menubar } from "@kobalte/core/menubar";
// or
import { Root, Menu, ... } from "@kobalte/core/menubar";
// or (deprecated)
import { Menubar } from "@kobalte/core";

Features (기능)

  • WAI ARIA Menubar 디자인 패턴을 따릅니다.
  • 모달(modal) 및 비모달(non-modal) 모드를 지원해요.
  • 서브메뉴를 지원해요.
  • 항목, 라벨, 항목 그룹을 지원해요.
  • 선택적 비결정(indeterminate) 상태를 갖는 체크 가능한 항목(단일 또는 다중)을 지원해요.
  • 비활성화된 항목을 지원해요.
  • 복잡한 항목 라벨링을 통한 접근성을 지원해요.
  • 키보드 열기 및 내비게이션을 지원해요.
  • 키보드 내비게이션 중 자동 스크롤을 지원해요.
  • 텍스트 입력으로 항목에 포커스할 수 있는 타입어헤드(typeahead)를 지원해요.
  • 선택적으로 화살표(arrow)를 렌더링할 수 있어요.
  • 포커스가 완전히 관리돼요.

Anatomy (구조)

메뉴바는 다음의 부분으로 이루어져요:

  • Menubar: 메뉴바의 루트 컨테이너예요.

메뉴는 다음의 부분들로 이루어져요:

  • Menubar.Menu: 각 메뉴의 컨테이너예요.
  • Menubar.Trigger: 메뉴를 토글하는 버튼이에요.
  • Menubar.Icon: 메뉴 트리거 안에 표시되는 작은 아이콘이에요. 메뉴가 열릴 수 있다는 사실을 시각적으로 암시하는 역할을 해요.
  • Menubar.Portal: 메뉴가 열려 있을 때 children을 body로 포털(portal)해요.
  • Menubar.Content: 메뉴가 열려 있을 때 렌더링할 콘텐츠를 담아요.
  • Menubar.Arrow: 메뉴 콘텐츠 옆에 렌더링되는 선택적 화살표 요소예요.
  • Menubar.Separator: 메뉴에서 항목들을 시각적으로 분리하는 데 사용돼요.
  • Menubar.Group: 여러 항목을 그룹화하는 데 사용돼요. 자동 라벨링을 통한 접근성을 보장하려면 Menubar.GroupLabel과 함께 사용하세요.
  • Menubar.GroupLabel: 그룹의 라벨을 렌더링하는 데 사용돼요. 화살표 키로 포커스할 수 없어요.
  • Menubar.Sub: 서브메뉴의 모든 부분을 담아요.
  • Menubar.SubTrigger: 서브메뉴를 여는 항목이에요. Menubar.Sub 안에 렌더링해야 해요.
  • Menubar.SubContent: 서브메뉴가 열려 있을 때 튀어나오는 컴포넌트예요. Menubar.Sub 안에 렌더링해야 해요.

메뉴 항목은 다음의 부분들로 이루어져요:

  • Menubar.Item: 선택 항목이에요.
  • Menubar.ItemLabel: 항목에 대해 알려줄 접근 가능한 라벨이에요.
  • Menubar.ItemDescription: 항목에 대해 알려줄 선택적 접근 가능한 설명이에요.
  • Menubar.ItemIndicator: 항목이 체크되었을 때 렌더링되는 시각적 표시예요.

체크 가능한 메뉴 항목은 다음의 부분들로 이루어져요:

  • Menubar.RadioGroup: 여러 Menubar.RadioItem을 그룹화하고 선택을 관리하는 데 사용돼요.
  • Menubar.RadioItem: 라디오처럼 제어되고 렌더링될 수 있는 항목이에요.
  • Menubar.CheckboxItem: 체크박스처럼 제어되고 렌더링될 수 있는 항목이에요.
<Menubar>
	<Menubar.Menu>
		<Menubar.Trigger>
			<Menubar.Icon />
		</Menubar.Trigger>

		<Menubar.Portal>
			<Menubar.Content>
				<Menubar.Arrow />

				<Menubar.Item>
					<Menubar.ItemLabel />
					<Menubar.ItemDescription />
				</Menubar.Item>

				<Menubar.Group>
					<Menubar.GroupLabel />
					<Menubar.Item />
				</Menubar.Group>

				<Menubar.CheckboxItem>
					<Menubar.ItemIndicator />
				</Menubar.CheckboxItem>

				<Menubar.RadioGroup>
					<Menubar.RadioItem>
						<Menubar.ItemIndicator />
					</Menubar.RadioItem>
				</Menubar.RadioGroup>

				<Menubar.Sub>
					<Menubar.SubTrigger />
					<Menubar.Portal>
						<Menubar.SubContent />
					</Menubar.Portal>
				</Menubar.Sub>

				<Menubar.Separator />
			</Menubar.Content>
		</Menubar.Portal>
	</Menubar.Menu>
</Menubar>

Example (예제)

기본 예제를 통해 메뉴바의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.

Usage (사용법)

메뉴바 트리거 중 하나를 메뉴를 여는 대신 링크로 만들려면 Menubar.Menu를 생략하고 Menubar.Trigger에 as="a"를 설정하세요.

<Menubar>
	<Menubar.Menu>
		<Menubar.Trigger>Opens a regular menu</Menubar.Trigger>
		<Menubar.Portal>
			<Menubar.Content>...</Menubar.Content>
		</Menubar.Portal>
	</Menubar.Menu>

	<Menubar.Trigger as="a" href="https://kobalte.dev">
		Opens a link on click and enter
	</Menubar.Trigger>
</Menubar>

Origin-aware animations (원점 기준 애니메이션)

계산된 원점(origin)에서 콘텐츠를 애니메이션 처리할 수 있도록, CSS 커스텀 프로퍼티 --kb-menu-content-transform-origin을 제공해요.

/* style.css */
.context-menu__content,
.context-menu__sub-content {
	transform-origin: var(--kb-menu-content-transform-origin);
	animation: contentHide 250ms ease-in forwards;
}

.context-menu__content[data-expanded],
.context-menu__sub-content[data-expanded] {
	animation: contentShow 250ms ease-out;
}

@keyframes contentShow {
	from {
		opacity: 0;
		transform: scale(0.96);
	}
	to {
		opacity: 1;
		transform: scale(1);
	}
}

@keyframes contentHide {
	from {
		opacity: 1;
		transform: scale(1);
	}
	to {
		opacity: 0;
		transform: scale(0.96);
	}
}

API Reference (API 참조)

Menubar는 @kobalte/core/menubar의 Root 임포트(그리고 더 이상 사용되지 않는 Menubar.Root)와 동일해요.

Prop Description
defaultValue string
The value of the menu that should be open when initially rendered. Use when you do not need to control the value state.
value string
The controlled value of the menu to open. Should be used in conjunction with onValueChange.
onValueChange (value: string | undefined | null) => void
Event handler called when the value changes.
loop boolean
When true, keyboard navigation will loop from last item to first, and vice versa.
focusOnAlt boolean
When true, click on alt by itself will focus this Menubar (some browsers interfere).
Prop Description
onOpenChange (open: boolean) => void
Event handler called when the open state of the menu changes.
id string
A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
modal boolean
Whether the menu should be the only visible content for screen readers, when set to true:
- interaction with outside elements will be disabled.
- scroll will be locked.
- focus will be locked inside the menu content.
- elements outside the menu content will not be visible for screen readers.
preventScroll boolean
Whether the scroll should be locked even if the menu is not modal.
forceMount boolean
Used to force mounting the menu (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
value string
A unique value that associates the item with an active value when the navigation menu is controlled. This prop is managed automatically when uncontrolled.

Menubar.Menu는 Menubar.Content의 배치(placement)를 커스텀하기 위해 다음의 prop들을 추가로 받아요.

Prop Description
placement Placement
The placement of the menu content.
gutter number
The distance between the menu content and the trigger element. By default, it's 0 plus half of the arrow offset, if it exists.
shift number
The skidding of the menu content along the trigger element.
flip boolean | string
Controls the behavior of the menu content when it overflows the viewport:
- If a boolean, specifies whether the menu content should flip to the opposite side when it overflows.
- If a string, indicates the preferred fallback placements when it overflows.
The placements must be spaced-delimited, e.g. "top left".
slide boolean
Whether the menu content should slide when it overflows.
overlap boolean
Whether the menu content can overlap the trigger element when it overflows.
sameWidth boolean
Whether the menu content should have the same width as the trigger element. This will be exposed to CSS as --kb-popper-anchor-width.
fitViewport boolean
Whether the menu content should fit the viewport. If this is set to true, the menu content will have maxWidth and maxHeight set to the viewport size. This will be exposed to CSS as --kb-popper-available-width and --kb-popper-available-height.
hideWhenDetached boolean
Whether to hide the menu content when the trigger element becomes occluded.
detachedPadding number
The minimum padding in order to consider the trigger element occluded.
arrowPadding number
The minimum padding between the arrow and the menu content corner.
overflowPadding number
The minimum padding between the menu content and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.
Prop Description
disabled boolean
Whether the context menu trigger is disabled or not.
Data attribute Description
data-expanded Present when the menu is open.
data-closed Present when the menu is close.
data-disabled Present when the trigger is disabled.

Menubar.Icon, Menubar.Content, Menubar.SubTrigger과 Menubar.SubContent는 동일한 data-expanded 속성을 공유해요.

포퍼 위치 지정자(popper positioner)는 Menubar.Content와 동일한 z-index를 복사해요.

Prop Description
onOpenAutoFocus (event: Event) => void
Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocus (event: Event) => void
Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
onEscapeKeyDown (event: KeyboardEvent) => void
Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside (event: PointerDownOutsideEvent) => void
Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside (event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside (event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.
Prop Description
size number
The size of the arrow.
Prop Description
textValue string
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the Menubar.ItemLabel part if provided, or fallback to the .textContent of the Menubar.Item. Use this when the content is complex, or you have non-textual content inside.
disabled boolean
Whether the item is disabled or not.
closeOnSelect boolean
Whether the menu should close when the item is activated.
onSelect () => void
Event handler called when the user selects an item (via mouse or keyboard).
Data attribute Description
data-disabled Present when the item is disabled.
data-highlighted Present when the item is highlighted.

Menubar.ItemLabel, Menubar.ItemDescription과 Menubar.ItemIndicator는 동일한 data-attribute를 공유해요.

Prop Description
forceMount boolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.
Prop Description
value string
The controlled value of the menu radio item to check.
defaultValue string
The value of the menu radio item that should be checked when initially rendered. Useful when you do not need to control the state of the radio group.
onChange (value: string) => void
Event handler called when the value changes.
disabled boolean
Whether the radio group is disabled or not.
Prop Description
value string
The value of the menu item radio.
textValue string
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the Menubar.ItemLabel part if provided, or fallback to the .textContent of the Menubar.Item. Use this when the content is complex, or you have non-textual content inside.
disabled boolean
Whether the item is disabled or not.
closeOnSelect boolean
Whether the menu should close when the item is checked.
onSelect () => void
Event handler called when the user selects an item (via mouse or keyboard).
Data attribute Description
data-disabled Present when the item is disabled.
data-checked Present when the item is checked.
data-highlighted Present when the item is highlighted.
Prop Description
checked boolean
The controlled checked state of the item.
defaultChecked boolean
The default checked state when initially rendered. Useful when you do not need to control the checked state.
onChange (checked: boolean) => void
Event handler called when the checked state of the item changes.
textValue string
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the Menubar.ItemLabel part if provided, or fallback to the .textContent of the Menubar.Item. Use this when the content is complex, or you have non-textual content inside.
indeterminate boolean
Whether the item is in an indeterminate state.
disabled boolean
Whether the item is disabled or not.
closeOnSelect boolean
Whether the menu should close when the item is checked/unchecked.
onSelect () => void
Event handler called when the user selects an item (via mouse or keyboard).
Data attribute Description
data-disabled Present when the item is disabled.
data-indeterminate Present when the item is in an indeterminate state.
data-checked Present when the item is checked.
data-highlighted Present when the item is highlighted.
Prop Description
open boolean
The controlled open state of the sub menu.
defaultOpen boolean
The default open state when initially rendered. Useful when you do not need to control the open state.
onOpenChange (open: boolean) => void
Event handler called when the open state of the sub menu changes.

Menubar.Sub는 Menubar.SubContent의 배치(placement)를 커스텀하기 위해 다음의 prop들을 추가로 받아요.

Prop Description
getAnchorRect (anchor?: HTMLElement) => AnchorRect | undefined
Function that returns the trigger element's DOMRect.
gutter number
The distance between the sub menu content and the trigger element. By default, it's 0 plus half of the arrow offset, if it exists.
shift number
The skidding of the sub menu content along the trigger element.
slide boolean
Whether the sub menu content should slide when it overflows.
overlap boolean
Whether the sub menu content can overlap the trigger element when it overflows.
fitViewport boolean
Whether the sub menu content should fit the viewport. If this is set to true, the sub menu content will have maxWidth and maxHeight set to the viewport size. This will be exposed to CSS as --kb-popper-available-width and --kb-popper-available-height.
hideWhenDetached boolean
Whether to hide the sub menu content when the trigger element becomes occluded.
detachedPadding number
The minimum padding in order to consider the trigger element occluded.
arrowPadding number
The minimum padding between the arrow and the sub menu content corner.
overflowPadding number
The minimum padding between the sub menu content and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.
Prop Description
textValue string
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the Menubar.SubTrigger. Use this when the content is complex, or you have non-textual content inside.
disabled boolean
Whether the sub menu trigger is disabled or not.
Data attribute Description
data-disabled Present when the item is disabled.
data-highlighted Present when the item is highlighted.
Prop Description
onEscapeKeyDown (event: KeyboardEvent) => void
Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside (event: PointerDownOutsideEvent) => void
Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside (event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside (event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

Rendered elements (렌더링되는 요소)

Component Default rendered element
Menubar div
Menubar.Menu none
Menubar.Trigger div
Menubar.Icon div
Menubar.Portal Portal
Menubar.Content div
Menubar.Arrow div
Menubar.Separator hr
Menubar.Group div
Menubar.GroupLabel span
Menubar.Sub none
Menubar.SubTrigger div
Menubar.SubContent div
Menubar.Item div
Menubar.ItemLabel div
Menubar.ItemDescription div
Menubar.ItemIndicator div
Menubar.RadioGroup div
Menubar.RadioItem div
Menubar.CheckboxItem div

Accessibility (접근성)

Keyboard Interactions (키보드 상호작용)

Key Description
Space When focus is on an item, activates the item.
Enter When focus is on an item, activates the item.
ArrowDown When focus is on an item, moves focus to the next item.
ArrowUp When focus is on an item, moves focus to the previous item.
ArrowRight When focus is on an item (not sub menu trigger), moves focus to the next menu.
ArrowLeft When focus is on an item (not sub menu item), moves focus to the previous menu.
ArrowRight / ArrowLeft When focus is on a sub menu trigger, opens or closes the submenu depending on reading direction.
Home When focus is on an item, moves focus to first item.
End When focus is on an item, moves focus to last item.
Esc Closes the menu.

더 알아보기 (Learn more)