@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
69 lines (68 loc) • 3.11 kB
TypeScript
import { type HTMLAttributes, type ReactElement, type ReactNode, type Ref } from 'react';
import type { DragInput, ListOrientation } from '../../core/controller.js';
import type { DndAnnouncements } from '../../core/messages.js';
/** One reorder, as `Sortable`'s `onChange` reports it. */
export interface SortableMove<T> {
/** The item that moved. */
item: T;
/** Its index before, from 0. */
from: number;
/** Its index now, from 0. */
to: number;
/** How it was moved. */
input: DragInput;
}
/** What `Sortable` passes to the function that renders each item. */
export interface SortableItemState {
/** The item's index, from 0. */
index: number;
/** Whether it's being dragged. */
isDragging: boolean;
}
export interface SortableProps<T> extends Omit<HTMLAttributes<HTMLUListElement>, 'children' | 'onChange' | 'defaultValue'> {
/** The items, in order. Pass with `onChange` to control the list. */
items?: readonly T[];
/** The starting items for a list that keeps its own order. */
defaultItems?: readonly T[];
/** Called once per drop that changes the order, with the new array and what moved. */
onChange?: (items: T[], move: SortableMove<T>) => void;
/** Renders one item. */
children: (item: T, state: SortableItemState) => ReactNode;
/** The list's accessible name ("Release checklist"). */
label: string;
/** Each item's name, as announcements and its handle say it. */
getLabel: (item: T) => string;
/** Each item's id, unique in the list. Default: the item's `id` field. */
getId?: (item: T) => string;
/** Items that can't be picked up. They still shift as others move around them. */
isDisabled?: (item: T) => boolean;
/** `vertical` (default) for a column of rows; `horizontal` for a row of items. */
orientation?: ListOrientation;
/**
* Drag by a `DragHandle` inside each item instead of the whole item. Use it whenever the
* items hold links, buttons or fields.
*/
handle?: boolean;
/** Replace any of the words the list says (see `Dnd`). */
announcements?: Partial<DndAnnouncements>;
}
/**
* A list you reorder by dragging, by pointer, touch or keyboard.
*
* @remarks
* `Sortable` renders a list of rows. Nothing moves while an item is dragged: a line shows
* where it will land, and the list reorders once, on drop, through `onChange`. By
* keyboard, Space or Enter picks an item up, the arrow keys move it, Space or Enter drops
* it and Escape puts it back, and each step is announced ("Write release notes moved to
* position 3 of 5").
*
* Pass `items` and `onChange` to own the order, or `defaultItems` to let the list keep
* it. `onChange` gets the new array, so `setItems` is usually all it needs. To move items
* in your own code, use `arrayMove`.
*
* Without `handle`, the whole row is the handle. With `handle`, put a `DragHandle` in each
* row; the rest of the row keeps its own clicks and keys.
*/
export declare const Sortable: <T>(props: SortableProps<T> & {
ref?: Ref<HTMLUListElement>;
}) => ReactElement;