Skip to content

Navbar 1

Sticky navbar with blur-on-scroll, desktop links and a full-screen mobile menu.

01 / 02

Installation

Run this in a project that already uses shadcn/ui. The CLI adds the block, its components and npm dependencies.

pnpm dlx shadcn@latest add https://ui.sumansharma.info.np/r/navbar-1.json
Prefer to iterate with AI?Open in

Usage

The CLI writes the block to components/navbar-1.tsx. Import it anywhere:

Loading...

Source

The CLI writes this component to components/navbar-1.tsx.

'use client'

import { useEffect, useState } from 'react'
import Link from 'next/link'
import { Menu, X } from 'lucide-react'
import { Logo } from '@/components/logo'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'

const links = [
    { label: 'Product', href: '#' },
    { label: 'Solutions', href: '#' },
    { label: 'Pricing', href: '#' },
    { label: 'Docs', href: '#' },
    { label: 'Changelog', href: '#' },
]

export default function Navbar() {
    const [open, setOpen] = useState(false)
    const [scrolled, setScrolled] = useState(false)

    useEffect(() => {
        const onScroll = () => setScrolled(window.scrollY > 8)
        onScroll()
        window.addEventListener('scroll', onScroll, { passive: true })
        return () => window.removeEventListener('scroll', onScroll)
    }, [])

    // The mobile menu is lg:hidden, so close it (and release the scroll lock) when the viewport grows past lg.
    useEffect(() => {
        const mq = window.matchMedia('(min-width: 1024px)')
        const onChange = (e: MediaQueryListEvent) => e.matches && setOpen(false)
        mq.addEventListener('change', onChange)
        return () => mq.removeEventListener('change', onChange)
    }, [])

    // Lock page scroll while the mobile menu is open.
    useEffect(() => {
        document.body.style.overflow = open ? 'hidden' : ''
        return () => {
            document.body.style.overflow = ''
        }
    }, [open])

    return (
        <>
            <header className={cn('sticky top-0 z-50 w-full border-b border-transparent transition-colors duration-300', (scrolled || open) && 'bg-background/80 border-border backdrop-blur-lg')}>
                <nav
                    aria-label="Main"
                    className="mx-auto flex h-16 max-w-6xl items-center justify-between gap-6 px-6">
                    <Link
                        href="/"
                        aria-label="Home"
                        className="flex items-center gap-2 font-semibold">
                        <Logo />
                        <span>Lyra</span>
                    </Link>

                    <ul className="hidden items-center gap-1 lg:flex">
                        {links.map((link) => (
                            <li key={link.label}>
                                <Link
                                    href={link.href}
                                    className="text-muted-foreground hover:text-foreground hover:bg-accent rounded-md px-3 py-2 text-sm transition-colors">
                                    {link.label}
                                </Link>
                            </li>
                        ))}
                    </ul>

                    <div className="hidden items-center gap-2 lg:flex">
                        <Button
                            asChild
                            variant="ghost"
                            size="sm">
                            <Link href="#">Log in</Link>
                        </Button>
                        <Button
                            asChild
                            size="sm">
                            <Link href="#">Get started</Link>
                        </Button>
                    </div>

                    <Button
                        variant="ghost"
                        size="icon"
                        className="lg:hidden"
                        aria-expanded={open}
                        aria-controls="mobile-menu"
                        aria-label={open ? 'Close menu' : 'Open menu'}
                        onClick={() => setOpen((v) => !v)}>
                        {open ? <X className="size-5" /> : <Menu className="size-5" />}
                    </Button>
                </nav>

                {open && (
                    <div
                        id="mobile-menu"
                        className="bg-background h-[calc(100dvh-4rem)] overflow-y-auto border-t px-6 pb-8 lg:hidden">
                        <ul className="divide-y">
                            {links.map((link) => (
                                <li key={link.label}>
                                    <Link
                                        href={link.href}
                                        onClick={() => setOpen(false)}
                                        className="block py-4 text-lg font-medium">
                                        {link.label}
                                    </Link>
                                </li>
                            ))}
                        </ul>
                        <div className="mt-6 grid gap-3">
                            <Button
                                asChild
                                variant="outline"
                                size="lg">
                                <Link href="#">Log in</Link>
                            </Button>
                            <Button
                                asChild
                                size="lg">
                                <Link href="#">Get started</Link>
                            </Button>
                        </div>
                    </div>
                )}
            </header>

            {/* Demo content so the sticky, blur-on-scroll behaviour is visible in the preview. */}
            <main className="mx-auto max-w-6xl px-6 py-24">
                <h1 className="max-w-xl text-balance text-4xl font-semibold">Scroll to see the header blur in.</h1>
                <div className="mt-12 grid gap-4 sm:grid-cols-2">
                    {Array.from({ length: 6 }).map((_, i) => (
                        <div
                            key={i}
                            className="bg-muted/50 h-48 rounded-xl border"
                        />
                    ))}
                </div>
            </main>
        </>
    )
}