Docs
Floating Navbar

Floating Navbar

A modern, animated navbar that elegantly shows/hides on scroll with dark mode support.

Scroll down to see the effect

The navbar will hide when scrolling down and show when scrolling up

Installation

Copy and paste the following code into your project.

components/ui/floating-navbar.tsx

import React, { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
 
export interface FloatingNavbarProps extends React.HTMLAttributes<HTMLElement> {
  /**
   * The offset in pixels before the navbar starts hiding/showing
   * @default 50
   */
  scrollThreshold?: number;
  /**
   * Whether to show the navbar when scrolling up
   * @default true
   */
  showOnScrollUp?: boolean;
  /**
   * Whether to hide the navbar when scrolling down
   * @default true
   */
  hideOnScrollDown?: boolean;
}
 
export function FloatingNavbar({
  children,
  className,
  scrollThreshold = 50,
  showOnScrollUp = true,
  hideOnScrollDown = true,
  ...props
}: FloatingNavbarProps) {
  const [isVisible, setIsVisible] = useState(true);
  const [lastScrollY, setLastScrollY] = useState(0);
 
  useEffect(() => {
    const handleScroll = () => {
      const currentScrollY = window.scrollY;
      const scrollDifference = currentScrollY - lastScrollY;
 
      // Show/hide based on scroll direction and threshold
      if (currentScrollY < scrollThreshold) {
        setIsVisible(true);
      } else if (scrollDifference > 0) {
        // Scrolling down
        hideOnScrollDown && setIsVisible(false);
      } else if (scrollDifference < 0) {
        // Scrolling up
        showOnScrollUp && setIsVisible(true);
      }
 
      setLastScrollY(currentScrollY);
    };
 
    window.addEventListener("scroll", handleScroll, { passive: true });
    return () => window.removeEventListener("scroll", handleScroll);
  }, [lastScrollY, scrollThreshold, showOnScrollUp, hideOnScrollDown]);
 
  return (
    <nav
      className={cn(
        "fixed left-1/2 top-6 z-50 -translate-x-1/2 transform-gpu rounded-full border bg-background/80 px-8 py-3 shadow-lg backdrop-blur-md transition-all duration-300 ease-in-out dark:border-neutral-800 dark:bg-neutral-950/75",
        isVisible ? "translate-y-0 opacity-100" : "-translate-y-full opacity-0",
        className,
      )}
      {...props}
    >
      {children}
    </nav>
  );
}

Update the import paths to match your project setup.

import FloatingNavbar from "@/components/ui/floating-navbar";

Props

PropTypeDefaultDescription
childrenReactNode-The content to be rendered inside the navbar
classNamestring-Additional CSS classes for customization
scrollThresholdnumber50Pixels to scroll before triggering show/hide animation
showOnScrollUpbooleantrueControls navbar visibility when scrolling up
hideOnScrollDownbooleantrueControls navbar visibility when scrolling down

Examples

Basic Usage

<FloatingNavbar>
  <nav className="flex items-center gap-6">
    <a href="#home">Home</a>
    <a href="#about">About</a>
  </nav>
</FloatingNavbar>

Custom Behavior

<FloatingNavbar
  scrollThreshold={100}
  showOnScrollUp={false}
  hideOnScrollDown={true}
  className="bg-primary text-primary-foreground"
>
  <nav className="flex items-center gap-6">
    <a href="#home">Home</a>
    <a href="#about">About</a>
  </nav>
</FloatingNavbar>

Styling

The component uses Tailwind CSS for styling and includes dark mode support by default. You can customize the appearance by:

  1. Adding custom classes via the className prop
  2. Using dark mode variants with the dark: prefix
  3. Modifying the backdrop blur and background opacity
  4. Adjusting the animation timing and easing
<FloatingNavbar className="dark:bg-zinc-900/75 dark:backdrop-blur-xl">
  {/* Your content */}
</FloatingNavbar>