UNPKG

@vuesax-alpha/nightly

Version:
72 lines (69 loc) 2.11 kB
import { defineComponent, inject, withDirectives, cloneVNode, Fragment, Text, Comment, h } from 'vue'; import { NOOP, isObject } from '@vue/shared'; import '../../../hooks/index.mjs'; import '../../../utils/index.mjs'; import { FORWARD_REF_INJECTION_KEY, useForwardRefDirective } from '../../../hooks/use-forward-ref/index.mjs'; import { debugWarn } from '../../../utils/error.mjs'; import { useNamespace } from '../../../hooks/use-namespace/index.mjs'; const NAME = "VsOnlyChild"; const OnlyChild = defineComponent({ name: NAME, setup(_, { slots, attrs }) { return () => { var _a, _b; const forwardRefInjection = inject(FORWARD_REF_INJECTION_KEY, void 0); const forwardRefDirective = useForwardRefDirective( (_a = forwardRefInjection == null ? void 0 : forwardRefInjection.setForwardRef) != null ? _a : NOOP ); const defaultSlot = (_b = slots.default) == null ? void 0 : _b.call(slots, attrs); if (!defaultSlot) return null; if (defaultSlot.length > 1) { debugWarn(NAME, "requires exact only one valid child."); return null; } const firstLegitNode = findFirstLegitChild(defaultSlot); if (!firstLegitNode) { debugWarn(NAME, "no valid child node found"); return null; } return withDirectives(cloneVNode(firstLegitNode), [ [forwardRefDirective] ]); }; } }); function findFirstLegitChild(node) { if (!node) return null; const children = node; for (const child of children) { if (isObject(child)) { switch (child.type) { case Comment: continue; case Text: case "svg": return wrapTextContent(child); case Fragment: return findFirstLegitChild(child.children); default: return child; } } return wrapTextContent(child); } return null; } function wrapTextContent(s) { const ns = useNamespace("only-child"); return h( "span", { className: ns.e("content") }, s ); } export { OnlyChild }; //# sourceMappingURL=only-child.mjs.map