"use client" import * as React from "react" import { cn } from "../../lib/utils" import type { ColumnDef } from "./types" import { DragHandleGripIcon } from "../ui/drag-handle-grip" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuShortcut, DropdownMenuTrigger, } from "../ui/dropdown-menu" export const ROW_DRAG_COLUMN_KEY = "drag" /** * The leading control column that holds each row's grip. Locked left like the * select column; narrow, so it costs one 6-unit grip of width. */ export const ROW_DRAG_COLUMN: ColumnDef = { key: ROW_DRAG_COLUMN_KEY, label: "", width: 36, minWidth: 36, defaultPin: "left", lockPin: true, } /** Prepends the drag column (before `select` when present) for `onRowsReorder` tables. */ export function withRowDragColumn>(columns: ColumnDef[]): ColumnDef[] { if (columns.some(c => c.key === ROW_DRAG_COLUMN_KEY)) return columns return [ROW_DRAG_COLUMN as ColumnDef, ...columns] } export type RowMove = "up" | "down" | "top" | "bottom" interface RowReorderHandleProps { rowId: string | number /** Row name for assistive tech, e.g. the primary column value. */ label: string /** When set the handle is inert and `title` explains why (sorted, grouped…). */ disabledReason?: string canMoveUp: boolean canMoveDown: boolean onMove: (move: RowMove) => void onDragStart: (e: React.DragEvent) => void onDragEnd: () => void } /** * The one control for reordering a row, three ways: drag it; focus it and press * Alt+↑/↓; or open its menu (Move up / down / to top / to bottom) — the * single-pointer alternative to dragging (WCAG 2.5.7). */ export function RowReorderHandle({ rowId, label, disabledReason, canMoveUp, canMoveDown, onMove, onDragStart, onDragEnd, }: RowReorderHandleProps) { // Radix's trigger opens on pointerdown and cancels it, and a cancelled // pointerdown never becomes a drag. So the grip is a plain draggable element // (a role="button" span, because Firefox will not drag a ) } return ( { e.stopPropagation() setOpen(o => !o) }} onDragStart={onDragStart} onDragEnd={onDragEnd} onKeyDown={e => { if (e.key === "Enter" || e.key === " ") { e.preventDefault() setOpen(o => !o) return } if (!e.altKey || (e.key !== "ArrowUp" && e.key !== "ArrowDown")) return e.preventDefault() if (e.key === "ArrowUp" && canMoveUp) onMove("up") if (e.key === "ArrowDown" && canMoveDown) onMove("down") }} > e.stopPropagation()} onCloseAutoFocus={e => { e.preventDefault() gripRef.current?.focus() }} > onMove("top")}>