Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vonovak/react-native-simple-toast
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.0.0
Choose a base ref
...
head repository: vonovak/react-native-simple-toast
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.0.1
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Jul 19, 2023

  1. docs: nicer list

    vonovak committed Jul 19, 2023
    Copy the full SHA
    a62c66a View commit details

Commits on Aug 1, 2023

  1. fix: update how toastWindow is obtained (#58)

    * fix: unbalanced calls to begin/end appearance transitions
    
    * fix: update how toastWindow is obtained
    
    see facebook/react-native#35716
    vonovak authored Aug 1, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5b8defb View commit details
  2. chore: release 3.0.1

    vonovak committed Aug 1, 2023
    Copy the full SHA
    f24a313 View commit details
Showing with 56 additions and 18 deletions.
  1. +3 −0 README.md
  2. +13 −8 example/src/App.tsx
  3. +2 −3 ios/RNSimpleToast.mm
  4. +37 −6 ios/RNToastViewController.m
  5. +1 −1 package.json
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -5,8 +5,11 @@ React Native Toast component for both Android and iOS. It just lets iOS users ha
## Summary

✅ supports both old and new architecture

✅ extremely simple fire-and-forget api, same as `ToastAndroid`

✅ renders on top of `Modal`s and `Alert`s

✅ customizable styling

## Screenshots
21 changes: 13 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -48,6 +48,12 @@ export default function App() {
style={{ backgroundColor: 'white' }}
>
<View style={styles.container}>
<Button
title={'simple toast'}
onPress={() => {
Toast.show('This is a toast.', Toast.SHORT);
}}
/>
<Button
onPress={() => {
setModalVisible(true);
@@ -59,15 +65,10 @@ export default function App() {
);
}, 500);
}}
title="Show Modal"
title="toast on top of Modal"
/>
{/*<Text>{JSON.stringify(Toast, null, 2)}</Text>*/}
<Button
title={'simple toast'}
onPress={() => {
Toast.show('This is a toast.', Toast.SHORT);
}}
/>

<Button
title={'styled toast'}
onPress={() => {
@@ -94,7 +95,11 @@ export default function App() {
onPress={() => {
Alert.alert('this is an alert');
setTimeout(() => {
Toast.showWithGravity('This is an alert toast.', 5, Toast.TOP);
Toast.showWithGravity(
'This is an alert toast.',
5,
Toast.CENTER,
);
}, 100);
}}
/>
5 changes: 2 additions & 3 deletions ios/RNSimpleToast.mm
Original file line number Diff line number Diff line change
@@ -148,16 +148,15 @@ - (CGPoint)rnToast_centerPointForPosition:(NSString *)gravity withToast:(UIView
safeInsets = view.safeAreaInsets;
}

CGFloat topPadding = safeInsets.top;
CGFloat bottomPadding = safeInsets.bottom;

if ([CSToastPositionTop isEqualToString:gravity]) {
CGFloat topPadding = safeInsets.top;
return CGPointMake(view.bounds.size.width / 2.0, (toast.frame.size.height / 2.0) + topPadding);
} else if ([CSToastPositionCenter isEqualToString:gravity]) {
return CGPointMake(view.bounds.size.width / 2.0, view.bounds.size.height / 2.0);
}

// default to bottom
CGFloat bottomPadding = safeInsets.bottom;
return CGPointMake(view.bounds.size.width / 2.0, (view.bounds.size.height - (toast.frame.size.height / 2.0)) - bottomPadding);
}

43 changes: 37 additions & 6 deletions ios/RNToastViewController.m
Original file line number Diff line number Diff line change
@@ -10,18 +10,36 @@ @interface RNToastViewController ()

@implementation RNToastViewController

- (UIWindow *)toastWindow {
// presenting directly from RCTSharedApplication().keyWindow won't work for Alerts
// which is why we have our own VC

- (UIWindow *)toastWindow
{
if (_toastWindow == nil) {
_toastWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
_toastWindow.rootViewController = [UIViewController new];
_toastWindow.windowLevel = UIWindowLevelAlert + 2;
_toastWindow.userInteractionEnabled = NO;
_toastWindow = [self getUIWindowFromScene];

if (_toastWindow == nil) {
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
_toastWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
} else {
// keyWindow is nil, so we cannot create and initialize _toastWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}

if (_toastWindow) {
_toastWindow.rootViewController = [UIViewController new];
_toastWindow.windowLevel = UIWindowLevelAlert + 1;
_toastWindow.userInteractionEnabled = NO;
}
}

return _toastWindow;
}

- (void)show:(void (^)(void))completion {
self.modalPresentationStyle = UIModalPresentationFullScreen;
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self.toastWindow setHidden:NO];
[self.toastWindow.rootViewController presentViewController:self animated:NO completion:completion];
}
@@ -36,4 +54,17 @@ - (void)hide {
_toastWindow = nil;
}

- (UIWindow *)getUIWindowFromScene
{
if (@available(iOS 13.0, *)) {
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive &&
[scene isKindOfClass:[UIWindowScene class]]) {
return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
}
}
}
return nil;
}

@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-simple-toast",
"version": "3.0.0",
"version": "3.0.1",
"description": "test",
"main": "lib/commonjs/index",
"module": "lib/module/index",