Kobalte.v0.13.7

Navigation Menu

A collection of links for navigating websites.

Import

ts
import { NavigationMenu } from "@kobalte/core/navigation-menu";
// or
import { Root, Menu, ... } from "@kobalte/core/navigation-menu";
ts
import { NavigationMenu } from "@kobalte/core/navigation-menu";
// or
import { Root, Menu, ... } from "@kobalte/core/navigation-menu";

Features

  • Follows the WAI ARIA Navigation Menubar design pattern.
  • Supports modal and non-modal modes.
  • Supports submenus.
  • Supports items, labels, groups of items.
  • Supports checkable items (single or multiple) with optional indeterminate state.
  • Support disabled items.
  • Complex item labeling support for accessibility.
  • Keyboard opening and navigation support.
  • Automatic scrolling support during keyboard navigation.
  • Typeahead to allow focusing items by typing text.
  • Optionally render a pointing arrow.
  • Focus is fully managed.

Anatomy

The navigation menu consists of:

  • NavigationMenu: The root container for a NavigationMenu.
  • NavigationMenu.Viewport: Contains the menu rendered by the open menu.
  • NavigationMenu.Arrow: An optional arrow element to render alongside the viewport.

The menu consists of:

  • NavigationMenu.Menu: The container of each menu.
  • NavigationMenu.Trigger: The button that toggles the menu.
  • NavigationMenu.Icon: A small icon that can be displayed inside the menu trigger as a visual affordance for the fact it can be open.
  • NavigationMenu.Portal: Portals its children into the NavigationMenu.Viewport when the menu is open.
  • NavigationMenu.Content: Contains the content to be rendered when the menu is open.
  • NavigationMenu.Separator: Used to visually separate items in the menu.
  • NavigationMenu.Group: Used to group multiple items. Use in conjunction with NavigationMenu.GroupLabel to ensure good accessibility via automatic labelling.
  • NavigationMenu.GroupLabel: Used to render the label of a group. It won't be focusable using arrow keys.
  • NavigationMenu.Sub: Contains all the parts of a submenu.
  • NavigationMenu.SubTrigger: An item that opens a submenu. Must be rendered inside NavigationMenu.Sub.
  • NavigationMenu.SubContent: The component that pops out when a submenu is open. Must be rendered inside NavigationMenu.Sub.

The menu item consists of:

  • NavigationMenu.Item: An item of the select.
  • NavigationMenu.ItemLabel: An accessible label to be announced for the item.
  • NavigationMenu.ItemDescription: An optional accessible description to be announced for the item.
  • NavigationMenu.ItemIndicator: The visual indicator rendered when the item is checked.

The checkable menu item consists of:

  • NavigationMenu.RadioGroup: Used to group multiple NavigationMenu.RadioItems and manage the selection.
  • NavigationMenu.RadioItem: An item that can be controlled and rendered like a radio.
  • NavigationMenu.CheckboxItem: An item that can be controlled and rendered like a checkbox.
tsx
<NavigationMenu>
<NavigationMenu.Menu>
<NavigationMenu.Trigger>
<NavigationMenu.Icon />
</NavigationMenu.Trigger>
<NavigationMenu.Portal>
<NavigationMenu.Content>
<NavigationMenu.Item>
<NavigationMenu.ItemLabel />
<NavigationMenu.ItemDescription />
</NavigationMenu.Item>
<NavigationMenu.Group>
<NavigationMenu.GroupLabel />
<NavigationMenu.Item />
</NavigationMenu.Group>
<NavigationMenu.CheckboxItem>
<NavigationMenu.ItemIndicator />
</NavigationMenu.CheckboxItem>
<NavigationMenu.RadioGroup>
<NavigationMenu.RadioItem>
<NavigationMenu.ItemIndicator />
</NavigationMenu.RadioItem>
</NavigationMenu.RadioGroup>
<NavigationMenu.Sub>
<NavigationMenu.SubTrigger />
<NavigationMenu.Portal>
<NavigationMenu.SubContent />
</NavigationMenu.Portal>
</NavigationMenu.Sub>
<NavigationMenu.Separator />
</NavigationMenu.Content>
</NavigationMenu.Portal>
</NavigationMenu.Menu>
<NavigationMenu.Viewport>
<NavigationMenu.Arrow />
</NavigationMenu.Viewport>
</NavigationMenu>
tsx
<NavigationMenu>
<NavigationMenu.Menu>
<NavigationMenu.Trigger>
<NavigationMenu.Icon />
</NavigationMenu.Trigger>
<NavigationMenu.Portal>
<NavigationMenu.Content>
<NavigationMenu.Item>
<NavigationMenu.ItemLabel />
<NavigationMenu.ItemDescription />
</NavigationMenu.Item>
<NavigationMenu.Group>
<NavigationMenu.GroupLabel />
<NavigationMenu.Item />
</NavigationMenu.Group>
<NavigationMenu.CheckboxItem>
<NavigationMenu.ItemIndicator />
</NavigationMenu.CheckboxItem>
<NavigationMenu.RadioGroup>
<NavigationMenu.RadioItem>
<NavigationMenu.ItemIndicator />
</NavigationMenu.RadioItem>
</NavigationMenu.RadioGroup>
<NavigationMenu.Sub>
<NavigationMenu.SubTrigger />
<NavigationMenu.Portal>
<NavigationMenu.SubContent />
</NavigationMenu.Portal>
</NavigationMenu.Sub>
<NavigationMenu.Separator />
</NavigationMenu.Content>
</NavigationMenu.Portal>
</NavigationMenu.Menu>
<NavigationMenu.Viewport>
<NavigationMenu.Arrow />
</NavigationMenu.Viewport>
</NavigationMenu>

Example

Orientation

Usage

Viewport size

Since the viewport content has a position of absolute to allow animating, Kobalte exports a custom CSS variable --kb-navigation-menu-viewport-height and --kb-navigation-menu-viewport-width. The height and width will be copied from the active NavigationMenu.Content, which can either be set using CSS or automatically calculated based on it's content. Combined with a transition CSS rule the size can be animated.

css
/* style.css */
.navigation-menu__viewport {
width: var(--kb-navigation-menu-viewport-width);
height: var(--kb-navigation-menu-viewport-height);
transition:
width,
height,
250ms ease;
}
css
/* style.css */
.navigation-menu__viewport {
width: var(--kb-navigation-menu-viewport-width);
height: var(--kb-navigation-menu-viewport-height);
transition:
width,
height,
250ms ease;
}

To make one of the navigation menu triggers a link instead of opening a menu, omit the NavigationMenu.Menu and set as="a" on the NavigationMenu.Trigger.

tsx
<NavigationMenu>
<NavigationMenu.Menu>
<NavigationMenu.Trigger>Opens a regular menu</NavigationMenu.Trigger>
<NavigationMenu.Portal>
<NavigationMenu.Content>...</NavigationMenu.Content>
</NavigationMenu.Portal>
</NavigationMenu.Menu>
<NavigationMenu.Trigger as="a" href="https://kobalte.dev">
Opens a link on click and enter
</NavigationMenu.Trigger>
<NavigationMenu.Viewport>
<NavigationMenu.Arrow />
</NavigationMenu.Viewport>
</NavigationMenu>
tsx
<NavigationMenu>
<NavigationMenu.Menu>
<NavigationMenu.Trigger>Opens a regular menu</NavigationMenu.Trigger>
<NavigationMenu.Portal>
<NavigationMenu.Content>...</NavigationMenu.Content>
</NavigationMenu.Portal>
</NavigationMenu.Menu>
<NavigationMenu.Trigger as="a" href="https://kobalte.dev">
Opens a link on click and enter
</NavigationMenu.Trigger>
<NavigationMenu.Viewport>
<NavigationMenu.Arrow />
</NavigationMenu.Viewport>
</NavigationMenu>

Origin-aware animations

We expose a CSS custom property --kb-menu-content-transform-origin which can be used to animate the viewport from its computed origin.

css
/* style.css */
.navigation-menu__viewport {
transform-origin: var(--kb-menu-content-transform-origin);
animation: viewportHide 250ms ease-in forwards;
}
.navigation-menu__viewport[data-expanded] {
animation: viewportShow 250ms ease-out;
}
@keyframes viewportShow {
from {
opacity: 0;
transform: rotateX(-20deg) scale(0.96);
}
to {
opacity: 1;
transform: rotateX(0deg) scale(1);
}
}
@keyframes viewportHide {
from {
opacity: 1;
transform: rotateX(0deg) scale(1);
}
to {
opacity: 0;
transform: rotateX(-10deg) scale(0.96);
}
}
css
/* style.css */
.navigation-menu__viewport {
transform-origin: var(--kb-menu-content-transform-origin);
animation: viewportHide 250ms ease-in forwards;
}
.navigation-menu__viewport[data-expanded] {
animation: viewportShow 250ms ease-out;
}
@keyframes viewportShow {
from {
opacity: 0;
transform: rotateX(-20deg) scale(0.96);
}
to {
opacity: 1;
transform: rotateX(0deg) scale(1);
}
}
@keyframes viewportHide {
from {
opacity: 1;
transform: rotateX(0deg) scale(1);
}
to {
opacity: 0;
transform: rotateX(-10deg) scale(0.96);
}
}

Content animations

For animating the content of the viewport we apply the attribute data-motion of type "to-start" | "to-end" | "from-start" | "from-end" on the NavigationMenu.Content.

css
/* style.css */
.navigation-menu__content[data-motion="from-start"] {
animation-name: enterFromLeft;
}
.navigation-menu__content[data-motion="from-end"] {
animation-name: enterFromRight;
}
.navigation-menu__content[data-motion="to-start"] {
animation-name: exitToLeft;
}
.navigation-menu__content[data-motion="to-end"] {
animation-name: exitToRight;
}
@keyframes enterFromRight {
from {
opacity: 0;
transform: translateX(200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes enterFromLeft {
from {
opacity: 0;
transform: translateX(-200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes exitToRight {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(200px);
}
}
@keyframes exitToLeft {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(-200px);
}
}
css
/* style.css */
.navigation-menu__content[data-motion="from-start"] {
animation-name: enterFromLeft;
}
.navigation-menu__content[data-motion="from-end"] {
animation-name: enterFromRight;
}
.navigation-menu__content[data-motion="to-start"] {
animation-name: exitToLeft;
}
.navigation-menu__content[data-motion="to-end"] {
animation-name: exitToRight;
}
@keyframes enterFromRight {
from {
opacity: 0;
transform: translateX(200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes enterFromLeft {
from {
opacity: 0;
transform: translateX(-200px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes exitToRight {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(200px);
}
}
@keyframes exitToLeft {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(-200px);
}
}

API Reference

NavigationMenu is equivalent to the Root import from @kobalte/core/NavigationMenu.

PropDescription
defaultValuestring
The value of the menu that should be open when initially rendered. Use when you do not need to control the value state.
valuestring
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.
loopboolean
When true, keyboard navigation will loop from last item to first, and vice versa.
delayDurationnumber
Delay before the menu opens on hover (default 200).
skipDelayDurationboolean
Open immediately if hovered again within delay (default 300).
loopboolean
When true, keyboard navigation will loop from last item to first, and vice versa.
focusOnAltboolean
When true, click on alt by itsef will focus this NavigationMenu (some browsers interfere).
forceMountboolean
Used to force mounting the menu (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
PropDescription
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.
PropDescription
sizenumber
The size of the arrow.
PropDescription
onOpenChange(open: boolean) => void
Event handler called when the open state of the menu changes.
idstring
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.
modalboolean
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.
preventScrollboolean
Whether the scroll should be locked even if the menu is not modal.
forceMountboolean
Used to force mounting the menu (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
valuestring
A unique value that associates the item with an active value when the navigation menu is controlled. This prop is managed automatically when uncontrolled.

NavigationMenu.Menu also accepts the following props to customize the placement of the NavigationMenu.Content.

PropDescription
placementPlacement
The placement of the menu content.
gutternumber
The distance between the menu content and the trigger element. By default, it's 0 plus half of the arrow offset, if it exists.
shiftnumber
The skidding of the menu content along the trigger element.
flipboolean | 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".
slideboolean
Whether the menu content should slide when it overflows.
overlapboolean
Whether the menu content can overlap the trigger element when it overflows.
sameWidthboolean
Whether the menu content should have the same width as the trigger element. This will be exposed to CSS as --kb-popper-anchor-width.
fitViewportboolean
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.
hideWhenDetachedboolean
Whether to hide the menu content when the trigger element becomes occluded.
detachedPaddingnumber
The minimum padding in order to consider the trigger element occluded.
arrowPaddingnumber
The minimum padding between the arrow and the menu content corner.
overflowPaddingnumber
The minimum padding between the menu content and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.
PropDescription
disabledboolean
Whether the context menu trigger is disabled or not.
Data attributeDescription
data-expandedPresent when the menu is open.
data-closedPresent when the menu is close.
data-disabledPresent when the trigger is disabled.

NavigationMenu.Icon, NavigationMenu.Content, NavigationMenu.SubTrigger and NavigationMenu.SubContent share the same data-expanded attribute.

PropDescription
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.
PropDescription
textValuestring
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the NavigationMenu.ItemLabel part if provided, or fallback to the .textContent of the NavigationMenu.Item. Use this when the content is complex, or you have non-textual content inside.
disabledboolean
Whether the item is disabled or not.
closeOnSelectboolean
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 attributeDescription
data-disabledPresent when the item is disabled.
data-highlightedPresent when the item is highlighted.

NavigationMenu.ItemLabel, NavigationMenu.ItemDescription and NavigationMenu.ItemIndicator shares the same data-attributes.

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.
PropDescription
valuestring
The controlled value of the menu radio item to check.
defaultValuestring
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.
disabledboolean
Whether the radio group is disabled or not.
PropDescription
valuestring
The value of the menu item radio.
textValuestring
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the NavigationMenu.ItemLabel part if provided, or fallback to the .textContent of the NavigationMenu.Item. Use this when the content is complex, or you have non-textual content inside.
disabledboolean
Whether the item is disabled or not.
closeOnSelectboolean
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 attributeDescription
data-disabledPresent when the item is disabled.
data-checkedPresent when the item is checked.
data-highlightedPresent when the item is highlighted.
PropDescription
checkedboolean
The controlled checked state of the item.
defaultCheckedboolean
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.
textValuestring
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the NavigationMenu.ItemLabel part if provided, or fallback to the .textContent of the NavigationMenu.Item. Use this when the content is complex, or you have non-textual content inside.
indeterminateboolean
Whether the item is in an indeterminate state.
disabledboolean
Whether the item is disabled or not.
closeOnSelectboolean
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 attributeDescription
data-disabledPresent when the item is disabled.
data-indeterminatePresent when the item is in an indeterminate state.
data-checkedPresent when the item is checked.
data-highlightedPresent when the item is highlighted.
PropDescription
openboolean
The controlled open state of the sub menu.
defaultOpenboolean
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.

NavigationMenu.Sub also accepts the following props to customize the placement of the NavigationMenu.SubContent.

PropDescription
getAnchorRect(anchor?: HTMLElement) => AnchorRect | undefined
Function that returns the trigger element's DOMRect.
gutternumber
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.
shiftnumber
The skidding of the sub menu content along the trigger element.
slideboolean
Whether the sub menu content should slide when it overflows.
overlapboolean
Whether the sub menu content can overlap the trigger element when it overflows.
fitViewportboolean
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.
hideWhenDetachedboolean
Whether to hide the sub menu content when the trigger element becomes occluded.
detachedPaddingnumber
The minimum padding in order to consider the trigger element occluded.
arrowPaddingnumber
The minimum padding between the arrow and the sub menu content corner.
overflowPaddingnumber
The minimum padding between the sub menu content and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.
PropDescription
textValuestring
Optional text used for typeahead purposes. By default, the typeahead behavior will use the .textContent of the NavigationMenu.SubTrigger. Use this when the content is complex, or you have non-textual content inside.
disabledboolean
Whether the sub menu trigger is disabled or not.
Data attributeDescription
data-disabledPresent when the item is disabled.
data-highlightedPresent when the item is highlighted.
PropDescription
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

ComponentDefault rendered element
NavigationMenuul
NavigationMenu.Viewportli
NavigationMenu.Arrowdiv
NavigationMenu.Menunone
NavigationMenu.Triggerdiv
NavigationMenu.Icondiv
NavigationMenu.PortalPortal
NavigationMenu.Contentul
NavigationMenu.Separatorhr
NavigationMenu.Groupdiv
NavigationMenu.GroupLabelspan
NavigationMenu.Subnone
NavigationMenu.SubTriggerdiv
NavigationMenu.SubContentdiv
NavigationMenu.Itema
NavigationMenu.ItemLabeldiv
NavigationMenu.ItemDescriptiondiv
NavigationMenu.ItemIndicatordiv
NavigationMenu.RadioGroupdiv
NavigationMenu.RadioItemdiv
NavigationMenu.CheckboxItemdiv

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on an item, activates the item.
EnterWhen focus is on an item, activates the item.
ArrowDownWhen focus is on an item, moves focus to the next item.
ArrowUpWhen focus is on an item, moves focus to the previous item.
ArrowRightWhen focus is on an item (not sub menu trigger), moves focus to the next menu.
ArrowLeftWhen focus is on an item (not sub menu item), moves focus to the previous menu.
ArrowRight / ArrowLeftWhen focus is on a sub menu trigger, opens or closes the submenu depending on reading direction.
HomeWhen focus is on an item, moves focus to first item.
EndWhen focus is on an item, moves focus to last item.
EscCloses the menu.