useAmbientMotion
Pause decorative loops offscreen, in the background and under Reduce Motion.
UI viewport sampling and background/reduced-motion pause for shared ambient effects.
Free (MIT)Version 1.1.0Released 13 Nov 2026iOS and Android
When to use it
- Pause decorative ambient motion when outside the viewport or backgrounded.
When not to use it
- Use a functional business clock for deadlines and status.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add use-ambient-motion- Adds
components/swipeui/hooks/use-ambient-motion.tsand 1 shared helper. - Installs react-native-reanimated, react-native-worklets. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
import { useAmbientMotion } from '@/components/swipeui/hooks/use-ambient-motion';
const { ref, phase, reduced } = useAmbientMotion(isRouteVisible);
const style = useAnimatedStyle(() => ({ opacity: reduced ? 1 : 0.7 + phase.get() * 0.3 }));
<Animated.View ref={ref} style={style} />;Attach the animated ref to the effect's outer view. The hook samples viewport intersection at most four times per second on UI and cancels loops outside the viewport, in the background, under Reduce Motion and on unmount. Set active=false for hidden routes or occlusion that geometric bounds cannot detect. Business timers such as Countdown use an absolute deadline instead.
Accessibility
- This helper adds no accessibility behavior; the rendered component owns its semantics.
Motion
- Uses the
ambientmotion role, so it follows your app's feel. Passmotionto tune this one instance. - Respects the shared feel and system Reduce Motion; cancels on background, viewport exit and unmount.
Performance
- Measures at most four times per second on UI; only app lifecycle changes update React state.
Notes for AI
- Attach the returned animated ref to an Animated.View. Set active=false for hidden navigation routes or occlusion that viewport bounds cannot detect.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.