Fix PopoutSection scrolling to center

This commit is contained in:
Jip Fr 2023-02-09 12:44:11 +01:00
parent f14606e579
commit 6d24e8aa81

View File

@ -24,17 +24,37 @@ export function PopoutSection(props: {
useEffect(() => { useEffect(() => {
if (inited.current) return; if (inited.current) return;
if (!ref.current) return; if (!ref.current) return;
const el = ref.current as HTMLDivElement; const el = ref.current as HTMLDivElement;
const active: HTMLDivElement | null = el.querySelector(".active");
if (active) { // Find nearest scroll container, or self
active?.scrollIntoView({ const wrapper: HTMLDivElement | null = el.classList.contains(
"overflow-y-auto"
)
? el
: el.closest(".overflow-y-auto");
const active: HTMLDivElement | null | undefined =
wrapper?.querySelector(".active");
if (wrapper && active) {
active.scrollIntoView({
block: "nearest", block: "nearest",
inline: "nearest", inline: "nearest",
}); });
el.scrollTo({
top: el.scrollTop + el.offsetHeight / 2 - active.offsetHeight / 2, const activeYPositionCentered =
active.getBoundingClientRect().top -
wrapper.getBoundingClientRect().top +
active.offsetHeight / 2;
if (activeYPositionCentered >= wrapper.offsetHeight / 2) {
// Check if the active element is below the vertical center line, then scroll it into center
wrapper.scrollTo({
top: activeYPositionCentered - wrapper.offsetHeight / 2,
}); });
} }
}
inited.current = true; inited.current = true;
}, [ref]); }, [ref]);