使用事件冒泡在 Web 组件上下文中的时机是什么?

huangapple go评论57阅读模式
英文:

When to use event bubbling in the context of web components

问题

我理解事件冒泡的技术用途(实现方式)。我不能得到一个很好的答案的是何时使用事件冒泡,尤其是在 web 组件的上下文中。

通常情况下,一些默认浏览器事件会冒泡,如点击事件,这是有道理的,因为子元素的点击也是父元素的点击。

对于一些只适用于子元素的特定事件,不让事件冒泡以使 API 更清晰是有道理的,因为冒泡的事件成为包含组件的 API 的一部分(?)。

例如具有事件冒泡的元素结构: https://lit.dev/playground/#gist=2554194ca19abd3915cf6b4779189f6c

也许冒泡的优点:

  • 在更高级别附加事件处理程序时代码更少
  • 对于 API 使用者来说不太清晰(除非有正确的文档说明),因为冒泡上来的事件会成为包含元素的 API 的一部分
  • ...

缺点:

  • 需要从包含元素重新分派事件,当需要在更高级别时
  • 如果文档没有正确记录并且未检查事件的来源,API 可能会变得模糊
  • ...

所以我的问题是:

  • 对我来说,我想尽可能使 API 显式,所以不冒泡,还有其他意见/想法吗?

  • 我在这里阅读了一些更多信息(https://open-wc.org/guides/knowledge/events/),但是否有更多的最佳实践或需要考虑的事项?

英文:

I understand the technical use of event bubbling (the how). What I cannot get a good answer to is WHEN to use event bubbling and especially within the context of web components.

In general some default browser events bubble as the click events which makes sense as a click on a child is also a click on the containing parent.

For some specific events which only apply to child elements it makes sense not to let events bubble to make an API more clear as the bubbled events become part of the API of the containing component (?)

For example element structure with event bubbling: https://lit.dev/playground/#gist=2554194ca19abd3915cf6b4779189f6c

Maybe pros of bubbling:

  • less code for attaching event handlers on higher levels
  • less clear (unless correctly documented) to the API consumer as events that bubble up become part of the API of the containing element
  • ...

cons:

  • events must be re-dispatched from containing elements when needed on a higher level
  • API can become fuzzy when not correctly documented and events not checked for the source of the event.
  • ...

So my questions are:

  • For me I would like to make an API as explicit as possible so no bubbling, any more opinition/thoughts?

  • I read some more info here (https://open-wc.org/guides/knowledge/events/), but are there more best practices or things to consider?

答案1

得分: 1

你列出了几乎所有的利弊。从我的经验来看,非常小心地、有目的地使用事件冒泡。它会很快失控。举个例子。

假设你给下拉菜单元素添加了一个冒泡的 close 事件。这很常见。你这样做是为了通知外部世界该元素已关闭。甚至可能在父类中定义以供通用使用。一切都很顺利,直到你将该元素放入自定义对话框中。事实证明,对话框在处理 close 事件时关闭了遮罩层。由于下拉菜单分派了此事件,因此当关闭下拉菜单时,对话框也会关闭。

这是使用冒泡事件的意外后果的一个例子。这可能是一个容易修复的错误,但当你的应用程序增长并且组件基础增长时,要进行这些更改就变得更加困难。

我最近编写了一个UI组件库,并决定除非确实需要向整个DOM通信,否则所有事件都不进行冒泡。这几乎从不会发生。事件重新定位不是那么大的问题,但通过这样做,你可以节省时间,以解决与冒泡事件不起作用的边缘情况相关的问题。

英文:

You listed pretty much all pros and cons. From my experience, use bubbling very carefully and purposely. It will get out of hands very quickly. To give you an example.

Say you add a bubbling close event to a dropdown element. Fairly common.You do this to inform the outside world that the element was closed. It might even be defined in a parent class for general use. Everything works fine until you put this element in a custom dialog. As it turns out the dialog closes the overlay when it handle the close event. Since the dropdown dispatches this event, the dialog will close when the dropdown is closed.

It's an example of unintended consequence of using bubbling events. It might be a mistake easy to fix but when your application grow and your components base grow it so much harder to make these changes.

I recently wrote a library of UI components and I decided that all events are non-bubbling unless they really have to communicate something to the whole DOM. Which is almost never. Events retargeting is not as much of a problem but by doing that you saves yourself time fixing problems related to edge cases where bubbling doesn't work.

huangapple
  • 本文由 发表于 2023年3月21日 00:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定