UNPKG

3.53 kBTypeScriptView Raw
1/// <reference types="react" />
2import { AbstractPureComponent2, Position } from "../../common";
3import { Props } from "../../common/props";
4import { IToastProps } from "./toast";
5export declare type IToastOptions = IToastProps & {
6 key: string;
7};
8export declare type ToasterPosition = typeof Position.TOP | typeof Position.TOP_LEFT | typeof Position.TOP_RIGHT | typeof Position.BOTTOM | typeof Position.BOTTOM_LEFT | typeof Position.BOTTOM_RIGHT;
9/** Instance methods available on a `<Toaster>` component instance. */
10export interface IToaster {
11 /**
12 * Shows a new toast to the user, or updates an existing toast corresponding to the provided key (optional).
13 *
14 * Returns the unique key of the toast.
15 */
16 show(props: IToastProps, key?: string): string;
17 /** Dismiss the given toast instantly. */
18 dismiss(key: string): void;
19 /** Dismiss all toasts instantly. */
20 clear(): void;
21 /** Returns the props for all current toasts. */
22 getToasts(): IToastOptions[];
23}
24/**
25 * Props supported by the `<Toaster>` component.
26 * These props can be passed as an argument to the static `Toaster.create(props?, container?)` method.
27 */
28export interface IToasterProps extends Props {
29 /**
30 * Whether a toast should acquire application focus when it first opens.
31 * This is disabled by default so that toasts do not interrupt the user's flow.
32 * Note that `enforceFocus` is always disabled for `Toaster`s.
33 *
34 * @default false
35 */
36 autoFocus?: boolean;
37 /**
38 * Whether pressing the `esc` key should clear all active toasts.
39 *
40 * @default true
41 */
42 canEscapeKeyClear?: boolean;
43 /**
44 * Whether the toaster should be rendered into a new element attached to `document.body`.
45 * If `false`, then positioning will be relative to the parent element.
46 *
47 * This prop is ignored by `Toaster.create()` as that method always appends a new element
48 * to the container.
49 *
50 * @default true
51 */
52 usePortal?: boolean;
53 /**
54 * Position of `Toaster` within its container.
55 *
56 * @default Position.TOP
57 */
58 position?: ToasterPosition;
59 /**
60 * The maximum number of active toasts that can be displayed at once.
61 *
62 * When the limit is about to be exceeded, the oldest active toast is removed.
63 *
64 * @default undefined
65 */
66 maxToasts?: number;
67}
68export interface IToasterState {
69 toasts: IToastOptions[];
70}
71export declare class Toaster extends AbstractPureComponent2<IToasterProps, IToasterState> implements IToaster {
72 static displayName: string;
73 static defaultProps: IToasterProps;
74 /**
75 * Create a new `Toaster` instance that can be shared around your application.
76 * The `Toaster` will be rendered into a new element appended to the given container.
77 */
78 static create(props?: IToasterProps, container?: HTMLElement): IToaster;
79 state: IToasterState;
80 private toastId;
81 show(props: IToastProps, key?: string): string;
82 dismiss(key: string, timeoutExpired?: boolean): void;
83 clear(): void;
84 getToasts(): IToastOptions[];
85 render(): JSX.Element;
86 protected validateProps({ maxToasts }: IToasterProps): void;
87 private isNewToastKey;
88 private dismissIfAtLimit;
89 private renderToast;
90 private createToastOptions;
91 private getPositionClasses;
92 private getDismissHandler;
93 private handleClose;
94}
95export declare const OverlayToaster: typeof Toaster;
96export declare type OverlayToasterProps = IToasterProps;