{"version":3,"file":"only-child.mjs","sources":["../../../../../../packages/components/slot/src/only-child.tsx"],"sourcesContent":["import {\n  cloneVNode,\n  Comment,\n  defineComponent,\n  Fragment,\n  Text,\n  withDirectives,\n  inject,\n} from 'vue'\nimport { NOOP, isObject } from '@vue/shared'\nimport { FORWARD_REF_INJECTION_KEY, useForwardRefDirective } from '@hongluan-ui/hooks'\nimport { debugWarn } from '@hongluan-ui/utils'\n\nimport type { VNode, Ref } from 'vue'\n\nconst NAME = 'OnlyChild'\n\nexport const OnlyChild = defineComponent({\n  name: NAME,\n  inheritAttrs: false,\n  setup(_, { slots, attrs }) {\n    const forwardRefInjection = inject(FORWARD_REF_INJECTION_KEY)\n    const forwardRefDirective = useForwardRefDirective(\n      forwardRefInjection?.setForwardRef ?? NOOP\n    )\n    return () => {\n      const defaultSlot = slots.default?.(attrs)\n      if (!defaultSlot) return null\n\n      if (defaultSlot.length > 1) {\n        debugWarn(NAME, 'requires exact only one valid child.')\n        return null\n      }\n\n      const firstLegitNode = findFirstLegitChild(defaultSlot)\n      if (!firstLegitNode) {\n        debugWarn(NAME, 'no valid child node found')\n        return null\n      }\n\n      return withDirectives(cloneVNode(firstLegitNode!, attrs), [\n        [forwardRefDirective],\n      ])\n    }\n  },\n})\n\nfunction findFirstLegitChild(node: VNode[] | undefined): VNode | null {\n  if (!node) return null\n  const children = node as VNode[]\n  for (const child of children) {\n    /**\n     * when user uses h(Fragment, [text]) to render plain string,\n     * this switch case just cannot handle, when the value is primitives\n     * we should just return the wrapped string\n     */\n    if (isObject(child)) {\n      switch (child.type) {\n        case Comment:\n          continue\n        case Text:\n        case 'svg':\n          return wrapTextContent(child)\n        case Fragment:\n          return findFirstLegitChild(child.children as VNode[])\n        default:\n          return child\n      }\n    }\n    return wrapTextContent(child)\n  }\n  return null\n}\n\nfunction wrapTextContent(s: string | VNode) {\n  return <span class=\"only-child-content\">{s}</span>\n}\n\nexport type OnlyChildExpose = {\n  forwardRef: Ref<HTMLElement>\n}\n"],"names":["NAME","OnlyChild","defineComponent","name","inheritAttrs","setup","slots","attrs","forwardRefInjection","inject","FORWARD_REF_INJECTION_KEY","forwardRefDirective","useForwardRefDirective","debugWarn","firstLegitNode","findFirstLegitChild","defaultSlot","node","isObject"],"mappings":";;;;;;;AAeA,MAAMA,IAAI,GAAG,WAAb;MAEaC,SAAS,GAAGC,eAAe,CAAC;AACvCC,EAAAA,IAAI,EAAEH,IADiC;AAEvCI,EAAAA,YAAY,EAAE,KAFyB;;AAGvCC,SAAK;AAAMC,IAAAA,KAAF;AAASC;AAAT;AACP,UAAMC,mBAAmB,GAAGC,MAAM,CAACC,yBAAD,CAAlC;AACA,UAAMC,mBAAmB,GAAGC,sBAAsB,CAChDJ,0CADgD;AAGlD,WAAO,MAAM;AACX;AACA;;AAEA;AACEK,sBAAS;AACT;AACD;;AAED,YAAMC,cAAc,GAAGC,mBAAmB,CAACC,WAAD,CAA1C;;AACA;AACEH,QAAAA;AACA;AACD;;AAED;AAGD;AACF;;AA3BsC;;AA8BzC;AACE;AACA,mBAAiBI;;AACjB;AACE;AACJ;AACA;AACA;AACA;AACI,QAAIC;AACF;AACE;AACE;;AACF;AACA,aAAK;AACH;;AACF,oBAAA;AACE;;AACF;;;;"}