-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathNotice.tsx
More file actions
183 lines (159 loc) · 5.21 KB
/
Notice.tsx
File metadata and controls
183 lines (159 loc) · 5.21 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
import { clsx } from 'clsx';
import KeyCode from '@rc-component/util/lib/KeyCode';
import warning from '@rc-component/util/lib/warning';
import * as React from 'react';
import type { NoticeConfig } from './interface';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
/**
* Maximum delay value for setTimeout in seconds (2^31 - 1 ms).
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value
*/
const MAX_DURATION = 2147483647 / 1000;
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,
} = props;
const [hovering, setHovering] = React.useState(false);
const [percent, setPercent] = React.useState(0);
const [spentTime, setSpentTime] = React.useState(0);
const mergedHovering = forcedHovering || hovering;
const rawDuration: number = typeof duration === 'number' ? duration : 0;
const mergedDuration: number = Math.min(rawDuration, MAX_DURATION);
const mergedShowProgress = mergedDuration > 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();
}
};
// ========================= Warn =========================
React.useEffect(() => {
warning(
rawDuration <= MAX_DURATION,
`\`duration\` exceeds the maximum supported value (${MAX_DURATION}s) and has been clamped.`,
);
}, [rawDuration]);
// ======================== Effect ========================
React.useEffect(() => {
if (!mergedHovering && mergedDuration > 0) {
const start = Date.now() - spentTime;
const timeout = setTimeout(
() => {
onInternalClose();
},
mergedDuration * 1000 - spentTime,
);
return () => {
if (pauseOnHover) {
clearTimeout(timeout);
}
setSpentTime(Date.now() - start);
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mergedDuration, 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 / (mergedDuration * 1000), 1);
setPercent(progress * 100);
if (progress < 1) {
calculate();
}
});
};
calculate();
return () => {
if (pauseOnHover) {
cancelAnimationFrame(animationFrame);
}
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mergedDuration, 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={`${noticePrefixCls}-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;