237 lines
6.6 KiB
TypeScript
237 lines
6.6 KiB
TypeScript
import { cn } from "~/lib/utils";
|
|
import { Menu } from "lucide-react";
|
|
import {
|
|
AnimatePresence,
|
|
MotionValue,
|
|
motion,
|
|
useMotionValue,
|
|
useSpring,
|
|
useTransform
|
|
} from "motion/react";
|
|
import { useRef, useState } from "react";
|
|
|
|
export const FloatingDock = ({
|
|
items,
|
|
desktopClassName,
|
|
mobileClassName
|
|
}: {
|
|
items: {
|
|
title: string;
|
|
icon: React.ReactNode;
|
|
href: string;
|
|
isActive?: boolean;
|
|
onClick?: () => void;
|
|
}[];
|
|
desktopClassName?: string;
|
|
mobileClassName?: string;
|
|
}) => {
|
|
return (
|
|
<>
|
|
<FloatingDockDesktop items={items} className={desktopClassName} />
|
|
<FloatingDockMobile items={items} className={mobileClassName} />
|
|
</>
|
|
);
|
|
};
|
|
|
|
const FloatingDockMobile = ({
|
|
items,
|
|
className
|
|
}: {
|
|
items: {
|
|
title: string;
|
|
icon: React.ReactNode;
|
|
href: string;
|
|
isActive?: boolean;
|
|
onClick?: () => void;
|
|
}[];
|
|
className?: string;
|
|
}) => {
|
|
const [open, setOpen] = useState(false);
|
|
return (
|
|
<div className={cn("relative block md:hidden", className)}>
|
|
<AnimatePresence>
|
|
{open && (
|
|
<motion.div
|
|
layoutId="nav"
|
|
className="absolute inset-x-0 bottom-full mb-2 flex flex-col gap-2"
|
|
>
|
|
{items.map((item, idx) => (
|
|
<motion.div
|
|
key={item.title}
|
|
initial={{ opacity: 0, y: 10 }}
|
|
animate={{
|
|
opacity: 1,
|
|
y: 0
|
|
}}
|
|
exit={{
|
|
opacity: 0,
|
|
y: 10,
|
|
transition: {
|
|
delay: idx * 0.05
|
|
}
|
|
}}
|
|
transition={{ delay: (items.length - 1 - idx) * 0.05 }}
|
|
>
|
|
<button
|
|
onClick={() => {
|
|
item.onClick?.();
|
|
setOpen(false);
|
|
}}
|
|
className={cn(
|
|
"flex h-10 w-10 items-center justify-center rounded-full transition-all border-2",
|
|
item.isActive
|
|
? "bg-blue-100 dark:bg-blue-900/50 border-blue-300 dark:border-blue-700 shadow-lg"
|
|
: "bg-gray-50 dark:bg-neutral-900 border-transparent hover:border-gray-300 dark:hover:border-neutral-700"
|
|
)}
|
|
>
|
|
<div className="h-4 w-4">{item.icon}</div>
|
|
</button>
|
|
</motion.div>
|
|
))}
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
<button
|
|
onClick={() => setOpen(!open)}
|
|
className="flex h-10 w-10 items-center justify-center rounded-full bg-gray-50 dark:bg-neutral-800 hover:bg-gray-100 dark:hover:bg-neutral-700 transition-colors"
|
|
>
|
|
<Menu className="h-5 w-5 text-neutral-500 dark:text-neutral-400" />
|
|
</button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const FloatingDockDesktop = ({
|
|
items,
|
|
className
|
|
}: {
|
|
items: {
|
|
title: string;
|
|
icon: React.ReactNode;
|
|
href: string;
|
|
isActive?: boolean;
|
|
onClick?: () => void;
|
|
}[];
|
|
className?: string;
|
|
}) => {
|
|
let mouseX = useMotionValue(Infinity);
|
|
return (
|
|
<motion.div
|
|
onMouseMove={(e) => mouseX.set(e.pageX)}
|
|
onMouseLeave={() => mouseX.set(Infinity)}
|
|
className={cn(
|
|
"mx-auto hidden h-16 items-end gap-4 rounded-2xl bg-gray-50/80 backdrop-blur-sm px-4 pb-3 md:flex dark:bg-neutral-900/80 border border-gray-200/50 dark:border-neutral-800/50 shadow-lg",
|
|
className
|
|
)}
|
|
>
|
|
{items.map((item) => (
|
|
<IconContainer mouseX={mouseX} key={item.title} {...item} />
|
|
))}
|
|
</motion.div>
|
|
);
|
|
};
|
|
|
|
function IconContainer({
|
|
mouseX,
|
|
title,
|
|
icon,
|
|
href,
|
|
isActive,
|
|
onClick
|
|
}: {
|
|
mouseX: MotionValue;
|
|
title: string;
|
|
icon: React.ReactNode;
|
|
href: string;
|
|
isActive?: boolean;
|
|
onClick?: () => void;
|
|
}) {
|
|
let ref = useRef<HTMLDivElement>(null);
|
|
|
|
let distance = useTransform(mouseX, (val) => {
|
|
let bounds = ref.current?.getBoundingClientRect() ?? { x: 0, width: 0 };
|
|
return val - bounds.x - bounds.width / 2;
|
|
});
|
|
|
|
let widthTransform = useTransform(distance, [-150, 0, 150], [40, 80, 40]);
|
|
let heightTransform = useTransform(distance, [-150, 0, 150], [40, 80, 40]);
|
|
|
|
let widthTransformIcon = useTransform(distance, [-150, 0, 150], [20, 40, 20]);
|
|
let heightTransformIcon = useTransform(
|
|
distance,
|
|
[-150, 0, 150],
|
|
[20, 40, 20]
|
|
);
|
|
|
|
let width = useSpring(widthTransform, {
|
|
mass: 0.1,
|
|
stiffness: 150,
|
|
damping: 12
|
|
});
|
|
let height = useSpring(heightTransform, {
|
|
mass: 0.1,
|
|
stiffness: 150,
|
|
damping: 12
|
|
});
|
|
|
|
let widthIcon = useSpring(widthTransformIcon, {
|
|
mass: 0.1,
|
|
stiffness: 150,
|
|
damping: 12
|
|
});
|
|
let heightIcon = useSpring(heightTransformIcon, {
|
|
mass: 0.1,
|
|
stiffness: 150,
|
|
damping: 12
|
|
});
|
|
|
|
const [hovered, setHovered] = useState(false);
|
|
|
|
return (
|
|
<button onClick={onClick} className="relative">
|
|
<motion.div
|
|
ref={ref}
|
|
style={{ width, height }}
|
|
onMouseEnter={() => setHovered(true)}
|
|
onMouseLeave={() => setHovered(false)}
|
|
className={cn(
|
|
"relative flex aspect-square items-center justify-center rounded-full border-2",
|
|
isActive
|
|
? "bg-gradient-to-br from-blue-100 to-blue-200 dark:from-blue-900/50 dark:to-blue-800/50 border-blue-300 dark:border-blue-700 shadow-lg"
|
|
: "bg-gray-100 dark:bg-neutral-800 border-transparent hover:border-gray-300 dark:hover:border-neutral-700 hover:shadow-md"
|
|
)}
|
|
>
|
|
<AnimatePresence>
|
|
{hovered && (
|
|
<motion.div
|
|
initial={{ opacity: 0, y: 10, x: "-50%" }}
|
|
animate={{ opacity: 1, y: 0, x: "-50%" }}
|
|
exit={{ opacity: 0, y: 2, x: "-50%" }}
|
|
className="absolute -top-8 left-1/2 w-fit rounded-md border border-gray-200 bg-white/90 backdrop-blur-sm px-2 py-0.5 text-xs whitespace-pre text-neutral-700 dark:border-neutral-700 dark:bg-neutral-800/90 dark:text-white shadow-lg"
|
|
>
|
|
{title}
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
<motion.div
|
|
style={{ width: widthIcon, height: heightIcon }}
|
|
className="flex items-center justify-center"
|
|
>
|
|
{icon}
|
|
</motion.div>
|
|
|
|
{/* Active indicator */}
|
|
{isActive && (
|
|
<motion.div
|
|
layoutId="activeIndicator"
|
|
className="absolute -bottom-1 left-1/2 w-2 h-2 bg-blue-500 rounded-full transform -translate-x-1/2"
|
|
initial={{ scale: 0 }}
|
|
animate={{ scale: 1 }}
|
|
transition={{ type: "spring", stiffness: 300, damping: 30 }}
|
|
/>
|
|
)}
|
|
</motion.div>
|
|
</button>
|
|
);
|
|
}
|