mini-pic.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { useTheme } from "next-themes";
  2. import { Image } from "@heroui/react";
  3. import { useRef } from "react";
  4. interface MiniPicProps {
  5. onClick?: () => void;
  6. showOverlay?: boolean;
  7. }
  8. const MiniPic = ({ onClick, showOverlay = false }: MiniPicProps) => {
  9. const { theme } = useTheme();
  10. const isDark = theme === "dark";
  11. const startPos = useRef<{ x: number; y: number } | null>(null);
  12. const handleStart = (x: number, y: number) => {
  13. startPos.current = { x, y };
  14. };
  15. const handleEnd = (x: number, y: number) => {
  16. if (!startPos.current) return;
  17. const dx = Math.abs(x - startPos.current.x);
  18. const dy = Math.abs(y - startPos.current.y);
  19. if (dx < 5 && dy < 5) {
  20. onClick?.();
  21. }
  22. startPos.current = null;
  23. };
  24. return (
  25. <div
  26. className="relative w-full h-full cursor-pointer"
  27. onMouseDown={(e) => handleStart(e.clientX, e.clientY)}
  28. onMouseUp={(e) => handleEnd(e.clientX, e.clientY)}
  29. onTouchStart={(e) => handleStart(e.touches[0].clientX, e.touches[0].clientY)}
  30. onTouchEnd={(e) => handleEnd(e.changedTouches[0].clientX, e.changedTouches[0].clientY)}
  31. >
  32. {isDark ? (
  33. <Image
  34. alt="cover"
  35. className="rounded-b-none object-cover"
  36. src="mini-dark.jpg"
  37. />
  38. ) : (
  39. <Image
  40. alt="cover"
  41. className="rounded-b-none object-cover"
  42. src="mini-light.jpg"
  43. />
  44. )}
  45. {showOverlay && (
  46. <div className="absolute inset-0 flex items-end justify-center pb-4 z-10 pointer-events-none">
  47. <span className="bg-black/50 text-white text-xs px-3 py-1.5 rounded-full backdrop-blur-sm">
  48. Click for 3D
  49. </span>
  50. </div>
  51. )}
  52. </div>
  53. );
  54. };
  55. export default MiniPic;