ガイド · 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 など。反応が終わると、要素はその時点の位置から自分のポーズへ戻ります。
onResponse は各エッジを報告し、respond={{ press: {} }} は見た目を何も変えずにジェスチャーだけを監視します。
Animated.Scope でデフォルトを共有する
Animated.Scope は、ポーズ名、tempo、アニメーションのデフォルトを内側の Animated 要素に渡します。
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.jsximport { 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 }] } }} />;