Navbar
Transparent until scroll, then white with a hairline. Mobile menu included.
Install
$ npx shadcn@latest add @nudgekit/navbarOr with no config: npx shadcn@latest add https://ui.nudgekit.app/r/navbar.json
Brings in @nudgekit/button, @nudgekit/logo-mark, lucide-react, and the theme.
Usage
usage.tsx
import { Navbar } from "@/components/navbar"
<Navbar
name="Capture"
links={[{ label: "Docs", href: "/docs" }, { label: "Pricing", href: "/pricing" }]}
signIn={{ label: "Sign in", href: "/login" }}
cta={{ label: "Get API key", href: "/signup" }}
/>Do
- Keep it to five links or fewer.
- Make the one button the sign-up action.
Don't
- Add a second filled button to the navbar.
Code
What the CLI writes into your project. It is yours to change.
components/navbar.tsx
"use client";
import * as React from "react";
import { Menu, X } from "lucide-react";
import { cn } from "@/lib/utils";
import { buttonVariants } from "@/components/ui/button";
import { LogoMark } from "@/components/logo-mark";
export interface NavbarProps {
name: string;
href?: string;
links: { label: string; href: string }[];
signIn?: { label: string; href: string };
cta: { label: string; href: string };
className?: string;
}
/** A transparent top bar that turns white with a hairline once the page scrolls. One primary button on the right. */
function Navbar({ name, href = "/", links, signIn, cta, className }: NavbarProps) {
const [scrolled, setScrolled] = React.useState(false);
const [open, setOpen] = React.useState(false);
React.useEffect(() => {
const onScroll = () => setScrolled(window.scrollY > 8);
onScroll();
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
return (
<header
data-slot="navbar"
className={cn(
"sticky top-0 z-40 border-b transition-colors duration-200",
scrolled || open ? "border-border bg-background/90 backdrop-blur" : "border-transparent bg-transparent",
className,
)}
>
<nav className="mx-auto flex h-[68px] max-w-[1120px] items-center gap-8 px-6" aria-label="Main">
<a href={href} className="mr-auto rounded-md focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-ring">
<LogoMark name={name} />
</a>
<div className="hidden items-center gap-7 text-[15px] md:flex">
{links.map((l) => (
<a key={l.href} href={l.href} className="text-foreground transition-colors hover:text-brand-ink">
{l.label}
</a>
))}
{signIn && (
<a href={signIn.href} className="text-foreground transition-colors hover:text-brand-ink">
{signIn.label}
</a>
)}
<a href={cta.href} className={buttonVariants({ size: "sm" })}>
{cta.label}
</a>
</div>
<button
type="button"
className="rounded-md p-2 text-foreground md:hidden"
aria-label={open ? "Close menu" : "Open menu"}
aria-expanded={open}
onClick={() => setOpen(!open)}
>
{open ? <X className="size-5" /> : <Menu className="size-5" />}
</button>
</nav>
{open && (
<div className="border-t border-border px-6 pb-6 pt-2 md:hidden">
{[...links, ...(signIn ? [signIn] : [])].map((l) => (
<a key={l.href} href={l.href} className="block border-b border-border py-3 text-[15px] text-foreground">
{l.label}
</a>
))}
<a href={cta.href} className={cn(buttonVariants(), "mt-4 w-full")}>
{cta.label}
</a>
</div>
)}
</header>
);
}
export { Navbar };