Data Table Draggable Group Header
virtuoso/data-table-draggable-group-headerFreeComponent
Drag-to-reorder column group header that moves an entire group of columns at once.
Install it
npx shadcn@latest add https://virtuoso.dev/r/data-table-draggable-group-header.jsonSource
1'use client'23import { useCellValue, usePublisher } from '@virtuoso.dev/data-table'4import {5 beginColumnDrag$,6 columnDragState$,7 endColumnDrag$,8 reorderColumnGroup$,9 setColumnDropTarget$,10} from '@virtuoso.dev/data-table/column-reorder'1112import { cn } from '@/lib/utils'1314interface DraggableGroupHeaderProps {15 label: string16 columnKeys: string[]17 stickyGroup?: 'left' | 'right'18}1920function DraggableGroupHeader({ label, columnKeys, stickyGroup }: DraggableGroupHeaderProps) {21 const dragState = useCellValue(columnDragState$)22 const beginColumnDrag = usePublisher(beginColumnDrag$)23 const endColumnDrag = usePublisher(endColumnDrag$)24 const setColumnDropTarget = usePublisher(setColumnDropTarget$)25 const reorderColumnGroup = usePublisher(reorderColumnGroup$)2627 const isGroupDrag = (dragState.sourceKeys?.length ?? 0) > 128 const isSelfDrag = isGroupDrag && dragState.sourceKeys?.[0] === columnKeys[0]29 const groupAnchorKey = dragState.dropTarget?.key30 const dropPosition =31 groupAnchorKey && (groupAnchorKey === columnKeys[0] || groupAnchorKey === columnKeys.at(-1)) ? dragState.dropTarget!.position : null3233 return (34 <div35 className={cn(36 'relative flex items-center border-b border-border bg-muted px-2 py-1 text-xs font-semibold whitespace-nowrap',37 stickyGroup && 'bg-muted/80'38 )}39 onDragOver={(ev) => {40 if (!isGroupDrag) {41 return42 }43 if (isSelfDrag) {44 return45 }46 if (dragState.sourceSticky !== stickyGroup) {47 return48 }49 ev.preventDefault()50 ev.dataTransfer.dropEffect = 'move'51 const rect = ev.currentTarget.getBoundingClientRect()52 const position = ev.clientX < rect.left + rect.width / 2 ? 'before' : 'after'53 const anchorKey = position === 'before' ? columnKeys[0]! : columnKeys.at(-1)!54 if (dragState.dropTarget?.key === anchorKey && dragState.dropTarget.position === position) {55 return56 }57 setColumnDropTarget({ key: anchorKey, position })58 }}59 onDragLeave={(ev) => {60 const next = ev.relatedTarget as Node | null61 if (next && ev.currentTarget.contains(next)) {62 return63 }64 setColumnDropTarget(null)65 }}66 onDrop={(ev) => {67 ev.preventDefault()68 if (!isGroupDrag || isSelfDrag || !dragState.sourceKeys) {69 setColumnDropTarget(null)70 return71 }72// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from virtuoso
All 6 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Data Tabledata-table | virtuoso | Components | Free | 1 dep | |
| Data Table Reorder Drop Zonedata-table-reorder-drop-zone | virtuoso | Components | Free | 2 deps · 4 files | |
| Data Table Reorder Gripdata-table-reorder-grip | virtuoso | Components | Free | 2 deps · 4 files | |
| Data Table Resize Handledata-table-resize-handle | virtuoso | Components | Free | 1 dep · 2 files | |
| Data Table Sort Header Buttondata-table-sort-header-button | virtuoso | Components | Free | 2 deps · 2 files |
