# Navbar **Category**: react **URL**: https://ui.darkcode.dev/en/docs/react/components/navbar **Source**: https://raw.githubusercontent.com/DarkCode-Developers/darkcode-ui/refs/heads/main/apps/docs/content/docs/en/react/components/(navigation)/navbar.mdx > A composable top navigation bar with hide-on-scroll, responsive mobile menu, and client-side routing support *** ## Import ```tsx import { Navbar } from '@darkcode-ui/react'; ``` ### Usage ```tsx "use client"; import {Button, Navbar} from "@darkcode-ui/react"; import {Sparkles} from "@gravity-ui/icons"; export function NavbarDefault() { return ( DarkCode Features Pricing Docs Blog ); } ``` ### Anatomy Import the Navbar component and access all parts using dot notation. ```tsx import { Navbar } from '@darkcode-ui/react'; export default () => ( ) ``` ### Docs Site A docs-site style navbar with a search bar, theme segment, and responsive mobile menu. ```tsx "use client"; import {Navbar, SearchField, ToggleButton, ToggleButtonGroup} from "@darkcode-ui/react"; import {Display, Moon, Sparkles, Sun} from "@gravity-ui/icons"; export function NavbarDocsSite() { return ( Docs Guide Components API Guide Components API ); } ``` ### With Dropdowns SaaS-style navbar with a team switcher, user dropdown, and mobile menu. ```tsx "use client"; import { Avatar, Button, Dropdown, InlineSelect, Label, ListBox, Navbar, Separator, } from "@darkcode-ui/react"; import {ArrowRightFromSquare, Gear, Person, Plus, Sparkles} from "@gravity-ui/icons"; const teams = [ {id: "acme", initials: "AC", name: "Acme Inc."}, {id: "globex", initials: "GX", name: "Globex Corp."}, {id: "umbrella", initials: "UM", name: "Umbrella LLC"}, ]; export function NavbarWithDropdowns() { return ( DarkCode {/* Team switcher */} AC {teams.map((team) => ( {team.initials} {team.name} ))} {/* User dropdown */} Dashboard Projects Settings ); } ``` ### Dashboard Breadcrumb-style dashboard navbar built with `InlineSelect` for the workspace, project, and timezone selectors. ```tsx "use client"; import {Button, InlineSelect, ListBox, Navbar} from "@darkcode-ui/react"; import {Bell, ChevronRight, Sparkles} from "@gravity-ui/icons"; const workspaces = [ {id: "acme", name: "Acme"}, {id: "globex", name: "Globex"}, ]; const projects = [ {id: "web", name: "Web App"}, {id: "mobile", name: "Mobile App"}, {id: "api", name: "API"}, ]; const timezones = [ {id: "utc", name: "UTC"}, {id: "est", name: "EST"}, {id: "pst", name: "PST"}, ]; const Crumb = ({ label, options, value, }: { label: string; options: {id: string; name: string}[]; value: string; }) => ( {options.map((option) => ( {option.name} ))} ); export function NavbarDashboard() { return ( {/* Breadcrumb-style selectors */}
); } ``` ### Compact A dense navbar tuned via `--spacing` and `height` overrides for a Luma-style compact layout. ```tsx "use client"; import type {CSSProperties} from "react"; import {Button, Navbar} from "@darkcode-ui/react"; import {Sparkles} from "@gravity-ui/icons"; export function NavbarCompact() { return ( Luma Explore Calendars Discover ); } ``` ### With Menu Responsive navbar with a mobile hamburger toggle and animated dropdown menu. ```tsx "use client"; import {Button, Navbar} from "@darkcode-ui/react"; import {Sparkles} from "@gravity-ui/icons"; export function NavbarWithMenu() { return ( DarkCode Features Pricing About {/* Mobile-only menu — animates in when the toggle is pressed */} Features Pricing About Contact ); } ``` ### Hide on Scroll Pass `hideOnScroll` to hide the navbar when the user scrolls down and reveal it on scroll up. ```tsx "use client"; import {Button, Navbar} from "@darkcode-ui/react"; import {Sparkles} from "@gravity-ui/icons"; import {useRef} from "react"; export function NavbarHideOnScroll() { const scrollRef = useRef(null); return (
DarkCode Features Pricing

Scroll down to hide the navbar, scroll up to reveal it.

{Array.from({length: 24}).map((_, index) => (
))}
); } ``` ### Centered Use `justify` on `Navbar.Content` to lay out the bar as brand · centered nav · actions without manual spacers. `justify="center"` lets the group grow and center, while `justify="end"` pushes the actions to the far edge. ```tsx "use client"; import {Button, Navbar} from "@darkcode-ui/react"; import {Sparkles} from "@gravity-ui/icons"; export function NavbarCentered() { return ( DarkCode {/* Centered nav fills the space between brand and actions */} Features Pricing Docs Blog ); } ``` ## Responsive Mobile Menu `Navbar.MenuToggle` and `Navbar.Menu` provide a built-in mobile dropdown. Use `hidden md:flex` on desktop-only groups and keep the toggle visible below the breakpoint: ```tsx {/* Desktop-only nav */} Features Pricing {/* Mobile-only menu — animates in when the toggle is pressed */} Features Pricing ``` The menu open state can be controlled via `isMenuOpen` / `onMenuOpenChange` on `Navbar`, or uncontrolled via `defaultMenuOpen`. By default the navbar locks body scroll while the menu is open — pass `shouldBlockScroll={false}` to disable. ## Compact Density Scale the entire navbar (paddings, gaps, icon sizes) by overriding the Tailwind v4 `--spacing` variable on the navbar or a wrapper, and tune the bar height with the `height` prop: ```tsx ``` - `--spacing` scales every Tailwind spacing utility (`gap-*`, `p-*`, `size-*`) inside the navbar - `height` overrides the internal `--navbar-height` CSS variable - `size="sm"` applies the built-in compact text + padding variants ## Client-Side Routing `Navbar.Item` renders a real `` element when `href` is set (and `Navbar.MenuItem` does the same for the mobile menu), so it works with any router. You have two ways to integrate client-side routing. ### Option 1 — `navigate` callback (recommended) Pass your router's push function to `Navbar`. Every `Navbar.Item` / `Navbar.MenuItem` with an `href` will route through it, preventing full-page reloads. ```tsx "use client"; import { useRouter } from "next/navigation"; import { Navbar } from "@darkcode-ui/react"; export function AppNavbar() { const router = useRouter(); return ( Acme Features Pricing ); } ``` ### Option 2 — `render` prop for a custom link component Since `Navbar.Item` and `Navbar.MenuItem` are polymorphic, you can swap the rendered element entirely to use a framework-specific `` component (preserving prefetch, scroll restoration, and any router-level behaviors): ```tsx import Link from "next/link"; import { Navbar } from "@darkcode-ui/react"; export function AppNavbar() { return ( } > Features ); } ``` ### External Links and Force Reload External URLs (starting with `http://` or `https://`) are automatically opened in a new tab via `window.open(href, "_blank", "noopener,noreferrer")`. For internal links that need a full page reload instead of client-side navigation, use the `forceReload` prop: ```tsx GitHub Legacy page ``` > If no `navigate` function is provided, `href` falls back to the browser's default navigation. ## Positioning Use the `position` prop to control how the navbar is anchored: - `"sticky"` *(default)* — sticks to the top of its scroll container (`sticky top-0`) - `"static"` — flows inline with the page - `"floating"` — sticky, but detached from the edges with rounded corners, margin, border, and a shadow ```tsx ``` ## Appearance - **`isBlurred`** *(default `true`)* — a frosted-glass backdrop: a translucent background plus `backdrop-blur`. Pass `isBlurred={false}` for a solid bar. - **`isBordered`** — adds a bottom border. - **Elevate on scroll** — once the scroll container has moved, the navbar gains a subtle shadow (`data-scrolled="true"`). Floating navbars keep their own shadow instead. ```tsx ``` ## Scroll Behavior - **`onScrollPositionChange(position)`** — called with the scroll offset as the container scrolls. - **`disableScrollHandler`** — skip the internal scroll listener entirely (no hide / elevate / callback). - **`disableAnimation`** — opt out of the menu, toggle, and hide-on-scroll transitions. ```tsx console.log(y)}>… ``` ## Programmatic Control Use the `useNavbar` hook inside children of `Navbar` for programmatic access to the menu state and scroll-hidden state: ```tsx import { useNavbar } from "@darkcode-ui/react"; function CloseMenuButton() { const { isMenuOpen, setMenuOpen } = useNavbar(); return ( ); } ``` ## Styling ### Customizing the component classes To customize the Navbar component classes, you can use the `@layer components` directive.
[Learn more](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes). ```css @layer components { .navbar { --navbar-height: 3.5rem; } .navbar__item { @apply font-semibold; } } ``` DarkCode UI follows the [BEM](https://getbem.com/) methodology to ensure component variants and states are reusable and easy to customize. ### CSS Classes The Navbar component uses these CSS classes ([View source styles](https://github.com/DarkCode-Developers/darkcode-ui/blob/main/packages/styles/components/navbar.css)): #### Base & Variant Classes - `.navbar` — Root `