mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 09:49:57 +08:00
8a3870fc31
* docs: add demo * refactor: init * refactor: of it * refactor: simple content * chore: unique func * chore: refactor * chore: support modal watermark * feat: support nest watermark * test: add test case * chore: fix lint * chore: bump rc-image * test: add test case * refactor: use same func
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import * as React from 'react';
|
|
import { useEvent } from 'rc-util';
|
|
import raf from 'rc-util/lib/raf';
|
|
import showWaveEffect from './WaveEffect';
|
|
import { ConfigContext } from '../../config-provider';
|
|
import useToken from '../../theme/useToken';
|
|
import { TARGET_CLS, type ShowWave } from './interface';
|
|
|
|
export default function useWave(
|
|
nodeRef: React.RefObject<HTMLElement>,
|
|
className: string,
|
|
component?: string,
|
|
) {
|
|
const { wave } = React.useContext(ConfigContext);
|
|
const [, token, hashId] = useToken();
|
|
|
|
const showWave = useEvent<ShowWave>((event) => {
|
|
const node = nodeRef.current!;
|
|
|
|
if (wave?.disabled || !node) {
|
|
return;
|
|
}
|
|
|
|
const targetNode = node.querySelector<HTMLElement>(`.${TARGET_CLS}`) || node;
|
|
|
|
const { showEffect } = wave || {};
|
|
|
|
// Customize wave effect
|
|
(showEffect || showWaveEffect)(targetNode, { className, token, component, event, hashId });
|
|
});
|
|
|
|
const rafId = React.useRef<number>();
|
|
|
|
// Merge trigger event into one for each frame
|
|
const showDebounceWave: ShowWave = (event) => {
|
|
raf.cancel(rafId.current!);
|
|
|
|
rafId.current = raf(() => {
|
|
showWave(event);
|
|
});
|
|
};
|
|
|
|
return showDebounceWave;
|
|
}
|