1

Bit of a tricky one and a unique use case.

I have a Vue 3 app, developing and building using Vite. i am instantiating a second Vue 3 app instance inside an iframe, relevant code pasted below. As i need unpolluted components and styles available for the second app in the iframe.


import { h, ref, createApp, onMounted, onBeforeUnmount, onBeforeUpdate } from "vue"

export default {
  setup(props, { attrs, slots, emit, expose }) {
  
        const iframeRef = ref(null)
        const iframeBody = ref(null)
        const iframeHead = ref(null)
        const iframeStyle = ref(null)
        let app = null
        let emitSelection;
   
onMounted(() => {
  iframeBody.value = iframeRef.value.contentDocument.body
  iframeHead.value = iframeRef.value.contentDocument.head
  const el = document.createElement("div")
  iframeBody.value.appendChild(el)
  app = createApp(MySecondAppLayout, props)
  app.use(MyUILibrary);
  app.mount(el)
})

return () => h("iframe", { ref: iframeRef })


}


Everything is working great, the second app instance imports it's own components and mount and behave correctly within the iframe. However, the SCSS/CSS styles are not included within the iframe context.

Is what i am attempting to do impossible? Or is there a tag i can inject into the iframe to include the styles from the imported components?

Alternatively should i/can i get vite output the style sheets from all of the single file components as a text file so i can paste it in manually at the top of the iframe?

Cade Embery
  • 433
  • 1
  • 5
  • 18
  • If you try to style the iframe from outside the iframe i don't think there is a way to achieve that. Maybe this link can help: https://stackoverflow.com/questions/6494721/override-body-style-for-content-in-an-iframe – h0p3zZ Apr 25 '22 at 13:13

1 Answers1

0

I was able to repackage my component library using vite, and then using this plugin: https://www.npmjs.com/package/vite-plugin-libcss i could import the css manually into the iframe.

Cade Embery
  • 433
  • 1
  • 5
  • 18