Components
Navigation Menu
A collection of links and menus for website navigation.
Anatomy#
Import the component and assemble its parts:
import { NavigationMenu } from '@egoist/solid-base/navigation-menu';
<NavigationMenu.Root>
<NavigationMenu.List>
<NavigationMenu.Item>
<NavigationMenu.Trigger>
<NavigationMenu.Icon />
</NavigationMenu.Trigger>
<NavigationMenu.Content>
<NavigationMenu.Link />
</NavigationMenu.Content>
</NavigationMenu.Item>
</NavigationMenu.List>
<NavigationMenu.Portal>
<NavigationMenu.Backdrop />
<NavigationMenu.Positioner>
<NavigationMenu.Popup>
<NavigationMenu.Arrow />
<NavigationMenu.Viewport />
</NavigationMenu.Popup>
</NavigationMenu.Positioner>
</NavigationMenu.Portal>
</NavigationMenu.Root>;Examples#
Nested submenus#
A <NavigationMenu.Root> can be nested within a higher-level <NavigationMenu.Content> part to
create a multi-level navigation menu.
Nested inline submenus#
For second-level navigation that should stay in the same panel, omit the nested
<NavigationMenu.Portal> and render only List + Viewport with a defaultValue.
Custom links#
<NavigationMenu.Link> renders an <a> element. Use the render prop to render your router's
link component instead, so navigation happens on the client:
import { A } from '@solidjs/router';
import { NavigationMenu } from '@egoist/solid-base/navigation-menu';
function Link(props: NavigationMenu.Link.Props) {
return (
<NavigationMenu.Link
{...props}
// `linkProps` carries `href`, the event handlers and the ARIA attributes.
render={(linkProps) => <A {...linkProps} href={props.href ?? ''} />}
/>
);
}Links don't close the menu when clicked by default; pass closeOnClick to close it, and active
to mark the link of the current page (it sets aria-current="page" and data-active).
Large menus#
When you have large menu content that doesn't fit in the viewport in some cases, you usually have two choices:
- Compress the navigation menu content
You can change the layout of the navigation menu to render less content or be more compact by
reducing the space it takes up. If your content is flexible, you can use the max-height
property on .Popup to limit the height of the navigation menu to let it compress itself while
preventing overflow.
.Content,
.Popup {
max-height: var(--available-height);
}- Make the navigation menu scrollable
.Content,
.Popup {
max-height: var(--available-height);
}
.Content {
overflow-y: auto;
}Native scrollbars are visible while transitioning content, so we recommend using the
Scroll Area component instead of native scrollbars to keep them
hidden, which also allows the Arrow to be centered correctly.
Closing animations#
The popup stays rendered until its closing animation finishes, so CSS transitions and animations
driven by [data-ending-style] work without extra setup.
To animate the closing with JavaScript instead, take control of the unmount: call
eventDetails.preventUnmountOnClose() in onValueChange when the value becomes null, then call
the unmount action once your animation is done. Close the menu with the close action instead
of setting value to null directly.
const actionsRef: { current: NavigationMenu.Root.Actions | null } = { current: null };
<NavigationMenu.Root
actionsRef={actionsRef}
onValueChange={(value, eventDetails) => {
if (value === null) {
eventDetails.preventUnmountOnClose();
animateOut().then(() => actionsRef.current?.unmount());
}
}}
>
...
</NavigationMenu.Root>;API reference#
<NavigationMenu.Root>
Groups all parts of the navigation menu. Renders a <nav> element at the root, or <div> element when nested.
actionsRefActionsRef<NavigationMenuRoot.Actions>—A ref to imperative actions. - unmount: Ends the closing phase of the navigation menu popup after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onValueChange first, otherwise the navigation menu popup completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the navigation menu imperatively when called.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the navigation menu is closed.
valueValue | nullnullThe controlled value of the navigation menu item that should be currently open. When non-nullish, the menu will be open. When nullish, the menu will be closed.
To render an uncontrolled navigation menu, use the defaultValue prop instead.
defaultValueValue | nullnullThe uncontrolled value of the item that should be initially selected.
To render a controlled navigation menu, use the value prop instead.
onValueChange| ((value: Value | null, eventDetails: NavigationMenuRoot.ChangeEventDetails) => void)—Callback fired when the value changes.
delaynumber50How long to wait before opening the navigation popup. Specified in milliseconds.
closeDelaynumber50How long to wait before closing the navigation popup. Specified in milliseconds.
orientation'horizontal' | 'vertical''horizontal'The orientation of the navigation menu.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Trigger>
Opens the navigation menu popup when hovered or clicked, revealing the associated content. Renders a <button> element.
disabledbooleanfalseWhether the component should ignore user interaction.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the trigger is disabled.
<NavigationMenu.List>
Contains a list of navigation menu items. Renders a <ul> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Item>
An individual navigation menu item. Renders a <li> element.
valueany—A unique value that identifies this navigation menu item. If no value is provided, a unique ID will be generated automatically. Use when controlling the navigation menu programmatically.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Content>
A container for the content of the navigation menu item that is moved into the popup when the item is active. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the content mounted in the DOM while the popup is closed. Ensures the content is present during server-side rendering for web crawlers.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-activation-directionWhich direction another trigger was activated from.
<NavigationMenu.Portal>
A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerPortalContainer—A parent element to render the portal element into.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Positioner>
Positions the navigation menu against the currently active trigger. Renders a <div> element.
anchor| Element | null | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)—An element to position the popup against. By default, the popup will be positioned against the trigger.
Accepts an element, a virtual element, a ref object, or a function (such as a signal accessor) that returns an element or a virtual element.
positionMethod'absolute' | 'fixed''absolute'Determines which CSS position property to use.
sideSide'bottom'Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
sideOffsetnumber | OffsetFunction0Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
alignAlign'center'How to align the popup relative to the specified side.
alignOffsetnumber | OffsetFunction0Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
collisionBoundaryBoundary'clipping-ancestors'An element or a rectangle that delimits the area that the popup is confined to.
collisionPaddingPadding5Additional space to maintain from the edge of the collision boundary.
stickybooleanfalseWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.
arrowPaddingnumber5Minimum distance to maintain between the arrow and the edges of the popup.
Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
disableAnchorTrackingbooleanfalseWhether to disable the popup from tracking any layout shift of its positioning anchor.
collisionAvoidanceCollisionAvoidance—Determines how to handle collisions when positioning the popup.
side controls overflow on the preferred placement axis (top/bottom or left/right): - 'flip': keep the requested side when it fits; otherwise try the opposite side (top and bottom, or left and right). - 'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible. - 'none': do not correct side-axis overflow.
align controls overflow on the alignment axis (start/center/end): - 'flip': keep side, but swap start and end when the requested alignment overflows. - 'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit. - 'none': do not correct alignment-axis overflow.
fallbackAxisSide controls fallback behavior on the perpendicular axis when the preferred axis cannot fit: - 'start': allow perpendicular fallback and try the logical start side first (top before bottom, or left before right in LTR). - 'end': allow perpendicular fallback and try the logical end side first (bottom before top, or right before left in LTR). - 'none': do not fallback to the perpendicular axis.
When side is 'shift', explicitly setting align only supports 'shift' or 'none'. If align is omitted, it defaults to 'flip'.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-instantPresent if animations should be instant.
--available-widthThe available width between the trigger and the edge of the viewport.
--available-heightThe available height between the trigger and the edge of the viewport.
--anchor-widthThe anchor's width.
--anchor-heightThe anchor's height.
--transform-originThe coordinates that this element is anchored to. Used for animations and transitions.
--positioner-widthThe fixed width of the positioner element.
--positioner-heightThe fixed height of the positioner element.
<NavigationMenu.Viewport>
The clipping viewport of the navigation menu's current content. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Backdrop>
A backdrop for the navigation menu popup. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<NavigationMenu.Popup>
A container for the navigation menu contents. Renders a <nav> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
--popup-widthThe fixed width of the popup element.
--popup-heightThe fixed height of the popup element.
<NavigationMenu.Arrow>
Displays an element pointing toward the navigation menu's current anchor. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-uncenteredPresent when the popup arrow is uncentered.
<NavigationMenu.Link>
A link in the navigation menu that can be used to navigate to a different page or section. Renders an <a> element.
activebooleanfalseWhether the link is the currently active page.
closeOnClickbooleanfalseWhether to close the navigation menu when the link is clicked.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-activePresent when the link is the currently active page.
<NavigationMenu.Icon>
An icon that indicates that the trigger button opens a menu.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.