ant-design/components/slider/SliderTooltip.tsx

39 lines
912 B
TypeScript
Raw Normal View History

import * as React from 'react';
2020-11-17 12:01:02 +08:00
import { useRef } from 'react';
import { composeRef } from 'rc-util/lib/ref';
import Tooltip, { TooltipProps } from '../tooltip';
const SliderTooltip = React.forwardRef<unknown, TooltipProps>((props, ref) => {
const { visible } = props;
2020-11-17 12:01:02 +08:00
const innerRef = useRef<any>(null);
2020-11-17 12:01:02 +08:00
const rafRef = useRef<number | null>(null);
function cancelKeepAlign() {
window.cancelAnimationFrame(rafRef.current!);
rafRef.current = null;
}
function keepAlign() {
rafRef.current = window.requestAnimationFrame(() => {
2020-11-17 12:01:02 +08:00
innerRef.current.forcePopupAlign();
rafRef.current = null;
keepAlign();
});
}
React.useEffect(() => {
if (visible) {
keepAlign();
} else {
cancelKeepAlign();
}
return cancelKeepAlign;
}, [visible]);
2020-11-17 12:01:02 +08:00
return <Tooltip ref={composeRef(innerRef, ref)} {...props} />;
});
export default SliderTooltip;