-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathNoticeList.tsx
More file actions
188 lines (169 loc) · 5.89 KB
/
NoticeList.tsx
File metadata and controls
188 lines (169 loc) · 5.89 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { CSSProperties, FC } from 'react';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { clsx } from 'clsx';
import type { CSSMotionProps } from '@rc-component/motion';
import { CSSMotionList } from '@rc-component/motion';
import type {
InnerOpenConfig,
NoticeConfig,
OpenConfig,
Placement,
StackConfig,
} from './interface';
import Notice from './Notice';
import { NotificationContext } from './NotificationProvider';
import useStack from './hooks/useStack';
export interface NoticeListProps {
configList?: OpenConfig[];
placement?: Placement;
prefixCls?: string;
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps);
stack?: StackConfig;
// Events
onAllNoticeRemoved?: (placement: Placement) => void;
onNoticeClose?: (key: React.Key) => void;
// Common
className?: string;
style?: CSSProperties;
}
const NoticeList: FC<NoticeListProps> = (props) => {
const {
configList,
placement,
prefixCls,
className,
style,
motion,
onAllNoticeRemoved,
onNoticeClose,
stack: stackConfig,
} = props;
const { classNames: ctxCls } = useContext(NotificationContext);
const dictRef = useRef<Record<string, HTMLDivElement>>({});
const [latestNotice, setLatestNotice] = useState<HTMLDivElement>(null);
const [hoverKeys, setHoverKeys] = useState<string[]>([]);
const keys = configList.map((config) => ({
config,
key: String(config.key),
}));
const [stack, { offset, threshold, gap }] = useStack(stackConfig);
const expanded = stack && (hoverKeys.length > 0 || keys.length <= threshold);
const placementMotion = typeof motion === 'function' ? motion(placement) : motion;
// Clean hover key
useEffect(() => {
if (stack && hoverKeys.length > 1) {
setHoverKeys((prev) =>
prev.filter((key) => keys.some(({ key: dataKey }) => key === dataKey)),
);
}
}, [hoverKeys, keys, stack]);
// Force update latest notice
useEffect(() => {
if (stack && dictRef.current[keys[keys.length - 1]?.key]) {
setLatestNotice(dictRef.current[keys[keys.length - 1]?.key]);
}
}, [keys, stack]);
return (
<CSSMotionList
key={placement}
className={clsx(prefixCls, `${prefixCls}-${placement}`, ctxCls?.list, className, {
[`${prefixCls}-stack`]: !!stack,
[`${prefixCls}-stack-expanded`]: expanded,
})}
style={style}
keys={keys}
motionAppear
{...placementMotion}
onAllRemoved={() => {
onAllNoticeRemoved(placement);
}}
>
{(
{ config, className: motionClassName, style: motionStyle, index: motionIndex },
nodeRef,
) => {
const { key, times } = config as InnerOpenConfig;
const strKey = String(key);
const {
className: configClassName,
style: configStyle,
classNames: configClassNames,
styles: configStyles,
...restConfig
} = config as NoticeConfig;
const dataIndex = keys.findIndex((item) => item.key === strKey);
// If dataIndex is -1, that means this notice has been removed in data, but still in dom
// Should minus (motionIndex - 1) to get the correct index because keys.length is not the same as dom length
const stackStyle: CSSProperties = {};
if (stack) {
const index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
const transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
if (index > 0) {
stackStyle.height = expanded
? dictRef.current[strKey]?.offsetHeight
: latestNotice?.offsetHeight;
// Transform
let verticalOffset = 0;
for (let i = 0; i < index; i++) {
verticalOffset += dictRef.current[keys[keys.length - 1 - i].key]?.offsetHeight + gap;
}
const transformY =
(expanded ? verticalOffset : index * offset) * (placement.startsWith('top') ? 1 : -1);
const scaleX =
!expanded && latestNotice?.offsetWidth && dictRef.current[strKey]?.offsetWidth
? (latestNotice?.offsetWidth - offset * 2 * (index < 3 ? index : 3)) /
dictRef.current[strKey]?.offsetWidth
: 1;
stackStyle.transform = `translate3d(${transformX}, ${transformY}px, 0) scaleX(${scaleX})`;
} else {
stackStyle.transform = `translate3d(${transformX}, 0, 0)`;
}
}
return (
<div
ref={nodeRef}
className={clsx(
`${prefixCls}-notice-wrapper`,
motionClassName,
configClassNames?.wrapper,
)}
style={{
...motionStyle,
...stackStyle,
...configStyles?.wrapper,
}}
onMouseEnter={() =>
setHoverKeys((prev) => (prev.includes(strKey) ? prev : [...prev, strKey]))
}
onMouseLeave={() => setHoverKeys((prev) => prev.filter((k) => k !== strKey))}
>
<Notice
{...restConfig}
ref={(node) => {
if (dataIndex > -1) {
dictRef.current[strKey] = node;
} else {
delete dictRef.current[strKey];
}
}}
prefixCls={prefixCls}
classNames={configClassNames}
styles={configStyles}
className={clsx(configClassName, ctxCls?.notice)}
style={configStyle}
times={times}
key={key}
eventKey={key}
onNoticeClose={onNoticeClose}
hovering={stack && hoverKeys.length > 0}
/>
</div>
);
}}
</CSSMotionList>
);
};
if (process.env.NODE_ENV !== 'production') {
NoticeList.displayName = 'NoticeList';
}
export default NoticeList;