A mega menu in React that glides between menus (2027)

Build a mega menu dropdown in React with Motion that stays open as you move between menus: the panel resizes to fit each one and the content slides in from the side the pointer came from.

5 min read

The real component. Click the picture to open it live and try it before you read how it was built.

The worst thing a mega menu can do is close and reopen every time you move to the next item. This one stays open and glides.

One panel, many menus

There is one panel. When you move to another menu, the panel resizes to fit the new content with a layout animation, instead of a second panel replacing the first.

src/registry/navbars/mega/navbar-mega.tsx

"use client";

import { useState, type ReactNode } from "react";
import { AnimatePresence, MotionConfig, motion } from "motion/react";
import { MenuToggle } from "@/registry/navbars/shared/menu-toggle";
import { NavCtaLink } from "@/registry/navbars/shared/nav-cta-link";
import { NavLinkList } from "@/registry/navbars/shared/nav-link-list";
import { NavMobilePanel } from "@/registry/navbars/shared/nav-mobile-panel";
import { SPRING_SETTLE } from "@/lib/motion";
import { cn } from "@/lib/utils";
import type { NavCta, NavItem } from "@/registry/navbars/shared/types";
import { MegaPanel } from "./mega-panel";

export interface NavbarMegaProps {
  logo: ReactNode;
  items: NavItem[];
  cta?: NavCta;
  className?: string;
}

// A clean white bar with a dropdown that stays open as you move between menus: the panel
// resizes to fit each one and the content slides across, instead of closing and reopening.
export function NavbarMega({ logo, items, cta, className }: NavbarMegaProps) {
  const [activeIndex, setActiveIndex] = useState<number | null>(null);
  const [direction, setDirection] = useState(0);
  const [isOpen, setIsOpen] = useState(false);
  const active = activeIndex === null ? null : items[activeIndex];

  const hoverItem = (index: number | null) => {
    if (index === null) return;
    if (!items[index].children) return setActiveIndex(null);
    setDirection(activeIndex === null ? 0 : Math.sign(index - activeIndex));
    setActiveIndex(index);
  };

  return (
    <MotionConfig reducedMotion="user">
      <header
        onMouseLeave={() => setActiveIndex(null)}
        className={cn("fixed inset-x-0 top-0 z-50 border-b bg-white/90 backdrop-blur-xl", className)}
      >
        <nav aria-label="Main" className="relative mx-auto flex h-16 max-w-6xl items-center justify-between gap-4 px-5">
          <a href="#top" aria-label="Home" className="text-ink">
            {logo}
          </a>
          <NavLinkList
            items={items}
            activeLabel={active?.label}
            onHoverChange={hoverItem}
            delay={0.1}
            className="hidden md:flex"
            linkClassName="h-9 px-3.5 text-ink/60 hover:text-ink focus-visible:text-ink"
            activeLinkClassName="text-primary"
            indicatorClassName="inset-0 rounded-full bg-accent"
          />
          <div className="flex items-center gap-1">
            {cta && <NavCtaLink cta={cta} variant="brand" size="sm" className="hidden h-9 px-4 md:inline-flex" />}
            <MenuToggle isOpen={isOpen} onToggle={() => setIsOpen((open) => !open)} className="text-ink md:hidden" />
          </div>
          <AnimatePresence>
            {active?.children && (
              <motion.div
                layout
                initial={{ opacity: 0, y: -8, scale: 0.98 }}
                animate={{ opacity: 1, y: 0, scale: 1 }}
                exit={{ opacity: 0, y: -8, scale: 0.98 }}
                transition={SPRING_SETTLE}
                className="absolute top-full left-1/2 mt-2 hidden w-150 -translate-x-1/2 overflow-hidden rounded-2xl border bg-white shadow-xl shadow-ink/5 md:block"
              >
                <AnimatePresence mode="popLayout" initial={false} custom={direction}>
                  <MegaPanel key={active.label} item={active} cta={cta} direction={direction} />
                </AnimatePresence>
              </motion.div>
            )}
          </AnimatePresence>
        </nav>
        <AnimatePresence initial={false}>
          {isOpen && <NavMobilePanel items={items} cta={cta} onNavigate={() => setIsOpen(false)} />}
        </AnimatePresence>
      </header>
    </MotionConfig>
  );
}
The navbar and its panel

Content slides with your hand

The new menu's content slides in from the side the pointer came from and the old one leaves the other way, so the motion follows your hand.

src/registry/navbars/mega/mega-panel.tsx

"use client";

import { ArrowUpRight } from "lucide-react";
import { motion } from "motion/react";
import { EASE_OUT, riseIn } from "@/lib/motion";
import type { NavCta, NavItem } from "@/registry/navbars/shared/types";

const SLIDE = 40;

// Content slides in from the side the pointer came from, and out the other way.
const variants = {
  enter: (direction: number) => ({ opacity: 0, x: direction * SLIDE }),
  center: { opacity: 1, x: 0 },
  exit: (direction: number) => ({ opacity: 0, x: direction * -SLIDE }),
};

interface MegaPanelProps {
  item: NavItem;
  cta?: NavCta;
  direction: number;
}

// One menu's content: its pages as rows with a blue icon tile, and a solid blue card to the right.
export function MegaPanel({ item, cta, direction }: MegaPanelProps) {
  const Icon = item.icon;
  return (
    <motion.div
      custom={direction}
      variants={variants}
      initial="enter"
      animate="center"
      exit="exit"
      transition={{ duration: 0.28, ease: EASE_OUT }}
      className="grid grid-cols-5 gap-2 p-2"
    >
      <ul className="col-span-3 flex flex-col gap-1">
        {item.children?.map((child, index) => (
          <motion.li key={child.label} {...riseIn(index, 0.04, 0.04)}>
            <a
              href={child.href}
              className="group/row flex items-start gap-3 rounded-xl p-3 transition-colors outline-none hover:bg-muted focus-visible:bg-muted"
            >
              <span className="grid size-9 shrink-0 place-items-center rounded-lg border bg-accent text-primary transition-colors group-hover/row:bg-primary group-hover/row:text-white">
                {Icon && <Icon className="size-4" />}
              </span>
              <span className="flex flex-col">
                <span className="text-sm font-medium text-ink">{child.label}</span>
                {child.description && <span className="text-xs text-muted-foreground">{child.description}</span>}
              </span>
            </a>
          </motion.li>
        ))}
      </ul>
      <a
        href={cta?.href ?? item.href}
        className="col-span-2 flex flex-col justify-between gap-6 rounded-xl bg-primary p-4 text-white outline-none focus-visible:ring-3 focus-visible:ring-ring/50"
      >
        <span className="text-xs font-medium tracking-widest text-white/70 uppercase">{item.label}</span>
        <span className="flex items-end justify-between gap-2 text-lg leading-snug font-semibold">
          {cta ? cta.label : `All ${item.label.toLowerCase()}`}
          <ArrowUpRight className="size-5 shrink-0" />
        </span>
      </a>
    </motion.div>
  );
}
One menu's content

More articles