"use client" /** Header: colgroup, header row, aria-sort and floating pin styles. */ import * as React from "react" import { cn } from "../../lib/utils" import { DataTableHeaderCell } from "./data-table-header-cell" import type { ColumnDef } from "./types" import type { TableState } from "./table-state-types" // ───────────────────────────────────────────────────────────────────────────── // Header // ───────────────────────────────────────────────────────────────────────────── export function ariaSortFor( colKey: string, sortKey: string, sortDir: "asc" | "desc", ): React.AriaAttributes["aria-sort"] { if (sortKey !== colKey) return "none" return sortDir === "asc" ? "ascending" : "descending" } /** * Counter-translate pinned header cells so they remain locked to the viewport * edge: the floating sticky header is horizontally translated as one table. * `data-dt-floating-pin` lets scroll sync mutate this transform without React. */ export function floatingPinnedStyle({ pin, innerWidth, readWrapWidth, tableWidth, scrollLeft, }: { pin: "left" | "right" | undefined innerWidth: number readWrapWidth: () => number | undefined tableWidth: number scrollLeft: number }): React.CSSProperties | undefined { if (!pin) return undefined // Use the sticky header's INNER scrollport width (body's `clientWidth`), // not the outer rectangle (`floatingHeaderStyle.width`), so the // right-pinned column locks to the visible inner edge and not 2px past it. const visibleWidth = innerWidth > 0 ? innerWidth : (readWrapWidth() ?? tableWidth) const maxScroll = Math.max(0, tableWidth - visibleWidth) const translateX = pin === "left" ? scrollLeft : scrollLeft - maxScroll return { position: "relative", transform: `translateX(${translateX}px)` } } export function DataTableColgroup>({ columns, colWidths, }: { columns: ColumnDef[] colWidths: Record }) { return ( {columns.map(col => ( ))} ) } /** One `` of header cells; shared by the inline and the floating sticky ``. */ export function DataTableHeaderRow>({ columns, floating, state, lastLeftPinKey, firstRightPinKey, ariaSortFor: ariaSortForKey, onHeaderKeyDown, styleFor, renderInner, }: { columns: ColumnDef[] floating: boolean state: TableState lastLeftPinKey: string | undefined firstRightPinKey: string | undefined ariaSortFor: (colKey: string) => React.AriaAttributes["aria-sort"] onHeaderKeyDown: (col: ColumnDef, e: React.KeyboardEvent) => void styleFor: (colKey: string) => React.CSSProperties | undefined renderInner: (col: ColumnDef) => React.ReactNode }) { return ( {columns.map(col => ( onHeaderKeyDown(col, e)} style={styleFor(col.key)} > {renderInner(col)} ))} ) } /** Inline `` classes: it turns invisible while the sticky clone paints on top. */ export function theadClassName(headerIsStuck: boolean, headerSurfaceActive: boolean, showColumnHeaders: boolean) { return cn( "bg-dt-header-bg", headerIsStuck && !headerSurfaceActive && "opacity-0", // Belt-and-suspenders: restore paint the same frame focus enters, // even before React state commits. headerIsStuck && "has-[:focus-visible]:!opacity-100 has-[:focus-visible]:relative has-[:focus-visible]:z-[60]", headerIsStuck && headerSurfaceActive && "relative z-[60]", !showColumnHeaders && "hidden", ) }