UNPKG

1.73 kBTypeScriptView Raw
1import * as React from 'react';
2import * as PropTypes from 'prop-types';
3// @ts-ignore
4import _get from 'lodash/get';
5
6import { withTheme } from '../styled';
7import { ThemeConfig } from '../types';
8import Portal from '../Portal';
9
10import { Toasts } from './styled';
11import ToastContainer from './ToastContainer';
12import Toast from './Toast';
13
14export type ToastManagerProps = {
15 theme?: {
16 fannypack: ThemeConfig;
17 };
18};
19
20export const ToastManager: React.FunctionComponent<ToastManagerProps> = ({ theme }) => {
21 const ToastComponent = _get(theme, 'fannypack.Toast.component', Toast);
22 const defaultProps = _get(theme, 'fannypack.Toast.defaultProps', {});
23 const placement = _get(theme, 'fannypack.Toast.placement', 'top-end');
24 const isStacked = _get(theme, 'fannypack.Toast.isStacked', true);
25 return (
26 <Portal>
27 <Toasts placement={placement}>
28 <ToastContainer placement={placement}>
29 {state =>
30 [...(isStacked ? state.toasts : [state.toasts[0]])].filter(Boolean).map(toast => (
31 <ToastComponent
32 key={toast.key}
33 onClickClose={() => state.remove({ key: toast.key })}
34 {...defaultProps}
35 {...toast}
36 >
37 {toast.message}
38 </ToastComponent>
39 ))
40 }
41 </ToastContainer>
42 </Toasts>
43 </Portal>
44 );
45};
46
47export const toastManagerPropTypes = {
48 theme: PropTypes.object as PropTypes.Validator<ToastManagerProps['theme']>
49};
50ToastManager.propTypes = toastManagerPropTypes;
51
52export const toastManagerDefaultProps = {
53 theme: {
54 fannypack: {}
55 }
56};
57ToastManager.defaultProps = toastManagerDefaultProps;
58
59export default withTheme(ToastManager);