forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvatarCropModal.tsx
More file actions
176 lines (160 loc) · 4.92 KB
/
AvatarCropModal.tsx
File metadata and controls
176 lines (160 loc) · 4.92 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
import * as React from 'react'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import Cropper from 'react-easy-crop'
import type { Area, Point } from 'react-easy-crop'
import { X } from 'lucide-react'
import { Button } from '~/ui'
interface AvatarCropModalProps {
open: boolean
onOpenChange: (open: boolean) => void
imageSrc: string
onCropComplete: (croppedBlob: Blob) => void
}
async function getCroppedImg(
imageSrc: string,
pixelCrop: Area,
outputSize: number = 256,
): Promise<Blob> {
const image = new Image()
image.crossOrigin = 'anonymous'
await new Promise<void>((resolve, reject) => {
image.onload = () => resolve()
image.onerror = reject
image.src = imageSrc
})
const canvas = document.createElement('canvas')
canvas.width = outputSize
canvas.height = outputSize
const ctx = canvas.getContext('2d')
if (!ctx) {
throw new Error('Could not get canvas context')
}
ctx.drawImage(
image,
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height,
0,
0,
outputSize,
outputSize,
)
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (blob) {
resolve(blob)
} else {
reject(new Error('Could not create blob'))
}
},
'image/jpeg',
0.9,
)
})
}
export function AvatarCropModal({
open,
onOpenChange,
imageSrc,
onCropComplete,
}: AvatarCropModalProps) {
const [crop, setCrop] = React.useState<Point>({ x: 0, y: 0 })
const [zoom, setZoom] = React.useState(1)
const [croppedAreaPixels, setCroppedAreaPixels] = React.useState<Area | null>(
null,
)
const [isProcessing, setIsProcessing] = React.useState(false)
const handleCropComplete = React.useCallback(
(_croppedArea: Area, croppedAreaPixels: Area) => {
setCroppedAreaPixels(croppedAreaPixels)
},
[],
)
const handleSave = React.useCallback(async () => {
if (!croppedAreaPixels) return
setIsProcessing(true)
try {
const croppedBlob = await getCroppedImg(imageSrc, croppedAreaPixels)
onCropComplete(croppedBlob)
onOpenChange(false)
} catch (error) {
console.error('Error cropping image:', error)
} finally {
setIsProcessing(false)
}
}, [imageSrc, croppedAreaPixels, onCropComplete, onOpenChange])
// Reset state when modal opens
React.useEffect(() => {
if (open) {
setCrop({ x: 0, y: 0 })
setZoom(1)
setCroppedAreaPixels(null)
}
}, [open])
return (
<DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>
<DialogPrimitive.Portal>
<DialogPrimitive.Overlay className="fixed inset-0 z-[999] bg-black/60 backdrop-blur-sm" />
<DialogPrimitive.Content className="fixed left-1/2 top-1/2 z-[1000] w-full max-w-md -translate-x-1/2 -translate-y-1/2 rounded-xl bg-white dark:bg-gray-900 p-6 shadow-xl">
<div className="flex items-center justify-between mb-4">
<DialogPrimitive.Title className="text-lg font-semibold text-gray-900 dark:text-gray-100">
Crop Profile Picture
</DialogPrimitive.Title>
<DialogPrimitive.Close className="rounded-full p-1 hover:bg-gray-100 dark:hover:bg-gray-800">
<X className="w-5 h-5 text-gray-500" />
</DialogPrimitive.Close>
</div>
<div className="relative w-full aspect-square bg-gray-100 dark:bg-gray-800 rounded-lg overflow-hidden">
<Cropper
image={imageSrc}
crop={crop}
zoom={zoom}
aspect={1}
cropShape="round"
showGrid={false}
onCropChange={setCrop}
onZoomChange={setZoom}
onCropComplete={handleCropComplete}
/>
</div>
<div className="mt-4">
<label
htmlFor="zoom"
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
>
Zoom
</label>
<input
id="zoom"
type="range"
min={1}
max={3}
step={0.1}
value={zoom}
onChange={(e) => setZoom(Number(e.target.value))}
className="w-full h-2 bg-gray-200 dark:bg-gray-700 rounded-lg appearance-none cursor-pointer accent-blue-500"
/>
</div>
<div className="mt-6 flex gap-3 justify-end">
<Button
variant="secondary"
size="sm"
onClick={() => onOpenChange(false)}
>
Cancel
</Button>
<Button
size="sm"
onClick={handleSave}
disabled={isProcessing || !croppedAreaPixels}
>
{isProcessing ? 'Processing...' : 'Save'}
</Button>
</div>
</DialogPrimitive.Content>
</DialogPrimitive.Portal>
</DialogPrimitive.Root>
)
}