Guides · 04

Input and gestures

Write pointer, drag, scroll and other input into values, and let other values follow them.

input(target, adapter)

An input adapter turns browser events into value writes. input(ref, adapter) attaches it as a controller; declared inside useAnimated, it is removed on unmount or when a dependency changes.

Adapters: dragInput, pinchInput, moveInput, scrollInput, wheelInput, viewportInput, mediaInput, longPressInput and swipeInput.

Preparing the example…

Drag with bounds and release motion

dragInput supports pointer and arrow-key dragging, bounds with elastic resistance, momentum after release and a settle function that picks the resting place from the projected position.

On touch screens it sets the target's touch-action while mounted so the browser does not take the gesture for scrolling. A value you set yourself wins.

Preparing the example…

Pinch, viewport and media

The optional third argument of input receives each response event, so an adapter can also start motion on its edges.

inputs.jsx
const card = useRef(null);

const pinch = useAnimated(
  { scale: 1, rotation: 0 },
  (value) => {
    input(card, pinchInput({ scale: value.scale, rotation: value.rotation, minScale: 0.5 }));
  },
  [],
);

const reveal = useAnimated(
  0,
  (value) => {
    input(card, viewportInput({ once: true }), (event) => {
      if (event.phase === 'in') spring(value, { toValue: 1 }).start();
    });
  },
  [],
);

Animated.event

React Native event mappings work on DOM events. On a scroll event, nativeEvent.contentOffset, contentSize and layoutMeasurement are read from the scrolled element, so ScrollView mappings port unchanged.

Header.jsx
const scrollY = useAnimatedValue(0);

<div onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }])} />;

Search

Type to search the documentation.