UNPKG

1.25 kBJavaScriptView Raw
1import Vue from 'vue';
2
3// Fragment will be available only in Vue.js 3
4const { Fragment, Comment } = Vue;
5
6function callIfNeeded(fnOrResult, args) {
7 return fnOrResult instanceof Function ? fnOrResult(args) : fnOrResult;
8}
9
10function isEmpty(vnode) {
11 if (!vnode || (Comment && vnode.type === Comment)) {
12 return true;
13 }
14
15 if (Array.isArray(vnode)) {
16 // eslint-disable-next-line unicorn/no-array-callback-reference
17 return vnode.every(isEmpty);
18 }
19
20 if (Fragment && vnode.type === Fragment) {
21 // Vue.js 3 fragment, check children
22 // eslint-disable-next-line unicorn/no-array-callback-reference
23 return vnode.children.every(isEmpty);
24 }
25
26 return false;
27}
28
29export function isSlotEmpty(vueInstance, slot, slotArgs) {
30 const isVue3 = Boolean(Fragment);
31
32 const slotContent = isVue3
33 ? // we need to check both $slots and $scopedSlots due to https://github.com/vuejs/core/issues/8869
34 // additionally, in @vue/compat $slot might be a function instead of array of vnodes (sigh)
35 callIfNeeded(vueInstance.$slots[slot] || vueInstance.$scopedSlots[slot], slotArgs)
36 : vueInstance.$scopedSlots[slot]?.(slotArgs);
37
38 // eslint-disable-next-line unicorn/no-array-callback-reference
39 return isEmpty(slotContent);
40}