/
Toast.tsx
104 lines (97 loc) · 2.75 KB
/
Toast.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { forwardRef, HTMLAttributes, ReactNode } from "react";
import cn from "classnames";
import {
CSSTransition,
CSSTransitionComponentProps,
} from "@react-md/transition";
import { bem } from "@react-md/utils";
import { DEFAULT_TOAST_CLASSNAMES, DEFAULT_TOAST_TIMEOUT } from "./constants";
export interface ToastProps
extends HTMLAttributes<HTMLDivElement>,
Omit<CSSTransitionComponentProps, "temporary"> {
/**
* Boolean if the main message content should be stacked above the action
* button. This prop is invalid if an `action` is not provided.
*/
stacked?: boolean;
/**
* Boolean if the children is a two line message. This applies some additional
* styles and unfortunately needs to be known before the toast is created.
*/
twoLines?: boolean;
/**
* An optional action button to display with the toast. This will be rendered
* to the right of the main toast's children if provided.
*/
action?: ReactNode | null;
/**
* Boolean if the toast is currently visible. This should be enabled on mount
* and then set to false once the toast has finished its display timeout.
*/
visible: boolean;
}
const block = bem("rmd-toast");
/**
* This is a very low-level component that can be used to animate a new toast in
* to a `Snackbar` as it is mainly just a wrapper of the `CSSTransition`
* component. If you are using this component, it is generally recommended to
* provide the `onEntered` callback as a function to start the hide visibility
* timer and the `onExited` callback to remove the current toast from your
* queue.
*/
export const Toast = forwardRef<HTMLDivElement, ToastProps>(function Toast(
{
className,
children,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
timeout = DEFAULT_TOAST_TIMEOUT,
classNames = DEFAULT_TOAST_CLASSNAMES,
action = null,
stacked = false,
twoLines = false,
visible,
...props
},
nodeRef
) {
return (
<CSSTransition
appear
nodeRef={nodeRef}
temporary
transitionIn={visible}
onEnter={onEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={onExited}
timeout={timeout}
classNames={classNames}
>
<div
{...props}
className={cn(
block({
stacked,
padded: !action || twoLines,
"two-lines": twoLines,
action,
"action-2": action && twoLines,
}),
className
)}
>
<span className={block("message", { action })}>{children}</span>
{action && (
<span className={block("action", { stacked })}>{action}</span>
)}
</div>
</CSSTransition>
);
});