-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathindex.tsx
More file actions
188 lines (176 loc) · 5 KB
/
index.tsx
File metadata and controls
188 lines (176 loc) · 5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* ImageZoom Component
*
* A React component that adds a click-to-zoom feature to images with smooth animations.
*
* Features:
* - Smooth fade and scale animations
* - Responsive sizing (max 90% of viewport)
* - Dark overlay background
* - Close on overlay click, escape key, or close button
* - Mobile-friendly
*
* Usage in MDX files:
* ```mdx
* import { ImageZoom } from '@site/src/components/ImageZoom';
*
* <ImageZoom
* src="path/to/your/image.png"
* alt="Description of the image"
* className="optional-custom-class"
* />
* ```
*
* Usage in React components:
* ```tsx
* import { ImageZoom } from '@site/src/components/ImageZoom';
*
* function YourComponent() {
* return (
* <ImageZoom
* src="/images/example.png"
* alt="Example image"
* className="optional-styling"
* />
* );
* }
* ```
*
* Props:
* @param {string} src - The source URL of the image
* @param {string} [alt] - Optional alt text for the image
* @param {string} [className] - Optional CSS class name for additional styling
*
* Dependencies:
* - @react-spring/web (for animations)
* - styled-components (for styling)
* - @radix-ui/react-dialog (for modal functionality)
*/
import React, { useState } from 'react';
import { useTransition, animated } from '@react-spring/web';
import styled from 'styled-components';
import * as Dialog from '@radix-ui/react-dialog';
import { createPortal } from 'react-dom';
interface ImageZoomProps {
src: string;
alt?: string;
className?: string;
}
export function ImageZoom({ src, alt, className }: ImageZoomProps) {
const [isOpen, setIsOpen] = useState(false);
const transitions = useTransition(isOpen, {
from: { opacity: 0, transform: 'scale(0.95)' },
enter: { opacity: 1, transform: 'scale(1)' },
leave: { opacity: 0, transform: 'scale(0.95)' },
config: { tension: 300, friction: 20 },
});
const overlayTransitions = useTransition(isOpen, {
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0 },
config: { duration: 200 },
});
return (
<>
<img
src={src}
alt={alt || ''}
className={`cursor-zoom-in ${className || ''}`}
onClick={() => setIsOpen(true)}
/>
{typeof document !== 'undefined' &&
createPortal(
<Dialog.Root open={isOpen} onOpenChange={setIsOpen}>
<Dialog.Portal>
{overlayTransitions(
(styles, item) =>
item && <OverlayBackground style={styles} onClick={() => setIsOpen(false)} />,
)}
{transitions(
(styles, item) =>
item && (
<div
style={{
position: 'fixed',
top: 0,
left: 0,
right: 0,
bottom: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
padding: '20px',
zIndex: 10000,
pointerEvents: 'none',
}}
>
<Content
style={{
...styles,
pointerEvents: 'auto',
}}
>
<CloseButton onClick={() => setIsOpen(false)}>
<CloseIcon />
</CloseButton>
<ZoomedImage
src={src}
alt={alt || ''}
onClick={(e) => e.stopPropagation()}
/>
</Content>
</div>
),
)}
</Dialog.Portal>
</Dialog.Root>,
document.body,
)}
</>
);
}
const OverlayBackground = styled(animated(Dialog.Overlay))`
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.8);
position: fixed;
inset: 0;
z-index: 9999;
cursor: zoom-out;
`;
const Content = styled(animated.div)`
position: relative;
max-width: 90vw;
max-height: 90vh;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
`;
const ZoomedImage = styled.img`
max-width: 90vw;
max-height: 90vh;
object-fit: contain;
border-radius: 4px;
`;
const CloseButton = styled.button`
position: absolute;
top: -40px;
right: 0;
background: none;
border: none;
color: white;
cursor: pointer;
padding: 8px;
&:hover {
opacity: 0.8;
}
`;
const CloseIcon = () => (
<svg width="24" height="24" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M15.9574 14.1689L8.59651 6.75098L6.73232 8.59598L14.1313 16.071L6.71338 23.4129L8.5964 25.2769L15.9574 17.8779L23.3943 25.2769L25.2392 23.4129L17.8213 16.071L25.2202 8.59598L23.3752 6.75098L15.9574 14.1689Z"
fill="currentColor"
/>
</svg>
);