-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathNotice.tsx
More file actions
168 lines (147 loc) · 4.6 KB
/
Notice.tsx
File metadata and controls
168 lines (147 loc) · 4.6 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
import { clsx } from 'clsx';
import KeyCode from '@rc-component/util/lib/KeyCode';
import * as React from 'react';
import type { NoticeConfig } from './interface';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
export interface NoticeProps extends Omit<NoticeConfig, 'onClose'> {
prefixCls: string;
className?: string;
style?: React.CSSProperties;
eventKey: React.Key;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onNoticeClose?: (key: React.Key) => void;
hovering?: boolean;
}
const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }>((props, ref) => {
const {
prefixCls,
style,
className,
duration = 4.5,
showProgress,
pauseOnHover = true,
eventKey,
content,
closable,
props: divProps,
onClick,
onNoticeClose,
times,
hovering: forcedHovering,
classNames,
styles,
} = props;
const [hovering, setHovering] = React.useState(false);
const [percent, setPercent] = React.useState(0);
const [spentTime, setSpentTime] = React.useState(0);
const mergedHovering = forcedHovering || hovering;
const mergedShowProgress = duration > 0 && showProgress;
// ======================== Close =========================
const onInternalClose = () => {
onNoticeClose(eventKey);
};
const onCloseKeyDown: React.KeyboardEventHandler<HTMLButtonElement> = (e) => {
if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
onInternalClose();
}
};
// ======================== Effect ========================
React.useEffect(() => {
if (!mergedHovering && duration > 0) {
const start = Date.now() - spentTime;
const timeout = setTimeout(
() => {
onInternalClose();
},
duration * 1000 - spentTime,
);
return () => {
if (pauseOnHover) {
clearTimeout(timeout);
}
setSpentTime(Date.now() - start);
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, mergedHovering, times]);
React.useEffect(() => {
if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
const start = performance.now();
let animationFrame: number;
const calculate = () => {
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame((timestamp) => {
const runtime = timestamp + spentTime - start;
const progress = Math.min(runtime / (duration * 1000), 1);
setPercent(progress * 100);
if (progress < 1) {
calculate();
}
});
};
calculate();
return () => {
if (pauseOnHover) {
cancelAnimationFrame(animationFrame);
}
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
// ======================== Closable ========================
const closableObj = React.useMemo(() => {
if (typeof closable === 'object' && closable !== null) {
return closable;
}
return {};
}, [closable]);
const ariaProps = pickAttrs(closableObj, true);
// ======================== Progress ========================
const validPercent = 100 - (!percent || percent < 0 ? 0 : percent > 100 ? 100 : percent);
// ======================== Render ========================
const noticePrefixCls = `${prefixCls}-notice`;
return (
<div
{...divProps}
ref={ref}
className={clsx(noticePrefixCls, className, { [`${noticePrefixCls}-closable`]: closable })}
style={style}
onMouseEnter={(e) => {
setHovering(true);
divProps?.onMouseEnter?.(e);
}}
onMouseLeave={(e) => {
setHovering(false);
divProps?.onMouseLeave?.(e);
}}
onClick={onClick}
>
{/* Content */}
<div className={`${noticePrefixCls}-content`}>{content}</div>
{/* Close Icon */}
{closable && (
<button
className={clsx(`${noticePrefixCls}-close`, classNames?.close)}
style={styles?.close}
onKeyDown={onCloseKeyDown}
aria-label="Close"
{...ariaProps}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onInternalClose();
}}
>
{closableObj.closeIcon ?? 'x'}
</button>
)}
{/* Progress Bar */}
{mergedShowProgress && (
<progress className={`${noticePrefixCls}-progress`} max="100" value={validPercent}>
{validPercent + '%'}
</progress>
)}
</div>
);
});
export default Notify;