UNPKG

3.14 kBTypeScriptView Raw
1import * as React from 'react';
2import * as PropTypes from 'prop-types';
3// @ts-ignore
4import _omit from 'lodash/omit';
5
6import { Box } from '../primitives';
7import { ButtonProps, buttonPropTypes } from '../Button/Button';
8import { LocalPaneProps, PaneProps, panePropTypes, paneDefaultProps } from '../Pane/Pane';
9import { Omit } from '../types';
10
11import ToastClose, { ToastCloseProps } from './ToastClose';
12import ToastContainer, { ToastContainerProps } from './ToastContainer';
13import ToastIcon from './ToastIcon';
14import ToastTitle from './ToastTitle';
15import _Toast, { CountdownBar, Content } from './styled';
16
17export type LocalToastProps = LocalPaneProps & {
18 autoDismissTimeout?: number;
19 children?: React.ReactNode;
20 closeButtonProps?: Omit<ButtonProps, 'children'>;
21 hasHorizontalBar?: boolean;
22 hasIcon?: boolean;
23 hasTint?: boolean;
24 hideCloseButton?: boolean;
25 onClickClose?: ToastCloseProps['onClickClose'];
26 showCountdownBar?: boolean;
27 title?: string;
28 type?: string;
29};
30export type ToastProps = PaneProps & LocalToastProps;
31export type ToastComponents = {
32 Container: React.FunctionComponent<ToastContainerProps>;
33};
34
35export const Toast: React.FunctionComponent<LocalToastProps> & ToastComponents = ({
36 autoDismissTimeout,
37 children,
38 closeButtonProps,
39 hasHorizontalBar,
40 hasIcon,
41 hideCloseButton,
42 onClickClose,
43 showCountdownBar,
44 title,
45 type,
46 ...props
47}) => (
48 <_Toast type={type} {...props}>
49 {showCountdownBar && (
50 <React.Fragment>
51 <CountdownBar isHorizontal={hasHorizontalBar} type={type} isBackground />
52 <CountdownBar isHorizontal={hasHorizontalBar} type={type} autoDismissTimeout={autoDismissTimeout} />
53 </React.Fragment>
54 )}
55 <Content showCountdownBar={showCountdownBar}>
56 {hasIcon && type && <ToastIcon type={type} size={children ? '300' : undefined} />}
57 <Box>
58 {title && <ToastTitle marginBottom={children ? 'minor-2' : undefined}>{title}</ToastTitle>}
59 {children}
60 </Box>
61 </Content>
62 {!hideCloseButton && (
63 <ToastClose isAbsolute={Boolean(children)} onClickClose={onClickClose} {...closeButtonProps} />
64 )}
65 </_Toast>
66);
67
68Toast.Container = ToastContainer;
69
70export const toastPropTypes = {
71 autoDismissTimeout: PropTypes.number,
72 children: PropTypes.node,
73 closeButtonProps: PropTypes.shape(_omit(buttonPropTypes, 'children')),
74 hasHorizontalBar: PropTypes.bool,
75 hasIcon: PropTypes.bool,
76 hasTint: PropTypes.bool,
77 hideCloseButton: PropTypes.bool,
78 onClickClose: PropTypes.func,
79 showCountdownBar: PropTypes.bool,
80 type: PropTypes.string,
81 title: PropTypes.string,
82 ...panePropTypes
83};
84Toast.propTypes = toastPropTypes;
85
86export const toastDefaultProps = {
87 ...paneDefaultProps,
88 autoDismissTimeout: undefined,
89 children: undefined,
90 closeButtonProps: {},
91 elevation: '300',
92 hasHorizontalBar: false,
93 hasIcon: true,
94 hasTint: false,
95 hideCloseButton: false,
96 onClickClose: undefined,
97 showCountdownBar: true,
98 title: undefined,
99 type: 'info'
100};
101Toast.defaultProps = toastDefaultProps;
102
103const C: React.FunctionComponent<ToastProps> & ToastComponents = Toast;
104export default C;