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

76 lines (75 loc) • 3.89 kB
import { type Space } from '@mitcsutt/kiln-ui'; import { type HTMLAttributes, type ReactElement, type Ref } from 'react'; import type { DragInput } from '../../core/controller.js'; import type { DndAnnouncements } from '../../core/messages.js'; import { type GridCompaction, type GridItem } from '../../data/layout.js'; /** * A width at which a grid's column count can change. They are Kiln's breakpoints * (`sm` 40em, `md` 48em, `lg` 64em, `xl` 80em), measured against the grid's own width * rather than the window's, so a grid in a narrow pane gets fewer columns. */ export type GridBreakpoint = 'base' | 'sm' | 'md' | 'lg' | 'xl'; /** One change, as `GridLayout`'s `onChange` reports it. */ export interface GridChange { /** The id of the item that moved or was resized. */ id: string; /** How it was changed. */ input: DragInput; } export interface GridLayoutProps<T extends GridItem = GridItem> extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> { /** The layout: one `{ i, x, y, w, h }` per item. Pass with `onChange` to control it. */ layout?: readonly T[]; /** The starting layout for a grid that keeps its own. */ defaultLayout?: readonly T[]; /** Called once per drop that changes the layout, never during a drag. */ onChange?: (layout: T[], change: GridChange) => void; /** * The column count, or one per breakpoint (`{ base: 4, md: 8, lg: 12 }`). The layout you * store is for the widest; narrower ones are generated from it. Default 12. */ columns?: number | Partial<Record<GridBreakpoint, number>>; /** The height of one row, as a space step. Default 9. */ rowHeight?: Space; /** The gap between items, as a space step. Default 4. */ gap?: Space; /** How gaps close after a change. Default `vertical`. */ compaction?: GridCompaction; /** The grid's accessible name ("Sales dashboard"). */ label: string; /** Replace any of the words the grid says (see `Dnd`). */ announcements?: Partial<DndAnnouncements>; } export interface GridLayoutItemProps extends Omit<HTMLAttributes<HTMLDivElement>, 'id'> { /** Matches the `i` of the item's entry in the layout. */ id: string; /** The item's name, as announcements and its handle say it ("Revenue"). */ label: string; /** Hide the resize grip. Keyboard resizing stays available unless the item is static. */ resizable?: boolean; } /** * A dashboard grid: items you move and resize on a grid of columns and rows, by pointer, * touch or keyboard. * * @remarks * The layout is plain data, one `{ i, x, y, w, h }` per item in grid units, so you can * store it as JSON. Each `GridLayout.Item` matches an entry by `id` and is dragged by the * `DragHandle` you put inside it; a grip in its corner resizes it. While an item is * dragged, a placeholder shows where it will land and the other items make room; the * layout changes once, on drop, through `onChange`. * * By keyboard, Space or Enter on a handle picks the item up. The arrow keys move it one * cell, Shift and the arrow keys resize it one cell, Space or Enter drops it and Escape * puts everything back. Each step is announced ("Revenue moved to row 2, column 7"). * * Gaps close upward by default (`compaction`). Give `columns` per breakpoint and the grid * picks a column count from its own width; you store the layout for the widest, and * narrower ones are generated from it, read-only. Use `appendItem`, `removeItem` and * `compact` to change the layout in your own code. Movement animates, except under * reduced motion. */ export declare const GridLayout: (<T extends GridItem = GridItem>(props: GridLayoutProps<T> & { ref?: Ref<HTMLDivElement>; }) => ReactElement) & { Item: import("react").ForwardRefExoticComponent<GridLayoutItemProps & import("react").RefAttributes<HTMLDivElement>>; };