react-native-toast-message 2.0.2 → 2.1.0-beta.1

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,7 +1,9 @@
1
1
  /// <reference types="react" />
2
- import { ToastProps, ToastShowParams } from './types';
2
+ import { ToastProps, ToastRef, ToastShowParams } from './types';
3
3
  export declare function Toast(props: ToastProps): JSX.Element;
4
4
  export declare namespace Toast {
5
+ var setRootRef: (ref: ToastRef) => void;
6
+ var setModalRef: (ref: ToastRef) => void;
5
7
  var show: (params: ToastShowParams) => void;
6
8
  var hide: (params?: void | undefined) => void;
7
9
  }
package/lib/src/Toast.js CHANGED
@@ -13,15 +13,33 @@ 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();
16
+ const rootRef = {
17
+ current: null
18
+ };
19
+ const modalRef = {
20
+ current: null
21
+ };
17
22
  export function Toast(props) {
23
+ const { setRef } = props;
18
24
  return (<LoggerProvider enableLogs={false}>
19
- <ToastRoot ref={toastRef} {...props}/>
25
+ <ToastRoot ref={setRef} {...props}/>
20
26
  </LoggerProvider>);
21
27
  }
28
+ Toast.setRootRef = (ref) => {
29
+ rootRef.current = ref;
30
+ };
31
+ Toast.setModalRef = (ref) => {
32
+ modalRef.current = ref;
33
+ };
34
+ /**
35
+ * Get Toast instance ref, by priority
36
+ */
37
+ function getRef() {
38
+ return modalRef.current || rootRef.current;
39
+ }
22
40
  Toast.show = (params) => {
23
- toastRef.current?.show(params);
41
+ getRef()?.show(params);
24
42
  };
25
43
  Toast.hide = (params) => {
26
- toastRef.current?.hide(params);
44
+ getRef()?.hide(params);
27
45
  };
@@ -101,11 +101,30 @@ 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
+ * Set a ref to your Toast instance using one of the provided methods:
115
+ * `Toast.setRootRef` or `Toast.setModalRef`
116
+ *
117
+ * ```js
118
+ * // To set a ref for the Toast rendered in your app root
119
+ * <Toast setRef={Toast.setRootRef} />
120
+ * ```
121
+ *
122
+ * ```js
123
+ * // To set a ref for the Toast rendered in a Modal
124
+ * <Toast setRef={Toast.setModalRef} />
125
+ * ```
126
+ */
127
+ setRef: (ref: ToastRef) => void;
109
128
  /**
110
129
  * Layout configuration for custom Toast types
111
130
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-toast-message",
3
- "version": "2.0.2",
3
+ "version": "2.1.0-beta.1",
4
4
  "description": "Toast message component for React Native",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -21,7 +21,8 @@
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",
@@ -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",