Skip to content

Navbar

A horizontal navigation bar with multiple styling variants for organizing links and actions. Supports icons, dropdown menus, and consistent visual hierarchy across your application.

Installation

Add Navbar 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 navbar
Adds
Navbar.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-slotif 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 @radix-ui/react-slot

Create or update your Navbar component

Copy this code to create your Navbar component

/components/ui/Navbar.tsx
import React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cn } from "@/lib/utils";

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

export type NavbarItemVariant = "default" | "stroke" | "ghost";

export interface NavbarRootProps extends React.HTMLAttributes<HTMLElement> {
    children: React.ReactNode;
}

export interface NavbarMenuProps extends React.HTMLAttributes<HTMLDivElement> {
    children: React.ReactNode;
}

type AnchorOnlyProps = Pick<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "target" | "rel" | "download" | "hrefLang" | "ping" | "referrerPolicy">;

export interface NavbarItemProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "className">, AnchorOnlyProps {
    children: React.ReactNode;
    /** Marks the item as the current page: active styling plus `aria-current` ("page" for links, "true" otherwise). */
    active?: boolean;
    /** Render the single child element (e.g. a Next.js `<Link>`) instead of the default element, merging Navbar.Item's props, classes and ref onto it. */
    asChild?: boolean;
    /** Renders an `<a href>`; without it the item is a `<button type="button">`. */
    href?: string;
    className?: string;
    variant?: NavbarItemVariant;
}

export interface ItemIconProps {
    as?: React.ReactElement<{ className?: string }>;
    className?: string;
}

// Define the extended type for NavbarItem with Icon
export type NavbarItemType = React.ForwardRefExoticComponent<NavbarItemProps & React.RefAttributes<HTMLButtonElement | HTMLAnchorElement>> & {
    Icon: React.FC<ItemIconProps>;
};

const navbarStyles = {
    default: "flex items-center gap-4 px-4 sm:px-6 lg:px-11 py-5 bg-surface border border-border",
};

const menuStyles = {
    default: "flex gap-1",
};

const itemStyles = {
    base: "group flex items-center no-underline gap-2 w-fit rounded-lg px-3 py-2 h-9 size-label-sm cursor-pointer transition-colors duration-base focus-ring disabled:cursor-not-allowed disabled:opacity-50",
    default: {
        normal: "text-muted-foreground hover:text-foreground",
        active: "bg-muted text-foreground",
    },
    stroke: {
        normal: "text-muted-foreground hover:text-foreground ring-1 ring-transparent",
        active: "ring-1 ring-border text-foreground",
    },
    ghost: {
        normal: "text-muted-foreground hover:text-foreground",
        active: "text-foreground",
    },
};

const iconStyles = "size-5 group-data-[state=active]:text-primary-strong";

/** The navigation landmark. Give it an `aria-label` when the page has more than one navigation. */
const Root = React.forwardRef<HTMLElement, NavbarRootProps>(({ className, children, ...props }, ref) => {
    return (
        <nav ref={ref} className={cn(navbarStyles.default, className)} {...props}>
            {children}
        </nav>
    );
});
Root.displayName = "Navbar.Root";

const Menu = React.forwardRef<HTMLDivElement, NavbarMenuProps>(({ className, children, ...props }, ref) => {
    return (
        <div ref={ref} className={cn(menuStyles.default, className)} {...props}>
            {children}
        </div>
    );
});
Menu.displayName = "Navbar.Menu";

const Icon: React.FC<ItemIconProps> = ({ as, className }) => {
    if (!as) return null;

    return React.cloneElement<{ className?: string; "aria-hidden"?: boolean }>(as, {
        "aria-hidden": true,
        className: cn(iconStyles, as.props.className, className),
    });
};
Icon.displayName = "Navbar.Icon";

const Item = React.forwardRef<HTMLButtonElement | HTMLAnchorElement, NavbarItemProps>(({ className, active = false, children, asChild = false, href, variant = "default", type, ...props }, ref) => {
    const variantStyles = itemStyles[variant] || itemStyles.default;
    const itemClasses = cn(itemStyles.base, variantStyles.normal, active && variantStyles.active, className);
    const state = active ? "active" : "inactive";

    if (asChild) {
        return (
            <Slot
                ref={ref}
                className={itemClasses}
                data-state={state}
                aria-current={active ? "page" : undefined}
                {...(href !== undefined ? { href } : {})}
                {...(type !== undefined ? { type } : {})}
                {...props}
            >
                {children}
            </Slot>
        );
    }

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

    return (
        <button ref={ref as React.Ref<HTMLButtonElement>} type={type ?? "button"} className={itemClasses} data-state={state} aria-current={active ? "true" : undefined} {...props}>
            {children}
        </button>
    );
}) as NavbarItemType;

// Attach the Icon component to Item
Item.Icon = Icon;
Item.displayName = "Navbar.Item";

// Create and export the Navbar namespace with properly typed components
const Navbar = {
    Root,
    Menu,
    Item,
    Icon,
};

export default Navbar;
export { Navbar, Root, Menu, Item, Icon };

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

Stroke Variant

Navbar items with border outlines for better visual separation.

Ghost Variant

Minimal styling with hover effects for secondary navigation.

Advanced Examples

Complete navbar examples with logos, icons, and dropdown menus.

API Reference

The Navbar 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 navbar structure. Renders a <nav> element.

PropTypeDefaultDescription
className"string""undefined"

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

children"ReactNode""undefined"

The content of the navbar.

A container for navbar items, typically used to group navigation links.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the navbar menu.

children"ReactNode""undefined"

The content of the navbar menu, typically Navbar.Item components.

Individual navigation item that can be styled in different variants and can contain icons.

PropTypeDefaultDescription
variant
"default""stroke""ghost"
"default"

The visual style variant of the navbar item.

active"boolean""false"

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

asChild"boolean""false"

Render the single child element (e.g. a Next.js Link) instead of the default element, merging the item's props, classes and ref onto it.

href"string""undefined"

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

className"string""undefined"

Additional CSS classes to apply to the navbar item.

children"ReactNode""undefined"

The content of the navbar item.

Icon component that can be used within a navbar item.

PropTypeDefaultDescription
as"ReactElement""undefined"

The icon element to render (rendered with aria-hidden; its own className is kept).

className"string""undefined"

Additional CSS classes to apply to the icon.