antd
Version:
An enterprise-class UI design language and React components implementation
116 lines • 3.74 kB
JavaScript
import classNames from 'classnames';
import CSSMotion from 'rc-motion';
import { render, unmount } from "rc-util/es/React/render";
import raf from "rc-util/es/raf";
import * as React from 'react';
import { getTargetWaveColor } from './util';
function validateNum(value) {
return Number.isNaN(value) ? 0 : value;
}
const WaveEffect = props => {
const {
className,
target
} = props;
const divRef = React.useRef(null);
const [color, setWaveColor] = React.useState(null);
const [borderRadius, setBorderRadius] = React.useState([]);
const [left, setLeft] = React.useState(0);
const [top, setTop] = React.useState(0);
const [width, setWidth] = React.useState(0);
const [height, setHeight] = React.useState(0);
const [enabled, setEnabled] = React.useState(false);
const waveStyle = {
left,
top,
width,
height,
borderRadius: borderRadius.map(radius => `${radius}px`).join(' ')
};
if (color) {
waveStyle['--wave-color'] = color;
}
function syncPos() {
const nodeStyle = getComputedStyle(target);
// Get wave color from target
setWaveColor(getTargetWaveColor(target));
const isStatic = nodeStyle.position === 'static';
// Rect
const {
borderLeftWidth,
borderTopWidth
} = nodeStyle;
setLeft(isStatic ? target.offsetLeft : validateNum(-parseFloat(borderLeftWidth)));
setTop(isStatic ? target.offsetTop : validateNum(-parseFloat(borderTopWidth)));
setWidth(target.offsetWidth);
setHeight(target.offsetHeight);
// Get border radius
const {
borderTopLeftRadius,
borderTopRightRadius,
borderBottomLeftRadius,
borderBottomRightRadius
} = nodeStyle;
setBorderRadius([borderTopLeftRadius, borderTopRightRadius, borderBottomRightRadius, borderBottomLeftRadius].map(radius => validateNum(parseFloat(radius))));
}
React.useEffect(() => {
if (target) {
// We need delay to check position here
// since UI may change after click
const id = raf(() => {
syncPos();
setEnabled(true);
});
// Add resize observer to follow size
let resizeObserver;
if (typeof ResizeObserver !== 'undefined') {
resizeObserver = new ResizeObserver(syncPos);
resizeObserver.observe(target);
}
return () => {
raf.cancel(id);
resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
};
}
}, []);
if (!enabled) {
return null;
}
return /*#__PURE__*/React.createElement(CSSMotion, {
visible: true,
motionAppear: true,
motionName: "wave-motion",
motionDeadline: 5000,
onAppearEnd: (_, event) => {
var _a;
if (event.deadline || event.propertyName === 'opacity') {
const holder = (_a = divRef.current) === null || _a === void 0 ? void 0 : _a.parentElement;
unmount(holder).then(() => {
holder === null || holder === void 0 ? void 0 : holder.remove();
});
}
return false;
}
}, _ref => {
let {
className: motionClassName
} = _ref;
return /*#__PURE__*/React.createElement("div", {
ref: divRef,
className: classNames(className, motionClassName),
style: waveStyle
});
});
};
export default function showWaveEffect(node, className) {
// Create holder
const holder = document.createElement('div');
holder.style.position = 'absolute';
holder.style.left = `0px`;
holder.style.top = `0px`;
node === null || node === void 0 ? void 0 : node.insertBefore(holder, node === null || node === void 0 ? void 0 : node.firstChild);
render( /*#__PURE__*/React.createElement(WaveEffect, {
target: node,
className: className
}), holder);
}