2022-12-28 23:20:22 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import CSSMotion from 'rc-motion';
|
|
|
|
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
|
|
|
|
|
|
|
export interface WaveEffectProps {
|
|
|
|
left: number;
|
|
|
|
top: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
color: string | null;
|
|
|
|
className: string;
|
|
|
|
borderRadius: number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const WaveEffect: React.FC<WaveEffectProps> = (props) => {
|
2023-01-02 16:34:35 +08:00
|
|
|
const { className, left, top, width, height, color, borderRadius } = props;
|
2022-12-28 23:20:22 +08:00
|
|
|
const divRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function validateNum(value: number) {
|
|
|
|
return Number.isNaN(value) ? 0 : value;
|
|
|
|
}
|
|
|
|
|
2023-01-02 16:34:35 +08:00
|
|
|
export default function showWaveEffect(node: HTMLElement, className: string) {
|
2022-12-28 23:20:22 +08:00
|
|
|
const nodeStyle = getComputedStyle(node);
|
|
|
|
|
|
|
|
// Get wave color from target
|
|
|
|
const waveColor = getTargetWaveColor(node);
|
|
|
|
|
|
|
|
// Get border radius
|
|
|
|
const {
|
|
|
|
borderTopLeftRadius,
|
|
|
|
borderTopRightRadius,
|
|
|
|
borderBottomLeftRadius,
|
|
|
|
borderBottomRightRadius,
|
|
|
|
} = nodeStyle;
|
|
|
|
|
|
|
|
// Create holder
|
|
|
|
const holder = document.createElement('div');
|
2023-01-02 16:34:35 +08:00
|
|
|
holder.style.position = 'absolute';
|
|
|
|
holder.style.left = `${node.offsetLeft}px`;
|
|
|
|
holder.style.top = `${node.offsetTop}px`;
|
|
|
|
node.parentElement?.appendChild(holder);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
render(
|
|
|
|
<WaveEffect
|
2023-01-02 16:34:35 +08:00
|
|
|
left={0}
|
|
|
|
top={0}
|
|
|
|
width={node.offsetWidth}
|
|
|
|
height={node.offsetHeight}
|
2022-12-28 23:20:22 +08:00
|
|
|
color={waveColor}
|
|
|
|
className={className}
|
|
|
|
borderRadius={[
|
|
|
|
borderTopLeftRadius,
|
|
|
|
borderTopRightRadius,
|
|
|
|
borderBottomRightRadius,
|
|
|
|
borderBottomLeftRadius,
|
2023-01-02 16:34:35 +08:00
|
|
|
].map((radius) => validateNum(parseFloat(radius)))}
|
2022-12-28 23:20:22 +08:00
|
|
|
/>,
|
|
|
|
holder,
|
|
|
|
);
|
|
|
|
}
|