Browse Source

add zoom in zoom out button

master
azri 3 weeks ago
parent
commit
4f899d0fd2
1 changed files with 60 additions and 5 deletions
  1. 60
    5
      src/components/ImageView/ImageView.jsx

+ 60
- 5
src/components/ImageView/ImageView.jsx View File

9
 } from "@mui/material";
9
 } from "@mui/material";
10
 import Grid from '@mui/material/Grid2';
10
 import Grid from '@mui/material/Grid2';
11
 import CloseIcon from "@mui/icons-material/Close";
11
 import CloseIcon from "@mui/icons-material/Close";
12
+import ZoomInIcon from '@mui/icons-material/ZoomIn';
13
+import ZoomOutIcon from '@mui/icons-material/ZoomOut';
12
 import defaultImage from "../../assets/images/default.png";
14
 import defaultImage from "../../assets/images/default.png";
13
 import { Carousel } from 'react-responsive-carousel';
15
 import { Carousel } from 'react-responsive-carousel';
14
 
16
 
21
 
23
 
22
   const [previewImage, setPreviewImage] = useState("");
24
   const [previewImage, setPreviewImage] = useState("");
23
   const [isZoomed, setIsZoomed] = useState(false);
25
   const [isZoomed, setIsZoomed] = useState(false);
26
+  const [zoomSize, setZoomSize] = useState(100)
24
   const [currentIndex, setCurrentIndex] = useState(0);
27
   const [currentIndex, setCurrentIndex] = useState(0);
25
 
28
 
26
   useEffect(() => {
29
   useEffect(() => {
43
     setPreviewImage(product?.images[index]?.url)
46
     setPreviewImage(product?.images[index]?.url)
44
   };
47
   };
45
 
48
 
49
+  const onZoomIn = () => {
50
+    if (zoomSize > 310) return
51
+    setZoomSize(zoomSize + 20)
52
+  }
53
+
54
+  const onZoomOut = () => {
55
+    if (zoomSize < 40) return
56
+    setZoomSize(zoomSize - 20)
57
+  }
58
+
59
+
46
   return (
60
   return (
47
     <Box sx={{ flexGrow: 1 }}>
61
     <Box sx={{ flexGrow: 1 }}>
48
       <Grid container spacing={2}>
62
       <Grid container spacing={2}>
60
             }}
74
             }}
61
           >
75
           >
62
 
76
 
63
-            <Carousel showStatus={false} showIndicators={false} onClickItem={() => { handleZoomToggle() }} onChange={(index) => handleSlideChange(index)} selectedItem={currentIndex}>
77
+            <Carousel
78
+              showStatus={false}
79
+              showIndicators={false}
80
+              onClickItem={() => {
81
+                handleZoomToggle()
82
+                setZoomSize(100)
83
+              }}
84
+              onChange={(index) => handleSlideChange(index)}
85
+              selectedItem={currentIndex}>
64
               {product?.images?.map(({ src, url }, index) => (
86
               {product?.images?.map(({ src, url }, index) => (
65
                 <Box
87
                 <Box
66
                   key={index}
88
                   key={index}
106
                   setCurrentIndex(index)
128
                   setCurrentIndex(index)
107
                 }}
129
                 }}
108
                 sx={{
130
                 sx={{
109
-                  p:0.4,
131
+                  p: 0.4,
110
                   borderRadius: 1,
132
                   borderRadius: 1,
111
                   backgroundColor: (previewImage == url) ? "#95AAC5" : "none"
133
                   backgroundColor: (previewImage == url) ? "#95AAC5" : "none"
112
                 }}
134
                 }}
128
       </Grid>
150
       </Grid>
129
 
151
 
130
       {/* Zoom Dialog */}
152
       {/* Zoom Dialog */}
131
-      <Dialog open={isZoomed} onClose={handleZoomToggle} maxWidth="lg">
153
+      <Dialog fullScreen open={isZoomed} onClose={handleZoomToggle} maxWidth="lg">
132
         <DialogContent
154
         <DialogContent
133
           sx={{
155
           sx={{
134
             position: "relative",
156
             position: "relative",
135
             overflow: "hidden", // Prevent scrolling on DialogContent
157
             overflow: "hidden", // Prevent scrolling on DialogContent
136
             height: "90vh", // Adjust height as needed
158
             height: "90vh", // Adjust height as needed
159
+            p: 0
137
           }}
160
           }}
138
         >
161
         >
139
           {/* Fixed position close button */}
162
           {/* Fixed position close button */}
143
               height: 50,
166
               height: 50,
144
               position: "absolute",
167
               position: "absolute",
145
               top: 10,
168
               top: 10,
146
-              right: 1.5,
169
+              right: {
170
+                xs: 1.5,
171
+                sm: 1.5,
172
+                md: 10
173
+              },
147
               zIndex: 2, // Ensure it stays above other content
174
               zIndex: 2, // Ensure it stays above other content
148
             }}
175
             }}
149
           >
176
           >
159
             >
186
             >
160
               <CloseIcon />
187
               <CloseIcon />
161
             </IconButton>
188
             </IconButton>
189
+            <IconButton
190
+              onClick={onZoomIn}
191
+              sx={{
192
+                backgroundColor: "white",
193
+                color: "black",
194
+                mt: 1,
195
+                "&:hover": {
196
+                  backgroundColor: "lightgray",
197
+                },
198
+              }}
199
+            >
200
+              <ZoomInIcon />
201
+            </IconButton>
202
+            <IconButton
203
+              onClick={onZoomOut}
204
+              sx={{
205
+                backgroundColor: "white",
206
+                color: "black",
207
+                mt: 1,
208
+                "&:hover": {
209
+                  backgroundColor: "lightgray",
210
+                },
211
+              }}
212
+            >
213
+              <ZoomOutIcon />
214
+            </IconButton>
162
           </Box>
215
           </Box>
163
 
216
 
164
           {/* Scrollable content */}
217
           {/* Scrollable content */}
166
             sx={{
219
             sx={{
167
               overflow: "auto", // Allow scrolling within this Box
220
               overflow: "auto", // Allow scrolling within this Box
168
               height: "100%", // Fill available height of DialogContent
221
               height: "100%", // Fill available height of DialogContent
222
+              display: "flex"
169
             }}
223
             }}
170
           >
224
           >
171
             <img
225
             <img
172
               src={previewImage || defaultImage}
226
               src={previewImage || defaultImage}
173
               alt="Zoomed Preview"
227
               alt="Zoomed Preview"
174
               style={{
228
               style={{
175
-                width: "100%",
229
+                width: `${zoomSize}%`,
230
+                margin: "auto auto",
176
                 height: "auto",
231
                 height: "auto",
177
               }}
232
               }}
178
             />
233
             />

Loading…
Cancel
Save