-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Determine if slot is empty #3056
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
Comments
Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server or StackOverflow. You have to check and call the function and look at the returned value with |
It wasn't a question. I know that I have to call the slots.default() function. The problem is that in vue2 the value is "undefined" as expected when the body is empty, but in vue3 the value is filled, even if the body is just a |
@matt-snider FYI: There are cases where vue3 will output a warning that slots are accessed outside the render function. (If you try this approach with the new composition API) |
I think this should do: function isEmptySlot(slot, data)
return slot(data).filter(vnode => vnode.type !== Comment).length < 1
}
isEmptySlot(slots.default, { someData: ... })
isEmptySlot(slots.default) |
@posva Thank you for your response! That is helpful 👍 |
If you want to be sure that the slot is considered empty even if the content is something like export const stripEmpty = (slot, data) => {
return slot(data).filter(vnode => vnode.type !== Comment && (vnode.type !== Text || vnode.children.trim() !== ''));
}; |
What problem does this feature solve?
In vue2 it was possible to test the existence of $scopedSlots.default to check if the slot has content. Even if the slot content was depending on conditions. (e.g.
<b v-if="false">content</b>
)For example in vue2 this was possible:
...
If the data did neither contain property
a
orb
none of the VEntry components are rendered, therefore no VDescriptionList needed to be rendered (which would affect the design and the semantic DOM interpreted by screenreaders).In vue3
this.$slots.default()
(orctx.slots.default()
for functional components) of a content like<b v-if="false">content</b>
is notundefined
(like it was in vue2) but instead is an array filled with one element.I use this feature in several parts of my project to detect if the rendered slot content is actually containing anything.
Currently I can't see any way to migrate my components from 2 to 3 without loosing this feature.
What does the proposed API look like?
Ideally there would be a way to read if a slot does contain anything.
I understand that it is required to execute the slot, because the fact if it's actually filled could be based on slot scoped parameters.
But in case there are no parameters, it seems like a huge waste to execute the code twice (once for detecting the value to see if it contains anything, and a second time to actually render it)
The text was updated successfully, but these errors were encountered: