UNPKG

tdesign-react

Version:
243 lines (237 loc) 7.28 kB
/** * tdesign v1.16.2 * (c) 2025 tdesign * @license MIT */ import { _ as _slicedToArray } from './dep-10d5731f.js'; import { _ as _objectWithoutProperties } from './dep-ed34fbd4.js'; import React, { forwardRef, useState, useEffect } from 'react'; import classNames from 'classnames'; import noop from '../_util/noop.js'; import { usePersistFn } from '../hooks/usePersistFn.js'; import MessageClose from '../message/MessageClose.js'; import MessageIcon from '../message/MessageIcon.js'; import { useMessageClass } from '../message/useMessageClass.js'; var DISTANCE = "32px"; var PLACEMENT_OFFSET = { center: { left: "50%", top: "50%", transform: "translateX(-50%) translateY(-50%)" }, left: { left: DISTANCE, top: "50%", transform: "translateY(-50%)" }, bottom: { bottom: DISTANCE, left: "50%", transform: "translateX(-50%)" }, right: { right: DISTANCE, top: "50%", transform: "translateY(-50%)" }, top: { top: DISTANCE, left: "50%", transform: "translateX(-50%)" }, "top-left": { left: DISTANCE, top: DISTANCE }, "top-right": { right: DISTANCE, top: DISTANCE }, "bottom-left": { left: DISTANCE, bottom: DISTANCE }, "bottom-right": { right: DISTANCE, bottom: DISTANCE } }; var PLACEMENT_LIST = Object.keys(PLACEMENT_OFFSET); var ANIMATION_OPTION = { duration: 200, easing: "linear" }; function setDomStyleAfterAnimation(dom, styleAfterAnimation) { var keys = Object.keys(styleAfterAnimation); for (var i = 0; i < keys.length; i += 1) { var key = keys[i]; dom.style[key] = styleAfterAnimation[key]; } } function getFadeInKeyframes(placement, offsetWidth, offsetHeight) { if (!PLACEMENT_LIST.includes(placement)) return null; if (["top-left", "left", "bottom-left"].includes(placement)) { return [{ opacity: 0, marginLeft: "-".concat(offsetWidth, "px") }, { opacity: 1, marginLeft: "0" }]; } if (["top-right", "right", "bottom-right"].includes(placement)) { return [{ opacity: 0, marginRight: "-".concat(offsetWidth, "px") }, { opacity: 1, marginRight: "0" }]; } if (["top", "center"].includes(placement)) { return [{ opacity: 0, marginTop: "-".concat(offsetHeight, "px") }, { opacity: 1, marginTop: "0" }]; } if (["bottom"].includes(placement)) { return [{ opacity: 0, transform: "translate3d(0, ".concat(offsetHeight, "px, 0)") }, { opacity: 1, transform: "translate3d(0, 0, 0)" }]; } return null; } function getFadeOutKeyframes(placement, offsetHeight) { if (!PLACEMENT_LIST.includes(placement)) return null; if (["bottom-left", "bottom", "bottom-right"].includes(placement)) { var marginOffset2 = "".concat(offsetHeight, "px"); return [{ opacity: 1, marginTop: "0px" }, { opacity: 0, marginTop: marginOffset2 }]; } var marginOffset = "-".concat(offsetHeight, "px"); return [{ opacity: 1, marginTop: "0px" }, { opacity: 0, marginTop: marginOffset }]; } function fadeIn(dom, placement) { var _dom$animate; if (!dom) return; var offsetWidth = (dom === null || dom === void 0 ? void 0 : dom.offsetWidth) || 0; var offsetHeight = (dom === null || dom === void 0 ? void 0 : dom.offsetHeight) || 0; var fadeInKeyframes = getFadeInKeyframes(placement, offsetWidth, offsetHeight); if (!fadeInKeyframes) return; var styleAfterFadeIn = fadeInKeyframes[fadeInKeyframes.length - 1]; setDomStyleAfterAnimation(dom, styleAfterFadeIn); (_dom$animate = dom.animate) === null || _dom$animate === void 0 || _dom$animate.call(dom, fadeInKeyframes, ANIMATION_OPTION); } function fadeOut(dom, placement, onFinish) { if (!dom) return; var offsetHeight = dom.offsetHeight || 0; var fadeOutKeyframes = getFadeOutKeyframes(placement, offsetHeight); if (!fadeOutKeyframes) { onFinish(); return; } var styleAfterFadeOut = fadeOutKeyframes[fadeOutKeyframes.length - 1]; setDomStyleAfterAnimation(dom, styleAfterFadeOut); var animation = dom.animate && dom.animate(fadeOutKeyframes, ANIMATION_OPTION); if (animation) { animation.onfinish = function () { dom.style.display = "none"; onFinish(); }; } else { dom.style.display = "none"; onFinish(); } } var _excluded = ["placement"]; var MessageComponent = /*#__PURE__*/forwardRef(function (props, ref) { var placement = props.placement, otherProps = _objectWithoutProperties(props, _excluded); var _useMessageClass = useMessageClass(), tdMessagePrefix = _useMessageClass.tdMessagePrefix, tdClassIsGenerator = _useMessageClass.tdClassIsGenerator; var _otherProps$theme = otherProps.theme, theme = _otherProps$theme === void 0 ? "info" : _otherProps$theme, className = otherProps.className, children = otherProps.children, style = otherProps.style, _otherProps$icon = otherProps.icon, icon = _otherProps$icon === void 0 ? true : _otherProps$icon, content = otherProps.content, closeBtn = otherProps.closeBtn, _otherProps$onCloseBt = otherProps.onCloseBtnClick, onCloseBtnClick = _otherProps$onCloseBt === void 0 ? noop : _otherProps$onCloseBt, _otherProps$onDuratio = otherProps.onDurationEnd, onDurationEnd = _otherProps$onDuratio === void 0 ? noop : _otherProps$onDuratio, _otherProps$onClose = otherProps.onClose, onClose = _otherProps$onClose === void 0 ? noop : _otherProps$onClose, duration = otherProps.duration; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isHovering = _useState2[0], setIsHovering = _useState2[1]; var onCloseFn = usePersistFn(onClose); var onDurationEndFn = usePersistFn(onDurationEnd); var onCloseBtnClickFn = usePersistFn(onCloseBtnClick); function handleCloseBtnClick(e) { onCloseBtnClickFn(e); onCloseFn({ trigger: "close-click" }); } useEffect(function () { if (ref && "current" in ref && ref.current && placement) { fadeIn(ref.current, placement); } }, []); useEffect(function () { if (!isHovering && duration > 0) { var timer = setTimeout(function () { onDurationEndFn(); onCloseFn({ trigger: "duration-end" }); }, duration); return function () { clearTimeout(timer); }; } }, [duration, isHovering, onCloseFn, onDurationEndFn]); return /* @__PURE__ */React.createElement("div", { ref: ref, key: "message", style: style, className: classNames(className, "".concat(tdMessagePrefix), tdClassIsGenerator(theme), closeBtn ? tdClassIsGenerator("closable") : ""), onMouseEnter: function onMouseEnter() { return setIsHovering(true); }, onMouseLeave: function onMouseLeave() { return setIsHovering(false); } }, icon === true ? /* @__PURE__ */React.createElement(MessageIcon, { theme: theme }) : icon, content ? content : children, /* @__PURE__ */React.createElement(MessageClose, { closeBtn: closeBtn, onCloseBtnClick: handleCloseBtnClick })); }); export { MessageComponent as M, PLACEMENT_OFFSET as P, fadeOut as f }; //# sourceMappingURL=dep-c1f1d297.js.map