dock.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, Tooltip } 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. tooltip?: string;
  62. url: string;
  63. }
  64. const DockIcon = ({
  65. size,
  66. magnification = DEFAULT_MAGNIFICATION,
  67. distance = DEFAULT_DISTANCE,
  68. mouseX,
  69. className,
  70. children,
  71. tooltip,
  72. url,
  73. ...props
  74. }: DockIconProps) => {
  75. const ref = useRef<HTMLDivElement>(null);
  76. const distanceCalc = useTransform(mouseX, (val: number) => {
  77. const bounds = ref.current?.getBoundingClientRect() ?? { x: 0, width: 0 };
  78. return val - bounds.x - bounds.width / 2;
  79. });
  80. let widthSync = useTransform(
  81. distanceCalc,
  82. [-distance, 0, distance],
  83. [40, magnification, 40]
  84. );
  85. let width = useSpring(widthSync, {
  86. mass: 0.1,
  87. stiffness: 150,
  88. damping: 12,
  89. });
  90. return (
  91. <Tooltip content={tooltip} placement="bottom">
  92. <Link isExternal color="foreground" href={url}>
  93. <motion.div
  94. ref={ref}
  95. className={cn(
  96. "flex aspect-square cursor-pointer items-center justify-center rounded-full bg-midnight border-2 border-transparent dark:border-knight dark:bg-darkBg",
  97. className
  98. )}
  99. style={{ width }}
  100. {...props}
  101. >
  102. {children}
  103. </motion.div>
  104. </Link>
  105. </Tooltip>
  106. );
  107. };
  108. DockIcon.displayName = "DockIcon";
  109. export { Dock, DockIcon, dockVariants };