Skip to content

Commit

Permalink
fix: use a ping pong approach instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jongomez committed Nov 7, 2023
1 parent 31c2c9e commit e5c20a8
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions packages/lsd-react/.storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<script>
function sendHeightToParent() {
const height =
document.documentElement.scrollHeight || document.body.scrollHeight
window.parent.postMessage(
{
type: 'iframeResize',
height: height,
},
'*',
)
}

document.addEventListener('DOMContentLoaded', function () {
// Send initial height.
sendHeightToParent()

// Ensure the interval is set only once.
if (!window.heightIntervalSet) {
window.heightIntervalSet = setInterval(sendHeightToParent, 1000)
// This script is used to send the height of the storybook iframe to the parent window.
window.addEventListener('message', function (event) {
if (event.data && event.data.type === 'requestHeight') {
const height =
document.documentElement.scrollHeight || document.body.scrollHeight
event.source.postMessage(
{
type: 'iframeHeightResponse',
height: height,
},
'*',
)
}
})
</script>

0 comments on commit e5c20a8

Please sign in to comment.