"use client" /** Scroll and focus effects behind DataTableInner. */ import * as React from "react" import { isEditableTarget } from "../../lib/editable-target" import { hasVisibleFocus } from "./table-helpers" // ───────────────────────────────────────────────────────────────────────────── // Scroll effects // ───────────────────────────────────────────────────────────────────────────── /** * Mount overflow check + scrollport width for sticky group headers on horizontal scroll. * Re-run when column widths / visibility change — scrollWidth alone is unreliable when * the table used to stretch with `w-full` (scrollWidth === clientWidth even with many cols). */ export function useScrollportSync({ checkOverflow, scrollRef, totalWidth, columnCount, }: { checkOverflow: () => void scrollRef: React.RefObject totalWidth: number columnCount: number }) { /** Last width published to `--dt-scrollport-width`; skips redundant writes. */ const lastScrollportVarWidth = React.useRef(-1) React.useLayoutEffect(() => { const syncScrollport = () => { // Reads before writes. Setting the custom property first made // `checkOverflow`'s `clientWidth` read flush layout again, so a rail drag // paid a forced synchronous layout on every frame. The guard also keeps // the write off frames where the width is unchanged (vertical scroll). checkOverflow() const el = scrollRef.current if (!el) return const width = el.clientWidth if (lastScrollportVarWidth.current === width) return lastScrollportVarWidth.current = width el.style.setProperty("--dt-scrollport-width", `${width}px`) } // Re-subscribing may mean a fresh scroll element, which carries no custom // property yet — force the first write of this subscription. lastScrollportVarWidth.current = -1 syncScrollport() const el = scrollRef.current if (!el) return const ro = new ResizeObserver(syncScrollport) ro.observe(el) const table = el.querySelector("table") if (table) ro.observe(table) return () => ro.disconnect() }, [totalWidth, columnCount, checkOverflow, scrollRef]) } type PinnedHintPlan = { kind: "skip" } | { kind: "reduced-motion" } | { kind: "nudge"; delta: number } /** Whether (and how far) the one-time pinned-column scroll nudge should run. */ function planPinnedScrollHint(el: HTMLElement): PinnedHintPlan { if (el.scrollLeft > 2) return { kind: "skip" } const maxScroll = el.scrollWidth - el.clientWidth if (maxScroll < 16) return { kind: "skip" } if (typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches) { return { kind: "reduced-motion" } } return { kind: "nudge", delta: Math.min(96, Math.max(28, Math.round(maxScroll * 0.14))) } } /** One-time horizontal nudge when the grid overflows and pins are active — hints that more columns scroll (overlay scrollbars, esp. Windows, are often invisible until interaction). */ export function usePinnedScrollHint({ isOverflowing, stickyPinsDisabled, colPins, scrollRef, }: { isOverflowing: boolean stickyPinsDisabled: boolean colPins: Record scrollRef: React.RefObject }) { const doneRef = React.useRef(false) React.useEffect(() => { const eligible = isOverflowing && !stickyPinsDisabled && Object.keys(colPins).length > 0 const el = scrollRef.current if (!eligible || doneRef.current || !el) return const plan = planPinnedScrollHint(el) if (plan.kind === "skip") return doneRef.current = true if (plan.kind === "reduced-motion") return const startDelayMs = 320 const dwellMs = 520 const t1 = window.setTimeout(() => { el.scrollTo({ left: plan.delta, behavior: "smooth" }) }, startDelayMs) const t2 = window.setTimeout(() => { el.scrollTo({ left: 0, behavior: "smooth" }) }, startDelayMs + dwellMs) return () => { window.clearTimeout(t1) window.clearTimeout(t2) } }, [isOverflowing, stickyPinsDisabled, colPins, scrollRef]) } /** * Polaris IndexTable parity: Esc clears the row selection while the bulk * bar is visible. `isEditableTarget` keeps Esc inside /