123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
- import _typeof from "@babel/runtime/helpers/esm/typeof";
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
- import classNames from 'classnames';
- import KeyCode from "rc-util/es/KeyCode";
- import * as React from 'react';
- import pickAttrs from "rc-util/es/pickAttrs";
- var Notify = /*#__PURE__*/React.forwardRef(function (props, ref) {
- var prefixCls = props.prefixCls,
- style = props.style,
- className = props.className,
- _props$duration = props.duration,
- duration = _props$duration === void 0 ? 4.5 : _props$duration,
- showProgress = props.showProgress,
- _props$pauseOnHover = props.pauseOnHover,
- pauseOnHover = _props$pauseOnHover === void 0 ? true : _props$pauseOnHover,
- eventKey = props.eventKey,
- content = props.content,
- closable = props.closable,
- _props$closeIcon = props.closeIcon,
- closeIcon = _props$closeIcon === void 0 ? 'x' : _props$closeIcon,
- divProps = props.props,
- onClick = props.onClick,
- onNoticeClose = props.onNoticeClose,
- times = props.times,
- forcedHovering = props.hovering;
- var _React$useState = React.useState(false),
- _React$useState2 = _slicedToArray(_React$useState, 2),
- hovering = _React$useState2[0],
- setHovering = _React$useState2[1];
- var _React$useState3 = React.useState(0),
- _React$useState4 = _slicedToArray(_React$useState3, 2),
- percent = _React$useState4[0],
- setPercent = _React$useState4[1];
- var _React$useState5 = React.useState(0),
- _React$useState6 = _slicedToArray(_React$useState5, 2),
- spentTime = _React$useState6[0],
- setSpentTime = _React$useState6[1];
- var mergedHovering = forcedHovering || hovering;
- var mergedShowProgress = duration > 0 && showProgress;
- // ======================== Close =========================
- var onInternalClose = function onInternalClose() {
- onNoticeClose(eventKey);
- };
- var onCloseKeyDown = function onCloseKeyDown(e) {
- if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
- onInternalClose();
- }
- };
- // ======================== Effect ========================
- React.useEffect(function () {
- if (!mergedHovering && duration > 0) {
- var start = Date.now() - spentTime;
- var timeout = setTimeout(function () {
- onInternalClose();
- }, duration * 1000 - spentTime);
- return function () {
- if (pauseOnHover) {
- clearTimeout(timeout);
- }
- setSpentTime(Date.now() - start);
- };
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [duration, mergedHovering, times]);
- React.useEffect(function () {
- if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
- var start = performance.now();
- var animationFrame;
- var calculate = function calculate() {
- cancelAnimationFrame(animationFrame);
- animationFrame = requestAnimationFrame(function (timestamp) {
- var runtime = timestamp + spentTime - start;
- var progress = Math.min(runtime / (duration * 1000), 1);
- setPercent(progress * 100);
- if (progress < 1) {
- calculate();
- }
- });
- };
- calculate();
- return function () {
- if (pauseOnHover) {
- cancelAnimationFrame(animationFrame);
- }
- };
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
- // ======================== Closable ========================
- var closableObj = React.useMemo(function () {
- if (_typeof(closable) === 'object' && closable !== null) {
- return closable;
- }
- if (closable) {
- return {
- closeIcon: closeIcon
- };
- }
- return {};
- }, [closable, closeIcon]);
- var ariaProps = pickAttrs(closableObj, true);
- // ======================== Progress ========================
- var validPercent = 100 - (!percent || percent < 0 ? 0 : percent > 100 ? 100 : percent);
- // ======================== Render ========================
- var noticePrefixCls = "".concat(prefixCls, "-notice");
- return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
- ref: ref,
- className: classNames(noticePrefixCls, className, _defineProperty({}, "".concat(noticePrefixCls, "-closable"), closable)),
- style: style,
- onMouseEnter: function onMouseEnter(e) {
- var _divProps$onMouseEnte;
- setHovering(true);
- divProps === null || divProps === void 0 || (_divProps$onMouseEnte = divProps.onMouseEnter) === null || _divProps$onMouseEnte === void 0 || _divProps$onMouseEnte.call(divProps, e);
- },
- onMouseLeave: function onMouseLeave(e) {
- var _divProps$onMouseLeav;
- setHovering(false);
- divProps === null || divProps === void 0 || (_divProps$onMouseLeav = divProps.onMouseLeave) === null || _divProps$onMouseLeav === void 0 || _divProps$onMouseLeav.call(divProps, e);
- },
- onClick: onClick
- }), /*#__PURE__*/React.createElement("div", {
- className: "".concat(noticePrefixCls, "-content")
- }, content), closable && /*#__PURE__*/React.createElement("a", _extends({
- tabIndex: 0,
- className: "".concat(noticePrefixCls, "-close"),
- onKeyDown: onCloseKeyDown,
- "aria-label": "Close"
- }, ariaProps, {
- onClick: function onClick(e) {
- e.preventDefault();
- e.stopPropagation();
- onInternalClose();
- }
- }), closableObj.closeIcon), mergedShowProgress && /*#__PURE__*/React.createElement("progress", {
- className: "".concat(noticePrefixCls, "-progress"),
- max: "100",
- value: validPercent
- }, validPercent + '%'));
- });
- export default Notify;
|