feat: image resizer
This commit is contained in:
@@ -26,6 +26,8 @@ type Props = {
|
||||
children?: React.ReactNode;
|
||||
caption?: string;
|
||||
onCaptionChange?: (caption: string) => void;
|
||||
imgRef?: React.Ref<HTMLImageElement>;
|
||||
resizeHandle?: React.ReactNode;
|
||||
};
|
||||
|
||||
const MAX_SIZE = 5 * 1024 * 1024;
|
||||
@@ -56,7 +58,7 @@ function fileToFileList(file: File): FileList {
|
||||
return dt.files;
|
||||
}
|
||||
|
||||
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, previewStyle, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange }: Props) {
|
||||
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, previewStyle, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange, imgRef, resizeHandle }: Props) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
||||
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null);
|
||||
@@ -124,16 +126,26 @@ export function ImageUploader({ id, name, label, description, error, variant = "
|
||||
|
||||
{displayUrl ? (
|
||||
<div className={EDITOR_WRAPPER_CLASS}>
|
||||
<div className="group relative rounded-md">
|
||||
<img src={displayUrl} alt="" className={previewClassName ?? EDITOR_IMG_CLASS} style={previewStyle} />
|
||||
<div
|
||||
className="relative rounded-md max-w-full"
|
||||
style={previewStyle}
|
||||
>
|
||||
<img
|
||||
ref={imgRef}
|
||||
src={displayUrl}
|
||||
alt=""
|
||||
className={previewClassName ?? "block w-full h-auto rounded-md border object-contain"}
|
||||
/>
|
||||
|
||||
<div className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<div className="absolute top-2 right-2">
|
||||
<Button type="button" variant="secondary" size="sm" onClick={() => inputRef.current?.click()} disabled={isUploading}>
|
||||
<RefreshCwIcon />
|
||||
Replace
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{resizeHandle}
|
||||
|
||||
{isUploading && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-background/90 rounded-md">
|
||||
<Spinner />
|
||||
|
||||
Reference in New Issue
Block a user