UNPKG

@vuesax-alpha/nightly

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