Iframe issue #499
-
Hey, we ran into an issue, some of our components is in an iframe and when they are using twin.macro their design is not shown since it is inside the parent document head rather in the iframe head. The first Is there a way to tell twin.macro that it should put the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hey, Interesting problem you've found there. Twin only converts the tailwind classes into styles and it's the css-in-js framework (eg: emotion/stitches) that takes care of where the styles are placed. So I'd suggest having a search of their issues section for any iframe workarounds. Please let me know you go on this, I'm keen to hear how you solve it. |
Beta Was this translation helpful? Give feedback.
-
@ben-rogerson Hi! We're encountering a similar problem trying to use Netlify CMS, which has a preview pane in an iframe. They have a very long-running (3 yrs +!) issue about trying to support jss libs, in which I asked about Emotion 11 support but they say that only v10 is supported: decaporg/decap-cms#793 (comment) (only about 3-4 replies above that are useful). I'm confused as to why I can't make it work with the latest twin and emotion though, since I'm able to get the active iframe element and its head element. The CMS might add a portal (I tried adding one but it just made content disappear), I am able to render a text div with a twin data tag on it, although the style isn't currently transferred to the iframe. I'm then using this HOC to apply the emotion cache: /* eslint-disable react/display-name */
import createCache from '@emotion/cache';
import { CacheProvider } from '@emotion/core';
import weakMemoize from '@emotion/weak-memoize';
import React from 'react';
import { GlobalStyles } from '~/styles/GlobalStyles';
const memoizedCreateCacheWithContainer = weakMemoize(
(container: HTMLHeadElement) => ({
...createCache({ key: 'cms-emotion-cache', container }),
stylis: (key: string, value: string) => [],
}),
);
export default (Component) => (props) => {
// asserting since we know this element exists in the CMS
const iframe: HTMLIFrameElement = document.querySelector('#preview-pane')!;
const head = iframe.contentDocument!.head;
const cache = memoizedCreateCacheWithContainer(head);
return (
<CacheProvider value={cache}>
<GlobalStyles />
<Component {...props} />
</CacheProvider>
);
}; The child is a div with a color style on it, which you do see getting created, albeit without the custom cache prefix of Do you have any idea how we might be able to get Twin to inject styles onto this iframe head? I know this is outside your domain but any ideas? Feel free to contact me via my Github email if you have time to consult with us privately -- this is some difficult engineering territory but we're eager for any kind of solution! Thanks again Ben |
Beta Was this translation helpful? Give feedback.
Hey,
Interesting problem you've found there.
Twin only converts the tailwind classes into styles and it's the css-in-js framework (eg: emotion/stitches) that takes care of where the styles are placed.
So I'd suggest having a search of their issues section for any iframe workarounds.
Please let me know you go on this, I'm keen to hear how you solve it.