Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Added resize event (#1037)
- Loading branch information
Showing
4 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
export function watchResize({ scope }: { scope: { channelId: string } }) { | ||
let lastHeight = 0; | ||
|
||
function getDocumentHeight(): number { | ||
if (typeof window === "undefined") return 0; | ||
|
||
const { body } = document; | ||
const html = document.documentElement; | ||
|
||
return Math.max(body.scrollHeight, body.offsetHeight, html.offsetHeight); | ||
} | ||
|
||
function sendResizeEvent() { | ||
const height = getDocumentHeight(); | ||
|
||
if (lastHeight !== height) { | ||
window.parent.postMessage( | ||
{ | ||
type: "resize", | ||
height, | ||
codesandbox: true, | ||
channelId: scope.channelId, | ||
}, | ||
"*" | ||
); | ||
} | ||
|
||
lastHeight = height; | ||
} | ||
|
||
sendResizeEvent(); | ||
|
||
let throttle: any; | ||
const observer = new MutationObserver(() => { | ||
if (throttle === undefined) { | ||
sendResizeEvent(); | ||
|
||
throttle = setTimeout(() => { | ||
throttle = undefined; | ||
}, 300); | ||
} | ||
}); | ||
|
||
observer.observe(document, { | ||
attributes: true, | ||
childList: true, | ||
subtree: true, | ||
}); | ||
|
||
/** | ||
* Ideally we should only use a `MutationObserver` to trigger a resize event, | ||
* however, we noted that it's not 100% reliable, so we went for polling strategy as well | ||
*/ | ||
setInterval(sendResizeEvent, 300); | ||
} |
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
61ccf7e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sandpack-docs – ./website/docs
sandpack.vercel.app
sandpack-docs-codesandbox1.vercel.app
sandpack-docs.codesandbox.io
sandpack-docs-git-main-codesandbox1.vercel.app