1 | import * as React from 'react';
|
2 |
|
3 | export type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | type OmitNever<T> = Pick<
|
14 | T,
|
15 | {
|
16 | [Prop in keyof T]: [T[Prop]] extends [never] ? never : Prop;
|
17 | }[keyof T]
|
18 | >;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | type Override<T, U> = Omit<T, keyof U> & U;
|
31 |
|
32 | type MarkInvalidVariantAsNever<T> = {
|
33 | [Key in keyof T]: T[Key] extends true ? T[Key] : T[Key] extends Record<string, unknown> ? T[Key] : never;
|
34 | };
|
35 |
|
36 | type GetWhitelistedVariants<V extends string, U> = OmitNever<MarkInvalidVariantAsNever<Override<Record<V, true>, U>>>;
|
37 |
|
38 | export interface TransitionDuration {
|
39 | enter?: number;
|
40 | exit?: number;
|
41 | }
|
42 |
|
43 | export type TransitionStatus = 'entering' | 'entered' | 'exiting' | 'exited' | 'unmounted';
|
44 |
|
45 | export interface TransitionComponentProps extends Omit<TransitionProps, 'children'> {
|
46 | children: (status: TransitionStatus, childProps: Record<string, any>) => React.ReactNode;
|
47 | nodeRef: React.RefObject<HTMLDivElement>;
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export interface TransitionHandlerProps {
|
54 | |
55 |
|
56 |
|
57 | onEnter: (node: HTMLElement, isAppearing: boolean, key: SnackbarKey) => void;
|
58 | |
59 |
|
60 |
|
61 | onEntered: (node: HTMLElement, isAppearing: boolean, key: SnackbarKey) => void;
|
62 | |
63 |
|
64 |
|
65 | onExit: (node: HTMLElement, key: SnackbarKey) => void;
|
66 | |
67 |
|
68 |
|
69 | onExited: (node: HTMLElement, key: SnackbarKey) => void;
|
70 | }
|
71 |
|
72 | export type SlideTransitionDirection = 'down' | 'left' | 'right' | 'up';
|
73 |
|
74 | export interface TransitionProps {
|
75 | appear?: boolean;
|
76 | |
77 |
|
78 |
|
79 | in?: boolean;
|
80 | |
81 |
|
82 |
|
83 | timeout?: number | TransitionDuration;
|
84 | |
85 |
|
86 |
|
87 | enter?: boolean;
|
88 | |
89 |
|
90 |
|
91 | exit?: boolean;
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | mountOnEnter?: boolean;
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | unmountOnExit?: boolean;
|
106 | |
107 |
|
108 |
|
109 |
|
110 | style?: React.CSSProperties;
|
111 | |
112 |
|
113 |
|
114 |
|
115 | direction?: SlideTransitionDirection;
|
116 | children: React.ReactNode;
|
117 | |
118 |
|
119 |
|
120 | onEnter?: (node: HTMLElement, isAppearing: boolean) => void;
|
121 | |
122 |
|
123 |
|
124 | onEntered?: (node: HTMLElement, isAppearing: boolean) => void;
|
125 | |
126 |
|
127 |
|
128 | onEntering?: (node: HTMLElement, isAppearing: boolean) => void;
|
129 | |
130 |
|
131 |
|
132 | onExit?: (node: HTMLElement) => void;
|
133 | |
134 |
|
135 |
|
136 | onExited?: (node: HTMLElement) => void;
|
137 | |
138 |
|
139 |
|
140 | onExiting?: (node: HTMLElement) => void;
|
141 | addEndListener?: (node: HTMLElement | HTMLDivElement, callback: () => void) => void;
|
142 | }
|
143 |
|
144 | export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;
|
145 |
|
146 |
|
147 | interface VariantOverrides {}
|
148 |
|
149 | type VariantMap = GetWhitelistedVariants<BaseVariant, VariantOverrides>;
|
150 |
|
151 | type BaseVariant = 'default' | 'error' | 'success' | 'warning' | 'info';
|
152 |
|
153 | export type VariantType = keyof VariantMap;
|
154 |
|
155 | export type SnackbarKey = string | number;
|
156 | export type CloseReason = 'timeout' | 'maxsnack' | 'instructed';
|
157 |
|
158 | export type SnackbarMessage = string | React.ReactNode;
|
159 | export type SnackbarAction = React.ReactNode | ((key: SnackbarKey) => React.ReactNode);
|
160 | export type SnackbarContentCallback =
|
161 | | React.ReactNode
|
162 | | ((key: SnackbarKey, message?: SnackbarMessage) => React.ReactNode);
|
163 |
|
164 | export type SnackbarClassKey =
|
165 | | 'root'
|
166 | | 'anchorOriginTopCenter'
|
167 | | 'anchorOriginBottomCenter'
|
168 | | 'anchorOriginTopRight'
|
169 | | 'anchorOriginBottomRight'
|
170 | | 'anchorOriginTopLeft'
|
171 | | 'anchorOriginBottomLeft';
|
172 |
|
173 | export type ContainerClassKey =
|
174 | | 'containerRoot'
|
175 | | 'containerAnchorOriginTopCenter'
|
176 | | 'containerAnchorOriginBottomCenter'
|
177 | | 'containerAnchorOriginTopRight'
|
178 | | 'containerAnchorOriginBottomRight'
|
179 | | 'containerAnchorOriginTopLeft'
|
180 | | 'containerAnchorOriginBottomLeft';
|
181 |
|
182 | export type CombinedClassKey = ContainerClassKey | SnackbarClassKey;
|
183 |
|
184 | export interface SnackbarOrigin {
|
185 | vertical: 'top' | 'bottom';
|
186 | horizontal: 'left' | 'center' | 'right';
|
187 | }
|
188 |
|
189 | export type SnackbarContentProps = React.HTMLAttributes<HTMLDivElement>;
|
190 |
|
191 | /**
|
192 | * @category Shared
|
193 | */
|
194 | export interface SharedProps<V extends VariantType = VariantType> extends Partial<TransitionHandlerProps> {
|
195 | className?: string;
|
196 | style?: React.CSSProperties;
|
197 | /**
|
198 | * The anchor of the `Snackbar`.
|
199 | * @default { horizontal: left, vertical: bottom }
|
200 | */
|
201 | anchorOrigin?: SnackbarOrigin;
|
202 | /**
|
203 | * The number of milliseconds to wait before automatically calling the
|
204 | * `onClose` function. By default snackbars get closed after 5000 milliseconds.
|
205 | * Set autoHideDuration to 'null' if you don't want snackbars to automatically close.
|
206 | * Alternatively pass `persist: true` in the options parameter of enqueueSnackbar.
|
207 | * @default 5000
|
208 | */
|
209 | autoHideDuration?: number | null;
|
210 | /**
|
211 | * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
|
212 | * @default false
|
213 | */
|
214 | disableWindowBlurListener?: boolean;
|
215 | /**
|
216 | * The component used for the transition. See how you can use a different transition:
|
217 | * https://notistack.com/examples/advanced/custom-transition
|
218 | * @default Slide
|
219 | */
|
220 | TransitionComponent?: React.JSXElementConstructor<TransitionProps & { children: React.ReactElement<any, any> }>;
|
221 | /**
|
222 | * The duration for the transition, in milliseconds.
|
223 | *
|
224 | * You may specify a single timeout for both enter and exit transitions:
|
225 | * ```js
|
226 | * timeout={500}
|
227 | * ```
|
228 | * or individually:
|
229 | * ```js
|
230 | * timeout={{ enter: 300, exit: 500 }}
|
231 | * ```
|
232 | * @default { enter: 225, exit: 195 }
|
233 | */
|
234 | transitionDuration?: TransitionProps['timeout'];
|
235 | /**
|
236 | * Properties applied to Transition component
|
237 | */
|
238 | TransitionProps?: Partial<TransitionProps>;
|
239 | /**
|
240 | * Used to easily display different variant of snackbars. When passed to `SnackbarProvider`
|
241 | * all snackbars inherit the `variant`, unless you override it in `enqueueSnackbar` options.
|
242 | * @default default
|
243 | */
|
244 | variant?: V;
|
245 | /**
|
246 | * Ignores displaying multiple snackbars with the same `message`
|
247 | * @default false
|
248 | */
|
249 | preventDuplicate?: boolean;
|
250 | /**
|
251 | * Callback used for getting action(s). actions are mostly buttons displayed in Snackbar.
|
252 | * @param {string|number} key key of a snackbar
|
253 | */
|
254 | action?: SnackbarAction;
|
255 | /**
|
256 | * Hides iconVariant if set to `true`.
|
257 | * @default false
|
258 | */
|
259 | hideIconVariant?: boolean;
|
260 | /**
|
261 | * Properties applied to the Snackbar root element. You'd only want to use
|
262 | * this prop to apply html attributes for accessibility or data-* attributes.
|
263 | */
|
264 | SnackbarProps?: React.HTMLAttributes<HTMLDivElement>;
|
265 | /**
|
266 | * Replace the snackbar. Callback used for displaying entirely customized snackbars.
|
267 | * @param {string|number} key key of a snackbar
|
268 | *
|
269 | * @ignore
|
270 | * @deprecated - Will be removed in future releases. You should use `Components` prop of
|
271 | * `SnackbarProvider` to display a custom snackbar. This is to have more control over
|
272 | * custom snackbars.
|
273 | */
|
274 | content?: SnackbarContentCallback;
|
275 | /**
|
276 | * Callback fired before snackbar requests to get closed.
|
277 | * The `reason` parameter can optionally be used to control the response to `onClose`.
|
278 | *
|
279 | * @param {object} event The event source of the callback
|
280 | * @param {string} reason Can be:`"timeout"` (`autoHideDuration` expired) or: `"maxsnack"`
|
281 | * (snackbar was closed because `maxSnack` has reached) or: `"instructed"` (snackbar was
|
282 | * closed programmatically)
|
283 | * @param {string|number|undefined} key key of a Snackbar. key will be `undefined` if closeSnackbar
|
284 | * is called with no key (user requested all the snackbars to be closed)
|
285 | */
|
286 | onClose?: (event: React.SyntheticEvent<any> | null, reason: CloseReason, key?: SnackbarKey) => void;
|
287 | }
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | export interface OptionsObject<V extends VariantType = VariantType> extends SharedProps<V> {
|
293 | |
294 |
|
295 |
|
296 |
|
297 | key?: SnackbarKey;
|
298 | |
299 |
|
300 |
|
301 |
|
302 | persist?: boolean;
|
303 | }
|
304 |
|
305 |
|
306 | interface InternalSnackAttributes {
|
307 | open: boolean;
|
308 | entered: boolean;
|
309 | requestClose: boolean;
|
310 | }
|
311 |
|
312 | type NeededByInternalSnack =
|
313 | | 'style'
|
314 | | 'persist'
|
315 | | 'variant'
|
316 | | 'anchorOrigin'
|
317 | | 'TransitionComponent'
|
318 | | 'TransitionProps'
|
319 | | 'transitionDuration'
|
320 | | 'hideIconVariant'
|
321 | | 'disableWindowBlurListener';
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | export interface InternalSnack
|
328 | extends RequiredBy<Omit<OptionsObject, 'key' | 'preventDuplicate'>, NeededByInternalSnack>,
|
329 | InternalSnackAttributes {
|
330 | id: SnackbarKey;
|
331 | message?: SnackbarMessage;
|
332 | iconVariant: Record<string, React.ReactNode>;
|
333 | }
|
334 |
|
335 | type NotNeededByCustomSnackbar =
|
336 | | keyof InternalSnackAttributes
|
337 | | keyof TransitionHandlerProps
|
338 | | 'onClose'
|
339 | | 'SnackbarProps'
|
340 | | 'disableWindowBlurListener'
|
341 | | 'TransitionComponent'
|
342 | | 'transitionDuration'
|
343 | | 'TransitionProps'
|
344 | | 'dense'
|
345 | | 'content';
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | export type CustomContentProps = Omit<InternalSnack, NotNeededByCustomSnackbar>;
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | export interface SnackbarProviderProps extends SharedProps {
|
356 | |
357 |
|
358 |
|
359 |
|
360 | children?: React.ReactNode | React.ReactNode[];
|
361 | |
362 |
|
363 |
|
364 |
|
365 | dense?: boolean;
|
366 | |
367 |
|
368 |
|
369 |
|
370 | maxSnack?: number;
|
371 | |
372 |
|
373 |
|
374 |
|
375 | domRoot?: HTMLElement;
|
376 | |
377 |
|
378 |
|
379 | classes?: Partial<ClassNameMap<CombinedClassKey>>;
|
380 | |
381 |
|
382 |
|
383 | iconVariant?: Partial<Record<VariantType, React.ReactNode>>;
|
384 | |
385 |
|
386 |
|
387 |
|
388 | ref?: React.Ref<SnackbarProvider>;
|
389 | |
390 |
|
391 |
|
392 | Components?: {
|
393 | [variant in VariantType]?: React.JSXElementConstructor<any>;
|
394 | };
|
395 | }
|
396 |
|
397 | type OptionsWithExtraProps<V extends VariantType> = VariantMap[V] extends true
|
398 | ? OptionsObject<V>
|
399 | : OptionsObject<V> & VariantMap[V];
|
400 |
|
401 | interface EnqueueSnackbar {
|
402 | <V extends VariantType>(options: OptionsWithExtraProps<V> & { message?: SnackbarMessage }): SnackbarKey;
|
403 | <V extends VariantType>(message: SnackbarMessage, options?: OptionsWithExtraProps<V>): SnackbarKey;
|
404 | }
|
405 |
|
406 | export interface ProviderContext {
|
407 | enqueueSnackbar: EnqueueSnackbar;
|
408 | closeSnackbar: (key?: SnackbarKey) => void;
|
409 | }
|
410 |
|
411 | export declare class SnackbarProvider extends React.Component<SnackbarProviderProps> {
|
412 | enqueueSnackbar: ProviderContext['enqueueSnackbar'];
|
413 | closeSnackbar: ProviderContext['closeSnackbar'];
|
414 | render(): React.ReactNode;
|
415 | }
|
416 |
|
417 | export declare function useSnackbar(): ProviderContext;
|
418 |
|
419 | export declare const enqueueSnackbar: ProviderContext['enqueueSnackbar'];
|
420 | export declare const closeSnackbar: ProviderContext['closeSnackbar'];
|
421 |
|
422 | export declare const SnackbarContent: (
|
423 | props: SnackbarContentProps & React.RefAttributes<HTMLDivElement>
|
424 | ) => React.ReactElement<any, any>;
|
425 |
|
426 | export declare const Transition: React.JSXElementConstructor<TransitionComponentProps>;
|
427 |
|
428 | export declare const MaterialDesignContent: (
|
429 | props: CustomContentProps & React.RefAttributes<HTMLDivElement>
|
430 | ) => React.ReactElement<any, any>;
|