이미지(Image) v2 → v3 마이그레이션
HeroUI v3에서 Image 컴포넌트가 제거된 내용을 정리한 가이드예요. 이 컴포넌트는 v3에서 없어졌으므로, 네이티브 HTML <img> 요소나 Next.js Image 컴포넌트를 Tailwind CSS 클래스와 함께 사용해야 해요.
출처: HeroUI
본문
참고: Image 컴포넌트는 HeroUI v3에서 제거되었어요. 대신 네이티브 HTML <img> 요소 또는 Next.js Image 컴포넌트를 Tailwind CSS 클래스와 함께 사용하세요.
주요 변경 사항
1. 컴포넌트 제거
v2: @heroui/react의 <Image> 컴포넌트
v3: 네이티브 HTML <img> 요소 또는 Next.js Image 컴포넌트
2. 기능 매핑
v2 Image 컴포넌트의 몇 가지 기능은 따로 대체해야 해요:
| v2 Feature | v3 Equivalent | Notes |
|---|---|---|
radius prop |
rounded-* Tailwind classes |
Use rounded-sm, rounded-md, rounded-lg, rounded-full |
shadow prop |
shadow-* Tailwind classes |
Use shadow-sm, shadow-md, shadow-lg |
isBlurred prop |
Manual blur implementation | Use CSS filter: blur() or Tailwind blur-* utilities |
isZoomed prop |
Manual hover zoom | Use hover:scale-* Tailwind classes |
fallbackSrc prop |
Manual error handling | Use onError handler with state |
disableSkeleton / Loading skeleton |
Manual loading state | Use React state + conditional rendering |
removeWrapper prop |
Direct rendering | No wrapper needed, render <img> directly |
구조 변화
v2에서는 Image가 네이티브 <img> 요소를 감싸는 컴포넌트였어요:
import { Image } from "@heroui/react";
export default function App() {
return (
<Image
src="https://example.com/image.jpg"
alt="Example image"
width={300}
height={200}
/>
);
}
v3에서는 네이티브 <img> 요소를 Tailwind CSS 클래스와 함께 직접 사용해요:
export default function App() {
return (
<img
src="https://example.com/image.jpg"
alt="Example image"
width={300}
height={200}
className="rounded-lg"
/>
);
}
마이그레이션 예제
Next.js Image와 함께 사용 (권장)
Next.js를 사용한다면 최적화된 Image 컴포넌트를 사용하세요:
v2:
import { Image } from "@heroui/react";
<Image
src="/image.jpg"
alt="Example"
width={300}
height={200}
/>
v3:
import Image from "next/image";
<Image
src="/image.jpg"
alt="Example"
width={300}
height={200}
className="rounded-lg"
/>
확대 효과와 함께
v2:
<Image isZoomed src="..." alt="..." />
v3:
<div className="relative overflow-hidden rounded-lg">
<img
src="..."
alt="..."
className="object-cover transition-transform duration-300 hover:scale-125"
/>
</div>
블러 효과와 함께
v2:
<Image isBlurred src="..." alt="..." />
v3:
<div className="relative">
<img
src="..."
alt="..."
className="relative z-10"
/>
<img
src="..."
alt=""
aria-hidden="true"
className="absolute inset-0 z-0 h-full w-full scale-105 object-cover blur-lg opacity-30 saturate-150"
/>
</div>
대체 이미지(Fallback)와 함께
v2:
<Image
src="https://example.com/image.jpg"
fallbackSrc="/fallback.jpg"
alt="Example"
/>
v3:
import { useState } from "react";
function ImageWithFallback({ src, fallbackSrc, alt, ...props }) {
const [imgSrc, setImgSrc] = useState(src);
return (
<img
src={imgSrc}
alt={alt}
onError={() => setImgSrc(fallbackSrc)}
{...props}
/>
);
}
<ImageWithFallback
src="https://example.com/image.jpg"
fallbackSrc="/fallback.jpg"
alt="Example"
/>
로딩 스켈레톤과 함께
v2:
<Image
src="https://example.com/image.jpg"
alt="Example"
disableSkeleton={false}
/>
v3:
import { useState } from "react";
function ImageWithSkeleton({ src, alt, ...props }) {
const [isLoading, setIsLoading] = useState(true);
const [hasError, setHasError] = useState(false);
return (
<div className="relative overflow-hidden rounded-lg bg-default-200">
{isLoading && (
<div className="absolute inset-0 animate-pulse bg-gradient-to-r from-transparent via-default-300 to-transparent" />
)}
<img
src={src}
alt={alt}
className={`transition-opacity duration-300 ${
isLoading ? "opacity-0" : "opacity-100"
}`}
onLoad={() => setIsLoading(false)}
onError={() => {
setIsLoading(false);
setHasError(true);
}}
{...props}
/>
</div>
);
}
<ImageWithSkeleton src="https://example.com/image.jpg" alt="Example" />
기능 결합
v2:
<Image
src="https://example.com/image.jpg"
alt="Example"
radius="lg"
shadow="md"
isZoomed
isBlurred
width={400}
height={300}
/>
v3:
<div className="relative overflow-hidden rounded-lg shadow-md">
<img
src="https://example.com/image.jpg"
alt="Example"
width={400}
height={300}
className="relative z-10 object-cover transition-transform duration-300 hover:scale-125"
/>
<img
src="https://example.com/image.jpg"
alt=""
aria-hidden="true"
className="absolute inset-0 z-0 h-full w-full scale-105 object-cover blur-lg opacity-30 saturate-150"
/>
</div>
재사용 가능한 Image 컴포넌트 만들기 (선택 사항)
비슷한 기능을 가진 이미지를 자주 사용한다면 재사용 가능한 컴포넌트를 만들 수 있어요:
v2:
import { Image } from "@heroui/react";
<Image
src="..."
radius="lg"
shadow="md"
isZoomed
/>
v3:
import { useState } from "react";
import { cn } from "@/lib/utils"; // or your cn utility
interface CustomImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {
radius?: "none" | "sm" | "md" | "lg" | "full";
shadow?: "none" | "sm" | "md" | "lg";
isZoomed?: boolean;
isBlurred?: boolean;
fallbackSrc?: string;
}
const radiusClasses = {
none: "rounded-none",
sm: "rounded-sm",
md: "rounded-md",
lg: "rounded-lg",
full: "rounded-full",
};
const shadowClasses = {
none: "shadow-none",
sm: "shadow-sm",
md: "shadow-md",
lg: "shadow-lg",
};
export function CustomImage({
src,
alt,
className,
radius = "lg",
shadow = "none",
isZoomed = false,
isBlurred = false,
fallbackSrc,
onError,
...props
}: CustomImageProps) {
const [imgSrc, setImgSrc] = useState(src);
const [isLoading, setIsLoading] = useState(true);
const handleError = (e: React.SyntheticEvent<HTMLImageElement, Event>) => {
if (fallbackSrc && imgSrc !== fallbackSrc) {
setImgSrc(fallbackSrc);
}
onError?.(e);
};
const imageElement = (
<img
src={imgSrc}
alt={alt}
className={cn(
radiusClasses[radius],
shadowClasses[shadow],
isZoomed && "object-cover transition-transform duration-300 hover:scale-125",
isLoading && "opacity-0",
"transition-opacity duration-300",
className
)}
onLoad={() => setIsLoading(false)}
onError={handleError}
{...props}
/>
);
if (isBlurred) {
return (
<div className={cn("relative", radiusClasses[radius], shadowClasses[shadow])}>
{imageElement}
<img
src={imgSrc}
alt=""
aria-hidden="true"
className={cn(
"absolute inset-0 z-0 h-full w-full scale-105 object-cover blur-lg opacity-30 saturate-150",
radiusClasses[radius]
)}
/>
</div>
);
}
if (isZoomed || isLoading) {
return (
<div className={cn("relative overflow-hidden", radiusClasses[radius], shadowClasses[shadow])}>
{isLoading && (
<div className="absolute inset-0 animate-pulse bg-gradient-to-r from-transparent via-default-300 to-transparent" />
)}
{imageElement}
</div>
);
}
return imageElement;
}
// Usage
<CustomImage
src="..."
alt="Example"
radius="lg"
shadow="md"
isZoomed
isBlurred
fallbackSrc="/fallback.jpg"
/>
완전한 예제
v2:
import { Image } from "@heroui/react";
export default function App() {
return (
<div className="space-y-4">
<Image
src="https://example.com/image1.jpg"
alt="Image 1"
width={300}
height={200}
radius="lg"
shadow="md"
/>
<Image
src="https://example.com/image2.jpg"
alt="Image 2"
width={300}
height={200}
isZoomed
radius="lg"
/>
<Image
src="https://example.com/image3.jpg"
alt="Image 3"
width={300}
height={200}
isBlurred
radius="full"
/>
</div>
);
}
v3:
export default function App() {
return (
<div className="space-y-4">
<img
src="https://example.com/image1.jpg"
alt="Image 1"
width={300}
height={200}
className="rounded-lg shadow-md"
/>
<div className="relative overflow-hidden rounded-lg">
<img
src="https://example.com/image2.jpg"
alt="Image 2"
width={300}
height={200}
className="object-cover transition-transform duration-300 hover:scale-125"
/>
</div>
<div className="relative rounded-full">
<img
src="https://example.com/image3.jpg"
alt="Image 3"
width={300}
height={300}
className="relative z-10 rounded-full"
/>
<img
src="https://example.com/image3.jpg"
alt=""
aria-hidden="true"
className="absolute inset-0 z-0 h-full w-full scale-105 object-cover rounded-full blur-lg opacity-30 saturate-150"
/>
</div>
</div>
);
}
요약
- 컴포넌트 제거:
Image컴포넌트는 v3에 더 이상 존재하지 않음 - Import 변경:
import { Image } from "@heroui/react"제거 - 네이티브 요소 사용: 네이티브 HTML
<img>요소 또는 Next.jsImage로 교체 - 기능: 블러, 확대, 스켈레톤, 폴백을 직접 구현
- 스타일링: radius, shadow, 효과에 Tailwind CSS 클래스를 직접 적용
마이그레이션 단계
- Import 제거:
@heroui/reactimport에서Image제거 - 컴포넌트 교체: 모든
<Image>인스턴스를<img>요소로 교체 - Tailwind 클래스 추가: 스타일링(radius, shadow)에 해당하는 Tailwind 클래스 적용
- 기능 구현: 필요하다면 블러, 확대, 스켈레톤, 폴백을 직접 구현
- Next.js Image 사용: Next.js를 쓴다면 최적화를 위해
next/image사용 고려 - 선택 사항: 자주 쓰는 패턴에 대해 재사용 가능한 래퍼 컴포넌트 생성
Next.js Image 컴포넌트
Next.js를 사용한다면 next/image의 Image 컴포넌트가 제공하는 기능은 다음과 같아요:
- 자동 이미지 최적화
- 기본 지연 로딩(lazy loading)
srcSet을 통한 반응형 이미지- 블러 자리 표시자(placeholder) 지원
- 내장 로딩 상태
import Image from "next/image";
<Image
src="/image.jpg"
alt="Example"
width={300}
height={200}
className="rounded-lg shadow-md"
placeholder="blur" // Optional: blur placeholder
blurDataURL="data:image/..." // Optional: base64 blur data
/>