Is there a way to make an iframe reactively render with state/signals from its parent?

Stuff that I tried: - Set state/setState in the iframe.window.someGlobal and access it in the iframe from there - Wrap createState in createRoot - Render the iframe solid app with runWithOwner with the owner of the parent app I know there are alternatives, but each of them have drawbacks: a. Send state changes between parent/iframe and update state with reconcile (Drawback: Performance) b. Render the iframe content directly in the parent (Drawback: CSS inconsistency, certain libraries with document side effects break, event delegation has to be manually registered) Currently I use the a.) alternative, but I'd really like to get rid of the state reconcilation somehow 🙈
8 Replies
bigmistqke
bigmistqke3y ago
what about window.postMessage?
Katja (katywings)
Jupp thats what I do in a.), I currently use postMessage and reconcile the state changes between parent<->iframe 😅
bigmistqke
bigmistqke3y ago
coool
Katja (katywings)
Ohh veerrry interesting 😮! Thank you for the demo 👍! Sadly in my case both sides need to work with "generic" solid components properly importing their dependencies. But atleast now i know that its definitely a tradeoff in the solid core, and not a problem with iframe security 🙂.
quick-vite
quick-vite2w ago
I know this was 3 years ago. But, on the off chance you still need this. I've been playing around with this idea for a library too, and, this component seems to work pretty consistently: https://github.com/solid-medley/camera-context/blob/cc48c3260ae5c355d57bdc5d68805cd84f443da0/lib/src/components/component-frame.tsx It creates a new root, and attaches it to the iframe's body element. You can use it like this:
const Example: Component = () => {

return <ComponentFrame
allow="fullscreen"
sandbox="allow-same-origin allow-scripts"
>
<YourGenericComponentsHere />
</ComponentFrame>
}
const Example: Component = () => {

return <ComponentFrame
allow="fullscreen"
sandbox="allow-same-origin allow-scripts"
>
<YourGenericComponentsHere />
</ComponentFrame>
}
Hope it helps. If you're using the sandbox property, it's important to use both allow-same-origin allow-scripts in it. Because, the allow-scripts runs solid, and, the allow-same-origin is necessary to access the document inside of the iframe.
Katja (katywings)
Thank you for sharing your solution 🙂❤️! Looks great for some use cases 👍. I also came up with something a couple months ago. Though it is a bit different in that it uses a second Solid instance in the iframe. If you are curious, I wrote an article about it back then: https://nitropage.org/blog/nitropage-v0-68#reworked-editor-state-synchronization
quick-vite
quick-vite2w ago
I think your solution is probably better, I've been tinkering and mine just renders the components in the frame but the solid context is in the parent document. I guess it's useful for style separation, but you can also do that with a Portal set to use shadow dom. I'll have a closer look at your article, thanks!

Did you find this page helpful?