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