{"version":3,"file":"render.esm.js","sources":["../../src/utils/render.ts"],"sourcesContent":["import { h, cloneVNode, Slots } from 'vue'\nimport { match } from './match'\n\nexport enum Features {\n /** No features at all */\n None = 0,\n\n /**\n * When used, this will allow us to use one of the render strategies.\n *\n * **The render strategies are:**\n * - **Unmount** _(Will unmount the component.)_\n * - **Hidden** _(Will hide the component using the [hidden] attribute.)_\n */\n RenderStrategy = 1,\n\n /**\n * When used, this will allow the user of our component to be in control. This can be used when\n * you want to transition based on some state.\n */\n Static = 2,\n}\n\nexport enum RenderStrategy {\n Unmount,\n Hidden,\n}\n\nexport function render({\n visible = true,\n features = Features.None,\n ...main\n}: {\n props: Record\n slot: Record\n attrs: Record\n slots: Slots\n name: string\n} & {\n features?: Features\n visible?: boolean\n}) {\n // Visible always render\n if (visible) return _render(main)\n\n if (features & Features.Static) {\n // When the `static` prop is passed as `true`, then the user is in control, thus we don't care about anything else\n if (main.props.static) return _render(main)\n }\n\n if (features & Features.RenderStrategy) {\n let strategy = main.props.unmount ?? true ? RenderStrategy.Unmount : RenderStrategy.Hidden\n\n return match(strategy, {\n [RenderStrategy.Unmount]() {\n return null\n },\n [RenderStrategy.Hidden]() {\n return _render({\n ...main,\n props: { ...main.props, hidden: true, style: { display: 'none' } },\n })\n },\n })\n }\n\n // No features enabled, just render\n return _render(main)\n}\n\nfunction _render({\n props,\n attrs,\n slots,\n slot,\n name,\n}: {\n props: Record\n slot: Record\n attrs: Record\n slots: Slots\n name: string\n}) {\n let { as, ...passThroughProps } = omit(props, ['unmount', 'static'])\n\n let children = slots.default?.(slot)\n\n if (as === 'template') {\n if (Object.keys(passThroughProps).length > 0 || Object.keys(attrs).length > 0) {\n let [firstChild, ...other] = children ?? []\n\n if (!isValidElement(firstChild) || other.length > 0) {\n throw new Error(\n [\n 'Passing props on \"template\"!',\n '',\n `The current component <${name} /> is rendering a \"template\".`,\n `However we need to passthrough the following props:`,\n Object.keys(passThroughProps)\n .concat(Object.keys(attrs))\n .map(line => ` - ${line}`)\n .join('\\n'),\n '',\n 'You can apply a few solutions:',\n [\n 'Add an `as=\"...\"` prop, to ensure that we render an actual element instead of a \"template\".',\n 'Render a single element as the child so that we can forward the props onto that element.',\n ]\n .map(line => ` - ${line}`)\n .join('\\n'),\n ].join('\\n')\n )\n }\n\n return cloneVNode(firstChild, passThroughProps as Record)\n }\n\n if (Array.isArray(children) && children.length === 1) {\n return children[0]\n }\n\n return children\n }\n\n return h(as, passThroughProps, children)\n}\n\nexport function omit>(object: T, keysToOmit: string[] = []) {\n let clone = Object.assign({}, object)\n for (let key of keysToOmit) {\n if (key in clone) delete clone[key]\n }\n return clone\n}\n\nfunction isValidElement(input: any): boolean {\n if (input == null) return false // No children\n if (typeof input.type === 'string') return true // 'div', 'span', ...\n if (typeof input.type === 'object') return true // Other components\n if (typeof input.type === 'function') return true // Built-ins like Transition\n return false // Comments, strings, ...\n}\n"],"names":["Features","RenderStrategy","render","visible","features","None","main","_render","Static","props","strategy","unmount","Unmount","Hidden","match","hidden","style","display","attrs","slots","slot","name","omit","as","passThroughProps","children","Object","keys","length","firstChild","other","isValidElement","Error","concat","map","line","join","cloneVNode","Array","isArray","h","object","keysToOmit","clone","assign","key","input","type"],"mappings":";;;;IAGYA;;AAAZ,WAAYA;AACV;AACAA,EAAAA,8BAAA,SAAA;AAEA;;;;;;;;AAOAA,EAAAA,wCAAA,mBAAA;AAEA;;;;;AAIAA,EAAAA,gCAAA,WAAA;AACD,CAlBD,EAAYA,QAAQ,KAARA,QAAQ,KAAA,CAApB;;IAoBYC;;AAAZ,WAAYA;AACVA,EAAAA,6CAAA,YAAA;AACAA,EAAAA,4CAAA,WAAA;AACD,CAHD,EAAYA,cAAc,KAAdA,cAAc,KAAA,CAA1B;;SAKgBC;0BACdC;MAAAA,oCAAU;2BACVC;MAAAA,sCAAWJ,QAAQ,CAACK;MACjBC;;AAWH;AACA,MAAIH,OAAJ,EAAa,OAAOI,OAAO,CAACD,IAAD,CAAd;;AAEb,MAAIF,QAAQ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