Skip to content

Commit 2ca35ba

Browse files
committed
feat(reanimated): useAnimatedScrollHandler, withDelay
1 parent d9000da commit 2ca35ba

File tree

7 files changed

+89
-2
lines changed

7 files changed

+89
-2
lines changed

.changeset/tall-sloths-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plextv/react-lightning-plugin-reanimated": minor
3+
---
4+
5+
Added useAnimatedScrollHandler, withDelay implementations.

packages/plugin-reanimated/src/animation/spring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function createSpringAnimation(
139139
const animation = {
140140
duration: 350,
141141
easing: `cubic-bezier(${1 / 3}, ${p1_y}, ${2 / 3}, ${p2_y})`,
142-
delay: 0,
142+
delay: config?.delay ?? 0,
143143
loop: false,
144144
repeat: 0,
145145
repeatDelay: 0,

packages/plugin-reanimated/src/animation/timing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function createTimingAnimation(
1414
return {
1515
duration: config?.duration ?? DefaultTimingConfig.duration,
1616
easing: 'linear',
17-
delay: 0,
17+
delay: config?.delay ?? 0,
1818
loop: false,
1919
repeat: 0,
2020
repeatDelay: 0,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type DependencyList, useCallback, useRef } from 'react';
2+
import type {
3+
ScrollHandlerProcessed,
4+
useAnimatedScrollHandler as useAnimatedScrollHandlerRN,
5+
} from 'react-native-reanimated-original';
6+
7+
type UseAnimatedScrollHandlerFn = (
8+
...args: Parameters<typeof useAnimatedScrollHandlerRN>
9+
) => ScrollHandlerProcessed;
10+
11+
export const useAnimatedScrollHandler: UseAnimatedScrollHandlerFn = (
12+
scrollHandlers,
13+
dependencies,
14+
) => {
15+
const inputs: DependencyList = dependencies ?? [];
16+
// We want to persist context between scroll events
17+
// The caller should use it, we won't do any assignment in
18+
// this function.
19+
const contextRef = useRef({});
20+
21+
return useCallback(
22+
(event) => {
23+
const context = contextRef.current;
24+
// Only allow onScroll event
25+
const reanimatedEvent = {
26+
eventName: 'onScroll',
27+
...event.nativeEvent,
28+
};
29+
30+
if (typeof scrollHandlers === 'function') {
31+
scrollHandlers(reanimatedEvent, context);
32+
return;
33+
}
34+
35+
if (scrollHandlers && typeof scrollHandlers.onScroll === 'function') {
36+
scrollHandlers.onScroll(reanimatedEvent, context);
37+
}
38+
},
39+
// biome-ignore lint/correctness/useExhaustiveDependencies: We're passing a dependencies array from the props
40+
inputs,
41+
);
42+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type {
2+
AnimationObject,
3+
withDelay as withDelayRN,
4+
} from 'react-native-reanimated-original';
5+
6+
export type WithDelayFn = (
7+
...args: Parameters<typeof withDelayRN>
8+
) => AnimationObject;
9+
10+
export const withDelay: WithDelayFn = (
11+
_delayMs,
12+
_nextAnimation,
13+
_reduceMotion,
14+
): AnimationObject => {
15+
// withTiming and withSpring supports `delay`. The client should pass it directly
16+
return typeof _nextAnimation === 'function'
17+
? (_nextAnimation as () => AnimationObject)()
18+
: (_nextAnimation as AnimationObject);
19+
};

packages/plugin-reanimated/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ export {
4646
SlideOutUp,
4747
} from './builders/Slide';
4848

49+
export { useAnimatedScrollHandler } from './exports/useAnimatedScrollHandler';
4950
export { useAnimatedStyle } from './exports/useAnimatedStyle';
5051
export { useComposedEventHandler } from './exports/useComposedEventHandler';
52+
export { withDelay } from './exports/withDelay';
5153
export { withRepeat } from './exports/withRepeat';
5254
export { withSpring } from './exports/withSpring';
5355
export { withTiming } from './exports/withTiming';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type {
2+
WithSpringConfig as BaseSpringConfig,
3+
WithTimingConfig as BaseTimingConfig,
4+
} from 'react-native-reanimated';
5+
6+
declare module 'react-native-reanimated' {
7+
interface WithTimingConfig extends BaseTimingConfig {
8+
delay?: number;
9+
}
10+
11+
interface WithSpringConfig extends BaseSpringConfig {
12+
delay?: number;
13+
}
14+
}
15+
16+
export type {
17+
WithSpringConfig as SpringConfig,
18+
WithTimingConfig as TimingConfig,
19+
} from 'react-native-reanimated';

0 commit comments

Comments
 (0)