Injecting styles into new windows opened via window.open
See original GitHub issueemotion
version: 9.2.5react
version: 16.8.2
Relevant code:
const popupWindow = window.open("", target, openOptionsToString(options));
popupWindowRef.current = popupWindow;
// I need to do this because my styles aren't injected into `popupWindow`.
popupWindow.document.head.innerHTML = window.document.head.innerHTML;
React.createPortal(props.children, popupWindowRef.current.document.body)
What you did: Given a component and the snippet above,
<ShowInNewWindow>
hello world
</ShowInNewWindow
The “hello world” rendered in the new window is rendered without any styling whatsoever, even though the page that triggered the new window has styles added to its <head>
.
Full reproduction on CodeSandbox
Expected The contents of the new window receive the same styling as the source window.
What I find particularly interesting is that the components on the new window have computed className
s – so that part works. It’s just that there are no styles injected into <head>
that is throwing me off.
cc @stereobooster who showed me this issue
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Open a new javascript window(.open) along with its CSS styling
Open a new javascript window(.open) along with its CSS styling ; <script> ; function printContent() { window.open().document.write($("#content").html()); window.
Read more >Window.open() - Web APIs | MDN
The open() method of the Window interface loads a specified resource into a new or existing browsing context (that is, a tab, a...
Read more >Why let someone know when a link opens a new window?
What I recommend as a solution to informing people that a link opens a new window is, in my opinion, much cleaner and...
Read more >Popups and window methods - The Modern JavaScript Tutorial
A popup can be opened by the open(url, name, params) call. It returns the reference to the newly opened window. Browsers block open...
Read more >Understanding the JavaScript Window Object - SitePoint
These properties and methods are made available through the window object. Every browser window, tab, popup, frame, and iframe has a window ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m also having difficulty styling components in a window opened with
window.open
. I forked @RPDeshaies’ sandbox and added the CacheProvider, but I’m still unable to style anything in the external window. I’m creating the cache w/createCache({key: "my-cache", container: externalWindow.document.head})
, and passing that cache as thevalue
for theCacheProvider
component. Is this the intended API usage?https://codesandbox.io/s/elastic-visvesvaraya-ucypu
If you set up a CodeSandbox that would do what you want, just without styling, then I could probably showcase this to you.