rn-toastify 1.0.12 → 2.0.0

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,49 +1,123 @@
1
- import React from 'react';
2
- import toastManagerInstance from '../context/ToastManager';
3
- import LoadingToast from '../components/LoadingToast';
4
- import SuccessToast from '../components/SuccessToast';
5
- import ErrorToast from '../components/ErrorToast';
6
- import CustomToast from '../components/CustomeToast';
7
- import EmojiToast from '../components/EmojiToast';
8
-
9
-
10
- const useToast = () => {
11
- const show = (content, options) => {
12
- toastManagerInstance.show(content, options);
13
- };
14
-
15
- const success = (message, options) => {
16
- show(<SuccessToast message={message} />, options);
17
- };
18
-
19
- const error = (message, options) => {
20
- show(<ErrorToast message={message} />, options);
21
- };
22
-
23
- const promise = (promise, { loading, success, error }) => {
24
- toastManagerInstance.promise(promise, {
25
- loading: <LoadingToast message={loading} />,
26
- success: <SuccessToast message={success} />,
27
- error: <ErrorToast message={error} />,
28
- });
29
- };
30
-
31
- const custom = (content, options) => {
32
- show(<CustomToast content={content} />, options);
33
- };
34
-
35
- const emoji = (message, emoji, options) => {
36
- show(<EmojiToast message={message} emoji={emoji} />, options);
37
- };
38
-
39
- return {
40
- show,
41
- success,
42
- error,
43
- promise,
44
- custom,
45
- emoji,
46
- };
47
- };
48
-
49
- export default useToast;
1
+ import React from 'react';
2
+ import toastManagerInstance from '../context/ToastManager';
3
+ import LoadingToast from '../components/LoadingToast';
4
+ import SuccessToast from '../components/SuccessToast';
5
+ import ErrorToast from '../components/ErrorToast';
6
+ import InfoToast from '../components/InfoToast';
7
+ import WarningToast from '../components/WarningToast';
8
+ import CustomToast from '../components/CustomToast';
9
+ import EmojiToast from '../components/EmojiToast';
10
+
11
+ /**
12
+ * useToast — imperative hook to show toast notifications from anywhere.
13
+ *
14
+ * @returns {{
15
+ * show: (content: React.ReactNode, options?: Object) => string,
16
+ * success: (message: string, options?: Object) => string,
17
+ * error: (message: string, options?: Object) => string,
18
+ * info: (message: string, options?: Object) => string,
19
+ * warning: (message: string, options?: Object) => string,
20
+ * promise: (promise: Promise, messages: Object, options?: Object) => Promise,
21
+ * custom: (content: React.ReactNode, options?: Object) => string,
22
+ * emoji: (message: string, emoji: string, options?: Object) => string,
23
+ * dismiss: (id: string) => void,
24
+ * dismissAll: () => void,
25
+ * update: (id: string, content: React.ReactNode, options?: Object) => void,
26
+ * isActive: (id: string) => boolean,
27
+ * }}
28
+ */
29
+ const useToast = () => {
30
+ const show = (content, options) => {
31
+ return toastManagerInstance.show(content, options);
32
+ };
33
+
34
+ const success = (message, options) => {
35
+ const { title, ...rest } = options || {};
36
+ return show(<SuccessToast title={title} message={message} />, rest);
37
+ };
38
+
39
+ const error = (message, options) => {
40
+ const { title, ...rest } = options || {};
41
+ return show(<ErrorToast title={title} message={message} />, rest);
42
+ };
43
+
44
+ const info = (message, options) => {
45
+ const { title, ...rest } = options || {};
46
+ return show(<InfoToast title={title} message={message} />, rest);
47
+ };
48
+
49
+ const warning = (message, options) => {
50
+ const { title, ...rest } = options || {};
51
+ return show(<WarningToast title={title} message={message} />, rest);
52
+ };
53
+
54
+ const promise = (promiseValue, { loading, success: successMsg, error: errorMsg }, options) => {
55
+ return toastManagerInstance.promise(
56
+ promiseValue,
57
+ {
58
+ loading: typeof loading === 'string'
59
+ ? <LoadingToast message={loading} />
60
+ : loading,
61
+ success: typeof successMsg === 'string'
62
+ ? <SuccessToast message={successMsg} />
63
+ : typeof successMsg === 'function'
64
+ ? (result) => {
65
+ const msg = successMsg(result);
66
+ return typeof msg === 'string' ? <SuccessToast message={msg} /> : msg;
67
+ }
68
+ : successMsg,
69
+ error: typeof errorMsg === 'string'
70
+ ? <ErrorToast message={errorMsg} />
71
+ : typeof errorMsg === 'function'
72
+ ? (err) => {
73
+ const msg = errorMsg(err);
74
+ return typeof msg === 'string' ? <ErrorToast message={msg} /> : msg;
75
+ }
76
+ : errorMsg,
77
+ },
78
+ options
79
+ );
80
+ };
81
+
82
+ const custom = (content, options) => {
83
+ return show(<CustomToast content={content} />, options);
84
+ };
85
+
86
+ const emoji = (message, emojiChar, options) => {
87
+ const { title, ...rest } = options || {};
88
+ return show(<EmojiToast title={title} message={message} emoji={emojiChar} />, rest);
89
+ };
90
+
91
+ const dismiss = (id) => {
92
+ toastManagerInstance.remove(id);
93
+ };
94
+
95
+ const dismissAll = () => {
96
+ toastManagerInstance.dismissAll();
97
+ };
98
+
99
+ const update = (id, content, options) => {
100
+ toastManagerInstance.update(id, content, options);
101
+ };
102
+
103
+ const isActive = (id) => {
104
+ return toastManagerInstance.isActive(id);
105
+ };
106
+
107
+ return {
108
+ show,
109
+ success,
110
+ error,
111
+ info,
112
+ warning,
113
+ promise,
114
+ custom,
115
+ emoji,
116
+ dismiss,
117
+ dismissAll,
118
+ update,
119
+ isActive,
120
+ };
121
+ };
122
+
123
+ export default useToast;
package/src/types.d.ts ADDED
@@ -0,0 +1,172 @@
1
+ import React from 'react';
2
+
3
+ // ─── Toast Options ───────────────────────────────────────────────────────────
4
+
5
+ export interface ToastOptions {
6
+ /** Duration in milliseconds before auto-dismiss. Use `Infinity` to persist. Default: 3000 */
7
+ duration?: number;
8
+ /** Position on screen. Default: 'top' */
9
+ position?: 'top' | 'bottom' | 'center';
10
+ /** Custom styles for the toast wrapper */
11
+ style?: object;
12
+ /** Bold title text (displayed above message) */
13
+ title?: string;
14
+ }
15
+
16
+ export interface PromiseMessages<T = any> {
17
+ /** Content to show while the promise is pending */
18
+ loading: string | React.ReactNode;
19
+ /** Content to show on success. Can be a string, element, or function receiving the result */
20
+ success: string | React.ReactNode | ((result: T) => string | React.ReactNode);
21
+ /** Content to show on error. Can be a string, element, or function receiving the error */
22
+ error: string | React.ReactNode | ((error: any) => string | React.ReactNode);
23
+ }
24
+
25
+ export interface PromiseOptions extends Omit<ToastOptions, 'title'> {
26
+ /** Duration for the success toast */
27
+ successDuration?: number;
28
+ /** Duration for the error toast */
29
+ errorDuration?: number;
30
+ }
31
+
32
+ // ─── useToast Hook ───────────────────────────────────────────────────────────
33
+
34
+ export interface UseToastReturn {
35
+ /** Show a raw toast with custom content */
36
+ show: (content: React.ReactNode, options?: ToastOptions) => string;
37
+ /** Show a success toast with green accent */
38
+ success: (message: string, options?: ToastOptions) => string;
39
+ /** Show an error toast with red accent */
40
+ error: (message: string, options?: ToastOptions) => string;
41
+ /** Show an info toast with blue accent */
42
+ info: (message: string, options?: ToastOptions) => string;
43
+ /** Show a warning toast with amber accent */
44
+ warning: (message: string, options?: ToastOptions) => string;
45
+ /** Handle promise states: loading → success/error */
46
+ promise: <T>(promise: Promise<T>, messages: PromiseMessages<T>, options?: PromiseOptions) => Promise<T>;
47
+ /** Show a toast with custom React content */
48
+ custom: (content: React.ReactNode, options?: Omit<ToastOptions, 'title'>) => string;
49
+ /** Show a toast with an emoji */
50
+ emoji: (message: string, emoji: string, options?: ToastOptions) => string;
51
+ /** Dismiss a specific toast by ID */
52
+ dismiss: (id: string) => void;
53
+ /** Dismiss all active toasts */
54
+ dismissAll: () => void;
55
+ /** Update an existing toast's content */
56
+ update: (id: string, content: React.ReactNode, options?: Partial<ToastOptions>) => void;
57
+ /** Check if a toast with the given ID is currently visible */
58
+ isActive: (id: string) => boolean;
59
+ }
60
+
61
+ declare function useToast(): UseToastReturn;
62
+ export default useToast;
63
+ export { useToast };
64
+
65
+ // ─── ToastContainer ──────────────────────────────────────────────────────────
66
+
67
+ export interface ToastContainerProps {
68
+ /** Force a specific theme. If omitted, follows system preference */
69
+ theme?: 'light' | 'dark';
70
+ /** Maximum number of visible toasts at once. Default: 3 */
71
+ maxVisible?: number;
72
+ /** Default toast position when not specified per-toast. Default: 'top' */
73
+ defaultPosition?: 'top' | 'bottom' | 'center';
74
+ /** Extra offset from the top edge (in addition to safe area). Default: 0 */
75
+ topOffset?: number;
76
+ /** Extra offset from the bottom edge. Default: 0 */
77
+ bottomOffset?: number;
78
+ /** Whether toasts can be swiped to dismiss. Default: true */
79
+ swipeable?: boolean;
80
+ }
81
+
82
+ export declare const ToastContainer: React.FC<ToastContainerProps>;
83
+
84
+ // ─── Toast Components ────────────────────────────────────────────────────────
85
+
86
+ export interface ToastComponentProps {
87
+ /** Bold title text */
88
+ title?: string;
89
+ /** Body message text */
90
+ message?: string;
91
+ /** Theme (injected by container, can be overridden) */
92
+ theme?: 'light' | 'dark';
93
+ /** Duration (injected by container for progress bar) */
94
+ duration?: number;
95
+ }
96
+
97
+ export interface EmojiToastProps extends ToastComponentProps {
98
+ /** Emoji character to display */
99
+ emoji?: string;
100
+ }
101
+
102
+ export interface CustomToastProps {
103
+ /** Custom React content to render */
104
+ content?: React.ReactNode;
105
+ /** Theme */
106
+ theme?: 'light' | 'dark';
107
+ }
108
+
109
+ export declare const SuccessToast: React.FC<ToastComponentProps>;
110
+ export declare const ErrorToast: React.FC<ToastComponentProps>;
111
+ export declare const InfoToast: React.FC<ToastComponentProps>;
112
+ export declare const WarningToast: React.FC<ToastComponentProps>;
113
+ export declare const LoadingToast: React.FC<ToastComponentProps>;
114
+ export declare const EmojiToast: React.FC<EmojiToastProps>;
115
+ export declare const CustomToast: React.FC<CustomToastProps>;
116
+ export declare const BaseToast: React.FC<any>;
117
+ export declare const ProgressBar: React.FC<{
118
+ duration?: number;
119
+ color?: string;
120
+ trackColor?: string;
121
+ paused?: boolean;
122
+ }>;
123
+
124
+ /** @deprecated Use `CustomToast` instead */
125
+ export declare const CustomeToast: React.FC<CustomToastProps>;
126
+
127
+ // ─── Toast animation wrapper ────────────────────────────────────────────────
128
+
129
+ export declare const Toast: React.FC<{
130
+ visible: boolean;
131
+ duration?: number;
132
+ position?: 'top' | 'bottom' | 'center';
133
+ children: React.ReactNode;
134
+ onHide?: () => void;
135
+ style?: object;
136
+ theme?: 'light' | 'dark';
137
+ }>;
138
+
139
+ // ─── ToastManager (singleton) ────────────────────────────────────────────────
140
+
141
+ export declare const ToastManager: {
142
+ show: (content: React.ReactNode, options?: ToastOptions, id?: string) => string;
143
+ remove: (id: string) => void;
144
+ dismissAll: () => void;
145
+ update: (id: string, content: React.ReactNode, options?: Partial<ToastOptions>) => void;
146
+ isActive: (id: string) => boolean;
147
+ promise: <T>(promise: Promise<T>, messages: PromiseMessages<T>, options?: PromiseOptions) => Promise<T>;
148
+ configure: (config: { maxVisible?: number }) => void;
149
+ };
150
+
151
+ // ─── Theme Utilities ─────────────────────────────────────────────────────────
152
+
153
+ export declare const TOAST_COLORS: {
154
+ success: { accent: string; iconBg: string; iconBorder: string; lightText: string; darkText: string; lightSubtext: string; darkSubtext: string };
155
+ error: { accent: string; iconBg: string; iconBorder: string; lightText: string; darkText: string; lightSubtext: string; darkSubtext: string };
156
+ info: { accent: string; iconBg: string; iconBorder: string; lightText: string; darkText: string; lightSubtext: string; darkSubtext: string };
157
+ warning: { accent: string; iconBg: string; iconBorder: string; lightText: string; darkText: string; lightSubtext: string; darkSubtext: string };
158
+ loading: { accent: string; iconBg: string; iconBorder: string; lightText: string; darkText: string; lightSubtext: string; darkSubtext: string };
159
+ };
160
+
161
+ export declare const TOAST_THEME: {
162
+ light: { background: string; title: string; message: string; border: string; shadow: string; progressTrack: string };
163
+ dark: { background: string; title: string; message: string; border: string; shadow: string; progressTrack: string };
164
+ };
165
+
166
+ export declare const TOAST_DEFAULTS: {
167
+ duration: number;
168
+ position: string;
169
+ maxVisible: number;
170
+ iconSize: number;
171
+ animationDuration: number;
172
+ };
@@ -1,28 +1,28 @@
1
- import {Dimensions, PixelRatio} from 'react-native';
2
- let {width, height} = Dimensions.get('window');
3
- const widthPercentageToDP = widthPercent => {
4
- const elemWidth =
5
- widthPercent === 'number' ? widthPercent : parseFloat(widthPercent);
6
-
7
- return PixelRatio.roundToNearestPixel((width * elemWidth) / 100);
8
- };
9
-
10
- const heightPercentageToDP = heightPercent => {
11
- const elemHeight =
12
- heightPercent === 'number' ? heightPercent : parseFloat(heightPercent);
13
-
14
- return PixelRatio.roundToNearestPixel((height * elemHeight) / 100);
15
- };
16
-
17
- const listenOrientationChange = that => {
18
- Dimensions.addEventListener('change', newDimensions => {
19
- width = newDimensions.window.width;
20
- height = newDimensions.window.height;
21
-
22
- that.setState({
23
- orientation: width < height ? 'portrait' : 'landscape',
24
- });
25
- });
26
- };
27
-
28
- export {widthPercentageToDP, heightPercentageToDP, listenOrientationChange};
1
+ import { Dimensions, PixelRatio } from 'react-native';
2
+
3
+ let { width, height } = Dimensions.get('window');
4
+
5
+ const widthPercentageToDP = (widthPercent) => {
6
+ const elemWidth =
7
+ typeof widthPercent === 'number' ? widthPercent : parseFloat(widthPercent);
8
+ return PixelRatio.roundToNearestPixel((width * elemWidth) / 100);
9
+ };
10
+
11
+ const heightPercentageToDP = (heightPercent) => {
12
+ const elemHeight =
13
+ typeof heightPercent === 'number' ? heightPercent : parseFloat(heightPercent);
14
+ return PixelRatio.roundToNearestPixel((height * elemHeight) / 100);
15
+ };
16
+
17
+ const listenOrientationChange = (callback) => {
18
+ const subscription = Dimensions.addEventListener('change', ({ window }) => {
19
+ width = window.width;
20
+ height = window.height;
21
+ if (typeof callback === 'function') {
22
+ callback(width < height ? 'portrait' : 'landscape');
23
+ }
24
+ });
25
+ return subscription;
26
+ };
27
+
28
+ export { widthPercentageToDP, heightPercentageToDP, listenOrientationChange };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * rn-toastify — Premium Design Tokens
3
+ * Inspired by iOS system notifications and Material Design 3
4
+ */
5
+
6
+ export const TOAST_COLORS = {
7
+ success: {
8
+ accent: '#22C55E',
9
+ accentSoft: '#DCFCE7',
10
+ accentSoftDark: 'rgba(34, 197, 94, 0.15)',
11
+ iconBg: '#DCFCE7',
12
+ iconBgDark: 'rgba(34, 197, 94, 0.18)',
13
+ icon: '#16A34A',
14
+ iconDark: '#4ADE80',
15
+ },
16
+ error: {
17
+ accent: '#EF4444',
18
+ accentSoft: '#FEE2E2',
19
+ accentSoftDark: 'rgba(239, 68, 68, 0.15)',
20
+ iconBg: '#FEE2E2',
21
+ iconBgDark: 'rgba(239, 68, 68, 0.18)',
22
+ icon: '#DC2626',
23
+ iconDark: '#FCA5A5',
24
+ },
25
+ info: {
26
+ accent: '#3B82F6',
27
+ accentSoft: '#DBEAFE',
28
+ accentSoftDark: 'rgba(59, 130, 246, 0.15)',
29
+ iconBg: '#DBEAFE',
30
+ iconBgDark: 'rgba(59, 130, 246, 0.18)',
31
+ icon: '#2563EB',
32
+ iconDark: '#93C5FD',
33
+ },
34
+ warning: {
35
+ accent: '#F59E0B',
36
+ accentSoft: '#FEF3C7',
37
+ accentSoftDark: 'rgba(245, 158, 11, 0.15)',
38
+ iconBg: '#FEF3C7',
39
+ iconBgDark: 'rgba(245, 158, 11, 0.18)',
40
+ icon: '#D97706',
41
+ iconDark: '#FCD34D',
42
+ },
43
+ loading: {
44
+ accent: '#8B5CF6',
45
+ accentSoft: '#EDE9FE',
46
+ accentSoftDark: 'rgba(139, 92, 246, 0.15)',
47
+ iconBg: '#EDE9FE',
48
+ iconBgDark: 'rgba(139, 92, 246, 0.18)',
49
+ icon: '#7C3AED',
50
+ iconDark: '#C4B5FD',
51
+ },
52
+ };
53
+
54
+ export const TOAST_THEME = {
55
+ light: {
56
+ background: 'rgba(255, 255, 255, 0.96)',
57
+ surface: 'rgba(248, 250, 252, 0.9)',
58
+ title: '#0F172A',
59
+ message: '#475569',
60
+ border: 'rgba(0, 0, 0, 0.04)',
61
+ shadow: 'rgba(100, 116, 139, 0.15)',
62
+ progressTrack: 'rgba(0, 0, 0, 0.05)',
63
+ },
64
+ dark: {
65
+ background: 'rgba(15, 23, 42, 0.96)',
66
+ surface: 'rgba(30, 41, 59, 0.9)',
67
+ title: '#F1F5F9',
68
+ message: '#94A3B8',
69
+ border: 'rgba(255, 255, 255, 0.08)',
70
+ shadow: 'rgba(0, 0, 0, 0.5)',
71
+ progressTrack: 'rgba(255, 255, 255, 0.06)',
72
+ },
73
+ };
74
+
75
+ export const TOAST_DEFAULTS = {
76
+ duration: 3000,
77
+ position: 'top',
78
+ maxVisible: 3,
79
+ iconSize: 22,
80
+ animationDuration: 350,
81
+ };