Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-sloths-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@plextv/react-lightning-plugin-reanimated": minor
---

Added useAnimatedScrollHandler, withDelay implementations.
2 changes: 1 addition & 1 deletion packages/plugin-reanimated/src/animation/spring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function createSpringAnimation(
const animation = {
duration: 350,
easing: `cubic-bezier(${1 / 3}, ${p1_y}, ${2 / 3}, ${p2_y})`,
delay: 0,
delay: config?.delay ?? 0,
loop: false,
repeat: 0,
repeatDelay: 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-reanimated/src/animation/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function createTimingAnimation(
return {
duration: config?.duration ?? DefaultTimingConfig.duration,
easing: 'linear',
delay: 0,
delay: config?.delay ?? 0,
loop: false,
repeat: 0,
repeatDelay: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type DependencyList, useCallback, useRef } from 'react';
import type {
ScrollHandlerProcessed,
useAnimatedScrollHandler as useAnimatedScrollHandlerRN,
} from 'react-native-reanimated-original';

type UseAnimatedScrollHandlerFn = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this just be type UseAnimatedScrollHandlerFn = typeof useAnimatedScrollHandlerRN?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having some typescript issues with the context type. If I spread the args everything seems more stable on the ts side.

...args: Parameters<typeof useAnimatedScrollHandlerRN>
) => ScrollHandlerProcessed;

export const useAnimatedScrollHandler: UseAnimatedScrollHandlerFn = (
scrollHandlers,
dependencies,
) => {
const inputs: DependencyList = dependencies ?? [];
// We want to persist context between scroll events
// The caller should use it, we won't do any assignment in
// this function.
const contextRef = useRef({});

return useCallback(
(event) => {
const context = contextRef.current;
// Only allow onScroll event
const reanimatedEvent = {
eventName: 'onScroll',
...event.nativeEvent,
};

if (typeof scrollHandlers === 'function') {
scrollHandlers(reanimatedEvent, context);
return;
}

if (scrollHandlers && typeof scrollHandlers.onScroll === 'function') {
scrollHandlers.onScroll(reanimatedEvent, context);
}
},
// biome-ignore lint/correctness/useExhaustiveDependencies: We're passing a dependencies array from the props
inputs,
);
};
19 changes: 19 additions & 0 deletions packages/plugin-reanimated/src/exports/withDelay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
AnimationObject,
withDelay as withDelayRN,
} from 'react-native-reanimated-original';

export type WithDelayFn = (
...args: Parameters<typeof withDelayRN>
) => AnimationObject;

export const withDelay: WithDelayFn = (
_delayMs,
nextAnimation,
_reduceMotion,
): AnimationObject => {
// withTiming and withSpring supports `delay`. The client should pass it directly
return typeof nextAnimation === 'function'
? (nextAnimation as () => AnimationObject)()
: (nextAnimation as AnimationObject);
};
2 changes: 2 additions & 0 deletions packages/plugin-reanimated/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export {
SlideOutUp,
} from './builders/Slide';

export { useAnimatedScrollHandler } from './exports/useAnimatedScrollHandler';
export { useAnimatedStyle } from './exports/useAnimatedStyle';
export { useComposedEventHandler } from './exports/useComposedEventHandler';
export { withDelay } from './exports/withDelay';
export { withRepeat } from './exports/withRepeat';
export { withSpring } from './exports/withSpring';
export { withTiming } from './exports/withTiming';
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin-reanimated/src/types/reanimated-extended.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
WithSpringConfig as BaseSpringConfig,
WithTimingConfig as BaseTimingConfig,
} from 'react-native-reanimated';

declare module 'react-native-reanimated' {
interface WithTimingConfig extends BaseTimingConfig {
delay?: number;
}

interface WithSpringConfig extends BaseSpringConfig {
delay?: number;
}
}

export type {
WithSpringConfig as SpringConfig,
WithTimingConfig as TimingConfig,
} from 'react-native-reanimated';