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

129 lines (128 loc) • 5.76 kB
import { type CompactType, type LayoutItem } from '../grid/engine/index.js'; /** * One item of a grid layout, in grid units: column `x`, row `y`, `w` columns wide and `h` * rows tall. `i` matches the `id` of its `GridLayout.Item`. A layout is a plain array of * these, so it serialises as is; extra fields of your own are kept through every change. */ export interface GridItem { /** The item's id, matching `GridLayout.Item`'s `id`. */ i: string; /** Column, from 0. */ x: number; /** Row, from 0. */ y: number; /** Width in columns. */ w: number; /** Height in rows. */ h: number; /** The narrowest the item can be resized to, in columns. Default 1. */ minW?: number; /** The widest the item can be resized to, in columns. Default the grid's column count. */ maxW?: number; /** The shortest the item can be resized to, in rows. Default 1. */ minH?: number; /** The tallest the item can be resized to, in rows. Default no limit. */ maxH?: number; /** A fixed item: it can't be moved or resized, and other items flow around it. */ static?: boolean; } /** * How a grid closes gaps: `vertical` pulls items up (the default), `horizontal` pulls them * left, and `none` leaves them where they're put. */ export type GridCompaction = 'vertical' | 'horizontal' | 'none'; /** Options for {@link compact}, {@link appendItem} and {@link removeItem}. */ export interface CompactOptions { /** The grid's column count. */ columns: number; /** How gaps close. Default `vertical`. */ compaction?: GridCompaction; } export declare const compactType: (compaction?: GridCompaction) => CompactType; /** Drops the engine's bookkeeping (`moved`) so a layout handed out is the consumer's shape. */ export declare function toPublic<T extends GridItem>(layout: readonly LayoutItem[]): T[]; /** * Closes the gaps in a layout and keeps every item inside the columns. Returns a new * layout in the same order; the input is left alone. * * @example * ```ts * compact([{ i: 'revenue', x: 0, y: 4, w: 6, h: 2 }], { columns: 12 }) * // [{ i: 'revenue', x: 0, y: 0, w: 6, h: 2 }] * ``` */ export declare function compact<T extends GridItem>(layout: readonly T[], options: CompactOptions): T[]; /** * Adds an item at the bottom of a layout, then compacts. `x` defaults to 0 and `w` is * clamped to the column count. * * @example * ```ts * appendItem(layout, { i: 'tickets', w: 4, h: 2 }, { columns: 12 }) * ``` */ export declare function appendItem<T extends GridItem>(layout: readonly T[], item: Omit<T, 'x' | 'y'> & { x?: number; y?: number; }, options: CompactOptions): T[]; /** * Removes the item with id `i` from a layout, then compacts so the gap closes. * * @example * ```ts * removeItem(layout, 'tickets', { columns: 12 }) * ``` */ export declare function removeItem<T extends GridItem>(layout: readonly T[], i: string, options: CompactOptions): T[]; /** The size limits that apply to an item in a grid of `columns`. */ export declare function sizeLimits(item: GridItem, columns: number): { minW: number; maxW: number; minH: number; maxH: number; }; /** * The layout with item `i` moved to column `x`, row `y` (clamped to the grid), its * neighbours pushed out of the way and the result compacted. A pure function of the * committed layout, so a preview never drifts from what a drop would commit. */ export declare function moveInLayout<T extends GridItem>(layout: readonly T[], i: string, x: number, y: number, options: CompactOptions): T[]; /** * The layout with item `i` resized to `w` × `h` (clamped to its limits and the columns) * and compacted, which pushes what it now covers out of the way. With no compaction, the * items it covers move down below it instead. */ export declare function resizeInLayout<T extends GridItem>(layout: readonly T[], i: string, w: number, h: number, options: CompactOptions): T[]; /** * The layout as it renders when item content needs more rows than the layout gives it: * each item is at least `rows[i]` rows tall, what it now covers moves down below it, and * the result is compacted. The layout keeps its own heights; this is only how it's drawn, * so no item ever paints over another. */ export declare function fitLayout<T extends GridItem>(layout: readonly T[], rows: Readonly<Record<string, number>>, options: CompactOptions): T[]; /** The number of rows a layout fills. */ export declare const layoutRows: (layout: readonly GridItem[]) => number; /** Where a drag asks an item to be, in grid units: the preview is computed from this. */ export interface GridRequest { x: number; y: number; w: number; h: number; } /** * The layout a drag would commit: the committed layout with item `i` moved to the * requested cell, then resized to the requested size. Always computed from the committed * layout, never from the last preview, so moving back undoes a move exactly. */ export declare function previewLayout<T extends GridItem>(committed: readonly T[], i: string, request: GridRequest, options: CompactOptions): T[]; /** * One keyboard step: the nearest request in `direction` that actually moves item `i`. * Compaction can pull an item straight back (one row down, in a vertically compacted * grid, often lands where it started), so a step keeps going until the item's place * changes, the way a person would expect one key press to move it past its neighbour. * `null` when it can't move that way at all. */ export declare function stepInLayout<T extends GridItem>(committed: readonly T[], i: string, request: GridRequest, direction: 'up' | 'down' | 'left' | 'right', options: CompactOptions): { request: GridRequest; layout: T[]; } | null;