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

195 lines (194 loc) • 6.82 kB
import type { DndAnnouncements } from './messages.js'; import type { Engine, EngineMove } from './engineTypes.js'; import { type Store } from './store.js'; /** How a drag is being driven. */ export type DragInput = 'keyboard' | 'pointer'; /** A list's direction: the arrow keys along it move an item through the list. */ export type ListOrientation = 'vertical' | 'horizontal'; /** The dragged item, as the event callbacks see it. */ export interface DndItem { /** The item's id. */ id: string; /** The item's name, as announcements say it. */ label: string; /** Whatever you passed as `data` when you made it draggable. */ data: unknown; /** For a sortable item: the list it started in. */ list?: string; /** For a sortable item: its index there, from 0. */ index?: number; } /** Where a dragged item would land: a drop zone, or a position in a list. */ export type DndTarget = { kind: 'zone'; /** The drop zone's id. */ id: string; /** The drop zone's name. */ label: string; /** Whatever you passed as the zone's `data`. */ data: unknown; } | { kind: 'list'; /** The list's id. */ list: string; /** The item's index in that list once dropped, from 0. */ index: number; }; /** Passed to `onDragStart`. */ export interface DragStartEvent { item: DndItem; input: DragInput; } /** Passed to `onDragOver` whenever the target changes. */ export interface DragOverEvent { item: DndItem; target: DndTarget | null; input: DragInput; } /** * Passed to `onDragEnd` when an item is dropped. A sortable item only ends here when the * drop moves it (to another slot, list or zone); a draggable always does, with `target` * `null` if it was over no zone. */ export interface DragEndEvent { item: DndItem; target: DndTarget | null; input: DragInput; } /** * Passed to `onDragCancel`: on Escape, focus leaving the handle or a cancelled pointer drag, * and when a sortable item is dropped where nothing moves (back in its own slot, or over * nothing). */ export interface DragCancelEvent { item: DndItem; input: DragInput; } export interface DndCallbacks { onDragStart?: (event: DragStartEvent) => void; onDragOver?: (event: DragOverEvent) => void; onDragEnd?: (event: DragEndEvent) => void; onDragCancel?: (event: DragCancelEvent) => void; } type Target = { kind: 'zone'; id: string; } | { kind: 'list'; list: string; index: number; } | { kind: 'custom'; }; export interface Operation { id: string; input: DragInput; target: Target | null; } export interface RootState { operation: Operation | null; /** Bumped on every announcement so the same words can be said twice. */ announcement: { text: string; key: number; }; /** A handle that should take focus once it renders (after a keyboard drop moves it). */ focusId: string | null; engine: Engine | null; /** What the scope says, with the consumer's overrides. */ messages: DndAnnouncements; } export type ArrowDirection = 'up' | 'down' | 'left' | 'right'; /** The behaviour a pattern with its own idea of a target (GridLayout) plugs in. */ export interface CustomBehavior { /** Said when a disabled item's handle is pressed, if there's a reason worth giving. */ blocked?: () => string | null; lift: (input: DragInput) => string; key: (direction: ArrowDirection, shift: boolean) => string | null; pointer: (move: EngineMove) => string | null; drop: () => string; cancel: () => string; } export interface DraggableRecord { id: string; label: string; data: unknown; disabled: boolean; /** `zone`: drops onto drop zones. `list`: sortable. `custom`: brings its own behaviour. */ kind: 'zone' | 'list' | 'custom'; list: string; index: number; custom: CustomBehavior | null; element: HTMLElement | null; handle: HTMLElement | null; } export interface DroppableRecord { id: string; label: string; data: unknown; disabled: boolean; accept: ((item: DndItem) => boolean) | undefined; /** Set when this droppable is a sortable list's container. */ list: string | undefined; orientation: ListOrientation; element: HTMLElement | null; onDrop: ((item: DndItem) => void) | undefined; } interface ListGroup { id: string; label: string; orientation: ListOrientation; items: DraggableRecord[]; } /** Wait this long after the last pointer move before announcing where the item is. */ export declare const POINTER_ANNOUNCE_DELAY = 300; export declare class DndController { #private; readonly store: Store<RootState>; readonly draggables: Map<string, DraggableRecord>; readonly droppables: Map<string, DroppableRecord>; callbacks: DndCallbacks; /** The ids of the scope's hidden instructions, which every handle points to. */ readonly instructionIds: { list: string; grid: string; }; constructor(messages: DndAnnouncements, scopeId: string); setCallbacks(callbacks: DndCallbacks): void; get messages(): DndAnnouncements; announce(text: string, debounce?: boolean): void; item(record: DraggableRecord): DndItem; /** The sortable lists in this scope, in document order. */ lists(): ListGroup[]; containerOf(list: string): DroppableRecord | undefined; /** Drop zones the item can land on, in document order. */ zonesFor(record: DraggableRecord): DroppableRecord[]; accepts(droppableId: string, sourceId: string): boolean; /** The items of `list` other than the dragged one: the slots a drop chooses between. */ others(list: ListGroup, sourceId: string): DraggableRecord[]; publicTarget(target: Target | null): DndTarget | null; get operation(): Operation | null; start(id: string, input: DragInput): void; /** Moves the drag's target. Announces it, debounced for a pointer. */ retarget(target: Target | null): void; drop(): void; cancel(): void; /** The keyboard model, shared by every handle. Returns true if it used the key. */ keyDown(id: string, event: { key: string; shiftKey: boolean; repeat: boolean; preventDefault: () => void; }): boolean; readonly pointer: { start: (sourceId: string) => void; move: (move: EngineMove) => void; end: (move: EngineMove, canceled: boolean) => void; }; /** Where the drop line goes, relative to item `id`, for the drag in progress. */ dropEdge(state: RootState, id: string): 'before' | 'after' | null; /** Whether droppable `id` is what the drag is over (an empty list counts when it's the target). */ isOver(state: RootState, id: string): boolean; destroy(): void; } export {};