Mount

Generated by website/scripts/generate-api.ts from packages/gridla/src. Do not edit by hand; run bun run generate in website/.

mountGrid turns an element into a canvas: it creates and positions one element per item, wires pointer and keyboard input, measures the canvas, and reports committed layouts through a GridHandle.

import { createTransferScope, GRID_DATA, itemViewsEqual, mountGrid, selectItemView } from 'gridla/dom'
import type { GridHandle, GridItemRenderer, GridItemView, GridPositioning, MountGridOptions, TransferScope } from 'gridla/dom'
ExportKindSummary
createTransferScopefunctionCreate a TransferScope.
GRID_DATAconstData attributes the pointer gesture looks for on pointer down.
GridHandletypeA mounted canvas.
GridItemRenderertypePaints the content of one item.
GridItemViewtypeEverything needed to paint one item: its current and pre-gesture rectangles plus its active, selected, shifted, and transferring flags.
GridPositioningtypeHow an element is placed: with a transform (default) or with left/top.
itemViewsEqualfunctionCompare two item views by value.
mountGridfunctionMount a grid on element: the element becomes the canvas (data-gridla-canvas), one child per item is created and positioned, and pointer and keyboard input drive the solvers.
MountGridOptionstypeOptions for mountGrid: every GridControllerOptions field (controlled layout or uncontrolled defaultLayout, callbacks, solver settings, a transfer scope), the pointer gesture callbacks, and how items are rendered.
selectItemViewfunctionDerive the GridItemView of itemId from controller state.
TransferScopetypeCoordinates item moves between canvases.

Functions

createTransferScope

function · interaction/transfer.ts:127

Create a TransferScope. Pass it to every createGridController whose items may move between each other (option scope); the controllers register themselves and unregister on destroy().

export function createTransferScope(): TransferScope

itemViewsEqual

function · dom/view.ts:64

Compare two item views by value.

export function itemViewsEqual(a: GridItemView, b: GridItemView): boolean

mountGrid

function · dom/mount.ts:155

Mount a grid on element: the element becomes the canvas (data-gridla-canvas), one child per item is created and positioned, and pointer and keyboard input drive the solvers. Nested layouts are nested mounts; give the inner and outer canvases the same scope from createTransferScope to move items between them. Returns a GridHandle; call destroy when the element goes away.

export function mountGrid<TData = unknown>(
  element: HTMLElement,
  options: MountGridOptions<TData> = {},
): GridHandle<TData>

selectItemView

function · dom/view.ts:29

Derive the GridItemView of itemId from controller state.

export function selectItemView<TData>(state: GridState<TData>, itemId: string): GridItemView

Constants

GRID_DATA

const · interaction/attributes.ts:6

Data attributes the pointer gesture looks for on pointer down. Adapters emit them on their elements: item carries the item id, dragHandle marks a surface that starts a move, resizeHandle plus edge mark a resize handle.

export const GRID_DATA: { readonly item: "data-gridla-item"; readonly dragHandle: "data-gridla-drag-handle"; readonly resizeHandle: "data-gridla-resize-handle"; readonly edge: "data-gridla-edge"; }

Types

GridHandle

type · dom/mount.ts:77

A mounted canvas. Keep it to sync a controlled layout, subscribe to state, change options, or tear everything down.

export type GridHandle<TData = unknown> = {
  /** The canvas element the grid was mounted on. */
  element: HTMLElement
  /** The underlying controller: store, actions, and gesture API. */
  controller: GridController<TData>
  /** Replace the layout. In controlled mode call this from `onLayoutChange`. */
  setLayout: (layout: GridLayout<TData>) => void
  /** The layout in effect, in the caller's coordinates (the last one set or committed). */
  getLayout: () => GridLayout<TData>
  /** Subscribe to controller state. Returns an unsubscribe function. */
  subscribe: (listener: (state: GridState<TData>) => void) => () => void
  /** Select an item, or clear the selection with `null`. */
  select: (itemId: string | null) => void
  /** Apply changed options; the canvas re-renders once. */
  setOptions: (options: Partial<MountGridOptions<TData>>) => void
  /** Remove listeners, observers, rendered elements, and unregister from the transfer scope. */
  destroy: () => void
}
MemberTypeDescription
elementHTMLElementThe canvas element the grid was mounted on.
controllerGridController\<TData\>The underlying controller: store, actions, and gesture API.
setLayout(layout: GridLayout\<TData\>) =\> voidReplace the layout. In controlled mode call this from onLayoutChange.
getLayout() =\> GridLayout\<TData\>The layout in effect, in the caller's coordinates (the last one set or committed).
subscribe(listener: (state: GridState\<TData\>) =\> void) =\> () =\> voidSubscribe to controller state. Returns an unsubscribe function.
select`(itemId: string \null) => void`
setOptions(options: Partial\<MountGridOptions\<TData\>\>) =\> voidApply changed options; the canvas re-renders once.
destroy() =\> voidRemove listeners, observers, rendered elements, and unregister from the transfer scope.

GridItemRenderer

type · dom/mount.ts:27

Paints the content of one item. Called once when the item's element is created and again whenever the item object or its GridItemView changes. Built-in resize handles are children of element too; replacing element.innerHTML is safe, they are re-attached after every call.

export type GridItemRenderer<TData = unknown> = (
  item: GridItem<TData>,
  element: HTMLElement,
  view: GridItemView,
) => void

GridItemView

type · dom/view.ts:10

Everything needed to paint one item: its current and pre-gesture rectangles plus its active, selected, shifted, and transferring flags. Passed to renderItem and computed by selectItemView.

export type GridItemView = {
  /** Where the item is painted right now (preview-aware). */
  rect: GridRect
  /** Where the item was before the current gesture. */
  baseRect: GridRect
  /** Cursor-tracked rect while this item is active; `null` otherwise. */
  activeRect: GridRect | null
  isActive: boolean
  isSelected: boolean
  /** True when this item moved in the preview because another item pushed it. */
  isShifted: boolean
  /** True while the active item is being previewed in another canvas. */
  isTransferring: boolean
  interaction: GridInteraction | null
}
MemberTypeDescription
rectGridRectWhere the item is painted right now (preview-aware).
baseRectGridRectWhere the item was before the current gesture.
activeRect`GridRect \null`
isActiveboolean
isSelectedboolean
isShiftedbooleanTrue when this item moved in the preview because another item pushed it.
isTransferringbooleanTrue while the active item is being previewed in another canvas.
interaction`GridInteraction \null`

GridPositioning

type · dom/view.ts:78

How an element is placed: with a transform (default) or with left/top.

export type GridPositioning = 'transform' | 'absolute'

MountGridOptions

type · dom/mount.ts:38

Options for mountGrid: every GridControllerOptions field (controlled layout or uncontrolled defaultLayout, callbacks, solver settings, a transfer scope), the pointer gesture callbacks, and how items are rendered.

export type MountGridOptions<TData = unknown> = GridControllerOptions<TData> &
  GridPointerGestureOptions & {
    /**
     * Paint an item's content. Without a renderer each item shows its id as
     * text (set once, when the element is created).
     */
    renderItem?: GridItemRenderer<TData>
    /**
     * Create the element for an item. Defaults to a `div`. Use it to adopt
     * elements that already exist (custom elements do this).
     */
    createItemElement?: (item: GridItem<TData>) => HTMLElement
    /** Dispose the element of an item that left the layout. Defaults to `element.remove()`. */
    removeItemElement?: (element: HTMLElement, itemId: string) => void
    /**
     * `true` (default): the whole item element is a drag surface. `false`:
     * only descendants marked `data-gridla-drag-handle` start a move.
     */
    draggable?: boolean
    /** Edges to render built-in resize handles for. Default: none. */
    resizeEdges?: readonly GridResizeEdge[]
    /** Class name for built-in resize handles. */
    resizeHandleClassName?: string
    /**
     * Render the drop outline (`data-gridla-preview`). `true` creates a `div`;
     * pass an element to use it instead. It is shown only while a gesture has
     * an accepted preview. Default `false`.
     */
    preview?: boolean | HTMLElement
    /** Position elements with `transform` (default) or with `left`/`top`. */
    positioning?: GridPositioning
    /** Render the cursor-tracked rect while dragging instead of the solved preview. Default `true`. */
    followPointer?: boolean
  }
MemberTypeDescription
renderItem?GridItemRenderer\<TData\>Paint an item's content. Without a renderer each item shows its id as text (set once, when the element is created).
createItemElement?((item: GridItem\<TData\>) =\> HTMLElement)Create the element for an item. Defaults to a div. Use it to adopt elements that already exist (custom elements do this).
removeItemElement?((element: HTMLElement, itemId: string) =\> void)Dispose the element of an item that left the layout. Defaults to element.remove().
draggable?booleantrue (default): the whole item element is a drag surface. false: only descendants marked data-gridla-drag-handle start a move.
resizeEdges?ReadonlyArray\<GridResizeEdge\>Edges to render built-in resize handles for. Default: none.
resizeHandleClassName?stringClass name for built-in resize handles.
preview?`boolean \HTMLElement`
positioning?GridPositioningPosition elements with transform (default) or with left/top.
followPointer?booleanRender the cursor-tracked rect while dragging instead of the solved preview. Default true.

TransferScope

type · interaction/transfer.ts:26

Coordinates item moves between canvases. The pointer decides the target: the deepest registered canvas under the pointer that accepts the item previews the drop; releasing there commits it.

export type TransferScope = {
  /** Add a canvas. Returns a function that removes it again. */
  register: (registration: TransferRegistration) => () => void
  /** Called by the source canvas on every pointer move during a drag (client coordinates). */
  track: (sourceId: string, itemId: string, client: GridPoint) => void
  /** Called by the source canvas on release. Returns `true` when a transfer happened. */
  drop: (sourceId: string) => boolean
  /** Abandon the current transfer session and clear any target preview. */
  cancel: () => void
}
MemberTypeDescription
register(registration: TransferRegistration) =\> () =\> voidAdd a canvas. Returns a function that removes it again.
track(sourceId: string, itemId: string, client: GridPoint) =\> voidCalled by the source canvas on every pointer move during a drag (client coordinates).
drop(sourceId: string) =\> booleanCalled by the source canvas on release. Returns true when a transfer happened.
cancel() =\> voidAbandon the current transfer session and clear any target preview.