Skip to content

Navs Vertical

A vertical navigation component for sidebar menus with support for icons, arrows, and expandable sub-items. Easily create hierarchical navigation structures with a clean, modern design.

Installation

Add Navs Vertical to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add navs-vertical
Adds
NavsVertical.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
react-iconsif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install react-icons

Create or update your NavsVertical component

Copy this code to create your NavsVertical component

/components/ui/NavsVertical.tsx
import * as React from "react";
import Link from "next/link";
import { RiArrowRightSLine } from "react-icons/ri";
import { cn } from "@/lib/utils";

/** Assigns a node to several refs (object or callback). */
function composeRefs<T>(...refs: Array<React.Ref<T> | undefined>): React.RefCallback<T> {
    return (node) => {
        for (const ref of refs) {
            if (typeof ref === "function") ref(node);
            else if (ref) ref.current = node;
        }
    };
}

// Server-compatible: no hooks or context, so NavsVertical can be rendered from server components.

// =========== Root ===========
export interface NavsVerticalRootProps extends React.HTMLAttributes<HTMLElement> {
    /** @deprecated Has no effect — mark the current item with `active` instead. */
    defaultValue?: string;
}

/** The navigation landmark. Give it an `aria-label` when the page has more than one navigation. */
const NavsVerticalRoot = React.forwardRef<HTMLElement, NavsVerticalRootProps>(
    // `defaultValue` is accepted for backward compatibility but must not reach the DOM.
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    ({ className, children, defaultValue: _defaultValue, ...props }, ref) => {
        return (
            <nav ref={ref} className={cn("w-full", className)} {...props}>
                {children}
            </nav>
        );
    }
);
NavsVerticalRoot.displayName = "NavsVertical.Root";

// =========== List ===========
const NavsVerticalList = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(({ className, ...props }, ref) => {
    return <div ref={ref} className={cn("space-y-2 w-full", className)} {...props} />;
});
NavsVerticalList.displayName = "NavsVertical.List";

// =========== Icon rendering ===========
type IconElementProps = React.HTMLAttributes<Element> & { ref?: React.Ref<Element> };

const isComponentType = (value: unknown): value is React.ElementType =>
    typeof value === "function" || (typeof value === "object" && value !== null && "$$typeof" in value && !React.isValidElement(value));

/** Renders an icon passed as an element (`as={<Icon />}`), a component (`as={Icon}`) or as children, merging classes and the ref. */
function renderIcon(source: React.ReactNode | React.ElementType, iconClassName: string, ref: React.ForwardedRef<Element> | undefined, props: React.HTMLAttributes<Element>) {
    if (React.isValidElement<IconElementProps>(source)) {
        return React.cloneElement(source, {
            "aria-hidden": true,
            ...props,
            className: cn(iconClassName, source.props.className),
            ref: composeRefs(source.props.ref, ref ?? undefined),
        });
    }
    if (isComponentType(source)) {
        const Component = source;
        return <Component ref={ref} aria-hidden="true" {...props} className={iconClassName} />;
    }
    return source ?? null;
}

export interface NavsVerticalIconProps extends React.HTMLAttributes<HTMLElement> {
    /** Icon element (`as={<RiUserLine />}`) or component (`as={RiUserLine}`). Falls back to `children`. */
    as?: React.ElementType | React.ReactNode;
}

// =========== Icon ===========
const NavsVerticalIcon = React.forwardRef<Element, NavsVerticalIconProps>(({ className, as, children, ...props }, ref) => {
    return renderIcon(as ?? children, cn("size-5 text-muted-foreground transition duration-base ease-out group-data-[active=true]/nav-item:text-primary-strong", className), ref, props);
});
NavsVerticalIcon.displayName = "NavsVertical.Icon";

// =========== Arrow ===========
// A <span> (phrasing content) so it is valid inside the item's <a> or <button>.
const NavsVerticalArrow = React.forwardRef<HTMLSpanElement, NavsVerticalIconProps>(({ className, as, children, ...props }, ref) => {
    const icon = renderIcon(as ?? children ?? <RiArrowRightSLine />, "size-5 text-muted-foreground", undefined, {});

    return (
        <span
            ref={ref}
            aria-hidden="true"
            className={cn(
                "ml-auto p-px text-muted-foreground rounded-full bg-surface dark:bg-border opacity-0 shadow-regular scale-75 transition ease-out",
                "group-data-[active=true]/nav-item:scale-100 group-data-[active=true]/nav-item:opacity-100",
                "group-focus/nav-item:scale-100 group-focus/nav-item:opacity-100",
                "group-active/nav-item:scale-100 group-active/nav-item:opacity-100",
                className
            )}
            {...props}
        >
            {icon}
        </span>
    );
});
NavsVerticalArrow.displayName = "NavsVertical.Arrow";

// =========== Item ===========
export interface NavsVerticalItemProps extends React.HTMLAttributes<HTMLElement> {
    /** Renders a Next.js `<Link>`; without it the item is a `<button type="button">`. */
    href?: string;
    /** Marks the item as current: active styling plus `aria-current` ("page" for links, "true" otherwise). */
    active?: boolean;
    /** Appends `NavsVertical.Arrow` after the label. */
    arrow?: boolean;
}

const itemFocusClasses = "focus-ring-inset focus:bg-muted focus:text-foreground";

const NavsVerticalItem = React.forwardRef<HTMLElement, NavsVerticalItemProps>(({ className, children, href, active = false, arrow = false, ...props }, ref) => {
    const hasArrow = React.Children.toArray(children).some((child) => React.isValidElement(child) && child.type === NavsVerticalArrow);

    const classes = cn(
        // base
        "group/nav-item w-full rounded-lg p-2 text-left size-label-sm font-medium text-muted-foreground",
        "transition duration-base ease-out",
        "no-underline",
        "grid auto-cols-auto grid-flow-col grid-cols-[auto,minmax(0,1fr)] items-center gap-1.5",
        // hover
        "hover:bg-muted",
        // focus: keyboard ring plus the existing focus background
        itemFocusClasses,
        // active (pressed)
        "active:bg-muted active:text-foreground",
        // active (current item)
        active && "bg-muted text-foreground",
        className
    );

    const content = (
        <>
            {children}
            {arrow && !hasArrow && <NavsVerticalArrow />}
        </>
    );

    if (href) {
        return (
            <Link href={href} className={classes} data-active={active} aria-current={active ? "page" : undefined} ref={ref as React.Ref<HTMLAnchorElement>} {...props}>
                {content}
            </Link>
        );
    }

    return (
        <button type="button" className={classes} data-active={active} aria-current={active ? "true" : undefined} ref={ref as React.Ref<HTMLButtonElement>} {...props}>
            {content}
        </button>
    );
});
NavsVerticalItem.displayName = "NavsVertical.Item";

// =========== SubList ===========
const NavsVerticalSubList = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(({ className, ...props }, ref) => {
    return (
        <div
            ref={ref}
            className={cn(
                "pl-4 mt-1 space-y-0.5", // Add padding to create hierarchy and spacing
                className
            )}
            {...props}
        />
    );
});
NavsVerticalSubList.displayName = "NavsVertical.SubList";

// =========== SubItem ===========
export interface NavsVerticalSubItemProps extends React.HTMLAttributes<HTMLElement> {
    /** Renders a Next.js `<Link>`; without it the sub-item is a `<button type="button">`. */
    href?: string;
    /** Marks the sub-item as current: active styling plus `aria-current` ("page" for links, "true" otherwise). */
    active?: boolean;
}

const NavsVerticalSubItem = React.forwardRef<HTMLElement, NavsVerticalSubItemProps>(({ className, children, href, active = false, ...props }, ref) => {
    const classes = cn(
        // base
        "w-full rounded-lg py-1.5 px-3 text-left size-label-sm font-medium text-muted-foreground",
        "transition duration-base ease-out",
        "flex items-center",
        "no-underline",
        // hover
        "hover:bg-muted",
        // focus: keyboard ring plus the existing focus background
        itemFocusClasses,
        // active (pressed)
        "active:bg-muted active:text-foreground",
        // active (current item)
        active && "bg-muted text-foreground",
        className
    );

    if (href) {
        return (
            <Link href={href} className={classes} data-active={active} aria-current={active ? "page" : undefined} ref={ref as React.Ref<HTMLAnchorElement>} {...props}>
                {children}
            </Link>
        );
    }

    return (
        <button type="button" className={classes} data-active={active} aria-current={active ? "true" : undefined} ref={ref as React.Ref<HTMLButtonElement>} {...props}>
            {children}
        </button>
    );
});
NavsVerticalSubItem.displayName = "NavsVertical.SubItem";

const NavsVertical = {
    Root: NavsVerticalRoot,
    List: NavsVerticalList,
    Item: NavsVerticalItem,
    SubItem: NavsVerticalSubItem,
    SubList: NavsVerticalSubList,
    Icon: NavsVerticalIcon,
    Arrow: NavsVerticalArrow,
};

export {
    NavsVertical,
    NavsVerticalRoot as Root,
    NavsVerticalList as List,
    NavsVerticalItem as Item,
    NavsVerticalSubItem as SubItem,
    NavsVerticalSubList as SubList,
    NavsVerticalIcon as Icon,
    NavsVerticalArrow as Arrow,
};

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

With Icon and Arrow

Enhanced navigation items with icons and arrow indicators for better visual hierarchy.

With Sub Items

Advanced navigation with expandable sections and nested sub-items using Accordion integration.

API Reference

The NavsVertical component is a compound component that consists of several parts. Each part has its own set of props that can be used to customize its appearance and behavior.

The container component that provides the main vertical navigation structure.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the root container (a <nav> landmark; pass aria-label when the page has several navigations).

defaultValue"string""undefined"

Deprecated: has no effect. Mark the current item with active instead.

children"ReactNode""undefined"

The content of the navigation component, typically NavsVertical.List.

A container for navigation items.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the list element.

children"ReactNode""undefined"

The content of the list, typically NavsVertical.Item components.

Individual navigation item that can be styled as active and can contain icons and arrows.

PropTypeDefaultDescription
href"string""undefined"

Renders the item as a Next.js Link to this URL. Without href the item is a button (type="button").

active"boolean""false"

Marks the current item: applies active styling and sets aria-current ("page" for links, "true" for buttons).

arrow"boolean""false"

When true, automatically adds an arrow icon to the item.

className"string""undefined"

Additional CSS classes to apply to the item.

children"ReactNode""undefined"

The content of the navigation item.

A container for nested navigation items, typically used within expandable sections.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the sub-list.

children"ReactNode""undefined"

The content of the sub-list, typically NavsVertical.SubItem components.

Individual nested navigation item with simplified styling for hierarchical navigation.

PropTypeDefaultDescription
href"string""undefined"

Renders the sub-item as a Next.js Link to this URL. Without href the sub-item is a button (type="button").

active"boolean""false"

Marks the current sub-item: applies active styling and sets aria-current ("page" for links, "true" for buttons).

className"string""undefined"

Additional CSS classes to apply to the sub-item.

children"ReactNode""undefined"

The content of the sub-item.

Icon component that can be used within a navigation item.

PropTypeDefaultDescription
as"ReactElement | ReactNode""undefined"

The icon component to render.

className"string""undefined"

Additional CSS classes to apply to the icon.

children"ReactNode""undefined"

The content of the icon, used if no 'as' prop is provided.

Arrow icon component that can be used within a navigation item, typically to indicate a dropdown or submenu.

PropTypeDefaultDescription
as"ReactElement | ReactNode""undefined"

The arrow icon component to render.

className"string""undefined"

Additional CSS classes to apply to the arrow icon.

children"ReactNode""undefined"

The content of the arrow icon, used if no 'as' prop is provided.