File size: 3,650 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { useState } from 'react';
import { Download, X, ZoomIn, ZoomOut, RotateCw } from 'lucide-react';

const ImagePreview = ({ src, alt, onClose, onDownload }) => {
  const [scale, setScale] = useState(1);
  const [rotation, setRotation] = useState(0);

  const handleZoomIn = () => {
    setScale(prev => Math.min(prev + 0.25, 3));
  };

  const handleZoomOut = () => {
    setScale(prev => Math.max(prev - 0.25, 0.5));
  };

  const handleRotate = () => {
    setRotation(prev => (prev + 90) % 360);
  };

  const handleReset = () => {
    setScale(1);
    setRotation(0);
  };

  const handleWheel = (e) => {
    e.preventDefault();
    const delta = e.deltaY > 0 ? -0.1 : 0.1;
    setScale(prev => Math.max(0.5, Math.min(3, prev + delta)));
  };

  return (
    <div 

      className="fixed inset-0 bg-black bg-opacity-90 z-50 flex items-center justify-center p-4"

      onClick={onClose}

    >

      <div 

        className="relative max-w-full max-h-full"

        onClick={(e) => e.stopPropagation()}

        onWheel={handleWheel}

      >

        {/* Image */}

        <img

          src={src}

          alt={alt}

          className="max-w-full max-h-full object-contain transition-transform duration-200"

          style={{ 

            transform: `scale(${scale}) rotate(${rotation}deg)`,

            cursor: scale > 1 ? 'grab' : 'default'

          }}

          draggable={false}

        />



        {/* Controls */}

        <div className="absolute top-4 right-4 flex space-x-2">

          <button

            onClick={handleZoomIn}

            className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

            title="Zoom In"

          >

            <ZoomIn size={20} />

          </button>

          <button

            onClick={handleZoomOut}

            className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

            title="Zoom Out"

          >

            <ZoomOut size={20} />

          </button>

          <button

            onClick={handleRotate}

            className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

            title="Rotate"

          >

            <RotateCw size={20} />

          </button>

          <button

            onClick={handleReset}

            className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

            title="Reset"

          >

            <span className="text-sm font-medium">Reset</span>

          </button>

          {onDownload && (

            <button

              onClick={onDownload}

              className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

              title="Download"

            >

              <Download size={20} />

            </button>

          )}

          <button

            onClick={onClose}

            className="bg-white bg-opacity-20 text-white p-3 rounded-lg hover:bg-opacity-30 transition-colors backdrop-blur-sm"

            title="Close"

          >

            <X size={20} />

          </button>

        </div>



        {/* Zoom Level Indicator */}

        <div className="absolute bottom-4 left-4 bg-black bg-opacity-50 text-white px-3 py-2 rounded-lg backdrop-blur-sm">

          <span className="text-sm">{Math.round(scale * 100)}%</span>

        </div>

      </div>

    </div>
  );
};

export default ImagePreview;