mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 22:48:29 +08:00
91878f82ad
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
* refactor: update ref logic * chore: clean up * chore: coverage adjust * test: coverage * chore: back of code
166 lines
4.4 KiB
TypeScript
166 lines
4.4 KiB
TypeScript
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
import CSSMotion from 'rc-motion';
|
|
import raf from 'rc-util/lib/raf';
|
|
import { render, unmount } from 'rc-util/lib/React/render';
|
|
import { composeRef } from 'rc-util/lib/ref';
|
|
|
|
import { TARGET_CLS } from './interface';
|
|
import type { ShowWaveEffect } from './interface';
|
|
import { getTargetWaveColor } from './util';
|
|
|
|
function validateNum(value: number) {
|
|
return Number.isNaN(value) ? 0 : value;
|
|
}
|
|
|
|
export interface WaveEffectProps {
|
|
className: string;
|
|
target: HTMLElement;
|
|
component?: string;
|
|
}
|
|
|
|
const WaveEffect: React.FC<WaveEffectProps> = (props) => {
|
|
const { className, target, component } = props;
|
|
const divRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
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);
|
|
|
|
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;
|
|
}
|
|
|
|
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: ResizeObserver;
|
|
if (typeof ResizeObserver !== 'undefined') {
|
|
resizeObserver = new ResizeObserver(syncPos);
|
|
|
|
resizeObserver.observe(target);
|
|
}
|
|
|
|
return () => {
|
|
raf.cancel(id);
|
|
resizeObserver?.disconnect();
|
|
};
|
|
}
|
|
}, []);
|
|
|
|
if (!enabled) {
|
|
return null;
|
|
}
|
|
|
|
const isSmallComponent =
|
|
(component === 'Checkbox' || component === 'Radio') && target?.classList.contains(TARGET_CLS);
|
|
|
|
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?.remove();
|
|
});
|
|
}
|
|
return false;
|
|
}}
|
|
>
|
|
{({ className: motionClassName }, ref) => (
|
|
<div
|
|
ref={composeRef(divRef, ref)}
|
|
className={classNames(className, motionClassName, { 'wave-quick': isSmallComponent })}
|
|
style={waveStyle}
|
|
/>
|
|
)}
|
|
</CSSMotion>
|
|
);
|
|
};
|
|
|
|
const showWaveEffect: ShowWaveEffect = (target, info) => {
|
|
const { component } = info;
|
|
|
|
// Skip if not support `render` since `rc-util` render not support React 19
|
|
// TODO: remove this check in v6
|
|
/* istanbul ignore next */
|
|
if (!render) {
|
|
return;
|
|
}
|
|
|
|
// Skip for unchecked checkbox
|
|
if (component === 'Checkbox' && !target.querySelector<HTMLInputElement>('input')?.checked) {
|
|
return;
|
|
}
|
|
|
|
// Create holder
|
|
const holder = document.createElement('div');
|
|
holder.style.position = 'absolute';
|
|
holder.style.left = '0px';
|
|
holder.style.top = '0px';
|
|
target?.insertBefore(holder, target?.firstChild);
|
|
|
|
render(<WaveEffect {...info} target={target} />, holder);
|
|
};
|
|
|
|
export default showWaveEffect;
|