| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- "use client";
- import { cva, type VariantProps } from "class-variance-authority";
- import { motion, useMotionValue, useSpring, useTransform } from "framer-motion";
- import React, { PropsWithChildren, useRef } from "react";
- import { Link } from "@nextui-org/react";
- import { cn } from "@/lib/utils";
- export interface DockProps extends VariantProps<typeof dockVariants> {
- className?: string;
- magnification?: number;
- distance?: number;
- children: React.ReactNode;
- }
- const DEFAULT_MAGNIFICATION = 60;
- const DEFAULT_DISTANCE = 140;
- const dockVariants = cva(
- "mx-auto w-max h-[58px] p-2 flex items-end gap-2 rounded-2xl",
- );
- const Dock = React.forwardRef<HTMLDivElement, DockProps>(
- (
- {
- className,
- children,
- magnification = DEFAULT_MAGNIFICATION,
- distance = DEFAULT_DISTANCE,
- ...props
- },
- ref,
- ) => {
- const mouseX = useMotionValue(Infinity);
- const renderChildren = () => {
- return React.Children.map(children, (child: any) => {
- return React.cloneElement(child, {
- mouseX: mouseX,
- magnification: magnification,
- distance: distance,
- });
- });
- };
- return (
- <motion.div
- ref={ref}
- onMouseLeave={() => mouseX.set(Infinity)}
- onMouseMove={(e) => mouseX.set(e.pageX)}
- {...props}
- className={cn(dockVariants({ className }), className)}
- >
- {renderChildren()}
- </motion.div>
- );
- },
- );
- Dock.displayName = "Dock";
- export interface DockIconProps {
- size?: number;
- magnification?: number;
- distance?: number;
- mouseX?: any;
- className?: string;
- children?: React.ReactNode;
- props?: PropsWithChildren;
- url: string;
- }
- const DockIcon = ({
- size,
- magnification = DEFAULT_MAGNIFICATION,
- distance = DEFAULT_DISTANCE,
- mouseX,
- className,
- children,
- url,
- ...props
- }: DockIconProps) => {
- const ref = useRef<HTMLDivElement>(null);
- const distanceCalc = useTransform(mouseX, (val: number) => {
- const bounds = ref.current?.getBoundingClientRect() ?? { x: 0, width: 0 };
- return val - bounds.x - bounds.width / 2;
- });
- let widthSync = useTransform(
- distanceCalc,
- [-distance, 0, distance],
- [40, magnification, 40],
- );
- let width = useSpring(widthSync, {
- mass: 0.1,
- stiffness: 150,
- damping: 12,
- });
- return (
- <Link isExternal color="foreground" href={url}>
- <motion.div
- ref={ref}
- className={cn(
- "flex aspect-square cursor-pointer items-center justify-center rounded-full bg-midnight dark:border-2 dark:border-knight dark:bg-darkBg",
- className,
- )}
- style={{ width }}
- {...props}
- >
- {children}
- </motion.div>
- </Link>
- );
- };
- DockIcon.displayName = "DockIcon";
- export { Dock, DockIcon, dockVariants };
|