What are the drawbacks of using Shadow DOM?

前端 未结 2 1298
余生分开走
余生分开走 2021-01-18 02:48

In the tutorials I see only the benefits of Shadow DOM, but there should be drawbacks as well. In which cases should we avoid using Shadow DOM?

2条回答
  •  北海茫月
    2021-01-18 03:09

    Shadow DOM features can be seen as drawback as much as benefits:

    Style isolation is a benefit if you want it but a drawback if a user wants to style a component with Shadow DOM from a global CSS stylesheet.

    DOM Shadowing is a benefit in some cases, but a drawback if an external script/library or extension needs to parse or select the content.

    There are many 3rd party libraries (or extensions) that won't work with Shadow DOM content because they were not designed to deal with it, or need some additional configuration to work with Shadow DOM.

    Examples:

    • Disqus comments integration
    • CodeMirror editor integration
    • Wiris math editor integration

    Also, extensions that parse HTML will be blocked at the Shadow DOM boundary: a benefit if you don't want to spied, a drawback if you consider them as a useful service.

    Event propagation is different inside and outside the Shadow DOM. So you may have some diffuclties to deal with UI events.

    Example :

    • TinyMCE integration

    Conclusion

    • Use Shadow DOM only if you want CSS style or DOM isolation.

    • Don't use Shadow DOM if you need to interact with some not compliant third party components or library.

提交回复
热议问题