ant-design/components/slider/SliderTooltip.tsx

45 lines
1.0 KiB
TypeScript
Raw Normal View History

import type { SliderRef } from 'rc-slider/lib/Slider';
2022-06-22 14:57:09 +08:00
import raf from 'rc-util/lib/raf';
import { composeRef } from 'rc-util/lib/ref';
import * as React from 'react';
import { useRef } from 'react';
import type { TooltipProps } from '../tooltip';
import Tooltip from '../tooltip';
const SliderTooltip = React.forwardRef<SliderRef, TooltipProps>((props, ref) => {
const { open } = 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() {
raf.cancel(rafRef.current!);
rafRef.current = null;
}
function keepAlign() {
rafRef.current = raf(() => {
innerRef.current?.forceAlign();
rafRef.current = null;
});
}
React.useEffect(() => {
if (open) {
keepAlign();
} else {
cancelKeepAlign();
}
return cancelKeepAlign;
}, [open, props.title]);
2020-11-17 12:01:02 +08:00
return <Tooltip ref={composeRef(innerRef, ref)} {...props} />;
});
if (process.env.NODE_ENV !== 'production') {
SliderTooltip.displayName = 'SliderTooltip';
}
export default SliderTooltip;