Forwarding slots to Custom Elements #12875
Unanswered
vonBrax
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I wanted to open an issue, but decided to gather some feedback first - and maybe an acceptable working solution?
I'm developing a Vue component library for internal use in our company. Our library is a simple wrapper around another library that exports Custom Elements. So we wrap the element properties, events and, of course, slots.
But we are currently having a hard time trying to forward Vue component slots to the webcomponent slots. This is more an ergonomic/usability problem than an issue per se.
Reproduction https://stackblitz.com/edit/bolt-vue-5ysf9snf?file=README.md
For example, let's say we have a
custom-dialog
custom element, with a slot for theheader
,body
(default) andfooter
. So we create a similar Vue component:While this solution works, we need to add an extra
div
element wrapping the Vue slot, so that we can set theslot
attribute and it ends up in the right place inside the custom element.The problem with that solution, is that some of the exported custom elements have styles that rely on the DOM structure and our extra
div
break some of these styles.What we ideally would want was something similar to:
... and then every direct "child" of the slot element would receive the
slot
attributeBut that doesn't work. I already been through a lot of similar issues around this, so I understand that the framework's opinion here is that the Component should have no knowledge about what it goes inside the slot, they should be isolated from each other.
While I understand the prerogative, it is also a fact that in the end, both Component and the slotted content will have a parent-child relationship in the DOM tree, and in a way one affects the other, so I don't think that is right to have such a strict level of isolation between them. While not recommended - or considered a best practice - to mess with the contents of a slot, I also think that Vue as a framework that aims to increase web-development speed and productivity, should not make it that difficult if a developer do wants to interact with a slotted content somehow for whatever reason.
I spent days already searching for solutions, reading documentation, reading source code, speaking to AI tools, and trying to develop a working solution to this problem, and although I did manage to do it, it still feels hacky and unsafe in some degree - my solution could in theory lead to an infinite rendering loop.
I've created a small reproduction: https://stackblitz.com/edit/bolt-vue-5ysf9snf?file=README.md
There I show the different approaches I took to find a working solution, the last one (
ParagraphWrapper5.vue
) being the one that works, but it's hacky and potentially dangerous.Apart from what is in the reproduction link, other solutions that we tried are:
MutationObserver
to set theslot
attribute in the slotted elementsref
element to target the slotted contentslot
attribute)In the end my question are:
slot
attribute on every direct descendant of a Vue slot element, preferably in a "Vue-friendly" way, without having to rely on apps consuming the library components to do that?Beta Was this translation helpful? Give feedback.
All reactions