Data Table Sort Header Button
virtuoso/data-table-sort-header-buttonFreeComponent
Slot-mounted sort button for column headers.
Install it
npx shadcn@latest add https://virtuoso.dev/r/data-table-sort-header-button.jsonSource
1'use client'23import { dispatchModelAction$, modelActionState$, useCellValue, usePublisher } from '@virtuoso.dev/data-table'4import { ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-react'56import { cn } from '@/lib/utils'78import type { HeaderSlotRenderParams } from '@virtuoso.dev/data-table'910export type SortDirection = 'asc' | 'desc'11export type SortPayload = {12 field: string13 direction: SortDirection14}1516export interface SortHeaderButtonProps extends Partial<HeaderSlotRenderParams> {17 action?: string18 className?: string19 field?: string20 getDirection?: (payload: unknown, field: string) => SortDirection | undefined21 getPayload?: (context: { direction: SortDirection | undefined; field: string; previousDirection: SortDirection | undefined }) => unknown22}2324function sortDirectionFromPayload(payload: unknown, field: string): SortDirection | undefined {25 if (typeof payload !== 'object' || payload === null) {26 return undefined27 }2829 const sort = payload as Partial<SortPayload>30 return sort.field === field && (sort.direction === 'asc' || sort.direction === 'desc') ? sort.direction : undefined31}3233export function SortHeaderButton({34 action = 'sort',35 className,36 column,37 field,38 getDirection = sortDirectionFromPayload,39 getPayload,40}: SortHeaderButtonProps) {41 const dispatch = usePublisher(dispatchModelAction$)42 const actionState = useCellValue(modelActionState$)43 const sortField = field ?? column?.field4445 if (!sortField) {46 return null47 }4849 const direction = getDirection(actionState[action]?.payload, sortField)50 const nextDirection: SortDirection | undefined = direction === 'asc' ? 'desc' : direction === 'desc' ? undefined : 'asc'51 const label =52 nextDirection === 'asc'53 ? `Sort ${sortField} ascending`54 : nextDirection === 'desc'55 ? `Sort ${sortField} descending`56 : `Clear ${sortField} sorting`57 const Icon = direction === 'asc' ? ArrowUp : direction === 'desc' ? ArrowDown : ArrowUpDown5859 return (60 <button61 aria-label={label}62 aria-pressed={direction !== undefined}63 className={cn(64 'ml-1 inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground/70 transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none disabled:pointer-events-none disabled:opacity-50',65 direction !== undefined && 'bg-muted text-foreground',66 className67 )}68// … 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 Draggable Group Headerdata-table-draggable-group-header | 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 |
