"use client" import * as React from "react" import { bindVerticalScrollListeners } from "../../lib/page-scroll-port" import { rafThrottle } from "../../lib/raf-throttle" /** * Pinned group divider: names the group whose rows are under the pin line, in a clone * under the sticky header, handing off to the next group like a contacts list. */ export function usePinnedGroupHeader>({ displayedGroups, effectiveHeaderStuck, tableWrapRef, }: { displayedGroups: { groupKey: string | null; groupLabel: string | null; rows: TData[]; }[] effectiveHeaderStuck: boolean tableWrapRef: React.RefObject }) { /* * ── Pinned group divider ──────────────────────────────────────────────── * * Grouping used to read as "the header stopped sticking": the column header * still pinned, but the divider that says *which group you are in* scrolled * away with its rows, so a long group left you reading a header with no * subject. The divider carried `sticky start-0`, which pins it horizontally * inside the scroll wrapper and does nothing vertically. * * So the divider gets the same treatment the column header already has: a * clone in the fixed wrapper, directly under the header. It names the group * whose rows are under the pin line, and when the next group's real divider * arrives it pushes the clone up out of a clipped window and takes over — the * hand-off you get in a phone contacts list, rather than a label that * teleports between groups. */ const floatingHeaderBoxRef = React.useRef(null) const groupClipRef = React.useRef(null) const groupSlideRef = React.useRef(null) const groupsWithLabels = React.useMemo( () => displayedGroups.filter(g => g.groupLabel), [displayedGroups], ) const hasGroupDividers = groupsWithLabels.length > 0 /** Index into `groupsWithLabels`; -1 while no group owns the pin line. */ const [pinnedGroupIndex, setPinnedGroupIndex] = React.useState(-1) React.useLayoutEffect(() => { if (!effectiveHeaderStuck || !hasGroupDividers) { setPinnedGroupIndex(-1) return } const wrapEl = tableWrapRef.current if (!wrapEl) return // Re-read on structural change only; the list is stable while scrolling. let dividers = Array.from( wrapEl.querySelectorAll("[data-dt-group-header]"), ) let lastSlide = Number.NaN const apply = () => { const boxEl = floatingHeaderBoxRef.current const slideEl = groupSlideRef.current const clipEl = groupClipRef.current if (!boxEl) return // Bottom of the column header — the line a divider pins to. const pinLine = boxEl.getBoundingClientRect().bottom // Past the end of the table there is no group to be inside of. if (wrapEl.getBoundingClientRect().bottom <= pinLine) { setPinnedGroupIndex(prev => (prev === -1 ? prev : -1)) return } let index = -1 for (let i = 0; i < dividers.length; i++) { if (dividers[i].getBoundingClientRect().top <= pinLine + 0.5) index = i else break } setPinnedGroupIndex(prev => (prev === index ? prev : index)) if (index === -1) return // How far the next divider has pushed into the pinned one. Measured off // the real row so the clone leaves at exactly the speed the row arrives. const barHeight = dividers[index].getBoundingClientRect().height const next = dividers[index + 1] const overlap = next ? Math.max(0, barHeight - (next.getBoundingClientRect().top - pinLine)) : 0 if (clipEl && clipEl.style.height !== `${barHeight}px`) { clipEl.style.height = `${barHeight}px` } if (slideEl && lastSlide !== overlap) { slideEl.style.transform = `translateY(${-overlap}px)` lastSlide = overlap } } apply() const scheduled = rafThrottle(apply) const unbindVerticalScroll = bindVerticalScrollListeners(scheduled) const resizeObserver = new ResizeObserver(() => scheduled()) resizeObserver.observe(wrapEl) // Rows collapse, load, or regroup without a scroll — re-collect and re-pin. const mutationObserver = new MutationObserver(() => { dividers = Array.from( wrapEl.querySelectorAll("[data-dt-group-header]"), ) scheduled() }) mutationObserver.observe(wrapEl, { childList: true, subtree: true }) return () => { scheduled.cancel() unbindVerticalScroll() resizeObserver.disconnect() mutationObserver.disconnect() } }, [effectiveHeaderStuck, hasGroupDividers, groupsWithLabels.length]) const pinnedGroup = pinnedGroupIndex >= 0 ? groupsWithLabels[pinnedGroupIndex] : undefined return { floatingHeaderBoxRef, groupClipRef, groupSlideRef, pinnedGroup, } }