import { Comment, type ComputedRef, Fragment, type MaybeRefOrGetter, Text, type VNode, computed, getCurrentInstance, isVNode, onMounted, toValue, useSlots } from 'vue' export type WhenCondition = MaybeRefOrGetter export function computedWhen( condition: WhenCondition, fn: (item: NonNullable) => T ): ComputedRef export function computedWhen( condition: WhenCondition, fn: (item: NonNullable) => T, whenFalse: D ): ComputedRef export function computedWhen( condition: WhenCondition, fn: (item: NonNullable) => T, whenFalse?: D ): ComputedRef { return computed(() => { const c = toValue(condition) return c ? fn(c) : (whenFalse as D) }) } export function computedArray(fn: (arr: T[]) => void): ComputedRef { return computed(() => { const arr: T[] = [] fn(arr) return arr }) } export function computedArrayWhen( condition: WhenCondition, fn: (arr: T[], item: NonNullable) => void ): ComputedRef { return computedWhen(condition, (c) => { const arr: T[] = [] fn(arr, c) return arr }, []) } function hasSlotContent(value: unknown): boolean { if (value == null || typeof value === 'boolean') { return false } if (typeof value === 'string') { return value.trim().length > 0 } if (typeof value === 'number' || typeof value === 'bigint') { return true } if (Array.isArray(value)) { return value.some(hasSlotContent) } if (!isVNode(value)) { return false } const vnode = value as VNode if (vnode.type === Comment) { return false } if (vnode.type === Text || vnode.type === Fragment) { return hasSlotContent(vnode.children) } // An element with only text children is judged by that text — // `
{{ maybeEmpty }}
` must not defeat SDesc's empty fallback. // Anything else is content in itself: a component, or an element whose // `children` is `null` (an icon, an ``, …), an array, or a slots object. if (typeof vnode.children === 'string') { return hasSlotContent(vnode.children) } return true } export function useHasSlotContent(name = 'default'): ComputedRef { const slots = useSlots() return computed(() => hasSlotContent(slots[name]?.())) } /** * Get the slot value. If the slot contains child nodes, it will get ignored * and treated as if it was empty. This composable is useful to get the plain * text out of the slot content. */ export function useSlotValue(name = 'default'): ComputedRef { const slots = useSlots() return computed(() => { const c = slots[name]?.()[0]?.children const v = typeof c === 'string' ? c.trim() : null return v !== '' ? v : null }) } export function tryOnMounted(fn: () => void): void { if (getCurrentInstance()) { onMounted(fn) } else if (!import.meta.env.SSR) { fn() } }