"use client" import * as React from "react" import { cn } from "../../lib/utils" import { EDGE_RESIZE_HANDLE_CLASS, verticalResizeSeparatorAria } from "../../lib/edge-resize-handle" import { ROW_DRAG_COLUMN_KEY } from "./row-reorder-handle" import type { ColumnDef } from "./types" import { CONTROL_COLUMN_PADDING, isControlColumn, resolvedColumnLabel, stickyShadow } from "./table-helpers" import type { useTableState } from "./use-table-state" interface HeaderCellClassInput { colKey: string isPinned: boolean isFree: boolean isEdgePinCol: boolean /** Another column is being dragged over this one. */ isDragTarget: boolean pin: "left" | "right" | undefined showGridlines: boolean } /** Classes for a column header cell; kept out of the component so its branches do not add to it. */ function headerCellClassName({ colKey, isPinned, isFree, isEdgePinCol, isDragTarget, pin, showGridlines, }: HeaderCellClassInput) { return cn( "group/th relative h-9 align-middle select-none", isControlColumn(colKey) ? cn(CONTROL_COLUMN_PADDING, "text-center") : "px-3 text-start", "text-xs font-medium text-muted-foreground tracking-wide", "bg-dt-header-bg border-b border-border", showGridlines && (!isEdgePinCol ? "border-e border-border last:border-e-0" : "last:border-e-0"), isPinned ? "z-40" : "z-30", isFree && "cursor-grab active:cursor-grabbing", isDragTarget && "bg-accent/40", isEdgePinCol && stickyShadow(pin), ) } type DragHandlers = Pick< React.ThHTMLAttributes, "onDragStart" | "onDragOver" | "onDrop" | "onDragEnd" > /** Drag-reorder handlers; only free (unpinned) columns can be grabbed. */ function dragHandlersFor( isFree: boolean, key: string, h: { handleDragStart: (key: string, e: React.DragEvent) => void handleDragOver: (key: string, e: React.DragEvent) => void handleDrop: (key: string) => void handleDragEnd: () => void }, ): DragHandlers { if (!isFree) return {} return { onDragStart: e => h.handleDragStart(key, e), onDragOver: e => h.handleDragOver(key, e), onDrop: () => h.handleDrop(key), onDragEnd: h.handleDragEnd, } } /** Resize handle exists for every column except the select / row-drag control columns. */ function isResizableColumn(key: string): boolean { return key !== "select" && key !== ROW_DRAG_COLUMN_KEY } function pinAttr(floating: boolean, pin: "left" | "right" | undefined) { return floating ? pin || undefined : undefined } /** One column header ``: drag-to-reorder, sticky pin styling and the resize handle. */ export function DataTableHeaderCell>({ col, floating, state, lastLeftPinKey, firstRightPinKey, ariaSort, onKeyDown, style, children, }: { col: ColumnDef /** True in the sticky clone, which stays mouse-only and has no resize-handle focus. */ floating: boolean state: ReturnType> lastLeftPinKey?: string firstRightPinKey?: string ariaSort?: React.AriaAttributes["aria-sort"] onKeyDown: (e: React.KeyboardEvent) => void style?: React.CSSProperties children: React.ReactNode }) { const { colPins, effectivePins, colWidths, startResize, showGridlines, draggedKey, dragOverKey } = state const isFree = !colPins[col.key] const isEdgePinCol = col.key === lastLeftPinKey || col.key === firstRightPinKey return ( {children} {/* Resize handle: a mouse-drag affordance only. It is out of the Tab order on purpose (it used to add a tabstop per column with no focus ring, a WCAG 2.4.7 failure); keyboard resize is a backlog item. `startResize` reads pageX deltas, so a drag works from either header. */} {isResizableColumn(col.key) && (
startResize(col.key, e)} className={EDGE_RESIZE_HANDLE_CLASS} /> )} ) }