"use client"
// Visit https://kaif-ui.vercel.app/ for more components like this
import { cn } from "@/lib/utils";
import { IconChevronUp } from "@tabler/icons-react";
import {
AnimatePresence,
MotionValue,
motion,
useMotionValue,
useSpring,
useTransform,
} from "framer-motion";
import Link from "next/link";
import { useRef, useState } from "react";
export const FloatingDock = ({
navigationItems,
desktopClassName,
mobileClassName,
}: {
navigationItems: { label: string; icon: React.ReactNode; link: string }[];
desktopClassName?: string;
mobileClassName?: string;
}) => {
return (
<>
<FloatingDockDesktop navigationItems={navigationItems} className={desktopClassName} />
<FloatingDockMobile navigationItems={navigationItems} className={mobileClassName} />
</>
);
};
const FloatingDockMobile = ({
navigationItems,
className,
}: {
navigationItems: { label: string; icon: React.ReactNode; link: string }[];
className?: string;
}) => {
const [open, setOpen] = useState(false);
return (
<div className={cn("relative block md:hidden", className)}>
<AnimatePresence>
{open && (
<motion.div
layoutId="nav"
className="absolute bottom-full mb-2 inset-x-0 flex flex-col gap-2"
>
{navigationItems.map((item, idx) => (
<motion.div
key={item.label}
initial={{ opacity: 0, y: 10 }}
animate={{
opacity: 1,
y: 0,
}}
exit={{
opacity: 0,
y: 10,
transition: {
delay: idx * 0.05,
},
}}
transition={{ delay: (navigationItems.length - 1 - idx) * 0.05 }}
>
<Link
href={item.link}
key={item.label}
className="h-10 w-10 rounded-full bg-gray-50 dark:bg-neutral-900 flex items-center justify-center"
>
<div className="h-4 w-4">{item.icon}</div>
</Link>
</motion.div>
))}
</motion.div>
)}
</AnimatePresence>
<button
onClick={() => setOpen(!open)}
className="h-10 w-10 rounded-full bg-gray-50 dark:bg-neutral-800 flex items-center justify-center"
>
<IconChevronUp
className={`h-5 w-5 text-neutral-500 dark:text-neutral-400 transition-transform duration-300 transform ${open ? "rotate-180" : "rotate-0"}`}
/>
</button>
</div>
);
};
const FloatingDockDesktop = ({
navigationItems,
className,
}: {
navigationItems: { label: string; icon: React.ReactNode; link: string }[];
className?: string;
}) => {
let mouseX = useMotionValue(Infinity);
return (
<motion.div
onMouseMove={(e) => mouseX.set(e.pageX)}
onMouseLeave={() => mouseX.set(Infinity)}
className={cn(
"mx-auto hidden md:flex h-16 gap-4 items-end rounded-2xl bg-gray-50 dark:bg-neutral-900 px-4 pb-3",
className
)}
>
{navigationItems.map((item) => (
<IconContainer mouseX={mouseX} key={item.label} {...item} />
))}
</motion.div>
);
};
function IconContainer({
mouseX,
label,
icon,
link,
}: {
mouseX: MotionValue;
label: string;
icon: React.ReactNode;
link: string;
}) {
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 (
<Link href={link}>
<motion.div
ref={ref}
style={{ width, height }}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
className="aspect-square rounded-full bg-gray-200 dark:bg-neutral-800 flex items-center justify-center relative"
>
<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="px-2 py-0.5 whitespace-pre rounded-md bg-gray-100 border dark:bg-neutral-800 dark:border-neutral-900 dark:text-white border-gray-200 text-neutral-700 absolute left-1/2 -translate-x-1/2 -top-8 w-fit text-xs"
>
{label}
</motion.div>
)}
</AnimatePresence>
<motion.div
style={{ width: widthIcon, height: heightIcon }}
className="flex items-center justify-center"
>
{icon}
</motion.div>
</motion.div>
</Link>
);
}