Skip to content

Commit

Permalink
Fix animatedRef on Fabric (software-mansion#4445)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

- Fixes software-mansion#4256

### Problem

If the scrollable component is not root, UI `scrollTo` doesn't work on
Fabric.

software-mansion#4384 - It was fixed here for Paper, but after this change app crashes
on Fabric, because `getScrollableNode` in both architectures returns
viewTag as a number, not component. It's working for Paper, because we
are passing either component or number to `findNodeHandle` method, but
on Fabric we need component ref.

### Fix
 
Instead of `getScrollableNode`, we can use `getNativeScrollRef` and get
scrollable reference.


### TODO

Unfortunately this method is not implemented yet for `Flash-list` ->
https://shopify.github.io/flash-list/docs/usage#:~:text=Unsupported%20methods%3A
so for now, UI `scrollTo` on FlashList works only for Paper
Architecture.

## Test plan

ScrollToExample:

- Paper, ScrollView
- Paper, FlatList
- Fabric, ScrollView
- Fabric, FlatList

---------

Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
  • Loading branch information
2 people authored and fluiddot committed Jun 5, 2023
1 parent da679da commit 4299372
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/reanimated2/hook/useAnimatedRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../shareables';

interface ComponentRef extends Component {
getNativeScrollRef?: () => ComponentRef;
getScrollableNode?: () => ComponentRef;
}

Expand All @@ -19,6 +20,15 @@ function getShareableShadowNodeFromComponent(
return getShadowNodeWrapperFromHostInstance(component);
}

function getComponentOrScrollableRef(component: ComponentRef): ComponentRef {
if (global._IS_FABRIC && component.getNativeScrollRef) {
return component.getNativeScrollRef();
} else if (!global._IS_FABRIC && component.getScrollableNode) {
return component.getScrollableNode();
}
return component;
}

const getTagValueFunction = global._IS_FABRIC
? getShareableShadowNodeFromComponent
: getTag;
Expand All @@ -31,11 +41,7 @@ export function useAnimatedRef<T extends ComponentRef>(): RefObjectFunction<T> {
const fun: RefObjectFunction<T> = <RefObjectFunction<T>>((component) => {
// enters when ref is set by attaching to a component
if (component) {
tag.value = getTagValueFunction(
component.getScrollableNode
? component.getScrollableNode()
: component
);
tag.value = getTagValueFunction(getComponentOrScrollableRef(component));
fun.current = component;
}
return tag.value;
Expand All @@ -50,7 +56,6 @@ export function useAnimatedRef<T extends ComponentRef>(): RefObjectFunction<T> {
},
});
registerShareableMapping(fun, remoteRef);

ref.current = fun;
}

Expand Down

0 comments on commit 4299372

Please sign in to comment.