Back

Morphing Navbar

A modern animated navbar header menu with smooth morphing hover state transitions. Built with spring physics and cubic easing for fluid micro-interactions.

Category
NavigationReact
CSS
Tailwind

Manual

Create a file and paste the following code into it.

src/components/nav-menu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Easing, motion, Transition } from "motion/react";

const navItems = [
  {
    label: "Expertises",
    href: "/",
  },
  {
    label: "Work",
    href: "/about",
  },
  {
    label: "About",
    href: "/about",
  },
  {
    label: "Contact",
    href: "/contact",
  },
];

export default function NavDemo() {
  return (
    <div className="h-dvh w-full flex items-center justify-center bg-[#faf4ec]">
      <nav className="flex items-center justify-center bg-white rounded-lg p-1">
        <ul className="flex items-center md:gap-4 gap-1 min-h-10">
          {navItems.map((item) => (
            <NavItem key={item.label} item={item} />
          ))}
        </ul>
      </nav>
    </div>
  );
}

const SPRING_CONFIG_TEXT = {
  type: "spring",
  stiffness: 320,
  damping: 32,
  mass: 1.3,
} as Transition;

const EASE_CUBIC_CONFIG = {
  duration: 0.5,
  ease: [0.32, 0.72, 0, 1] as Easing,
} as Transition;

function NavItem({ item }: { item: (typeof navItems)[0] }) {
  return (
    <li className="text-xs md:text-base font-semibold">
      <motion.a
        href={item.href}
        className="relative px-2 py-2 rounded-md overflow-hidden block"
        initial="initial"
        whileHover="hover"
        variants={{
          initial: {},
          hover: {},
        }}
      >
        <span className="relative text-transparent">{item.label}</span>

        <motion.span
          className="z-2 absolute inset-0 w-full h-full flex items-center justify-center"
          initial={{
            y: 0,
            scale: 1,
            rotate: 0,
          }}
          variants={{
            hover: {
              y: -100,
              scale: 0.5,
              rotate: -30,
            },
          }}
          transition={SPRING_CONFIG_TEXT}
        >
          {item.label}
        </motion.span>

        <motion.span
          className="absolute inset-0 bg-orange-500 w-full h-full scale-x-150 z-1 overflow-hidden"
          initial={{
            y: 100,
            rotate: -40,
          }}
          variants={{
            hover: {
              y: 0,
              rotate: 0,
            },
          }}
          transition={EASE_CUBIC_CONFIG}
        >
          <motion.span
            className="absolute inset-0 bg-neutral-900 w-full h-full"
            initial={{
              y: 150,
              rotate: -60,
            }}
            variants={{
              hover: {
                y: 0,
                rotate: 0,
              },
            }}
            transition={EASE_CUBIC_CONFIG}
          ></motion.span>
        </motion.span>

        <motion.span
          className="z-2 absolute inset-0 flex items-center justify-center text-white"
          initial={{
            y: 180,
            rotate: -60,
            scale: 0.5,
          }}
          variants={{
            hover: {
              y: 0,
              rotate: 0,
              scale: 1,
            },
          }}
          transition={SPRING_CONFIG_TEXT}
        >
          {item.label}
        </motion.span>
      </motion.a>
    </li>
  );
}

Update the import paths to match your project setup.

Similar screens