Subscribe on changes!

Annotations have an impact on the length of the slot

avatar
Nov 22nd 2023

This problem does not occur in production mode, because Vue compiler will delete comment nodes in production mode.

avatar
Nov 23rd 2023

Comment nodes are allowed as valid child elements. Although they don't affect the rendering of the component's default slot, they might lead to confusion when users check for the presence of the default slot using the useSlots API (related issue #7699).

The issue with inconsistent results in slot.default().length in the provided minimal reproduction is caused by different handling of comment nodes when passed into the default slot of a component in packages/compiler-core/src/transforms/vSlot.ts. This behavior depends on whether <template v-slot> is present or not (in L325, L342):

https://github.com/vuejs/core/blob/1c525f75a3d17a6356d5f66765623c0ae7c0ebcc/packages/compiler-core/src/transforms/vSlot.ts#L323-L344

Deciding on changes to how comment nodes are handled in slots, requires further discussion and work. If you need to check whether there is actual content rendered in the default slot using useSlots, you can use the following workarounds:

  1. Remove comment nodes to avoid interference.
  2. Filter out comment nodes when checking, as below:
<script setup>
import { useSlots, Comment } from 'vue';
const foo = useSlots()
  .default()
  .filter((i) => i.type !== Comment);
</script>

Hope this helps.

avatar
Nov 23rd 2023

Comment nodes are allowed as valid child elements. Although they don't affect the rendering of the component's default slot, they might lead to confusion when users check for the presence of the default slot using the useSlots API (related issue #7699).

The issue with inconsistent results in slot.default().length in the provided minimal reproduction is caused by different handling of comment nodes when passed into the default slot of a component in packages/compiler-core/src/transforms/vSlot.ts. This behavior depends on whether <template v-slot> is present or not (in L325, L342):

https://github.com/vuejs/core/blob/1c525f75a3d17a6356d5f66765623c0ae7c0ebcc/packages/compiler-core/src/transforms/vSlot.ts#L323-L344

Deciding on changes to how comment nodes are handled in slots, requires further discussion and work. If you need to check whether there is actual content rendered in the default slot using useSlots, you can use the following workarounds:

  1. Remove comment nodes to avoid interference.
  2. Filter out comment nodes when checking, as below:
<script setup>
import { useSlots, Comment } from 'vue';
const hasDefaultSlot = useSlots()
  .default()
  .filter((i) => i.type !== Comment);
</script>

Hope this helps.

Weird, not to be exaggerated things, but I need to filter them out