-
Notifications
You must be signed in to change notification settings - Fork 4
feat(reanimated): useAnimatedScrollHandler, withDelay #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@plextv/react-lightning-plugin-reanimated": minor | ||
| --- | ||
|
|
||
| Added useAnimatedScrollHandler, withDelay implementations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/plugin-reanimated/src/exports/useAnimatedScrollHandler.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = ( | ||
| ...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, | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/plugin-reanimated/src/types/reanimated-extended.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
contexttype. If I spread the args everything seems more stable on the ts side.