react-native-toast-message 2.1.0-beta.1 → 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.
@@ -1,9 +1,7 @@
1
1
  /// <reference types="react" />
2
- import { ToastProps, ToastRef, ToastShowParams } from './types';
3
- export declare function Toast(props: ToastProps): JSX.Element;
2
+ import { ToastProps, ToastShowParams } from './types';
3
+ export declare function Toast({ nestingLevel, ...rest }: ToastProps): JSX.Element;
4
4
  export declare namespace Toast {
5
- var setRootRef: (ref: ToastRef) => void;
6
- var setModalRef: (ref: ToastRef) => void;
7
5
  var show: (params: ToastShowParams) => void;
8
6
  var hide: (params?: void | undefined) => void;
9
7
  }
package/lib/src/Toast.js CHANGED
@@ -13,29 +13,38 @@ 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 rootRef = {
17
- current: null
18
- };
19
- const modalRef = {
20
- current: null
21
- };
22
- export function Toast(props) {
23
- const { setRef } = props;
16
+ const refs = [];
17
+ export function Toast({ nestingLevel = 0, ...rest }) {
24
18
  return (<LoggerProvider enableLogs={false}>
25
- <ToastRoot ref={setRef} {...props}/>
19
+ <ToastRoot ref={(ref) => {
20
+ refs[nestingLevel] = {
21
+ current: ref
22
+ };
23
+ }} {...rest}/>
26
24
  </LoggerProvider>);
27
25
  }
28
- Toast.setRootRef = (ref) => {
29
- rootRef.current = ref;
30
- };
31
- Toast.setModalRef = (ref) => {
32
- modalRef.current = ref;
33
- };
34
26
  /**
35
- * Get Toast instance ref, by priority
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
+ * ```
36
40
  */
37
41
  function getRef() {
38
- return modalRef.current || rootRef.current;
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;
39
48
  }
40
49
  Toast.show = (params) => {
41
50
  getRef()?.show(params);
@@ -111,20 +111,39 @@ export declare type ToastRef = {
111
111
  */
112
112
  export declare type ToastProps = {
113
113
  /**
114
- * Set a ref to your Toast instance using one of the provided methods:
115
- * `Toast.setRootRef` or `Toast.setModalRef`
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).
116
122
  *
117
123
  * ```js
118
- * // To set a ref for the Toast rendered in your app root
119
- * <Toast setRef={Toast.setRootRef} />
124
+ * <>
125
+ * <Toast />
126
+ * <Modal>
127
+ * <Toast nestingLevel={1} />
128
+ * </Modal>
129
+ * </>
120
130
  * ```
121
131
  *
132
+ * If you have nested Modals, the `nestingLevel` prop needs to be adjusted accordingly:
133
+ *
122
134
  * ```js
123
- * // To set a ref for the Toast rendered in a Modal
124
- * <Toast setRef={Toast.setModalRef} />
135
+ * <>
136
+ * <Toast />
137
+ * <Modal>
138
+ * <Toast nestingLevel={1} />
139
+ * <Modal>
140
+ * <Toast nestingLevel={2} />
141
+ * </Modal>
142
+ * </Modal>
143
+ * </>
125
144
  * ```
126
145
  */
127
- setRef: (ref: ToastRef) => void;
146
+ nestingLevel?: number;
128
147
  /**
129
148
  * Layout configuration for custom Toast types
130
149
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-toast-message",
3
- "version": "2.1.0-beta.1",
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",