2022-12-28 23:20:22 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import CSSMotion from 'rc-motion';
|
2023-01-03 16:08:20 +08:00
|
|
|
import raf from 'rc-util/lib/raf';
|
2022-12-28 23:20:22 +08:00
|
|
|
import { render, unmount } from 'rc-util/lib/React/render';
|
|
|
|
import classNames from 'classnames';
|
2023-01-02 16:34:35 +08:00
|
|
|
import { getTargetWaveColor } from './util';
|
2022-12-28 23:20:22 +08:00
|
|
|
|
2023-01-03 16:08:20 +08:00
|
|
|
function validateNum(value: number) {
|
|
|
|
return Number.isNaN(value) ? 0 : value;
|
|
|
|
}
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
export interface WaveEffectProps {
|
|
|
|
className: string;
|
2023-01-03 16:08:20 +08:00
|
|
|
target: HTMLElement;
|
2022-12-28 23:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const WaveEffect: React.FC<WaveEffectProps> = (props) => {
|
2023-01-03 16:08:20 +08:00
|
|
|
const { className, target } = props;
|
2022-12-28 23:20:22 +08:00
|
|
|
const divRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
2023-01-03 16:08:20 +08:00
|
|
|
const [color, setWaveColor] = React.useState<string | null>(null);
|
|
|
|
const [borderRadius, setBorderRadius] = React.useState<number[]>([]);
|
|
|
|
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);
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
const waveStyle = {
|
|
|
|
left,
|
|
|
|
top,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
borderRadius: borderRadius.map((radius) => `${radius}px`).join(' '),
|
|
|
|
} as React.CSSProperties & {
|
|
|
|
[name: string]: number | string;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (color) {
|
|
|
|
waveStyle['--wave-color'] = color;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:08:20 +08:00
|
|
|
function syncPos() {
|
|
|
|
const nodeStyle = getComputedStyle(target);
|
|
|
|
|
|
|
|
// Get wave color from target
|
|
|
|
setWaveColor(getTargetWaveColor(target));
|
|
|
|
|
2023-01-12 17:52:19 +08:00
|
|
|
const isStatic = nodeStyle.position === 'static';
|
|
|
|
|
2023-01-03 16:08:20 +08:00
|
|
|
// Rect
|
2023-01-12 17:52:19 +08:00
|
|
|
const { borderLeftWidth, borderTopWidth } = nodeStyle;
|
2023-01-19 11:50:12 +08:00
|
|
|
setLeft(isStatic ? target.offsetLeft : validateNum(-parseFloat(borderLeftWidth)));
|
|
|
|
setTop(isStatic ? target.offsetTop : validateNum(-parseFloat(borderTopWidth)));
|
2023-01-03 16:08:20 +08:00
|
|
|
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: ResizeObserver;
|
|
|
|
if (typeof ResizeObserver !== 'undefined') {
|
|
|
|
resizeObserver = new ResizeObserver(syncPos);
|
|
|
|
|
|
|
|
resizeObserver.observe(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
raf.cancel(id);
|
|
|
|
resizeObserver?.disconnect();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
return (
|
|
|
|
<CSSMotion
|
|
|
|
visible
|
|
|
|
motionAppear
|
|
|
|
motionName="wave-motion"
|
|
|
|
motionDeadline={5000}
|
|
|
|
onAppearEnd={(_, event) => {
|
|
|
|
if (event.deadline || (event as TransitionEvent).propertyName === 'opacity') {
|
|
|
|
const holder = divRef.current?.parentElement!;
|
|
|
|
unmount(holder).then(() => {
|
|
|
|
holder.parentElement?.removeChild(holder);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{({ className: motionClassName }) => (
|
|
|
|
<div ref={divRef} className={classNames(className, motionClassName)} style={waveStyle} />
|
|
|
|
)}
|
|
|
|
</CSSMotion>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-02 16:34:35 +08:00
|
|
|
export default function showWaveEffect(node: HTMLElement, className: string) {
|
2022-12-28 23:20:22 +08:00
|
|
|
// Create holder
|
|
|
|
const holder = document.createElement('div');
|
2023-01-02 16:34:35 +08:00
|
|
|
holder.style.position = 'absolute';
|
2023-01-03 16:08:20 +08:00
|
|
|
holder.style.left = `0px`;
|
|
|
|
holder.style.top = `0px`;
|
2023-01-12 17:52:19 +08:00
|
|
|
node?.insertBefore(holder, node?.firstChild);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
2023-01-03 16:08:20 +08:00
|
|
|
render(<WaveEffect target={node} className={className} />, holder);
|
2022-12-28 23:20:22 +08:00
|
|
|
}
|