File size: 1,308 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import * as React from 'react';
import useEvent from 'rc-util/lib/hooks/useEvent';
import raf from 'rc-util/lib/raf';
import { ConfigContext } from '../../config-provider';
import useToken from '../../theme/useToken';
import { TARGET_CLS } from './interface';
import type { ShowWave, WaveComponent } from './interface';
import showWaveEffect from './WaveEffect';
const useWave = (
nodeRef: React.RefObject<HTMLElement>,
className: string,
component?: WaveComponent,
) => {
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>(null);
// Merge trigger event into one for each frame
const showDebounceWave: ShowWave = (event) => {
raf.cancel(rafId.current!);
rafId.current = raf(() => {
showWave(event);
});
};
return showDebounceWave;
};
export default useWave;
|