@mitcsutt/kiln-dnd
Version:
Kiln drag and drop: sortable lists, boards, drop zones and grid layouts with one keyboard model and one announcer, rendered through kiln-ui
41 lines (40 loc) • 1.77 kB
TypeScript
import { type KeyboardEvent as ReactKeyboardEvent } from 'react';
import type { CustomBehavior, DndController, DraggableRecord, DragInput } from './controller.js';
export interface DragSourceOptions {
id: string;
label: string;
data: unknown;
disabled: boolean;
kind: DraggableRecord['kind'];
list?: string;
index?: number;
custom?: CustomBehavior;
/** Also register the element as a drop target, so a pointer over it reports its id (lists). */
droppable?: boolean;
/** `none` for grid items, which draw their own drag preview. */
feedback?: 'clone' | 'none';
/** Which instructions the handle points to. */
instructions: 'list' | 'grid';
/** Whether the item is dragged by a separate handle element. */
hasHandle: boolean;
/** Whether the handle should be reachable by keyboard (a pointer-only resize grip isn't). */
keyboard?: boolean;
}
/** The props a drag handle (or a whole draggable item) needs. Spread them onto it. */
export interface DragHandleAttributes {
tabIndex?: number;
'aria-describedby'?: string;
'data-kiln-dnd-handle': '';
onKeyDown: (event: ReactKeyboardEvent) => void;
}
export interface DragSourceState {
/** Set the item's root element. */
ref: (element: HTMLElement | null) => void;
/** Set the handle element (the root itself when there is no separate handle). */
handleRef: (element: HTMLElement | null) => void;
/** Spread onto the handle (or the root when there is no separate handle). */
handleProps: DragHandleAttributes;
/** How the item is being dragged right now, or `null`. */
dragging: DragInput | null;
}
export declare function useDragSource(controller: DndController, options: DragSourceOptions): DragSourceState;