UNPKG

@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

80 lines (79 loc) • 3.49 kB
import { type HeadingLevel } from '@mitcsutt/kiln-ui'; import { type HTMLAttributes, type ReactElement, type ReactNode, type Ref } from 'react'; import type { DragInput } from '../../core/controller.js'; import type { DndAnnouncements } from '../../core/messages.js'; import { type ListPosition } from '../../data/lists.js'; /** One column of a `Board`. */ export interface BoardColumn { /** The key of its items in `items`. */ id: string; /** Its heading, and its name in announcements ("Doing"). */ label: string; } /** One move, as `Board`'s `onChange` reports it. */ export interface BoardMove<T> { /** The item that moved. */ item: T; /** Where it was: its column and index there. */ from: ListPosition; /** Where it is now. */ to: ListPosition; /** How it was moved. */ input: DragInput; } /** What `Board` passes to the function that renders each item. */ export interface BoardItemState { /** The item's column. */ column: string; /** Its index there, from 0. */ index: number; /** Whether it's being dragged. */ isDragging: boolean; } export interface BoardProps<T> extends Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'onChange' | 'defaultValue'> { /** The columns, in order. */ columns: readonly BoardColumn[]; /** The items of each column, keyed by column id. Pass with `onChange` to control the board. */ items?: Readonly<Record<string, readonly T[]>>; /** The starting items for a board that keeps its own state. */ defaultItems?: Readonly<Record<string, readonly T[]>>; /** Called once per drop that moves an item, with every column's items and what moved. */ onChange?: (items: Record<string, T[]>, move: BoardMove<T>) => void; /** Renders one item. */ children: (item: T, state: BoardItemState) => ReactNode; /** The board's accessible name ("Release board"). */ label: string; /** Each item's name, as announcements and its handle say it. */ getLabel: (item: T) => string; /** Each item's id, unique across the board. Default: the item's `id` field. */ getId?: (item: T) => string; /** Items that can't be picked up. */ isDisabled?: (item: T) => boolean; /** Drag by a `DragHandle` inside each item instead of the whole item. */ handle?: boolean; /** The level of the column headings. Default 3. */ headingLevel?: HeadingLevel; /** Replace any of the words the board says (see `Dnd`). */ announcements?: Partial<DndAnnouncements>; } /** * Columns of items you move within and between columns by dragging, by pointer, touch or * keyboard: a kanban board. * * @remarks * `Board` takes the columns in order and each column's items as a map, * `{ todo: […], doing: […] }`. A line shows where a dragged item will land, and the board * changes once, on drop, through `onChange`, which gets the whole new map. To move items * in your own code, use `moveBetween`. * * By keyboard, Space or Enter picks an item up. Up and Down move it within its column, * Left and Right move it to the next column, Space or Enter drops it and Escape puts it * back. Each step is announced with the column's name ("Fix the export button moved to * position 1 of 3 in Doing"). * * Each column has a heading (level 3 unless you set `headingLevel`) and a count. An empty * column still takes drops. */ export declare const Board: <T>(props: BoardProps<T> & { ref?: Ref<HTMLDivElement>; }) => ReactElement;