Skip to content

Commit d8b076d

Browse files
committed
fixed keyboard visibility handler for android and iOS
1 parent 82cd515 commit d8b076d

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img alt="RNRooster" src="./cover.png" />
22

3-
An elegant Toast solution for react-native apps. Built using Typescript, hooks, contexts and providers. It works on Android and iOS platforms
3+
An elegant Toast solution for react-native apps. Built using Typescript, hooks and context. It works on Android and iOS platforms
44

55
<p align="center">
66
<img alt="Demo" src="./demo.gif" />
@@ -10,18 +10,17 @@ An elegant Toast solution for react-native apps. Built using Typescript, hooks,
1010

1111
I've been working on React-Native projects for a long time and I use toast very often, but I still haven't found a library that is flexible enough for handling toasts. So, I decided to work on this to build a flexible and functional Toast library powerful like [react-toastify](https://github.com/fkhadra/react-toastify)
1212

13+
## ⬇️ Installation
1314

14-
## 📦 Features
15+
```bash
16+
yarn add react-native-rooster styled-components
17+
```
1518

16-
- [x] Automatically keyboard listener. If keyboard is open the toast will not be hidden by it
19+
or
1720

18-
- [x] Support for different types of toasts (success, info, warning)
19-
- [ ] Support for different positions (top, center, top-left, top-right and so on...)
20-
- [ ] Support for icons
21-
- [ ] Support for multiple toasts being shown at the same time
22-
- [ ] Support for toasts be used inside regular functions not only components' functions
23-
24-
Have more ideas? Put it here!
21+
```bash
22+
npm i react-native-rooster styled-components
23+
```
2524

2625
## 🛠 How it works
2726

@@ -41,7 +40,8 @@ Now you can use our hooks to show your toasts:
4140
import { useToast } from 'react-native-rooster';
4241

4342
addToast({
44-
title: 'Error',
43+
type: 'success', //optional - default: info
44+
title: 'Error', //optional - default: none
4545
message: 'An error ocurred'
4646
})
4747
```
@@ -64,10 +64,14 @@ Removes the last toast from screen.
6464

6565
You can change some configs globally. Right now, we only support `bgColor` for each type of toast. The default colors for each type of toast are:
6666

67-
* <p style="color:#7890f0;">Info: #7890f0</p>
68-
* <p style="color:#35d0ba;">Success: #35d0ba</p>
69-
* <p style="color:#ff9100;">Warning: #ff9100</p>
70-
* <p style="color:#d92027;">Error: #d92027</p>
67+
- ![#7890f0](https://via.placeholder.com/15/7890f0/000000?text=+) `Info: #7890f0`
68+
69+
- ![#35d0ba](https://via.placeholder.com/15/35d0ba/000000?text=+) `Success: #35d0ba`
70+
71+
- ![#ff9100](https://via.placeholder.com/15/ff9100/000000?text=+) `Warning: #ff9100`
72+
73+
- ![#d92027](https://via.placeholder.com/15/d92027/000000?text=+) `Error: #d92027`
74+
7175

7276
For example:
7377

@@ -81,6 +85,18 @@ addToast({
8185
})
8286
```
8387

88+
## 📦 Features
89+
90+
- [x] Automatically keyboard listener. If keyboard is open the toast will not be hidden by it
91+
92+
- [x] Support for different types of toasts (success, info, warning)
93+
- [ ] Support for different positions (top, center, top-left, top-right and so on...)
94+
- [ ] Support for icons
95+
- [ ] Support for multiple toasts being shown at the same time
96+
- [ ] Support for toasts be used inside regular functions not only components' functions
97+
98+
Have more ideas? Put it here!
99+
84100
## 🤝 Contributing
85101

86102
You can clone this repository and just run:

demo.gif

60.6 KB
Loading

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"eslint-plugin-import": "^2.22.0",
4040
"eslint-plugin-jsx-a11y": "^6.3.1",
4141
"eslint-plugin-prettier": "^3.1.4",
42-
"eslint-plugin-react": "^7.20.3",
42+
"eslint-plugin-react": "^7.20.4",
4343
"eslint-plugin-react-hooks": "^4.0.8",
4444
"jest": "^26.1.0",
4545
"metro-react-native-babel-preset": "^0.61.0",

example/yalc.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "v1",
33
"packages": {
44
"react-native-rooster": {
5-
"signature": "d1a5cabae7fc1734e46439e949739b0b",
5+
"signature": "7e82ec7ac183ad6321c085bf0463484e",
66
"file": true
77
}
88
}

example/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,10 +2802,10 @@ eslint-plugin-react-hooks@^4.0.8:
28022802
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.0.8.tgz#a9b1e3d57475ccd18276882eff3d6cba00da7a56"
28032803
integrity sha512-6SSb5AiMCPd8FDJrzah+Z4F44P2CdOaK026cXFV+o/xSRzfOiV1FNFeLl2z6xm3yqWOQEZ5OfVgiec90qV2xrQ==
28042804

2805-
eslint-plugin-react@^7.20.3:
2806-
version "7.20.3"
2807-
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.20.3.tgz#0590525e7eb83890ce71f73c2cf836284ad8c2f1"
2808-
integrity sha512-txbo090buDeyV0ugF3YMWrzLIUqpYTsWSDZV9xLSmExE1P/Kmgg9++PD931r+KEWS66O1c9R4srLVVHmeHpoAg==
2805+
eslint-plugin-react@^7.20.4:
2806+
version "7.20.4"
2807+
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.20.4.tgz#c14d2631221ec694ddd84557d7152f44b66e4aa0"
2808+
integrity sha512-y4DOQ0LrzuDQFEAnYFGjJMRHQQqfTco02qiWI00eGQYikHTzC15S5aRHGWSffnThv8sBpsmFBLky3K5keniAJg==
28092809
dependencies:
28102810
array-includes "^3.1.1"
28112811
array.prototype.flatmap "^1.2.3"
@@ -5916,7 +5916,7 @@ react-native-reanimated@^1.10.1:
59165916
fbjs "^1.0.0"
59175917

59185918
"react-native-rooster@file:.yalc/react-native-rooster":
5919-
version "1.0.0-65dd391c"
5919+
version "1.0.0"
59205920

59215921
react-native-safe-area-context@^3.1.1:
59225922
version "3.1.1"

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
"main": "build/index.js",
66
"module": "build/index.es.js",
77
"jsnext:main": "build/index.es.js",
8-
"keywords": [],
8+
"keywords": [
9+
"react-native",
10+
"react-native-rooster",
11+
"ios",
12+
"android",
13+
"toast"
14+
],
915
"author": "mCodex",
1016
"license": "MIT",
1117
"homepage": "https://github.com/mcodex/react-native-rooster",

src/components/ToastContainer/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
2+
import { KeyboardAvoidingView, Platform } from 'react-native';
23

3-
import useKeyboard from 'hooks/useKeyboard';
44
import useToast from 'hooks/useToast';
5+
import useKeyboard from 'hooks/useKeyboard';
56

67
import { Container, Title, Message } from './styles';
78

@@ -29,20 +30,29 @@ const Toast: React.FC<IToastComponent> = (props) => {
2930
});
3031
}, [messages, removeToast]);
3132

33+
/**
34+
* @TODO find a better way to handle Toast's visibility when keyboard is opened.
35+
* On Android KeyboardAvoidingView works perfectly, but not on iOS.
36+
* On the other hand, the useKeyboard works perfectly on iOS, but not on Android which gives wrong calculation
37+
*/
38+
const handleBottomPadding = useMemo(() => {
39+
return Platform.OS === 'ios' ? keyboardHeight : 20;
40+
}, [keyboardHeight]);
41+
3242
return (
33-
<>
43+
<KeyboardAvoidingView>
3444
{messages.map(({ id, title, message, type }) => (
3545
<Container
3646
key={id}
37-
bottom={keyboardHeight}
47+
bottom={handleBottomPadding}
3848
type={type}
3949
bgColor={bgColor}
4050
>
4151
{title && <Title>{title}</Title>}
4252
<Message>{message}</Message>
4353
</Container>
4454
))}
45-
</>
55+
</KeyboardAvoidingView>
4656
);
4757
};
4858

0 commit comments

Comments
 (0)