Skip to content

Determine if slot is empty #3056

Closed
Closed
@plehnen

Description

@plehnen

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:

<template functional> <!-- for VDescriptionList component -->
  <dl v-if="$scopedSlots.default">
    <slot />
  </dl>
</template>

...

<VDescriptionList>
  <VEntry :data="data" field="a" optional />
  <VEntry :data="data" field="b" optional />
</VDescriptionList>

If the data did neither contain property a or b 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() (or ctx.slots.default() for functional components) of a content like <b v-if="false">content</b> is not undefined (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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions