mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 20:19:13 +00:00
Co-authored-by: Lindsay M <126139086+lindsaym-fa@users.noreply.github.com> Co-authored-by: lindsaym-fa <dev@lindsaym.design>
38 lines
1007 B
JavaScript
38 lines
1007 B
JavaScript
/**
|
|
* Sync iframe height with its content page (for same-origin iframes)
|
|
* NOT CURRENTLY USED ANYWHERE
|
|
*/
|
|
for (let iframe of document.querySelectorAll('iframe')) {
|
|
if (iframe.contentDocument) {
|
|
// Already loaded
|
|
syncIframeHeight(iframe);
|
|
}
|
|
|
|
iframe.onload = () => {
|
|
console.log('iframe loaded');
|
|
if (iframe.contentDocument) {
|
|
// Same origin
|
|
iframe.contentWindow.iframe = iframe;
|
|
syncIframeHeight(iframe);
|
|
const resizeObserver = new ResizeObserver(entries => {
|
|
for (let entry of entries) {
|
|
if (entry.target === iframe.contentDocument.body) {
|
|
syncIframeHeight(iframe);
|
|
}
|
|
}
|
|
});
|
|
|
|
resizeObserver.observe(iframe.contentDocument.body);
|
|
window.addEventListener('turbo:render', syncIframeHeight(iframe));
|
|
}
|
|
};
|
|
}
|
|
|
|
function syncIframeHeight(iframe) {
|
|
iframe.style.height = '0px';
|
|
|
|
requestAnimationFrame(() => {
|
|
iframe.style.height = iframe.contentDocument.body.scrollHeight + 'px';
|
|
});
|
|
}
|