UNPKG

5.78 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3import _typeof from "@babel/runtime/helpers/esm/typeof";
4import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
5import classNames from 'classnames';
6import KeyCode from "rc-util/es/KeyCode";
7import * as React from 'react';
8import pickAttrs from "rc-util/es/pickAttrs";
9var Notify = /*#__PURE__*/React.forwardRef(function (props, ref) {
10 var prefixCls = props.prefixCls,
11 style = props.style,
12 className = props.className,
13 _props$duration = props.duration,
14 duration = _props$duration === void 0 ? 4.5 : _props$duration,
15 showProgress = props.showProgress,
16 _props$pauseOnHover = props.pauseOnHover,
17 pauseOnHover = _props$pauseOnHover === void 0 ? true : _props$pauseOnHover,
18 eventKey = props.eventKey,
19 content = props.content,
20 closable = props.closable,
21 _props$closeIcon = props.closeIcon,
22 closeIcon = _props$closeIcon === void 0 ? 'x' : _props$closeIcon,
23 divProps = props.props,
24 onClick = props.onClick,
25 onNoticeClose = props.onNoticeClose,
26 times = props.times,
27 forcedHovering = props.hovering;
28 var _React$useState = React.useState(false),
29 _React$useState2 = _slicedToArray(_React$useState, 2),
30 hovering = _React$useState2[0],
31 setHovering = _React$useState2[1];
32 var _React$useState3 = React.useState(0),
33 _React$useState4 = _slicedToArray(_React$useState3, 2),
34 percent = _React$useState4[0],
35 setPercent = _React$useState4[1];
36 var _React$useState5 = React.useState(0),
37 _React$useState6 = _slicedToArray(_React$useState5, 2),
38 spentTime = _React$useState6[0],
39 setSpentTime = _React$useState6[1];
40 var mergedHovering = forcedHovering || hovering;
41 var mergedShowProgress = duration > 0 && showProgress;
42
43 // ======================== Close =========================
44 var onInternalClose = function onInternalClose() {
45 onNoticeClose(eventKey);
46 };
47 var onCloseKeyDown = function onCloseKeyDown(e) {
48 if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
49 onInternalClose();
50 }
51 };
52
53 // ======================== Effect ========================
54 React.useEffect(function () {
55 if (!mergedHovering && duration > 0) {
56 var start = Date.now() - spentTime;
57 var timeout = setTimeout(function () {
58 onInternalClose();
59 }, duration * 1000 - spentTime);
60 return function () {
61 if (pauseOnHover) {
62 clearTimeout(timeout);
63 }
64 setSpentTime(Date.now() - start);
65 };
66 }
67 // eslint-disable-next-line react-hooks/exhaustive-deps
68 }, [duration, mergedHovering, times]);
69 React.useEffect(function () {
70 if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
71 var start = performance.now();
72 var animationFrame;
73 var calculate = function calculate() {
74 cancelAnimationFrame(animationFrame);
75 animationFrame = requestAnimationFrame(function (timestamp) {
76 var runtime = timestamp + spentTime - start;
77 var progress = Math.min(runtime / (duration * 1000), 1);
78 setPercent(progress * 100);
79 if (progress < 1) {
80 calculate();
81 }
82 });
83 };
84 calculate();
85 return function () {
86 if (pauseOnHover) {
87 cancelAnimationFrame(animationFrame);
88 }
89 };
90 }
91 // eslint-disable-next-line react-hooks/exhaustive-deps
92 }, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
93
94 // ======================== Closable ========================
95 var closableObj = React.useMemo(function () {
96 if (_typeof(closable) === 'object' && closable !== null) {
97 return closable;
98 }
99 if (closable) {
100 return {
101 closeIcon: closeIcon
102 };
103 }
104 return {};
105 }, [closable, closeIcon]);
106 var ariaProps = pickAttrs(closableObj, true);
107
108 // ======================== Progress ========================
109 var validPercent = 100 - (!percent || percent < 0 ? 0 : percent > 100 ? 100 : percent);
110
111 // ======================== Render ========================
112 var noticePrefixCls = "".concat(prefixCls, "-notice");
113 return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
114 ref: ref,
115 className: classNames(noticePrefixCls, className, _defineProperty({}, "".concat(noticePrefixCls, "-closable"), closable)),
116 style: style,
117 onMouseEnter: function onMouseEnter(e) {
118 var _divProps$onMouseEnte;
119 setHovering(true);
120 divProps === null || divProps === void 0 || (_divProps$onMouseEnte = divProps.onMouseEnter) === null || _divProps$onMouseEnte === void 0 || _divProps$onMouseEnte.call(divProps, e);
121 },
122 onMouseLeave: function onMouseLeave(e) {
123 var _divProps$onMouseLeav;
124 setHovering(false);
125 divProps === null || divProps === void 0 || (_divProps$onMouseLeav = divProps.onMouseLeave) === null || _divProps$onMouseLeav === void 0 || _divProps$onMouseLeav.call(divProps, e);
126 },
127 onClick: onClick
128 }), /*#__PURE__*/React.createElement("div", {
129 className: "".concat(noticePrefixCls, "-content")
130 }, content), closable && /*#__PURE__*/React.createElement("a", _extends({
131 tabIndex: 0,
132 className: "".concat(noticePrefixCls, "-close"),
133 onKeyDown: onCloseKeyDown,
134 "aria-label": "Close"
135 }, ariaProps, {
136 onClick: function onClick(e) {
137 e.preventDefault();
138 e.stopPropagation();
139 onInternalClose();
140 }
141 }), closableObj.closeIcon), mergedShowProgress && /*#__PURE__*/React.createElement("progress", {
142 className: "".concat(noticePrefixCls, "-progress"),
143 max: "100",
144 value: validPercent
145 }, validPercent + '%'));
146});
147export default Notify;
\No newline at end of file