import {ComponentChildren, h} from 'preact' import {useEffect, useRef} from 'preact/hooks' import {Link} from './link' export function Container({ children, href, }: { children: ComponentChildren href?: string }) { const ref = useRef(null) const prevHeight = useRef(0) useEffect(() => { if (ref.current) { const observer = new ResizeObserver(entries => { const entry = entries[0] if (!entry) return let {height} = entry.contentRect height += 2 // border top and bottom if (height !== prevHeight.current) { prevHeight.current = height window.parent.postMessage( {height, id: new URLSearchParams(window.location.search).get('id')}, '*', ) } }) observer.observe(ref.current) return () => observer.disconnect() } }, []) return (
{ if (ref.current && href) { // forwardRef requires preact/compat - let's keep it simple // to keep the bundle size down const anchor = ref.current.querySelector('a') if (anchor) { anchor.click() } } }}> {href && }
{children}
) }