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: hideHeaderIfNecessary for Fabric #1491

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
87 changes: 87 additions & 0 deletions FabricTestExample/src/Test1153.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as React from 'react';
import {Button, View} from 'react-native';
import {NavigationContainer, ParamListBase} from '@react-navigation/native';
import {
createNativeStackNavigator,
NativeStackNavigationProp,
} from 'react-native-screens/native-stack';
import {SafeAreaView} from 'react-native-safe-area-context';
import { ScrollView } from 'react-native-gesture-handler';

const Stack = createNativeStackNavigator();

export default function App() {
return (
<View style={{flex: 1}}>
<NavigationContainer>
<Stack.Navigator screenOptions={{gestureEnabled: true}}>
<Stack.Screen
name="First"
component={First}
options={{
gestureEnabled: true,
headerTranslucent: true,
searchBar: {
placeholder: 'Interesting places...',
obscureBackground: false,
hideWhenScrolling: false,
},
}}
/>
<Stack.Screen
name="Second"
component={Second}
options={{
headerShown: false,
}}
/>
</Stack.Navigator>
</NavigationContainer>
</View>
);
}

function First({
navigation,
}: {
navigation: NativeStackNavigationProp<ParamListBase>;
}) {
return (
<ScrollView
style={{
backgroundColor: '#000',
flex: 1,
}}
contentInsetAdjustmentBehavior="automatic">
<SafeAreaView>
<Button
title="Tap me for second screen"
onPress={() => {
navigation.push('Second');
}}
/>
</SafeAreaView>
</ScrollView>
);
}

function Second({
navigation,
}: {
navigation: NativeStackNavigationProp<ParamListBase>;
}) {
return (
<View style={{backgroundColor: '#777', flex: 1, justifyContent: 'center'}}>
<Button
title="Tap me to go back"
onPress={() => {
navigation.pop();
}}
/>
<Button
title="Open this screen again"
onPress={() => navigation.push('Second')}
/>
</View>
);
}
67 changes: 34 additions & 33 deletions ios/RNSScreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,8 @@ - (void)viewWillAppear:(BOOL)animated
// The _isSwiping is still true, but we don't want to notify then
_shouldNotify = NO;
}
#ifdef RN_FABRIC_ENABLED
#else

[self hideHeaderIfNecessary];
#endif // RN_FABRIC_ENABLED
// as per documentation of these methods
_goingForward = [self isBeingPresented] || [self isMovingToParentViewController];

Expand Down Expand Up @@ -1068,35 +1066,6 @@ - (RNSScreenView *)screenView
#endif
}

#ifdef RN_FABRIC_ENABLED
#pragma mark - Fabric specific

- (void)setViewToSnapshot:(UIView *)snapshot
{
// modals of native stack seem not to support
// changing their view by just setting the view
if (_initialView.stackPresentation != RNSScreenStackPresentationPush) {
UIView *superView = self.view.superview;
[self.view removeFromSuperview];
self.view = snapshot;
[superView addSubview:self.view];
} else {
[self.view removeFromSuperview];
self.view = snapshot;
}
}

- (void)resetViewToScreen
{
if (self.view != _initialView) {
[self.view removeFromSuperview];
self.view = _initialView;
}
}

#else
#pragma mark - Paper specific

- (void)hideHeaderIfNecessary
{
#if !TARGET_OS_TV
Expand All @@ -1107,7 +1076,10 @@ - (void)hideHeaderIfNecessary
if (@available(iOS 13.0, *)) {
NSUInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self];

if (currentIndex > 0 && [self.screenView.reactSubviews[0] isKindOfClass:[RNSScreenStackHeaderConfig class]]) {
// we need to check whether reactSubviews array is empty, because on Fabric child nodes are unmounted first ->
// reactSubviews array may be empty
if (currentIndex > 0 && [self.screenView.reactSubviews count] &&
kkafar marked this conversation as resolved.
Show resolved Hide resolved
[self.screenView.reactSubviews[0] isKindOfClass:[RNSScreenStackHeaderConfig class]]) {
UINavigationItem *prevNavigationItem =
[self.navigationController.viewControllers objectAtIndex:currentIndex - 1].navigationItem;
RNSScreenStackHeaderConfig *config = ((RNSScreenStackHeaderConfig *)self.screenView.reactSubviews[0]);
Expand All @@ -1131,6 +1103,35 @@ - (void)hideHeaderIfNecessary
#endif
}

#ifdef RN_FABRIC_ENABLED
#pragma mark - Fabric specific

- (void)setViewToSnapshot:(UIView *)snapshot
{
// modals of native stack seem not to support
// changing their view by just setting the view
if (_initialView.stackPresentation != RNSScreenStackPresentationPush) {
UIView *superView = self.view.superview;
[self.view removeFromSuperview];
self.view = snapshot;
[superView addSubview:self.view];
} else {
[self.view removeFromSuperview];
self.view = snapshot;
}
}

- (void)resetViewToScreen
{
if (self.view != _initialView) {
[self.view removeFromSuperview];
self.view = _initialView;
}
}

#else
#pragma mark - Paper specific

- (void)traverseForScrollView:(UIView *)view
{
if (![[self.view valueForKey:@"_bridge"] valueForKey:@"_jsThread"]) {
Expand Down