Created
July 1, 2020 11:06
-
-
Save BetterProgramming/cd161c1a720505a05d2e525a3c133b33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
const Iframe = (props) => { | |
let iframe_ref = null; | |
const writeHTML = (frame) => { | |
if (!frame) { | |
return; | |
} | |
iframe_ref = frame; | |
let doc = frame.contentDocument; | |
doc.open(); | |
doc.write(props.content); | |
doc.close(); | |
frame.style.width = '100%'; | |
frame.style.height = `${frame.contentWindow.document.body.scrollHeight}px`; | |
}; | |
return ( | |
<iframe src="about:blank" scrolling="no" frameBorder="0" ref{writeHTML} | |
/> | |
); | |
}; | |
export default Iframe; |
i mean frame.contentDocument.documentElement.scrollHeight instead of frame.contentDocument.body.scrollHeight
@Andrii256: could you post the updated script with the changes you proposed?
I agree: frame.contentDocument.documentElement.scrollHeight
seems to be better.
I get error with frame.contentDocument.body.scrollHeight
Also, how to handle changes in the height of the content? E.g. if there are items that collapse and expand to be displayed within the Iframe?
Simply loading it during onload doesn't suffice.
I dint try it so I'm not sure that it will help with Iframe, but it looks like there resizeObserver API. Possibly it can halp:) https://stackoverflow.com/questions/14866775/detect-document-height-change
nice work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and even better use not body.scrollHeight, but html.scrollHeight