-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshadow.mjs.map
1 lines (1 loc) · 7.06 KB
/
shadow.mjs.map
1
{"version":3,"file":"shadow.mjs","sources":["src/shadow.tsx"],"sourcesContent":["import { defineComponent, h, ref, Teleport, onBeforeMount, onMounted, computed, reactive } from 'vue'\nimport type { App, VNode } from 'vue'\n\ntype GShadowRoot = typeof global.ShadowRoot.prototype\n\nexport function makeShadow(el: Element) {\n return makeShadowRaw(el, el.childNodes)\n}\nexport function makeShadowRaw(rootEl: Element, childNodes?: Iterable<Node>) {\n try {\n const oldroot = rootEl.shadowRoot\n if (oldroot != null) {\n console.error('[shadow] Attach shadow multiple times', rootEl, childNodes, oldroot)\n return\n } else {\n const shadow_root = rootEl.attachShadow({ mode: 'open' })\n if (childNodes) putDomIntoShadow(shadow_root, childNodes)\n return shadow_root\n }\n } catch (e) {\n console.error('[shadow] make shadow-root failed', rootEl, childNodes)\n console.error(e)\n }\n}\n\n// function removeShadow(rootEl: Element): Element {\n// const newroot = rootEl.cloneNode() as Element\n// while (rootEl.hasChildNodes()) {\n// newroot.appendChild(rootEl.firstChild!)\n// }\n// rootEl.parentElement!.replaceChild(newroot, rootEl)\n// console.log('removeShadow', newroot)\n// return newroot\n// }\n\nfunction putDomIntoShadow(shadow_root: GShadowRoot, childNodes: Iterable<Node>) {\n const fragment = document.createDocumentFragment()\n for (const node of childNodes) {\n fragment.appendChild(node)\n }\n shadow_root.appendChild(fragment)\n}\n\nconst virtual_root = document.createDocumentFragment()\n\nexport const ShadowStyle = defineComponent({\n props: {\n media: String,\n nonce: String,\n },\n setup(props, { slots }) {\n return (): VNode => (\n <style media={props.media} nonce={props.nonce}>\n {slots.default?.()}\n </style>\n )\n },\n})\n\nexport interface ShadowRootExpose {\n shadow_root?: GShadowRoot\n}\n\nexport const ShadowRoot = withType<{\n install: typeof install\n Style: typeof ShadowStyle\n}>()(\n defineComponent({\n props: {\n abstract: {\n type: Boolean,\n default: false,\n },\n tag: {\n type: String,\n default: 'div',\n },\n },\n setup(props, { slots, expose }) {\n const abstract = ref(false)\n\n const el = ref<HTMLElement>()\n const teleport_el = ref<HTMLElement>()\n const shadow_root = ref<GShadowRoot>()\n\n const teleport_target = computed(() => shadow_root.value ?? virtual_root)\n\n const ex: ShadowRootExpose = reactive({\n shadow_root,\n })\n expose(ex)\n\n onBeforeMount(() => {\n abstract.value = props.abstract\n })\n\n onMounted(() => {\n if (abstract.value) {\n if (teleport_el.value!.parentElement!.shadowRoot) {\n shadow_root.value = teleport_el.value!.parentElement!.shadowRoot\n } else {\n shadow_root.value = makeShadowRaw(teleport_el.value!.parentElement!)\n }\n } else {\n shadow_root.value = makeShadowRaw(el.value!)\n }\n })\n\n return (): VNode => {\n const child_part = (\n <Teleport ref={teleport_el} to={teleport_target.value}>\n {[slots.default?.()]}\n </Teleport>\n )\n if (abstract.value) return child_part\n return <props.tag ref={el}>{child_part}</props.tag>\n }\n },\n install,\n Style: ShadowStyle,\n })\n)\n\nfunction withType<W>(): <T>(obj: T) => T & W {\n return obj => obj as any\n}\n\nexport function install(app: App) {\n app.component('shadow-root', ShadowRoot)\n\n app.directive('shadow', {\n beforeMount(el: HTMLElement) {\n makeShadow(el)\n },\n })\n}\n\nexport { ShadowRoot as shadow_root, ShadowStyle as shadow_style }\nexport default { ShadowRoot, ShadowStyle, shadow_root: ShadowRoot, shadow_style: ShadowStyle, install }\n"],"names":[],"mappings":";;SAKgB,UAAU,CAAC,EAAW;IAClC,OAAO,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,CAAA;AAC3C,CAAC;SACe,aAAa,CAAC,MAAe,EAAE,UAA2B;IACtE,IAAI;QACA,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAA;QACjC,IAAI,OAAO,IAAI,IAAI,EAAE;YACjB,OAAO,CAAC,KAAK,CAAC,uCAAuC,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;YACnF,OAAM;SACT;aAAM;YACH,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