Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tooltip can't disappear when mouseleave on Paragraph (#38509) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions components/typography/Base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,19 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
// ========================== Copyable ==========================
const [enableCopy, copyConfig] = useMergedConfig<CopyConfig>(copyable);
const [copied, setCopied] = React.useState(false);
const copyIdRef = React.useRef<number>();
const [copyTooltipVisible, setCopyTootipVisible] = React.useState(false);
const copyIdsRef = React.useRef<number[]>([]);

const copyVisibleTime = 3000;

const copyOptions: Pick<CopyConfig, 'format'> = {};
if (copyConfig.format) {
copyOptions.format = copyConfig.format;
}

const cleanCopyId = () => {
window.clearTimeout(copyIdRef.current!);
const cleanCopyIds = () => {
copyIdsRef.current.forEach((id) => window.clearTimeout(id));
copyIdsRef.current = [];
};

const onCopyClick = (e?: React.MouseEvent<HTMLDivElement>) => {
Expand All @@ -211,15 +215,27 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
setCopied(true);

// Trigger tips update
cleanCopyId();
copyIdRef.current = window.setTimeout(() => {
cleanCopyIds();
const copyTimeoutId = window.setTimeout(() => {
setCopied(false);
}, 3000);
}, copyVisibleTime);
const copyVisibleTimeoutId = window.setTimeout(() => {
setCopyTootipVisible(false);
}, copyVisibleTime - 50);
copyIdsRef.current.push(copyTimeoutId, copyVisibleTimeoutId);

copyConfig.onCopy?.(e);
};

React.useEffect(() => cleanCopyId, []);
const onCopyMouseOver = () => {
setCopyTootipVisible(true);
};

const onCopyMouseLeft = () => {
setCopyTootipVisible(false);
};

React.useEffect(() => cleanCopyIds, []);

// ========================== Ellipsis ==========================
const [isLineClampSupport, setIsLineClampSupport] = React.useState(false);
Expand Down Expand Up @@ -459,12 +475,16 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
const systemStr = copied ? textLocale.copied : textLocale.copy;
const ariaLabel = typeof copyTitle === 'string' ? copyTitle : systemStr;

const tooltipVisible = copyTooltipVisible && !!copyTitle;

return (
<Tooltip key="copy" title={copyTitle}>
<Tooltip key="copy" title={copyTitle} visible={tooltipVisible}>
<TransButton
className={classNames(`${prefixCls}-copy`, copied && `${prefixCls}-copy-success`)}
onClick={onCopyClick}
aria-label={ariaLabel}
onMouseOver={onCopyMouseOver}
onMouseLeave={onCopyMouseLeft}
>
{copied
? getNode(iconNodes[1], <CheckOutlined />, true)
Expand Down
16 changes: 16 additions & 0 deletions components/typography/__tests__/copy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,21 @@ describe('Typography copy', () => {
expect(spy.mock.calls[0][0]).toEqual(nextText);
jest.useRealTimers();
});

it('tooltip should hide when mouse leave', () => {
const { container } = render(
<Base component="p" copyable>
test copy
</Base>,
);

expect(container.getElementsByTagName('span')).toHaveLength(1);
const button = container.getElementsByTagName('span')[0];

fireEvent.mouseOver(button);
expect(container.querySelector('.ant-tooltip-open')).not.toBeNull();
fireEvent.mouseLeave(button);
expect(container.querySelector('.ant-tooltip-open')).toBeNull();
});
});
});