dock.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use client";
  2. import { cva, type VariantProps } from "class-variance-authority";
  3. import { motion, useMotionValue, useSpring, useTransform } from "framer-motion";
  4. import React, { PropsWithChildren, useRef } from "react";
  5. import { Link } from "@nextui-org/react";
  6. import { cn } from "@/lib/utils";
  7. export interface DockProps extends VariantProps<typeof dockVariants> {
  8. className?: string;
  9. magnification?: number;
  10. distance?: number;
  11. children: React.ReactNode;
  12. }
  13. const DEFAULT_MAGNIFICATION = 60;
  14. const DEFAULT_DISTANCE = 140;
  15. const dockVariants = cva(
  16. "mx-auto w-max h-[58px] p-2 flex items-end gap-2 rounded-2xl",
  17. );
  18. const Dock = React.forwardRef<HTMLDivElement, DockProps>(
  19. (
  20. {
  21. className,
  22. children,
  23. magnification = DEFAULT_MAGNIFICATION,
  24. distance = DEFAULT_DISTANCE,
  25. ...props
  26. },
  27. ref,
  28. ) => {
  29. const mouseX = useMotionValue(Infinity);
  30. const renderChildren = () => {
  31. return React.Children.map(children, (child: any) => {
  32. return React.cloneElement(child, {
  33. mouseX: mouseX,
  34. magnification: magnification,
  35. distance: distance,
  36. });
  37. });
  38. };
  39. return (
  40. <motion.div
  41. ref={ref}
  42. onMouseLeave={() => mouseX.set(Infinity)}
  43. onMouseMove={(e) => mouseX.set(e.pageX)}
  44. {...props}
  45. className={cn(dockVariants({ className }), className)}
  46. >
  47. {renderChildren()}
  48. </motion.div>
  49. );
  50. },
  51. );
  52. Dock.displayName = "Dock";
  53. export interface DockIconProps {
  54. size?: number;
  55. magnification?: number;
  56. distance?: number;
  57. mouseX?: any;
  58. className?: string;
  59. children?: React.ReactNode;
  60. props?: PropsWithChildren;
  61. url: string;
  62. }
  63. const DockIcon = ({
  64. size,
  65. magnification = DEFAULT_MAGNIFICATION,
  66. distance = DEFAULT_DISTANCE,
  67. mouseX,
  68. className,
  69. children,
  70. url,
  71. ...props
  72. }: DockIconProps) => {
  73. const ref = useRef<HTMLDivElement>(null);
  74. const distanceCalc = useTransform(mouseX, (val: number) => {
  75. const bounds = ref.current?.getBoundingClientRect() ?? { x: 0, width: 0 };
  76. return val - bounds.x - bounds.width / 2;
  77. });
  78. let widthSync = useTransform(
  79. distanceCalc,
  80. [-distance, 0, distance],
  81. [40, magnification, 40],
  82. );
  83. let width = useSpring(widthSync, {
  84. mass: 0.1,
  85. stiffness: 150,
  86. damping: 12,
  87. });
  88. return (
  89. <Link isExternal color="foreground" href={url}>
  90. <motion.div
  91. ref={ref}
  92. className={cn(
  93. "flex aspect-square cursor-pointer items-center justify-center rounded-full bg-midnight dark:border-2 dark:border-knight dark:bg-darkBg",
  94. className,
  95. )}
  96. style={{ width }}
  97. {...props}
  98. >
  99. {children}
  100. </motion.div>
  101. </Link>
  102. );
  103. };
  104. DockIcon.displayName = "DockIcon";
  105. export { Dock, DockIcon, dockVariants };