use-iframe-height.tsx

src/hooks/use-iframe-height.tsx
'use client'
import { useEffect } from 'react'
// ----------------------------------------------------------------------
const IframeHeight = () => {
useEffect(() => {
// Only run if inside an iframe
if (window.self === window.top) {
return
}
const sendHeight = () => {
const height = document.body.scrollHeight
window.parent.postMessage(
{
type: 'height',
value: height,
},
'*'
)
}
// Send initial height
sendHeight()
// Watch for resizes
const resizeObserver = new ResizeObserver(() => {
sendHeight()
})
resizeObserver.observe(document.body)
// Listen for height requests from parent
window.addEventListener('message', (event) => {
if (event.data?.type === 'requestHeight') sendHeight()
})
return () => {
resizeObserver.disconnect()
}
}, [])
return null
}
export default IframeHeight

Support

Talk to the developers of this project to learn more

We have been building professional websites for big clients for over 15 years. Gallop templates and blocks is our best foundation for SEO websites and web apps.

© 2026 Web Plant Media, LLC