가이드 · 03

Animated 컴포넌트

목적지로 움직이고, 제스처에 반응하고, 자연스럽게 떠나는 평범한 요소를 렌더합니다.

Animated.<tag>

Animated.div, Animated.button, Animated.svg를 비롯한 모든 HTML·SVG 태그는 래퍼 없이 평범한 요소를 렌더합니다. style은 값을 받고, transform은 React Native처럼 배열로 쓸 수 있습니다.

이 요소들의 유한한 transform·opacity 모션은 스프링을 포함해 브라우저 재생 대상입니다.

예제를 준비하는 중…

seed, pose, poses, tempo

seed는 첫 프레임, pose는 지금의 목적지(객체 또는 poses 항목의 이름), tempo는 타이밍 또는 스프링입니다. tempo만 바꾸면 바뀌지 않은 목적지를 다시 재생하지 않습니다.

onRest는 요소가 도착하면 호출됩니다.

Card.jsx
<Animated.article
  seed={{ opacity: 0, transform: [{ translateY: 16 }] }}
  pose={selected ? 'raised' : 'rest'}
  poses={{
    rest: { opacity: 1, transform: [{ translateY: 0 }, { scale: 1 }] },
    raised: { opacity: 1, transform: [{ translateY: -8 }, { scale: 1.04 }] },
  }}
  tempo={{ spring: { stiffness: 300, damping: 26 } }}
  onRest={() => console.log('arrived')}
/>;

제스처에 반응하기

respond는 제스처를 임시 목적지에 연결합니다. hover, focus, focusWithin, press, longPress, drag, inView 등이 있습니다. 반응이 끝나면 요소는 어디에 있든 그 자리에서 자기 pose로 돌아갑니다.

onResponse는 가장자리마다 알려 주고, respond={{ press: {} }}는 화면을 바꾸지 않고 제스처만 관찰합니다.

예제를 준비하는 중…

Animated.Scope로 기본값 공유하기

Animated.Scope는 안쪽 Animated 요소에 pose 이름, tempo, 애니메이션 기본값을 넘깁니다.

Menu.jsx
<Animated.Scope tempo={{ spring: { stiffness: 380, damping: 30 } }}>
  <Animated.li pose={{ opacity: 1 }} />
  <Animated.li pose={{ opacity: 1 }} />
</Animated.Scope>;

내 컴포넌트에 쓰기

Animated.wrap(Component)(React Native의 createAnimatedComponent)은 DOM 요소 하나로 이어지는 ref 전달 컴포넌트가 같은 props를 받게 합니다.

Link.jsx
import { forwardRef } from 'react';
import { Animated } from '@kkwaenggwari/animated';

const Link = forwardRef((props, ref) => <a ref={ref} {...props} />);
const AnimatedLink = Animated.wrap(Link);

<AnimatedLink href="/pricing" respond={{ hover: { transform: [{ translateX: 4 }] } }} />;

검색

입력하면 문서를 검색합니다.