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

Tooltip 组件隐藏后,希望能删除 DOM 节点 #19536

Closed
1 task done
hudidit opened this issue Nov 3, 2019 · 13 comments · Fixed by react-component/tooltip#216 or #24341
Closed
1 task done

Tooltip 组件隐藏后,希望能删除 DOM 节点 #19536

hudidit opened this issue Nov 3, 2019 · 13 comments · Fixed by react-component/tooltip#216 or #24341
Assignees
Labels
💡 Feature Request help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request. Inactive

Comments

@hudidit
Copy link

hudidit commented Nov 3, 2019

  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

现状

Tooltip 展示时会向 DOM 中插入一个 div 节点,以及一些子节点。但是隐藏时并没有删除这个节点。当页面中有很多个 Tooltip 实例时,每展示一个就会向 DOM 中插入一组节点,越来越多。
如图所示:

预期

希望 Tooltip 在隐藏后,能够删除对应的 DOM 节点。或者,提供一个配置项,支持用户选择是否要清理 DOM 节点。
好处:

  1. 页面 DOM 结构更干净。
  2. 尽管多数时候可能影响不大,但是回收 DOM 节点,减少内存占用,是一种比较常见的最佳实践。

What does the proposed API look like?

参考 Ant Modal 组件的 destroyOnClose,可以增加一个 destroyOnHide 参数。
e.g.

<Tooltip title="hover text" destroyOnHide>text</Tooltip>
@afc163
Copy link
Member

afc163 commented Nov 3, 2019

有一个 destroyTooltipOnHide

https://github.com/react-component/tooltip

@afc163 afc163 closed this as completed Nov 3, 2019
@Arthur-Conan-Dog
Copy link

有一个 destroyTooltipOnHide

https://github.com/react-component/tooltip

Hi there,

I think destroyTooltipOnHide does not quite solve @hudidit 's problem, since only the tooltip div inside will be removed after mouse leave. eg:

Hovering:

<div style="position: absolute; top: 0px; left: 0px; width: 100%;">
  <div>
    <div
      class="rc-tooltip rc-tooltip-placement-right "
      style="left: 737px; top: 28px;"
    >
      <div class="rc-tooltip-content">
        <div class="rc-tooltip-arrow" />
        <div class="rc-tooltip-inner" role="tooltip">
          <span>powered by rc-tooltip</span>
        </div>
      </div>
    </div>
  </div>
</div>

Mouse leave:

<div style="position: absolute; top: 0px; left: 0px; width: 100%;">
  <div></div>
</div>

This strange div container stays.

After a little deep dive, I found that this behavior is caused by the usage of rc-trigger, which gives a default div container for Portal component.

Check this: https://github.com/react-component/trigger/blob/2.2.2/src/index.js#L585
and https://github.com/react-component/trigger/blob/2.2.2/src/index.js#L394

Also checked the lastest working on version 4.x.x-alpha of rc-trigger, this problem still exists.

It's better if we could also clean up the appended child, say during componentWillUnmount.

@yoyo837
Copy link
Contributor

yoyo837 commented Nov 4, 2019

@Arthur-Conan-Dog I think you are right.

@afc163 afc163 reopened this Nov 4, 2019
@shaodahong
Copy link
Member

thanks, PortalWrapper has autoDestroy prop, but we didn't use it, maybe we can add autoDestroy prop

https://github.com/react-component/util/blob/fb03ea5db96a7c6ef58387f0df9c181ecc0fcc05/src/PortalWrapper.js#L128

@shaodahong shaodahong added 💡 Feature Request help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request. labels Nov 6, 2019
@ant-design-bot
Copy link
Contributor

Hello @hudidit. We totally like your proposal/feedback, welcome to send us Pull Request for it. Please send your Pull Request to proper branch (feature branch for new feature, master for bugfix and other changes), fill the [Pull Request Template] here, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. Appreciate it advance and we are looking forword to your contribution!

你好 @hudidit, 我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到正确的分支(新特性发到 feature 分支,其他发到 master 分支),务必填写 Pull Request 内的预设模板,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待您的贡献!

giphy

@kerm1it
Copy link
Member

kerm1it commented May 16, 2020

#178trigger添加了autoDestroy属性。

@kerm1it
Copy link
Member

kerm1it commented May 19, 2020

@afc163 看看tooltip的那个pr,没问题就可以合了。

@zombieJ
Copy link
Member

zombieJ commented May 19, 2020

感觉应该改进 destroyTooltipOnHide,而不是添加一个 autoDestroy

@kerm1it
Copy link
Member

kerm1it commented May 19, 2020

这样可以的,主要加是为了做兼容,你说的这种方法更好。

@zombieJ
Copy link
Member

zombieJ commented May 19, 2020

可以加到 minor version 里

@kerm1it
Copy link
Member

kerm1it commented May 20, 2020

@hudidit 最终没有增加autoDestroy, 而是在原来的destroyTooltipOnHide 增加了{ keepParent?: boolean }类型,同时兼容原来的boolean类型,只有当keepParent等于false才会删除父元素,后期没有问题后会可能会直接取消兼容直接删除父元素。

@afc163
Copy link
Member

afc163 commented May 21, 2020

antd 还要升级一下。

@yoyo837
Copy link
Contributor

yoyo837 commented May 21, 2020

需要reopen吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💡 Feature Request help wanted The suggestion or request has been accepted, we need you to help us by sending a pull request. Inactive
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants