react-native-toast-message 2.0.0 → 2.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,18 @@ Headers are one of:
9
9
 
10
10
  - `Added`, `Changed`, `Removed`, `Fixed` or `Breaking`.
11
11
 
12
+ ## [2.0.2]
13
+
14
+ ### Fixed
15
+
16
+ - Fast swipe up re-creates the toast [#280](https://github.com/calintamas/react-native-toast-message/issues/280)
17
+
18
+ ## [2.0.1]
19
+
20
+ ### Fixed
21
+
22
+ - `ansi-regex` vulnerability by upgrading to `@testing-library/jest-native v4.0.4` ([#277](https://github.com/calintamas/react-native-toast-message/pull/277))
23
+
12
24
  ## [2.0.0]
13
25
 
14
26
  ### Breaking
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![npm version](https://img.shields.io/npm/v/react-native-toast-message)](https://www.npmjs.com/package/react-native-toast-message)
4
4
  [![npm downloads](https://img.shields.io/npm/dw/react-native-toast-message)](https://www.npmjs.com/package/react-native-toast-message)
5
5
  [![Build](https://github.com/calintamas/react-native-toast-message/workflows/tests/badge.svg)](https://github.com/calintamas/react-native-toast-message/actions?query=workflow%3Atests)
6
- [![Coverage Status](https://coveralls.io/repos/github/calintamas/react-native-toast-message/badge.svg)](https://coveralls.io/github/calintamas/react-native-toast-message)
6
+ [![Coverage Status](https://coveralls.io/repos/github/calintamas/react-native-toast-message/badge.svg?branch=master)](https://coveralls.io/github/calintamas/react-native-toast-message?branch=master)
7
7
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
8
 
9
9
  Animated toast message component for React Native.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { ToastProps, ToastShowParams } from './types';
3
- export declare function Toast(props: ToastProps): JSX.Element;
3
+ export declare function Toast({ nestingLevel, ...rest }: ToastProps): JSX.Element;
4
4
  export declare namespace Toast {
5
5
  var show: (params: ToastShowParams) => void;
6
6
  var hide: (params?: void | undefined) => void;
package/lib/src/Toast.js CHANGED
@@ -13,15 +13,42 @@ const ToastRoot = React.forwardRef((props, ref) => {
13
13
  }));
14
14
  return (<ToastUI isVisible={isVisible} options={options} data={data} hide={hide} show={show} config={config}/>);
15
15
  });
16
- const toastRef = React.createRef();
17
- export function Toast(props) {
16
+ const refs = [];
17
+ export function Toast({ nestingLevel = 0, ...rest }) {
18
18
  return (<LoggerProvider enableLogs={false}>
19
- <ToastRoot ref={toastRef} {...props}/>
19
+ <ToastRoot ref={(ref) => {
20
+ refs[nestingLevel] = {
21
+ current: ref
22
+ };
23
+ }} {...rest}/>
20
24
  </LoggerProvider>);
21
25
  }
26
+ /**
27
+ * Get the active Toast instance `ref`, by priority.
28
+ * The "highest" Toast in the `View` hierarchy has the highest priority.
29
+ *
30
+ * For example, a Toast inside a `Modal`, would have a higher priority than a Toast inside App's Root
31
+ * (which has a default `nestingLevel` of 0)
32
+ * ```js
33
+ * <>
34
+ * <Toast nestingLevel={0} />
35
+ * <Modal>
36
+ * <Toast nestingLevel={1} />
37
+ * </Modal>
38
+ * </>
39
+ * ```
40
+ */
41
+ function getRef() {
42
+ const reversePriority = [...refs].reverse();
43
+ const activeRef = reversePriority.find((ref) => ref?.current !== null);
44
+ if (!activeRef) {
45
+ return null;
46
+ }
47
+ return activeRef.current;
48
+ }
22
49
  Toast.show = (params) => {
23
- toastRef.current?.show(params);
50
+ getRef()?.show(params);
24
51
  };
25
52
  Toast.hide = (params) => {
26
- toastRef.current?.hide(params);
53
+ getRef()?.hide(params);
27
54
  };
@@ -11,6 +11,12 @@ export declare type AnimatedContainerProps = {
11
11
  onHide: () => void;
12
12
  onRestorePosition?: () => void;
13
13
  };
14
+ /**
15
+ * Produces a positive damping value.
16
+ *
17
+ * To note: `moveY` becomes negative when going off-screen. By making sure the value
18
+ * produced is always positive, we avoid issues like: https://github.com/calintamas/react-native-toast-message/issues/280
19
+ */
14
20
  export declare function dampingFor(gesture: PanResponderGestureState, position: ToastPosition): number;
15
21
  export declare function animatedValueFor(gesture: PanResponderGestureState, position: ToastPosition, damping: number): number;
16
22
  export declare function AnimatedContainer({ children, isVisible, position, topOffset, bottomOffset, keyboardOffset, onHide, onRestorePosition }: AnimatedContainerProps): JSX.Element;
@@ -6,15 +6,21 @@ import { noop } from '../utils/func';
6
6
  import { bound } from '../utils/number';
7
7
  import { getTestId } from '../utils/test-id';
8
8
  import { styles } from './AnimatedContainer.styles';
9
+ /**
10
+ * Produces a positive damping value.
11
+ *
12
+ * To note: `moveY` becomes negative when going off-screen. By making sure the value
13
+ * produced is always positive, we avoid issues like: https://github.com/calintamas/react-native-toast-message/issues/280
14
+ */
9
15
  export function dampingFor(gesture, position) {
10
16
  const { moveY } = gesture;
11
17
  switch (position) {
12
18
  case 'bottom': {
13
19
  const { height: screenHeight } = Dimensions.get('screen');
14
- return screenHeight - moveY;
20
+ return Math.abs(screenHeight - moveY);
15
21
  }
16
22
  case 'top':
17
- return moveY;
23
+ return Math.abs(moveY);
18
24
  default:
19
25
  throw new Error(`Toast position: ${position} not implemented`);
20
26
  }
@@ -101,11 +101,49 @@ export declare type ToastConfigParams<Props> = {
101
101
  export declare type ToastConfig = {
102
102
  [key: string]: (params: ToastConfigParams<any>) => React.ReactNode;
103
103
  };
104
+ export declare type ToastRef = {
105
+ show: (params: ToastShowParams) => void;
106
+ hide: (params: ToastHideParams) => void;
107
+ };
104
108
  /**
105
109
  * `props` that can be set on the Toast instance.
106
110
  * They act as defaults for all Toasts that are shown.
107
111
  */
108
112
  export declare type ToastProps = {
113
+ /**
114
+ * Nesting level for the Toast instance.
115
+ * The "higher" a Toast instance is in the `View` hierarchy, the bigger its nesting level value.
116
+ * Default `nestingLevel = 0`.
117
+ *
118
+ * By setting this prop, you can show Toasts inside Modals, no matter how nested they would be.
119
+ *
120
+ * For example, a Toast inside a `Modal`, would have a bigger `nestingLevel`
121
+ * than a Toast inside App's Root (which has the default `nestingLevel` of 0).
122
+ *
123
+ * ```js
124
+ * <>
125
+ * <Toast />
126
+ * <Modal>
127
+ * <Toast nestingLevel={1} />
128
+ * </Modal>
129
+ * </>
130
+ * ```
131
+ *
132
+ * If you have nested Modals, the `nestingLevel` prop needs to be adjusted accordingly:
133
+ *
134
+ * ```js
135
+ * <>
136
+ * <Toast />
137
+ * <Modal>
138
+ * <Toast nestingLevel={1} />
139
+ * <Modal>
140
+ * <Toast nestingLevel={2} />
141
+ * </Modal>
142
+ * </Modal>
143
+ * </>
144
+ * ```
145
+ */
146
+ nestingLevel?: number;
109
147
  /**
110
148
  * Layout configuration for custom Toast types
111
149
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-toast-message",
3
- "version": "2.0.0",
3
+ "version": "2.1.0-beta.2",
4
4
  "description": "Toast message component for React Native",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -21,13 +21,14 @@
21
21
  "prettier": "./node_modules/.bin/prettier --write",
22
22
  "lint": "./node_modules/.bin/eslint --fix",
23
23
  "lint-staged": "./node_modules/.bin/lint-staged",
24
- "test": "./node_modules/.bin/jest"
24
+ "test": "./node_modules/.bin/jest",
25
+ "yalc:push": "yarn build && yalc publish --push"
25
26
  },
26
27
  "author": "Calin Tamas <calintamas2@gmail.com>",
27
28
  "license": "MIT",
28
29
  "devDependencies": {
29
30
  "@babel/core": "^7.15.8",
30
- "@testing-library/jest-native": "^4.0.2",
31
+ "@testing-library/jest-native": "^4.0.4",
31
32
  "@testing-library/react-hooks": "^7.0.2",
32
33
  "@testing-library/react-native": "^8.0.0",
33
34
  "@types/jest": "^27.0.1",
@@ -41,7 +42,8 @@
41
42
  "prettier": "^2.4.1",
42
43
  "prettier-plugin-import-sort": "^0.0.7",
43
44
  "react-test-renderer": "^17.0.2",
44
- "typescript": "^4.4.3"
45
+ "typescript": "^4.4.3",
46
+ "yalc": "^1.0.0-pre.53"
45
47
  },
46
48
  "peerDependencies": {
47
49
  "react": ">=17.0.2",