Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect } from 'react';
import { Button, StyleSheet, View } from 'react-native';
import Animated, {
getViewProp,
useAnimatedRef,
useAnimatedStyle,
useSharedValue,
Expand All @@ -25,9 +24,7 @@ export default function GetViewPropExample() {
});

const handlePress = async () => {
// @ts-ignore this is fine
const viewTag = animatedRef() as number;
const result = await getViewProp(viewTag, 'opacity');
const result = await animatedRef.getViewProp<number>('opacity');
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we want getViewProp to be in the public API of Reanimated. This method is meant for runtime tests and should not be used by apps as far as I know.

Copy link
Member Author

Choose a reason for hiding this comment

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

Mhm, it was exported as a function but not documented, so maybe you are right that this was meant only for the internal use. What should I do then if it is not a part of the public API? Can I restore previous changes that just removed the unused param from the function signature and not care about backwards compatibility?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's not a part of the public API then we can import it from src/ directly in the runtime tests, instead of putting it in public exports.

Copy link
Member

Choose a reason for hiding this comment

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

  1. Let's not care about backwards compatibility
  2. Let's convert it from ref method back to a standalone function
  3. I'm okay with moving it to src/ directory

console.log(result);
};

Expand Down
1 change: 0 additions & 1 deletion apps/common-app/src/apps/reanimated/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ export const EXAMPLES: Record<string, Example> = {
icon: '🔎',
title: 'getViewProp',
screen: GetViewPropExample,
missingOnFabric: true,
},
LogExample: {
icon: '⌨',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
SHOULD_BE_USE_WEB,
} from '../common';
import type {
ComponentWithInstanceMethods,
LayoutAnimationBatchItem,
ShadowNodeWrapper,
StyleProps,
Value3D,
ValueRotation,
WrapperRef,
} from '../commonTypes';
import type {
CSSAnimationUpdates,
Expand Down Expand Up @@ -133,9 +133,8 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
}

getViewProp<T>(
viewTag: number,
component: ComponentWithInstanceMethods,
propName: string,
component: WrapperRef, // required on Fabric
callback?: (result: T) => void
) {
const shadowNodeWrapper = getShadowNodeWrapperFromRef(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
ReanimatedError,
} from '../../common';
import type {
ComponentWithInstanceMethods,
ShadowNodeWrapper,
StyleProps,
Value3D,
ValueRotation,
WrapperRef,
} from '../../commonTypes';
import { SensorType } from '../../commonTypes';
import type {
Expand Down Expand Up @@ -253,9 +253,8 @@ class JSReanimated implements IReanimatedModule {
}

getViewProp<T>(
_viewTag: number,
_component: ComponentWithInstanceMethods,
_propName: string,
_component?: WrapperRef | null,
_callback?: (result: T) => void
): Promise<T> {
throw new ReanimatedError('getViewProp is not available in JSReanimated.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import type { SerializableRef, WorkletFunction } from 'react-native-worklets';

import type {
ComponentWithInstanceMethods,
LayoutAnimationBatchItem,
ShadowNodeWrapper,
StyleProps,
Value3D,
ValueRotation,
WrapperRef,
} from '../commonTypes';
import type {
CSSAnimationUpdates,
Expand Down Expand Up @@ -93,9 +93,8 @@ export interface ReanimatedModuleProxy {
export interface IReanimatedModule
extends Omit<ReanimatedModuleProxy, 'getViewProp'> {
getViewProp<TValue>(
viewTag: number,
component: ComponentWithInstanceMethods | null,
propName: string,
component: WrapperRef | null,
callback?: (result: TValue) => void
): Promise<TValue>;
}
4 changes: 3 additions & 1 deletion packages/react-native-reanimated/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,6 @@ type InstanceMethods = {
__internalInstanceHandle?: AnyRecord;
};

export type WrapperRef = (React.Component & InstanceMethods) | InstanceMethods;
export type ComponentWithInstanceMethods =
| (React.Component & InstanceMethods)
| InstanceMethods;
38 changes: 20 additions & 18 deletions packages/react-native-reanimated/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { createSerializable } from 'react-native-worklets';
import { logger, ReanimatedError } from './common';
import type {
AnimatedKeyboardOptions,
ComponentWithInstanceMethods,
LayoutAnimationBatchItem,
SensorConfig,
SensorType,
SharedValue,
Value3D,
ValueRotation,
WrapperRef,
} from './commonTypes';
import { ReanimatedModule } from './ReanimatedModule';
import { SensorContainer } from './SensorContainer';
Expand Down Expand Up @@ -46,35 +46,37 @@ export const isReanimated3 = () => {
*/
export const isConfigured = isReanimated3;

export function getViewProp<T>(
viewTag: number,
propName: string,
component?: WrapperRef | null // required on Fabric
export function getViewPropImpl<T = unknown>(
component: ComponentWithInstanceMethods | null,
propName: string
): Promise<T> {
if (!component) {
throw new ReanimatedError(
'Function `getViewProp` requires a component to be passed as an argument on Fabric.'
'Function `getViewProp` requires a component to be passed as an argument.'
);
}

// eslint-disable-next-line @typescript-eslint/no-misused-promises
return new Promise((resolve, reject) => {
return ReanimatedModule.getViewProp(
viewTag,
propName,
component,
(result: T) => {
if (typeof result === 'string' && result.slice(0, 6) === 'error:') {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(result);
} else {
resolve(result);
}
return ReanimatedModule.getViewProp(component, propName, (result: T) => {
if (typeof result === 'string' && result.slice(0, 6) === 'error:') {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(result);
} else {
resolve(result);
}
);
});
});
}

export function getViewProp<T = unknown>(
_viewTag: number,
propName: string,
component: ComponentWithInstanceMethods | null
): Promise<T> {
return getViewPropImpl(component, propName);
}

function getSensorContainer(): SensorContainer {
if (!global.__sensorContainer) {
global.__sensorContainer = new SensorContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type { StyleProp } from 'react-native';
import { Platform, StyleSheet } from 'react-native';

import { IS_JEST, ReanimatedError, SHOULD_BE_USE_WEB } from '../../common';
import type { ShadowNodeWrapper, WrapperRef } from '../../commonTypes';
import type {
ComponentWithInstanceMethods,
ShadowNodeWrapper,
} from '../../commonTypes';
import type {
AnimatedComponentRef,
IAnimatedComponentInternalBase,
Expand Down Expand Up @@ -91,7 +94,7 @@ export default class AnimatedComponent<
viewTag = viewInfo.viewTag ?? -1;
viewName = viewInfo.viewName;
shadowNodeWrapper = getShadowNodeWrapperFromRef(
this as WrapperRef,
this as ComponentWithInstanceMethods,
hostInstance
);
}
Expand Down
19 changes: 11 additions & 8 deletions packages/react-native-reanimated/src/fabricUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
'use strict';
import type { ShadowNodeWrapper, WrapperRef } from './commonTypes';
import type {
ComponentWithInstanceMethods,
ShadowNodeWrapper,
} from './commonTypes';
import type { HostInstance } from './platform-specific/findHostInstance';
import { findHostInstance } from './platform-specific/findHostInstance';

Expand All @@ -9,7 +12,7 @@ let getInternalInstanceHandleFromPublicInstance: (ref: unknown) => {
};

export function getShadowNodeWrapperFromRef(
ref: WrapperRef,
component: ComponentWithInstanceMethods,
hostInstance?: HostInstance
): ShadowNodeWrapper {
if (getInternalInstanceHandleFromPublicInstance === undefined) {
Expand All @@ -25,15 +28,15 @@ export function getShadowNodeWrapperFromRef(
}
}

const resolvedRef =
ref.getScrollResponder?.()?.getNativeScrollRef?.() ??
ref.getNativeScrollRef?.() ??
ref;
const resolvedComponent =
component.getScrollResponder?.()?.getNativeScrollRef?.() ??
component.getNativeScrollRef?.() ??
component;

const resolvedInstance =
ref?.__internalInstanceHandle ??
component?.__internalInstanceHandle ??
getInternalInstanceHandleFromPublicInstance(
hostInstance ?? findHostInstance(resolvedRef)
hostInstance ?? findHostInstance(resolvedComponent)
);

return resolvedInstance?.stateNode?.node;
Expand Down
11 changes: 6 additions & 5 deletions packages/react-native-reanimated/src/hook/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import type { WorkletFunction } from 'react-native-worklets';
import type {
AnimatedPropsAdapterFunction,
AnimatedStyle,
ComponentWithInstanceMethods,
ShadowNodeWrapper,
WrapperRef,
} from '../commonTypes';
import type { AnimatedProps } from '../createAnimatedComponent/commonTypes';
import type { ReanimatedHTMLElement } from '../ReanimatedModule/js-reanimated';
Expand All @@ -30,17 +30,18 @@ export type MaybeObserverCleanup = (() => void) | undefined;

export type AnimatedRefObserver = (tag: number | null) => MaybeObserverCleanup;

export type AnimatedRef<TRef extends WrapperRef> = {
(ref?: TRef | null):
export type AnimatedRef<TComponent extends ComponentWithInstanceMethods> = {
(ref?: TComponent | null):
| ShadowNodeWrapper // Native
| HTMLElement; // web
current: TRef | null;
current: TComponent | null;
observe: (observer: AnimatedRefObserver) => void;
getViewProp: <T = unknown>(propName: string) => Promise<T>;
getTag?: () => number | null;
};

// Might make that type generic if it's ever needed.
export type AnimatedRefOnJS = AnimatedRef<WrapperRef>;
export type AnimatedRefOnJS = AnimatedRef<ComponentWithInstanceMethods>;

/**
* `AnimatedRef` is mapped to this type on the UI thread via a serializable
Expand Down
55 changes: 34 additions & 21 deletions packages/react-native-reanimated/src/hook/useAnimatedRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
} from 'react-native-worklets';

import { SHOULD_BE_USE_WEB } from '../common/constants';
import type { ShadowNodeWrapper, WrapperRef } from '../commonTypes';
import type {
ComponentWithInstanceMethods,
ShadowNodeWrapper,
} from '../commonTypes';
import { getViewPropImpl } from '../core';
import { getShadowNodeWrapperFromRef } from '../fabricUtils';
import { makeMutable } from '../mutables';
import { findNodeHandle } from '../platformFunctions/findNodeHandle';
Expand All @@ -17,27 +21,31 @@ import type {
MaybeObserverCleanup,
} from './commonTypes';

function getComponentOrScrollable(ref: WrapperRef) {
return ref.getNativeScrollRef?.() ?? ref.getScrollableNode?.() ?? ref;
function getComponentOrScrollable(component: ComponentWithInstanceMethods) {
return (
component.getNativeScrollRef?.() ??
component.getScrollableNode?.() ??
component
);
}

function useAnimatedRefBase<TRef extends WrapperRef>(
getWrapper: (ref: TRef) => ShadowNodeWrapper
): AnimatedRef<TRef> {
function useAnimatedRefBase<TComponent extends ComponentWithInstanceMethods>(
getWrapper: (component: TComponent) => ShadowNodeWrapper
): AnimatedRef<TComponent> {
const observers = useRef<Map<AnimatedRefObserver, MaybeObserverCleanup>>(
new Map()
).current;
const wrapperRef = useRef<ShadowNodeWrapper | null>(null);
const resultRef = useRef<AnimatedRef<TRef> | null>(null);
const shadowNodeWrapperRef = useRef<ShadowNodeWrapper | null>(null);
const resultRef = useRef<AnimatedRef<TComponent> | null>(null);

if (!resultRef.current) {
const fun = <AnimatedRef<TRef>>((ref) => {
if (ref) {
wrapperRef.current = getWrapper(ref);
const fun = <AnimatedRef<TComponent>>((component) => {
if (component) {
shadowNodeWrapperRef.current = getWrapper(component);

// We have to unwrap the tag from the shadow node wrapper.
fun.getTag = () => findNodeHandle(getComponentOrScrollable(ref));
fun.current = ref;
fun.getTag = () => findNodeHandle(getComponentOrScrollable(component));
fun.current = component;

if (observers.size) {
const currentTag = fun?.getTag?.() ?? null;
Expand All @@ -52,7 +60,7 @@ function useAnimatedRefBase<TRef extends WrapperRef>(
}
}

return wrapperRef.current;
return shadowNodeWrapperRef.current;
});

fun.observe = (observer: AnimatedRefObserver) => {
Expand All @@ -66,6 +74,9 @@ function useAnimatedRefBase<TRef extends WrapperRef>(
};
};

fun.getViewProp = (propName: string) =>
getViewPropImpl(fun.current, propName);

fun.current = null;
resultRef.current = fun;
}
Expand All @@ -74,15 +85,15 @@ function useAnimatedRefBase<TRef extends WrapperRef>(
}

function useAnimatedRefNative<
TRef extends WrapperRef = React.Component,
>(): AnimatedRef<TRef> {
TComponent extends ComponentWithInstanceMethods = React.Component,
>(): AnimatedRef<TComponent> {
const [sharedWrapper] = useState(() =>
makeMutable<ShadowNodeWrapper | null>(null)
);

const resultRef = useAnimatedRefBase<TRef>((ref) => {
const resultRef = useAnimatedRefBase<TComponent>((component) => {
const currentWrapper = getShadowNodeWrapperFromRef(
getComponentOrScrollable(ref)
getComponentOrScrollable(component)
);

sharedWrapper.value = currentWrapper;
Expand All @@ -104,9 +115,11 @@ function useAnimatedRefNative<
}

function useAnimatedRefWeb<
TRef extends WrapperRef = React.Component,
>(): AnimatedRef<TRef> {
return useAnimatedRefBase<TRef>((ref) => getComponentOrScrollable(ref));
TComponent extends ComponentWithInstanceMethods = React.Component,
>(): AnimatedRef<TComponent> {
return useAnimatedRefBase<TComponent>((component) =>
getComponentOrScrollable(component)
);
}

/**
Expand Down
Loading