-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move
react-native-window-view
to screens package (#1096)
Moved the `WindowView` to this repo and renamed it to `RNSFullWindowOverlay`. Removed the necessity of adding changes to `AppDelegate.m` by providing an extension of `UIWindow` in the package.
- Loading branch information
Showing
8 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as React from 'react'; | ||
|
||
import { Button, StyleSheet, View, Modal } from 'react-native'; | ||
import {FullWindowOverlay} from 'react-native-screens'; | ||
import { | ||
createNativeStackNavigator, | ||
NativeStackNavigationProp, | ||
} from 'react-native-screens/native-stack'; | ||
import { NavigationContainer, ParamListBase } from '@react-navigation/native'; | ||
|
||
function Home({ | ||
navigation, | ||
}: { | ||
navigation: NativeStackNavigationProp<ParamListBase>; | ||
}) { | ||
const [isShowModal, setIsShowModal] = React.useState(false); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<Button title="show rn modal" onPress={() => setIsShowModal(true)} /> | ||
<Button | ||
title="Go to RNScreens modal" | ||
onPress={() => navigation.navigate('Modal')} | ||
/> | ||
<Modal | ||
animationType="slide" | ||
visible={isShowModal} | ||
presentationStyle="pageSheet" | ||
> | ||
<View style={styles.container}> | ||
<Button | ||
title="dismiss rn modal" | ||
onPress={() => setIsShowModal(false)} | ||
/> | ||
</View> | ||
</Modal> | ||
<FullWindowOverlay style={{position: 'absolute', width: '100%', height: '100%', justifyContent: 'center'}}> | ||
<View style={styles.box} /> | ||
<Button title="click me" onPress={() => console.warn('clicked')} /> | ||
</FullWindowOverlay> | ||
</View> | ||
); | ||
} | ||
|
||
function ModalScreen({ | ||
navigation, | ||
}: { | ||
navigation: NativeStackNavigationProp<ParamListBase>; | ||
}) { | ||
return ( | ||
<View style={styles.container}> | ||
<Button title="dismiss modal" onPress={() => navigation.goBack()} /> | ||
</View> | ||
); | ||
} | ||
|
||
const NativeStack = createNativeStackNavigator(); | ||
|
||
export default function App() { | ||
return ( | ||
<NavigationContainer> | ||
<NativeStack.Navigator> | ||
<NativeStack.Screen name="Home" component={Home} /> | ||
<NativeStack.Screen | ||
name="Modal" | ||
component={ModalScreen} | ||
options={{ stackPresentation: 'modal' }} | ||
/> | ||
</NativeStack.Navigator> | ||
</NavigationContainer> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'flex-start', | ||
justifyContent: 'center', | ||
}, | ||
|
||
box: { | ||
width: 40, | ||
height: 40, | ||
borderRadius: 10, | ||
borderWidth: 2, | ||
borderColor: 'black', | ||
backgroundColor: 'red', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import <React/RCTInvalidating.h> | ||
#import <React/RCTView.h> | ||
#import <React/RCTViewManager.h> | ||
|
||
@interface RNSFullWindowOverlayManager : RCTViewManager | ||
|
||
@end | ||
|
||
@interface RNSFullWindowOverlayContainer : UIView | ||
|
||
@end | ||
|
||
@interface RNSFullWindowOverlay : RCTView <RCTInvalidating> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
#import "RNSFullWindowOverlay.h" | ||
|
||
#import <React/RCTTouchHandler.h> | ||
|
||
@implementation RNSFullWindowOverlayContainer | ||
|
||
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event | ||
{ | ||
for (UIView *view in [self subviews]) { | ||
if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) { | ||
return YES; | ||
} | ||
} | ||
return NO; | ||
} | ||
|
||
@end | ||
|
||
@implementation RNSFullWindowOverlay { | ||
__weak RCTBridge *_bridge; | ||
RNSFullWindowOverlayContainer *_container; | ||
CGRect _reactFrame; | ||
RCTTouchHandler *_touchHandler; | ||
} | ||
|
||
- (instancetype)initWithBridge:(RCTBridge *)bridge | ||
{ | ||
if (self = [super init]) { | ||
_bridge = bridge; | ||
_reactFrame = CGRectNull; | ||
_container = self.container; | ||
[self show]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)reactSetFrame:(CGRect)frame | ||
{ | ||
_reactFrame = frame; | ||
[_container setFrame:frame]; | ||
} | ||
|
||
- (void)addSubview:(UIView *)view | ||
{ | ||
[_container addSubview:view]; | ||
} | ||
|
||
- (RNSFullWindowOverlayContainer *)container | ||
{ | ||
if (_container == nil) { | ||
_container = [[RNSFullWindowOverlayContainer alloc] initWithFrame:_reactFrame]; | ||
} | ||
|
||
return _container; | ||
} | ||
|
||
- (void)show | ||
{ | ||
UIWindow *window = RCTSharedApplication().delegate.window; | ||
[window addSubview:_container]; | ||
} | ||
|
||
- (void)hide | ||
{ | ||
if (!_container) { | ||
return; | ||
} | ||
|
||
[_container removeFromSuperview]; | ||
} | ||
|
||
- (void)didMoveToWindow | ||
{ | ||
if (self.window == nil) { | ||
[self hide]; | ||
[_touchHandler detachFromView:_container]; | ||
} else { | ||
if (_touchHandler == nil) { | ||
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge]; | ||
} | ||
[_touchHandler attachToView:_container]; | ||
} | ||
} | ||
|
||
- (void)invalidate | ||
{ | ||
[self hide]; | ||
_container = nil; | ||
} | ||
|
||
@end | ||
|
||
@implementation RNSFullWindowOverlayManager | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
- (UIView *)view | ||
{ | ||
return [[RNSFullWindowOverlay alloc] initWithBridge:self.bridge]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface UIWindow (RNScreens) | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#import "RNSFullWindowOverlay.h" | ||
#import "UIWindow+RNScreens.h" | ||
|
||
@implementation UIWindow (RNScreens) | ||
|
||
- (void)didAddSubview:(UIView *)subview | ||
{ | ||
if (![subview isKindOfClass:[RNSFullWindowOverlayContainer class]]) { | ||
for (UIView *view in self.subviews) { | ||
if ([view isKindOfClass:[RNSFullWindowOverlayContainer class]]) { | ||
[self bringSubviewToFront:view]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters