A resizable-navbar component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { IconMenu2, IconX } from "@tabler/icons-react";
4import {
5 motion,
6 AnimatePresence,
7 useScroll,
8 useMotionValueEvent,
9} from "motion/react";
10
11import React, { useRef, useState } from "react";
12
13
14interface NavbarProps {
15 children: React.ReactNode;
16 className?: string;
17}
18
19interface NavBodyProps {
20 children: React.ReactNode;
21 className?: string;
22 visible?: boolean;
23}
24
25interface NavItemsProps {
26 items: {
27 name: string;
28 link: string;
29 }[];
30 className?: string;
31 onItemClick?: () => void;
32}
33
34interface MobileNavProps {
35 children: React.ReactNode;
36 className?: string;
37 visible?: boolean;
38}
39
40interface MobileNavHeaderProps {
41 children: React.ReactNode;
42 className?: string;
43}
44
45interface MobileNavMenuProps {
46 children: React.ReactNode;
47 className?: string;
48 isOpen: boolean;
49 onClose: () => void;
50}
51
52export const Navbar = ({ children, className }: NavbarProps) => {
53 const ref = useRef<HTMLDivElement>(null);
54 const { scrollY } = useScroll({
55 target: ref,
56 offset: ["start start", "end start"],
57 });
58 const [visible, setVisible] = useState<boolean>(false);
59
60 useMotionValueEvent(scrollY, "change", (latest) => {
61 if (latest > 100) {
62 setVisible(true);
63 } else {
64 setVisible(false);
65 }
66 });
67
68 return (
69 <motion.div
70 ref={ref}
71 // IMPORTANT: Change this to class of `fixed` if you want the navbar to be fixed
72 className={cn("sticky inset-x-0 top-20 z-40 w-full", className)}
73 >
74 {React.Children.map(children, (child) =>
75 React.isValidElement(child)
76 ? React.cloneElement(
77 child as React.ReactElement<{ visible?: boolean }>,
78 { visible },
79 )
80 : child,
81 )}
82 </motion.div>
83 );
84};
85
86export const NavBody = ({ children, className, visible }: NavBodyProps) => {
87 return (
88 <motion.div
89 animate={{
90 backdropFilter: visible ? "blur(10px)" : "none",
91 boxShadow: visible
92 ? "0 0 24px rgba(34, 42, 53, 0.06), 0 1px 1px rgba(0, 0, 0, 0.05), 0 0 0 1px rgba(34, 42, 53, 0.04), 0 0 4px rgba(34, 42, 53, 0.08), 0 16px 68px rgba(47, 48, 55, 0.05), 0 1px 0 rgba(255, 255, 255, 0.1) inset"
93 : "none",
94 width: visible ? "40%" : "100%",
95 y: visible ? 20 : 0,
96 }}
97 transition={{
98 type: "spring",
99 stiffness: 200,
100 damping: 50,
101 }}
102 style={{
103 minWidth: "800px",
104 }}
105 className={cn(
106 "relative z-[60] mx-auto hidden w-full max-w-7xl flex-row items-center justify-between self-start rounded-full bg-transparent px-4 py-2 lg:flex dark:bg-transparent",
107 visible && "bg-white/80 dark:bg-neutral-950/80",
108 className,
109 )}
110 >
111 {children}
112 </motion.div>
113 );
114};
115
116export const NavItems = ({ items, className, onItemClick }: NavItemsProps) => {
117 const [hovered, setHovered] = useState<number | null>(null);
118
119 return (
120 <motion.div
121 onMouseLeave={() => setHovered(null)}
122 className={cn(
123 "absolute inset-0 hidden flex-1 flex-row items-center justify-center space-x-2 text-sm font-medium text-zinc-600 transition duration-200 hover:text-zinc-800 lg:flex lg:space-x-2",
124 className,
125 )}
126 >
127 {items.map((item, idx) => (
128 <a
129 onMouseEnter={() => setHovered(idx)}
130 onClick={onItemClick}
131 className="relative px-4 py-2 text-neutral-600 dark:text-neutral-300"
132 key={`link-${idx}`}
133 href={item.link}
134 >
135 {hovered === idx && (
136 <motion.div
137 layoutId="hovered"
138 className="absolute inset-0 h-full w-full rounded-full bg-gray-100 dark:bg-neutral-800"
139 />
140 )}
141 <span className="relative z-20">{item.name}</span>
142 </a>
143 ))}
144 </motion.div>
145 );
146};
147
148export const MobileNav = ({ children, className, visible }: MobileNavProps) => {
149 return (
150 <motion.div
151 animate={{
152 backdropFilter: visible ? "blur(10px)" : "none",
153 boxShadow: visible
154 ? "0 0 24px rgba(34, 42, 53, 0.06), 0 1px 1px rgba(0, 0, 0, 0.05), 0 0 0 1px rgba(34, 42, 53, 0.04), 0 0 4px rgba(34, 42, 53, 0.08), 0 16px 68px rgba(47, 48, 55, 0.05), 0 1px 0 rgba(255, 255, 255, 0.1) inset"
155 : "none",
156 width: visible ? "90%" : "100%",
157 paddingRight: visible ? "12px" : "0px",
158 paddingLeft: visible ? "12px" : "0px",
159 borderRadius: visible ? "4px" : "2rem",
160 y: visible ? 20 : 0,
161 }}
162 transition={{
163 type: "spring",
164 stiffness: 200,
165 damping: 50,
166 }}
167 className={cn(
168 "relative z-50 mx-auto flex w-full max-w-[calc(100vw-2rem)] flex-col items-center justify-between bg-transparent px-0 py-2 lg:hidden",
169 visible && "bg-white/80 dark:bg-neutral-950/80",
170 className,
171 )}
172 >
173 {children}
174 </motion.div>
175 );
176};
177
178export const MobileNavHeader = ({
179 children,
180 className,
181}: MobileNavHeaderProps) => {
182 return (
183 <div
184 className={cn(
185 "flex w-full flex-row items-center justify-between",
186 className,
187 )}
188 >
189 {children}
190 </div>
191 );
192};
193
194export const MobileNavMenu = ({
195 children,
196 className,
197 isOpen,
198 onClose,
199}: MobileNavMenuProps) => {
200 return (
201 <AnimatePresence>
202 {isOpen && (
203 <motion.div
204 initial={{ opacity: 0 }}
205 animate={{ opacity: 1 }}
206 exit={{ opacity: 0 }}
207 className={cn(
208 "absolute inset-x-0 top-16 z-50 flex w-full flex-col items-start justify-start gap-4 rounded-lg bg-white px-4 py-8 shadow-[0_0_24px_rgba(34,_42,_53,_0.06),_0_1px_1px_rgba(0,_0,_0,_0.05),_0_0_0_1px_rgba(34,_42,_53,_0.04),_0_0_4px_rgba(34,_42,_53,_0.08),_0_16px_68px_rgba(47,_48,_55,_0.05),_0_1px_0_rgba(255,_255,_255,_0.1)_inset] dark:bg-neutral-950",
209 className,
210 )}
211 >
212 {children}
213 </motion.div>
214 )}
215 </AnimatePresence>
216 );
217};
218
219export const MobileNavToggle = ({
220 isOpen,
221 onClick,
222}: {
223 isOpen: boolean;
224 onClick: () => void;
225}) => {
226 return isOpen ? (
227 <IconX className="text-black dark:text-white" onClick={onClick} />
228 ) : (
229 <IconMenu2 className="text-black dark:text-white" onClick={onClick} />
230 );
231};
232
233export const NavbarLogo = () => {
234 return (
235 <a
236 href="#"
237 className="relative z-20 mr-4 flex items-center space-x-2 px-2 py-1 text-sm font-normal text-black"
238 >
239 <img
240 src="https://assets.aceternity.com/logo-dark.png"
241 alt="logo"
242 width={30}
243 height={30}
244 />
245 <span className="font-medium text-black dark:text-white">Startup</span>
246 </a>
247 );
248};
249
250export const NavbarButton = ({
251 href,
252 as: Tag = "a",
253 children,
254 className,
255 variant = "primary",
256 ...props
257}: {
258 href?: string;
259 as?: React.ElementType;
260 children: React.ReactNode;
261 className?: string;
262 variant?: "primary" | "secondary" | "dark" | "gradient";
263} & (
264 | React.ComponentPropsWithoutRef<"a">
265 | React.ComponentPropsWithoutRef<"button">
266)) => {
267 const baseStyles =
268 "px-4 py-2 rounded-md bg-white button bg-white text-black text-sm font-bold relative cursor-pointer hover:-translate-y-0.5 transition duration-200 inline-block text-center";
269
270 const variantStyles = {
271 primary:
272 "shadow-[0_0_24px_rgba(34,_42,_53,_0.06),_0_1px_1px_rgba(0,_0,_0,_0.05),_0_0_0_1px_rgba(34,_42,_53,_0.04),_0_0_4px_rgba(34,_42,_53,_0.08),_0_16px_68px_rgba(47,_48,_55,_0.05),_0_1px_0_rgba(255,_255,_255,_0.1)_inset]",
273 secondary: "bg-transparent shadow-none dark:text-white",
274 dark: "bg-black text-white shadow-[0_0_24px_rgba(34,_42,_53,_0.06),_0_1px_1px_rgba(0,_0,_0,_0.05),_0_0_0_1px_rgba(34,_42,_53,_0.04),_0_0_4px_rgba(34,_42,_53,_0.08),_0_16px_68px_rgba(47,_48,_55,_0.05),_0_1px_0_rgba(255,_255,_255,_0.1)_inset]",
275 gradient:
276 "bg-gradient-to-b from-blue-500 to-blue-700 text-white shadow-[0px_2px_0px_0px_rgba(255,255,255,0.3)_inset]",
277 };
278
279 return (
280 <Tag
281 href={href || undefined}
282 className={cn(baseStyles, variantStyles[variant], className)}
283 {...props}
284 >
285 {children}
286 </Tag>
287 );
288};
289