Notice.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _typeof from "@babel/runtime/helpers/esm/typeof";
  4. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  5. import classNames from 'classnames';
  6. import KeyCode from "rc-util/es/KeyCode";
  7. import * as React from 'react';
  8. import pickAttrs from "rc-util/es/pickAttrs";
  9. var 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. // ======================== Close =========================
  43. var onInternalClose = function onInternalClose() {
  44. onNoticeClose(eventKey);
  45. };
  46. var onCloseKeyDown = function onCloseKeyDown(e) {
  47. if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
  48. onInternalClose();
  49. }
  50. };
  51. // ======================== Effect ========================
  52. React.useEffect(function () {
  53. if (!mergedHovering && duration > 0) {
  54. var start = Date.now() - spentTime;
  55. var timeout = setTimeout(function () {
  56. onInternalClose();
  57. }, duration * 1000 - spentTime);
  58. return function () {
  59. if (pauseOnHover) {
  60. clearTimeout(timeout);
  61. }
  62. setSpentTime(Date.now() - start);
  63. };
  64. }
  65. // eslint-disable-next-line react-hooks/exhaustive-deps
  66. }, [duration, mergedHovering, times]);
  67. React.useEffect(function () {
  68. if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
  69. var start = performance.now();
  70. var animationFrame;
  71. var calculate = function calculate() {
  72. cancelAnimationFrame(animationFrame);
  73. animationFrame = requestAnimationFrame(function (timestamp) {
  74. var runtime = timestamp + spentTime - start;
  75. var progress = Math.min(runtime / (duration * 1000), 1);
  76. setPercent(progress * 100);
  77. if (progress < 1) {
  78. calculate();
  79. }
  80. });
  81. };
  82. calculate();
  83. return function () {
  84. if (pauseOnHover) {
  85. cancelAnimationFrame(animationFrame);
  86. }
  87. };
  88. }
  89. // eslint-disable-next-line react-hooks/exhaustive-deps
  90. }, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
  91. // ======================== Closable ========================
  92. var closableObj = React.useMemo(function () {
  93. if (_typeof(closable) === 'object' && closable !== null) {
  94. return closable;
  95. }
  96. if (closable) {
  97. return {
  98. closeIcon: closeIcon
  99. };
  100. }
  101. return {};
  102. }, [closable, closeIcon]);
  103. var ariaProps = pickAttrs(closableObj, true);
  104. // ======================== Progress ========================
  105. var validPercent = 100 - (!percent || percent < 0 ? 0 : percent > 100 ? 100 : percent);
  106. // ======================== Render ========================
  107. var noticePrefixCls = "".concat(prefixCls, "-notice");
  108. return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
  109. ref: ref,
  110. className: classNames(noticePrefixCls, className, _defineProperty({}, "".concat(noticePrefixCls, "-closable"), closable)),
  111. style: style,
  112. onMouseEnter: function onMouseEnter(e) {
  113. var _divProps$onMouseEnte;
  114. setHovering(true);
  115. divProps === null || divProps === void 0 || (_divProps$onMouseEnte = divProps.onMouseEnter) === null || _divProps$onMouseEnte === void 0 || _divProps$onMouseEnte.call(divProps, e);
  116. },
  117. onMouseLeave: function onMouseLeave(e) {
  118. var _divProps$onMouseLeav;
  119. setHovering(false);
  120. divProps === null || divProps === void 0 || (_divProps$onMouseLeav = divProps.onMouseLeave) === null || _divProps$onMouseLeav === void 0 || _divProps$onMouseLeav.call(divProps, e);
  121. },
  122. onClick: onClick
  123. }), /*#__PURE__*/React.createElement("div", {
  124. className: "".concat(noticePrefixCls, "-content")
  125. }, content), closable && /*#__PURE__*/React.createElement("a", _extends({
  126. tabIndex: 0,
  127. className: "".concat(noticePrefixCls, "-close"),
  128. onKeyDown: onCloseKeyDown,
  129. "aria-label": "Close"
  130. }, ariaProps, {
  131. onClick: function onClick(e) {
  132. e.preventDefault();
  133. e.stopPropagation();
  134. onInternalClose();
  135. }
  136. }), closableObj.closeIcon), mergedShowProgress && /*#__PURE__*/React.createElement("progress", {
  137. className: "".concat(noticePrefixCls, "-progress"),
  138. max: "100",
  139. value: validPercent
  140. }, validPercent + '%'));
  141. });
  142. export default Notify;