/** @jsxImportSource @emotion/react */ import { LoadingSpinner } from '@/@dble_layout'; import React, { ChangeEvent, ReactNode, useRef } from 'react'; interface Props { children?: ReactNode; theme?: 'light' | 'dark'; onUpload: (e: any) => void; onCancel: () => void; loading?: boolean; source: string; alt?: string; size?: { zIndex?: number; width?: 'auto' | '100%' | '100vw'; height?: 'auto' | '100%' | '100vh'; minWidth?: number | string; maxWidth?: number | string; minHeight?: number | string; maxHeight?: number | string; }; ratio?: { x?: number; y?: number }; borderRadius?: number; backgroundColor?: string; } // const ImageUploader = (props: Props) => { const { source, alt = '업로드 이미지', onUpload, onCancel, loading } = props; const { width = '100%', minWidth, maxWidth } = props.size ?? {}; const { height, minHeight = 360, maxHeight } = props.size ?? {}; const { theme = 'light', ratio = { x: 2, y: 1 } } = props; const { backgroundColor, borderRadius = 18 } = props; const uploadRef = useRef(null); const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); if (e.dataTransfer.files && e.dataTransfer.files[0]) { encodeFileToBase64(e.dataTransfer.files[0]); } }; const encodeFileToBase64 = (fileBlob: File) => { const reader = new FileReader(); reader.readAsDataURL(fileBlob); reader.onload = () => onUpload(reader.result); reader.onerror = error => console.error('File reading error:', error); }; const handleOnUpload = (e: ChangeEvent) => { if (e.target.files && e.target.files[0]) { encodeFileToBase64(e.target.files[0]); } }; const VARIANTS = { light: { readBg: '#f5f5f5', cancelTabColor: '#cccccc' }, dark: { readBg: '#333', cancelTabColor: '#666' }, } as const; return (
{source ? ( <> {alt} uploadRef.current?.click()} css={{ borderRadius, width: '100%', height: '100%', minWidth: minWidth, minHeight: minHeight, maxWidth: maxWidth, maxHeight: maxHeight, objectFit: 'cover', }} /> <> {!!onCancel && (
취소
)} ) : (
{loading ? ( ) : ( <>{props.children ?? } )}
)}
); }; // // const CameraIcon = ({ size, fill }: { size: number; fill: string }) => { return ( ); }; const themes: any = { button: { borderRadius: 100, display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'absolute', bottom: 0, right: 0, zIndex: 10, maxWidth: 28, minWidth: 28, maxHeight: 28, minHeight: 28, }, input: { width: '100%', height: '100%', position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, opacity: '0', cursor: 'pointer', }, }; export default ImageUploader;