Accessing a slot's actual children programatically #9414
Unanswered
theoephraim
asked this question in
Help/Questions
Replies: 1 comment 8 replies
-
Can you provide a minimum reproduction such as a playground or git repository? such as use playground. |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building some kind of weird layout utility components (these are functional render fn only components) and have run into some tricky situations...
Basically I'm trying to access child elements within a slot so that I can do things like wrap each child in a div, insert extra divs between elements, count the number of children, etc... Some of the use cases I need can be solved with css only, but let's forget about that for a second and just assume this could be a useful pattern in some scenarios.
While I can access a slot's direct children (
const children = slot.default?.() || []
), in order to actually get the children elements, I need to:v-if
v-for
) and templatesMost of this is doable and seems to work fine, however I've run into two issues:
Issue 1 - I have no way of detecting a component that has a root node hidden by
v-if
(ex:<HiddenThing />
which internally is<div v-if="false">...
)Issue 2 - in some very specific scenarios, the children are not always re-rendering properly (the vnode still points to the previous version of the component), but only in the built version of my app?! (ie works in
vite dev
but not invite build
). I've been unable to create a minimal reproduction but was able to fix the issue by wrapping the child in an additionalFragment
. This seems like a deep vue internals bug, but I can't quite track down what is happening.reproduction here:
https://stackblitz.com/~/github.com/theoephraim/vue-functional-rendering-test
As I mentioned I've been unable to get issue 2 to reproduce in this minimal environment - but this is why theres a lot of extra stuff in there (pinia, api requests, etc). I've commented out the extra Fragment fix for now but it's there.
Beta Was this translation helpful? Give feedback.
All reactions