Guides · 03

Animated components

Render ordinary elements that move to a destination, respond to gestures and depart gracefully.

Animated.<tag>

Animated.div, Animated.button, Animated.svg and every other HTML or SVG tag render the ordinary element with no wrapper. Their style accepts values, and transforms can be arrays as in React Native.

Finite transform and opacity motion on these elements, including springs, is eligible for browser playback.

Preparing the example…

seed, pose, poses and tempo

seed is the first frame, pose the current destination (an object or the name of an entry in poses), and tempo a timing or spring. Changing tempo alone does not replay an unchanged destination.

onRest fires when the element arrives.

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 to gestures

respond maps gestures to temporary destinations: hover, focus, focusWithin, press, longPress, drag, inView and more. When a response ends, the element returns to its pose from wherever it is.

onResponse reports each edge, and respond={{ press: {} }} observes a gesture without changing anything visually.

Preparing the example…

Share defaults with Animated.Scope

Animated.Scope passes a pose name, tempo and animation defaults to the Animated elements inside it.

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

Your own components

Animated.wrap(Component) — createAnimatedComponent in React Native — makes a ref-forwarding component that resolves to one DOM element accept the same 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 }] } }} />;

Search

Type to search the documentation.