/
ToastContainer.tsx
100 lines (90 loc) · 2.39 KB
/
ToastContainer.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
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
export type ToastPosition =
| 'top-start'
| 'top-center'
| 'top-end'
| 'middle-start'
| 'middle-center'
| 'middle-end'
| 'bottom-start'
| 'bottom-center'
| 'bottom-end';
export interface ToastContainerProps
extends BsPrefixProps,
React.HTMLAttributes<HTMLElement> {
position?: ToastPosition;
containerPosition?: string;
}
const propTypes = {
/**
* @default 'toast-container'
*/
bsPrefix: PropTypes.string,
/**
* Where the toasts will be placed within the container.
*/
position: PropTypes.oneOf<ToastPosition>([
'top-start',
'top-center',
'top-end',
'middle-start',
'middle-center',
'middle-end',
'bottom-start',
'bottom-center',
'bottom-end',
]),
/**
* Specify the positioning method for the container.
*/
containerPosition: PropTypes.string,
};
const positionClasses = {
'top-start': 'top-0 start-0',
'top-center': 'top-0 start-50 translate-middle-x',
'top-end': 'top-0 end-0',
'middle-start': 'top-50 start-0 translate-middle-y',
'middle-center': 'top-50 start-50 translate-middle',
'middle-end': 'top-50 end-0 translate-middle-y',
'bottom-start': 'bottom-0 start-0',
'bottom-center': 'bottom-0 start-50 translate-middle-x',
'bottom-end': 'bottom-0 end-0',
};
const ToastContainer: BsPrefixRefForwardingComponent<
'div',
ToastContainerProps
> = React.forwardRef<HTMLDivElement, ToastContainerProps>(
(
{
bsPrefix,
position,
containerPosition,
className,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
...props
},
ref,
) => {
bsPrefix = useBootstrapPrefix(bsPrefix, 'toast-container');
return (
<Component
ref={ref}
{...props}
className={classNames(
bsPrefix,
position && positionClasses[position],
containerPosition && `position-${containerPosition}`,
className,
)}
/>
);
},
);
ToastContainer.displayName = 'ToastContainer';
ToastContainer.propTypes = propTypes;
export default ToastContainer;