Skip to content
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

fix: take preload into account when freezing children #2566

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions apps/src/tests/TestFreeze.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function HomeScreen({
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
<Button
title="Preload Details"
onPress={() => navigation.preload('Details')}
/>
<Button
title="Preload Details2"
onPress={() => navigation.preload('Details2')}
/>
</View>
);
}
Expand Down Expand Up @@ -84,6 +92,7 @@ function App() {
}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
<Stack.Screen name="Details2" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
Expand Down
7 changes: 6 additions & 1 deletion src/components/Screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
React.useImperativeHandle(ref, () => innerRef.current!, []);
const prevActivityState = usePrevious(props.activityState);

const setRef = (ref: ViewConfig) => {

Check warning on line 168 in src/components/Screen.tsx

View workflow job for this annotation

GitHub Actions / install-and-lint

'ref' is already declared in the upper scope on line 163 column 31
innerRef.current = ref;
props.onComponentRef?.(ref);
};
Expand All @@ -177,6 +177,7 @@
const {
enabled = screensEnabled(),
freezeOnBlur = freezeEnabled(),
shouldFreeze,
...rest
} = props;

Expand Down Expand Up @@ -233,7 +234,7 @@
gestureResponseDistance,
onGestureCancel,
style,
...props

Check warning on line 237 in src/components/Screen.tsx

View workflow job for this annotation

GitHub Actions / install-and-lint

'props' is already declared in the upper scope on line 163 column 24
} = rest;

if (active !== undefined && activityState === undefined) {
Expand All @@ -255,7 +256,7 @@
}
}

const handleRef = (ref: ViewConfig) => {

Check warning on line 259 in src/components/Screen.tsx

View workflow job for this annotation

GitHub Actions / install-and-lint

'ref' is already declared in the upper scope on line 163 column 31
if (ref?.viewConfig?.validAttributes?.style) {
ref.viewConfig.validAttributes.style = {
...ref.viewConfig.validAttributes.style,
Expand All @@ -271,8 +272,12 @@
}
};

const freeze =
freezeOnBlur &&
(isNativeStack ? Boolean(shouldFreeze) : activityState === 0);
satya164 marked this conversation as resolved.
Show resolved Hide resolved

return (
<DelayedFreeze freeze={freezeOnBlur && activityState === 0}>
<DelayedFreeze freeze={freeze}>
<AnimatedScreen
{...props}
/**
Expand Down Expand Up @@ -352,7 +357,7 @@
style,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onComponentRef,
...props

Check warning on line 360 in src/components/Screen.tsx

View workflow job for this annotation

GitHub Actions / install-and-lint

'props' is already declared in the upper scope on line 163 column 24
} = rest;

if (active !== undefined && activityState === undefined) {
Expand All @@ -360,7 +365,7 @@
}
return (
<Animated.View
style={[style, { display: activityState !== 0 ? 'flex' : 'none' }]}

Check warning on line 368 in src/components/Screen.tsx

View workflow job for this annotation

GitHub Actions / install-and-lint

Inline style: { display: "activityState !== 0 ? 'flex' : 'none'" }
ref={setRef}
{...props}
/>
Expand Down
30 changes: 1 addition & 29 deletions src/components/ScreenStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ import {
ScreenStackProps,
} from '../types';
import { GHContext, RNSScreensRefContext } from '../contexts';
import { freezeEnabled } from '../core';
import DelayedFreeze from './helpers/DelayedFreeze';
import warnOnce from 'warn-once';

// Native components
import ScreenStackNativeComponent, {
NativeProps,
} from '../fabric/ScreenStackNativeComponent';

function isFabric() {
return 'nativeFabricUIManager' in global;
}

const assertGHProvider = (
ScreenGestureDetector: (
props: PropsWithChildren<GestureProviderProps>,
Expand Down Expand Up @@ -69,35 +63,13 @@ function ScreenStack(props: ScreenStackProps) {
passedScreenRefs?.current ?? {},
);
const ref = React.useRef(null);
const size = React.Children.count(children);
const ScreenGestureDetector = React.useContext(GHContext);
const gestureDetectorBridge = React.useRef<GestureDetectorBridge>({
stackUseEffectCallback: _stackRef => {
// this method will be overriden in GestureDetector
},
});

// freezes all screens except the top one
const childrenWithFreeze = React.Children.map(children, (child, index) => {
// @ts-expect-error it's either SceneView in v6 or RouteView in v5
const { props, key } = child;
const descriptor = props?.descriptor ?? props?.descriptors?.[key];
const isFreezeEnabled =
descriptor?.options?.freezeOnBlur ?? freezeEnabled();

// On Fabric, when screen is frozen, animated and reanimated values are not updated
// due to component being unmounted. To avoid this, we don't freeze the previous screen there
const freezePreviousScreen = isFabric()
? size - index > 2
: size - index > 1;

return (
<DelayedFreeze freeze={isFreezeEnabled && freezePreviousScreen}>
{child}
</DelayedFreeze>
);
});

React.useEffect(() => {
gestureDetectorBridge.current.stackUseEffectCallback(ref);
});
Expand Down Expand Up @@ -130,7 +102,7 @@ function ScreenStack(props: ScreenStackProps) {
onFinishTransitioning as NativeProps['onFinishTransitioning']
}
ref={ref}>
{childrenWithFreeze}
{children}
</ScreenStackNativeComponent>
</ScreenGestureDetector>
</RNSScreensRefContext.Provider>
Expand Down
3 changes: 3 additions & 0 deletions src/components/ScreenStackItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function ScreenStackItem(
children,
headerConfig,
activityState,
shouldFreeze,
stackPresentation,
contentStyle,
style,
Expand Down Expand Up @@ -132,6 +133,7 @@ function ScreenStackItem(
enabled
isNativeStack
activityState={activityState}
shouldFreeze={shouldFreeze}
stackPresentation={stackPresentation}
hasLargeHeader={headerConfig?.largeTitle ?? false}
style={[style, internalScreenStyle]}
Expand All @@ -142,6 +144,7 @@ function ScreenStackItem(
enabled
isNativeStack
activityState={activityState}
shouldFreeze={shouldFreeze}
hasLargeHeader={headerConfig?.largeTitle ?? false}
style={StyleSheet.absoluteFill}>
{content}
Expand Down
4 changes: 4 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export type SearchBarPlacement = 'automatic' | 'inline' | 'stacked';
export interface ScreenProps extends ViewProps {
active?: 0 | 1 | Animated.AnimatedInterpolation<number>;
activityState?: 0 | 1 | 2 | Animated.AnimatedInterpolation<number>;
/**
* Boolean indicating that the screen should be frozen with `react-freeze`.
*/
shouldFreeze?: boolean;
children?: React.ReactNode;
/**
* Boolean indicating that swipe dismissal should trigger animation provided by `stackAnimation`. Defaults to `false`.
Expand Down
Loading