'use client' import { useCallback, useMemo, type ComponentType, type ReactNode } from 'react' import { DndContext as RawDndContext, closestCenter, KeyboardSensor, PointerSensor, useSensor, useSensors, MeasuringStrategy, type DragEndEvent, type DndContextProps, } from '@dnd-kit/core' import { arrayMove, SortableContext, sortableKeyboardCoordinates, rectSortingStrategy, useSortable, } from '@dnd-kit/sortable' import { restrictToParentElement } from '@dnd-kit/modifiers' import { CSS } from '@dnd-kit/utilities' // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface SortableMediaItem { /** Stable unique id for the item (the object URL preview works well). */ id: string /** Image preview source. */ src: string /** Per-image alt text (accessibility description). */ alt?: string } interface SortableMediaGridProps { items: SortableMediaItem[] /** Called with the new ordering after a drag or keyboard reorder. */ onReorder: (items: SortableMediaItem[]) => void /** Called with the id of an item to remove. */ onRemove: (id: string) => void /** Called when the user edits an item's alt text. */ onEditAlt?: (id: string) => void } // --------------------------------------------------------------------------- // Single sortable tile // --------------------------------------------------------------------------- interface SortableTileProps { item: SortableMediaItem index: number total: number onRemove: (id: string) => void onEditAlt?: (id: string) => void } function SortableTile({ item, index, total, onRemove, onEditAlt }: SortableTileProps) { const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: item.id, }) const style = useMemo( () => ({ transform: CSS.Transform.toString(transform), transition, zIndex: isDragging ? 10 : undefined, opacity: isDragging ? 0.85 : 1, }), [transform, transition, isDragging], ) return (
{/* Drag handle — also the keyboard-reorder grip. */} {/* Remove button. */} {/* Order badge. */} {index + 1} {/* Alt-text badge — lit when a description has been added. */} {onEditAlt && ( )} {/* eslint-disable-next-line @next/next/no-img-element */} {item.alt
) } // --------------------------------------------------------------------------- // Grid // --------------------------------------------------------------------------- const measuringConfig = { droppable: { strategy: MeasuringStrategy.Always }, } const modifiers = [restrictToParentElement] // @dnd-kit ships types compiled against a different @types/react major than // this app, which makes TS reject DndContext as a JSX component. Re-type it // against this app's React types (behaviour is identical at runtime). const DndContext = RawDndContext as unknown as ComponentType< DndContextProps & { children?: ReactNode } > /** * Keyboard-accessible drag-to-reorder grid for selected composer images. * Adapted from munia's CreatePostSort/CreatePostSortItem to the product's * stack (object-URL previews, CSS-variable theme, no custom svg_components). */ export default function SortableMediaGrid({ items, onReorder, onRemove, onEditAlt }: SortableMediaGridProps) { const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }), ) const handleDragEnd = useCallback( (event: DragEndEvent) => { const { active, over } = event if (!over || active.id === over.id) return const oldIndex = items.findIndex((i) => i.id === active.id) const newIndex = items.findIndex((i) => i.id === over.id) if (oldIndex === -1 || newIndex === -1) return onReorder(arrayMove(items, oldIndex, newIndex)) }, [items, onReorder], ) const ids = useMemo(() => items.map((i) => i.id), [items]) return (
{items.map((item, index) => (
))}
) }