This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
/
container.tsx
77 lines (69 loc) · 2.06 KB
/
container.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
/** @jsx jsx */
import React, { PropsWithChildren } from 'react'
import { jsx } from '@emotion/core'
import PropTypes from 'prop-types'
import { Styler, childrenProps, placementsProps, stylerProps, PlacementOption } from './types'
import { isDev, getStylesCSS } from './utils'
import { scrollStyles } from './styles'
import { classNamePrefix } from './styles'
const containerClassName = `${classNamePrefix}__container`
const placementsCSS = {
'top-left': { top: 0, left: 0 },
'top-center': { top: 0, left: '50%', transform: 'translateX(-50%)' },
'top-right': { top: 0, right: 0 },
'bottom-left': { bottom: 0, left: 0 },
'bottom-center': { bottom: 0, left: '50%', transform: 'translateX(-50%)' },
'bottom-right': { bottom: 0, right: 0 },
}
interface ContainerValueProps {
hasToasts: boolean
placement: PlacementOption
}
export type ContainerStyler = Styler<ContainerValueProps> | undefined
export type ContainerProps = ContainerValueProps & {
styler: ContainerStyler
onMouseEnter?: (ev: React.MouseEvent<HTMLElement>) => void
onMouseLeave?: (ev: React.MouseEvent<HTMLElement>) => void
} & React.HTMLProps<HTMLDivElement>
export const ToastContainer: React.FC<PropsWithChildren<ContainerProps>> = ({
children,
hasToasts,
placement,
onMouseEnter,
onMouseLeave,
styler,
...divProps
}) => (
<div
className={containerClassName}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
css={[
scrollStyles,
{
overflowWrap: 'break-word',
wordBreak: 'break-word',
boxSizing: 'border-box',
maxHeight: '100%',
overflow: 'hidden',
padding: '1rem',
pointerEvents: hasToasts ? 'auto' : 'none',
position: 'fixed',
zIndex: 1000,
...placementsCSS[placement],
...getStylesCSS(styler, { hasToasts, placement }),
},
]}
{...divProps}
>
{children}
</div>
)
if (isDev) {
ToastContainer.propTypes = {
hasToasts: PropTypes.bool.isRequired,
placement: placementsProps.isRequired,
styler: stylerProps,
children: childrenProps,
}
}