Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created September 14, 2022 18:01
Show Gist options
  • Select an option

  • Save mendes5/ab538a6cd50336a4964d4d39b813d928 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/ab538a6cd50336a4964d4d39b813d928 to your computer and use it in GitHub Desktop.
A container that can have many elements but will not influence its container height, to do that, it should be linked to another element inside the same container to get its height from.
const LinkedHeightContainer = ({ children, link }) => {
const ref = useRef(null);
useEffect(() => {
const observer = new ResizeObserver((entries) => {
const entry = entries[entries.length - 1];
const height = `${entry.contentRect.height}px`;
ref.current.style.height = height;
});
observer.observe(link.current);
return () => {
observer.disconnect();
};
}, [link]);
return (
<div style={{ height: 0 }}>
<div style={{ overflowY: "auto" }} ref={ref}>
{children}
</div>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment