UNPKG

7.58 kBTypeScriptView Raw
1import React from 'react';
2import { CloseButtonProps, IconProps } from './components';
3type Nullable<T> = {
4 [P in keyof T]: T[P] | null;
5};
6export type TypeOptions = 'info' | 'success' | 'warning' | 'error' | 'default';
7export type Theme = 'light' | 'dark' | 'colored' | (string & {});
8export type ToastPosition = 'top-right' | 'top-center' | 'top-left' | 'bottom-right' | 'bottom-center' | 'bottom-left';
9export interface ToastContentProps<Data = unknown> {
10 closeToast: () => void;
11 toastProps: ToastProps;
12 data: Data;
13}
14export type ToastContent<T = unknown> = React.ReactNode | ((props: ToastContentProps<T>) => React.ReactNode);
15export type ToastIcon = false | ((props: IconProps) => React.ReactNode) | React.ReactElement<IconProps>;
16export type Id = number | string;
17export type ToastTransition = React.FC<ToastTransitionProps> | React.ComponentClass<ToastTransitionProps>;
18/**
19 * ClassName for the elements - can take a function to build a classname or a raw string that is cx'ed to defaults
20 */
21export type ToastClassName = ((context?: {
22 type?: TypeOptions;
23 defaultClassName?: string;
24 position?: ToastPosition;
25 rtl?: boolean;
26}) => string) | string;
27export interface ClearWaitingQueueParams {
28 containerId?: Id;
29}
30export type DraggableDirection = 'x' | 'y';
31interface CommonOptions {
32 /**
33 * Pause the timer when the mouse hover the toast.
34 * `Default: true`
35 */
36 pauseOnHover?: boolean;
37 /**
38 * Pause the toast when the window loses focus.
39 * `Default: true`
40 */
41 pauseOnFocusLoss?: boolean;
42 /**
43 * Remove the toast when clicked.
44 * `Default: false`
45 */
46 closeOnClick?: boolean;
47 /**
48 * Set the delay in ms to close the toast automatically.
49 * Use `false` to prevent the toast from closing.
50 * `Default: 5000`
51 */
52 autoClose?: number | false;
53 /**
54 * Set the default position to use.
55 * `One of: 'top-right', 'top-center', 'top-left', 'bottom-right', 'bottom-center', 'bottom-left'`
56 * `Default: 'top-right'`
57 */
58 position?: ToastPosition;
59 /**
60 * Pass a custom close button.
61 * To remove the close button pass `false`
62 */
63 closeButton?: boolean | ((props: CloseButtonProps) => React.ReactNode) | React.ReactElement<CloseButtonProps>;
64 /**
65 * An optional css class to set for the progress bar.
66 */
67 progressClassName?: ToastClassName;
68 /**
69 * An optional style to set for the progress bar.
70 */
71 progressStyle?: React.CSSProperties;
72 /**
73 * An optional css class to set for the toast content.
74 */
75 bodyClassName?: ToastClassName;
76 /**
77 * An optional inline style to apply for the toast content.
78 */
79 bodyStyle?: React.CSSProperties;
80 /**
81 * Hide or show the progress bar.
82 * `Default: false`
83 */
84 hideProgressBar?: boolean;
85 /**
86 * Pass a custom transition see https://fkhadra.github.io/react-toastify/custom-animation/
87 */
88 transition?: ToastTransition;
89 /**
90 * Allow toast to be draggable
91 * `Default: 'touch'`
92 */
93 draggable?: boolean | 'mouse' | 'touch';
94 /**
95 * The percentage of the toast's width it takes for a drag to dismiss a toast
96 * `Default: 80`
97 */
98 draggablePercent?: number;
99 /**
100 * Specify in which direction should you swipe to dismiss the toast
101 * `Default: "x"`
102 */
103 draggableDirection?: DraggableDirection;
104 /**
105 * Define the ARIA role for the toast
106 * `Default: alert`
107 * https://www.w3.org/WAI/PF/aria/roles
108 */
109 role?: string;
110 /**
111 * Set id to handle multiple container
112 */
113 containerId?: Id;
114 /**
115 * Fired when clicking inside toaster
116 */
117 onClick?: (event: React.MouseEvent) => void;
118 /**
119 * Support right to left display.
120 * `Default: false`
121 */
122 rtl?: boolean;
123 /**
124 * Used to display a custom icon. Set it to `false` to prevent
125 * the icons from being displayed
126 */
127 icon?: ToastIcon;
128 /**
129 * Theme to use.
130 * `One of: 'light', 'dark', 'colored'`
131 * `Default: 'light'`
132 */
133 theme?: Theme;
134}
135export interface ToastOptions<Data = unknown> extends CommonOptions {
136 /**
137 * An optional css class to set.
138 */
139 className?: ToastClassName;
140 /**
141 * Called when toast is mounted.
142 */
143 onOpen?: <T = {}>(props: T) => void;
144 /**
145 * Called when toast is unmounted.
146 */
147 onClose?: <T = {}>(props: T) => void;
148 /**
149 * An optional inline style to apply.
150 */
151 style?: React.CSSProperties;
152 /**
153 * Set the toast type.
154 * `One of: 'info', 'success', 'warning', 'error', 'default'`
155 */
156 type?: TypeOptions;
157 /**
158 * Set a custom `toastId`
159 */
160 toastId?: Id;
161 /**
162 * Used during update
163 */
164 updateId?: Id;
165 /**
166 * Set the percentage for the controlled progress bar. `Value must be between 0 and 1.`
167 */
168 progress?: number | string;
169 /**
170 * Add a delay in ms before the toast appear.
171 */
172 delay?: number;
173 isLoading?: boolean;
174 data?: Data;
175}
176export interface UpdateOptions<T = unknown> extends Nullable<ToastOptions<T>> {
177 /**
178 * Used to update a toast.
179 * Pass any valid ReactNode(string, number, component)
180 */
181 render?: ToastContent<T>;
182}
183export interface ToastContainerProps extends CommonOptions {
184 /**
185 * An optional css class to set.
186 */
187 className?: ToastClassName;
188 /**
189 * Will stack the toast with the newest on the top.
190 */
191 stacked?: boolean;
192 /**
193 * Whether or not to display the newest toast on top.
194 * `Default: false`
195 */
196 newestOnTop?: boolean;
197 /**
198 * An optional inline style to apply.
199 */
200 style?: React.CSSProperties;
201 /**
202 * An optional inline style to apply for the toast.
203 */
204 toastStyle?: React.CSSProperties;
205 /**
206 * An optional css class for the toast.
207 */
208 toastClassName?: ToastClassName;
209 /**
210 * Limit the number of toast displayed at the same time
211 */
212 limit?: number;
213}
214export interface ToastTransitionProps {
215 isIn: boolean;
216 done: () => void;
217 position: ToastPosition | string;
218 preventExitTransition: boolean;
219 nodeRef: React.RefObject<HTMLElement>;
220 children?: React.ReactNode;
221 playToast(): void;
222}
223/**
224 * @INTERNAL
225 */
226export interface ToastProps extends ToastOptions {
227 isIn: boolean;
228 staleId?: Id;
229 toastId: Id;
230 key: Id;
231 transition: ToastTransition;
232 closeToast: () => void;
233 position: ToastPosition;
234 children?: ToastContent;
235 draggablePercent: number;
236 draggableDirection?: DraggableDirection;
237 progressClassName?: ToastClassName;
238 className?: ToastClassName;
239 bodyClassName?: ToastClassName;
240 deleteToast: () => void;
241 theme: Theme;
242 type: TypeOptions;
243 collapseAll: () => void;
244 stacked?: boolean;
245}
246/**
247 * @INTERNAL
248 */
249export interface NotValidatedToastProps extends Partial<ToastProps> {
250 toastId: Id;
251}
252/**
253 * @INTERNAL
254 */
255export interface Toast {
256 content: ToastContent;
257 props: ToastProps;
258 toggle?: (v: boolean) => void;
259}
260export type ToastItemStatus = 'added' | 'removed' | 'updated';
261export interface ToastItem<Data = {}> {
262 content: ToastContent<Data>;
263 id: Id;
264 theme?: Theme;
265 type?: TypeOptions;
266 isLoading?: boolean;
267 containerId?: Id;
268 data: Data;
269 icon?: ToastIcon;
270 status: ToastItemStatus;
271}
272export type OnChangeCallback = (toast: ToastItem) => void;
273export type IdOpts = {
274 id?: Id;
275 containerId?: Id;
276};
277export {};