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

feat: don't set display: 'none' on frozen screens #1208

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
3 changes: 3 additions & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-vars */
import React from 'react';

import {enableFreeze} from 'react-native-screens';
import {ReanimatedScreenProvider} from 'react-native-screens/reanimated';

import Test42 from './src/Test42';
Expand Down Expand Up @@ -65,6 +66,8 @@ import Test1162 from './src/Test1162';
import Test1188 from './src/Test1188';
import TestFreeze from './src/TestFreeze';

enableFreeze(true);

export default function App() {
return (
<ReanimatedScreenProvider>
Expand Down
5 changes: 1 addition & 4 deletions TestsExample/src/TestFreeze.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {useState, useEffect} from 'react';
import {View, Text, Button, ScrollView} from 'react-native';
import {enableFreeze} from 'react-native-screens';
import {NavigationContainer, ParamListBase} from '@react-navigation/native';
// import {createStackNavigator} from '@react-navigation/stack';
// import {createNativeStackNavigator} from '@react-navigation/native-stack';
Expand All @@ -9,8 +8,6 @@ import {
NativeStackNavigationProp,
} from 'react-native-screens/native-stack';

enableFreeze(true);

const store = new Set<Dispatch>();

type Dispatch = (value: number) => void;
Expand Down Expand Up @@ -52,7 +49,7 @@ function DetailsScreen({
navigation: NativeStackNavigationProp<ParamListBase>;
}) {
const value = useValue();
// should only 2 renders appear at the time
// only 1 'render' should appear at the time
console.log('render', value);
return (
<ScrollView>
Expand Down
79 changes: 51 additions & 28 deletions src/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,32 @@ function MaybeFreeze({
function ScreenStack(props: ScreenStackProps) {
if (ENABLE_FREEZE) {
const { children, ...rest } = props;
const count = React.Children.count(children);
const childrenWithProps = React.Children.map(children, (child, index) => {
return <Freeze freeze={count - index > 2}>{child}</Freeze>;
});
const size = React.Children.count(children);
// freezes all screens except the top one
const childrenWithFreeze = React.Children.map(children, (child, index) => (
<Freeze freeze={size - index > 1}>{child}</Freeze>
));
return (
<ScreensNativeModules.NativeScreenStack {...rest}>
{childrenWithProps}
{childrenWithFreeze}
</ScreensNativeModules.NativeScreenStack>
);
}
return <ScreensNativeModules.NativeScreenStack {...props} />;
}

// Incomplete type, all accessible properties available at:
// react-native/Libraries/Components/View/ReactNativeViewViewConfig.js
interface ViewConfig extends View {
viewConfig: {
validAttributes: {
style: {
display: boolean;
};
};
};
}

class Screen extends React.Component<ScreenProps> {
private ref: React.ElementRef<typeof View> | null = null;
private closing = new Animated.Value(0);
Expand Down Expand Up @@ -215,28 +228,38 @@ class Screen extends React.Component<ScreenProps> {
const processedColor = processColor(statusBarColor);

return (
<AnimatedNativeScreen
{...props}
statusBarColor={processedColor}
activityState={activityState}
ref={this.setRef}
onTransitionProgress={
!isNativeStack
? undefined
: Animated.event(
[
{
nativeEvent: {
progress: this.progress,
closing: this.closing,
goingForward: this.goingForward,
<MaybeFreeze freeze={activityState === 0}>
<AnimatedNativeScreen
{...props}
statusBarColor={processedColor}
activityState={activityState}
// This prevents showing blank screen when navigating between multiple screens with freezing
// https://github.com/software-mansion/react-native-screens/pull/1208
ref={(ref: ViewConfig) => {
if (ref?.viewConfig?.validAttributes?.style) {
kacperkapusciak marked this conversation as resolved.
Show resolved Hide resolved
ref.viewConfig.validAttributes.style = {
...ref.viewConfig.validAttributes.style,
display: false,
};
}
this.setRef(ref);
}}
onTransitionProgress={
!isNativeStack
? undefined
: Animated.event(
[
{
nativeEvent: {
progress: this.progress,
closing: this.closing,
goingForward: this.goingForward,
},
},
},
],
{ useNativeDriver: true }
)
}>
<MaybeFreeze freeze={activityState === 0}>
],
{ useNativeDriver: true }
)
}>
{!isNativeStack ? ( // see comment of this prop in types.tsx for information why it is needed
children
) : (
Expand All @@ -249,8 +272,8 @@ class Screen extends React.Component<ScreenProps> {
{children}
</TransitionProgressContext.Provider>
)}
</MaybeFreeze>
</AnimatedNativeScreen>
</AnimatedNativeScreen>
</MaybeFreeze>
);
} else {
// same reason as above
Expand Down