-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.tsx
executable file
·151 lines (138 loc) · 4.08 KB
/
portal.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useEffect, useState, RefObject, useRef } from 'react'
import PropTypes from 'prop-types'
import { STYLES } from '../styles'
import { NotifyOpts, NotifyStyle, NotifyContainersStyle, NotifyPosition } from '../types'
import { CONSTANTS } from '../constants'
import { NotifyContainer } from './container'
import { useNotify } from '../hooks/useNotify'
export interface NotifyPortalProps {
className?: string
noAnimation?: boolean
allowHTML?: boolean
children?: React.ReactNode | string
style?: NotifyStyle | false
}
export interface NotifyPortalState {
notifications: NotifyOpts[]
}
export function NotifyPortal(props: NotifyPortalProps) {
const {
className,
style = {},
noAnimation = false,
allowHTML = false,
children,
...passThrough
} = props
const [isMounted, setIsMounted] = useState(false)
const [overrideStyle, setOverrideStyle] = useState<NotifyStyle | null>(null)
const [overrideWidth, setOverrideWidth] = useState<NotifyStyle | null>(null)
useEffect(function onInit() {
if (style !== false) {
setOverrideStyle(style)
}
setIsMounted(true)
return function onDestroy() {
setIsMounted(false)
}
}, [])
const { state, api } = useNotify()
const elements: { [key: string]: string } = {
notification: 'NotificationItem',
title: 'Title',
messageWrapper: 'MessageWrapper',
dismiss: 'Dismiss',
action: 'Action',
actionWrapper: 'ActionWrapper'
}
const wrapper = () => {
if (!overrideStyle) {
return {}
}
return { ...STYLES.Wrapper, ...overrideStyle.Wrapper }
}
const container = (position: NotifyPosition) => {
if (!overrideStyle) {
return {}
}
const override: Partial<NotifyContainersStyle> = overrideStyle.Containers || {}
let newOverrideWidth = STYLES.Containers.DefaultStyle.width
if (override.DefaultStyle && override.DefaultStyle.width) {
newOverrideWidth = override.DefaultStyle.width
}
const overridePos = override[position]
if (overridePos && overridePos.width) {
newOverrideWidth = overridePos.width
}
if (newOverrideWidth !== setOverrideWidth) {
setOverrideWidth(newOverrideWidth)
}
return {
...STYLES.Containers.DefaultStyle,
...STYLES.Containers[position],
...override.DefaultStyle,
...override[position]
}
}
const byElement = (element: any) => {
return (level: string) => {
const _element = elements[element]
const override = overrideStyle ? (overrideStyle as any)[_element] || {} : {}
if (!overrideStyle) {
return {}
}
return {
...STYLES[_element].DefaultStyle,
...STYLES[_element][level],
...override.DefaultStyle,
...override[level]
}
}
}
const _getStyles: any = {
overrideWidth: overrideWidth,
overrideStyle: overrideStyle,
elements: elements,
// setOverrideStyle: setOverrideStyle,
wrapper: wrapper,
container: container,
byElement: byElement
}
let containers = null
const notifications = state.notifications
if (notifications.length) {
// Loop over each position (e.g. "tr" for top-right, "bl" for bottom-left)
containers = Object.keys(CONSTANTS.positions).map(position => {
// Filter just the notifications with that position
const _notifications = notifications.filter(notification => {
return position === notification.position
})
// If there are none, skip it.
if (!_notifications.length) {
return null
}
// emit a container
return (
<NotifyContainer
notify={api}
key={position}
position={position as NotifyPosition}
notifications={_notifications}
getStyles={_getStyles}
noAnimation={noAnimation}
allowHTML={allowHTML}
/>
)
})
}
return (
<div {...passThrough} data-testid={className} style={wrapper()}>
{containers}
</div>
)
}
NotifyPortal.propTypes = {
style: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
noAnimation: PropTypes.bool,
allowHTML: PropTypes.bool
}