애니메이션 (Animation)
HeroUI v3 컴포넌트에 부드러운 애니메이션과 전환을 추가하는 방법을 안내해요.
출처: HeroUI Animation
본문
HeroUI 컴포넌트는 내장 CSS 전환, 사용자 지정 CSS 애니메이션, Framer Motion 같은 JavaScript 라이브러리를 포함한 여러 애니메이션 방식을 지원해요.
내장 애니메이션 (Built-in Animations)
HeroUI 컴포넌트는 상태를 애니메이션에 노출하기 위해 데이터 속성을 사용해요:
/* Popover entrance/exit */
.popover[data-entering] {
@apply animate-in zoom-in-90 fade-in-0 duration-200;
}
.popover[data-exiting] {
@apply animate-out zoom-out-95 fade-out duration-150;
}
/* Button press effect */
.button:active,
.button[data-pressed="true"] {
transform: scale(0.97);
}
/* Accordion expansion */
.accordion__panel[aria-hidden="false"] {
@apply h-[var(--panel-height)] opacity-100;
}
스타일링을 위한 상태 속성:
[data-hovered="true"]- 호버 상태[data-pressed="true"]- 활성/눌림 상태[data-focus-visible="true"]- 키보드 포커스[data-disabled="true"]- 비활성 상태[data-entering]/[data-exiting]- 전환 상태[aria-expanded="true"]- 펼침 상태
CSS 애니메이션 (CSS Animations)
Tailwind 유틸리티 사용:
// Pulse on hover
<Button className="hover:animate-pulse">
Hover me
</Button>
// Fade in entrance
<Alert className="animate-fade-in">
Welcome message
</Alert>
// Staggered list
<div className="space-y-2">
<Card className="animate-fade-in animate-delay-100">Item 1</Card>
<Card className="animate-fade-in animate-delay-200">Item 2</Card>
</div>
사용자 지정 전환 (Custom transitions):
/* Slower accordion */
.accordion__panel {
@apply transition-all duration-500;
}
/* Bouncy button */
.button:active {
animation: bounce 0.3s;
}
@keyframes bounce {
50% { transform: scale(0.95); }
}
Framer Motion
HeroUI 컴포넌트는 고급 애니메이션을 위해 Framer Motion과 원활하게 동작해요.
기본 사용 (Basic usage):
import { motion } from 'framer-motion';
import { Button } from '@heroui/react';
const MotionButton = motion(Button);
<MotionButton
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
Animated Button
</MotionButton>
등장 애니메이션 (Entrance animations):
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
<Alert>
<Alert.Title>Welcome!</Alert.Title>
</Alert>
</motion.div>
레이아웃 애니메이션 (Layout animations):
import { AnimatePresence, motion } from 'framer-motion';
function Tabs({ items, selected }) {
return (
<div className="flex gap-2">
{items.map((item, i) => (
<Button key={i} onPress={() => setSelected(i)}>
{item}
{selected === i && (
<motion.div
layoutId="active"
className="absolute inset-0 bg-accent"
transition={{ type: "spring", bounce: 0.2 }}
/>
)}
</Button>
))}
</div>
);
}
렌더 props (Render Props)
컴포넌트 상태에 따라 동적 애니메이션을 적용할 수 있어요:
<Button>
{({ isPressed, isHovered }) => (
<motion.span
animate={{
scale: isPressed ? 0.95 : isHovered ? 1.05 : 1
}}
>
Interactive Button
</motion.span>
)}
</Button>
접근성 (Accessibility)
모션 선호도 존중 (Respecting motion preferences): HeroUI는 Tailwind의 motion-reduce: 유틸리티를 사용해 사용자의 모션 선호도를 자동으로 존중해요. 모든 내장 전환과 애니메이션은 사용자가 시스템 설정에서 "reduce motion"을 활성화하면 비활성화돼요.
HeroUI는 Tailwind의 motion-reduce: 변형을 확장해 기본 prefers-reduced-motion 미디어 쿼리와 data-reduce-motion 속성을 모두 지원해요.
/* HeroUI pattern - uses Tailwind's motion-reduce: */
.button {
@apply transition-colors motion-reduce:transition-none;
}
/* Expands to support both approaches: */
@media (prefers-reduced-motion: reduce) {
.button {
transition: none;
}
}
[data-reduce-motion="true"] .button {
transition: none;
}
Framer Motion과 함께:
import { useReducedMotion } from 'framer-motion';
function AnimatedCard() {
const shouldReduceMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: shouldReduceMotion ? 0 : 0.5 }}
>
<Card>Content</Card>
</motion.div>
);
}
전역적으로 애니메이션 비활성화: <html> 또는 <body> 태그에 data-reduce-motion="true"를 추가해요:
<html data-reduce-motion="true">
<!-- All HeroUI animations will be disabled -->
</html>
HeroUI는 사용자의 prefers-reduced-motion: reduce 설정을 자동으로 감지해 그에 따라 애니메이션을 비활성화해요.
성능 팁 (Performance Tips)
GPU 가속 속성 사용: 부드러운 애니메이션을 위해 transform과 opacity를 선호해요:
/* Good - GPU accelerated */
.slide-in {
transform: translateX(-100%);
transition: transform 0.3s;
}
/* Avoid - Triggers layout */
.slide-in {
left: -100%;
transition: left 0.3s;
}
Will-change 최적화: 애니메이션 최적화에 will-change를 사용하되, 애니메이션하지 않을 때는 제거해요:
.button {
will-change: transform;
}
.button:not(:hover) {
will-change: auto;
}
다음 단계 (Next Steps)
더 알아보기 (Learn more)
HeroUI Animation에 대해 더 알아보려면 공식 문서를 확인해 주세요.