Solvers
Generated by website/scripts/generate-api.ts from packages/gridla/src. Do not edit by hand; run bun run generate in website/.
Move, resize, place, and transfer. Each solver takes a layout plus an intent, returns a new layout, and names the strategy that produced it. Inputs are never mutated.
import { edgeAlignedSlots, moveItem, placeItem, pushAndShrinkSiblings, resizeByShrinkingNeighbors, resizeItem, scaleSizeBetweenCanvases, transferItem } from 'gridla'
import type { MoveItemInput, NewGridItem, PlaceItemInput, ResizeItemInput, SolveOptions, SolveResult, TransferItemInput, TransferResult } from 'gridla'
Functions
edgeAlignedSlots
function · core/solver/shared.ts:295
Every position aligned to a sibling or canvas edge where item could sit.
export function edgeAlignedSlots<T>(
item: GridItem<T>,
baseItems: readonly GridItem<T>[],
bounds: GridBounds,
gap: number,
): GridItem<T>[]
moveItem
function · core/solver/move.ts:1196
Move one item to a requested position. Siblings are pushed, swapped,
reordered, or shrunk as needed. Returns a new layout; inputs are not
mutated.
export function moveItem<T = unknown>({
layout,
itemId,
position,
options,
}: MoveItemInput<T>): SolveResult<T>
placeItem
function · core/solver/place.ts:332
Insert an item into the layout. Give a position for a top-left intent
or a pointer for a cursor-centered intent. Returns a new layout.
export function placeItem<T = unknown>({
layout,
item: input,
position,
pointer,
options,
}: PlaceItemInput<T>): SolveResult<T>
pushAndShrinkSiblings
function · core/solver/move.ts:204
Push siblings forward along axis; when the chain hits the canvas edge,
shrink the pushed siblings proportionally (down to min or 40% of size).
export function pushAndShrinkSiblings<T>(
active: GridItem<T>,
baseItems: readonly GridItem<T>[],
bounds: GridBounds,
gap: number,
axis: GridAxis,
snapDistance: number,
): GridItem<T>[] | null
resizeByShrinkingNeighbors
function · core/solver/resize.ts:120
Trim neighbors that the resized rectangle newly collides with. Neighbors
that already overlapped the original rect, ghosts, and locked items are not
touched; a locked collision refuses the resize.
export function resizeByShrinkingNeighbors<T>({
baseItems,
gap,
original,
resized,
}: {
baseItems: readonly GridItem<T>[]
gap: number
original: GridItem<T>
resized: GridItem<T>
}): InternalResult<T> | null
resizeItem
function · core/solver/resize.ts:215
Resize one item. Provide edge + delta for interactive resizing, or
rect for programmatic resizing. Returns a new layout; inputs are not
mutated.
export function resizeItem<T = unknown>({
layout,
itemId,
edge,
delta,
rect,
options,
}: ResizeItemInput<T>): SolveResult<T>
scaleSizeBetweenCanvases
function · core/solver/transfer.ts:54
Scale a size authored against source so it covers the same fraction of
target. Used to keep items visually consistent across canvases.
export function scaleSizeBetweenCanvases(
size: GridSize,
source: GridLayout['canvas'],
target: GridLayout['canvas'],
): GridSize
transferItem
function · core/solver/transfer.ts:68
Move an item from source into target at pointer. Returns both updated
layouts. Neither input is mutated.
export function transferItem<T = unknown>({
source,
target,
itemId,
pointer,
size,
options,
}: TransferItemInput<T>): TransferResult<T>
Types
type · core/solver/move.ts:1183
Input for moveItem.
export type MoveItemInput<T = unknown> = {
layout: GridLayout<T>
itemId: string
/** Requested top-left position in canvas coordinates. */
position: GridPoint
options?: SolveOptions
}
NewGridItem
type · core/solver/place.ts:298
Item description accepted by placeItem. Position is optional.
export type NewGridItem<T = unknown> = GridItemSize & {
id: string
x?: number
y?: number
policy?: GridItem<T>['policy']
data?: T
}
type · core/solver/place.ts:311
Input for placeItem. Give position for a top-left intent, or pointer to
center the item on a point.
export type PlaceItemInput<T = unknown> = {
layout: GridLayout<T>
/** Item to insert. Its `id` must not already be in the layout unless replacing it. */
item: NewGridItem<T>
/** Requested top-left. Overrides `item.x`/`item.y`. */
position?: GridPoint
/** Pointer location in canvas coordinates. The item is centered on it. */
pointer?: GridPoint
options?: SolveOptions
}
type · core/solver/resize.ts:195
Input for resizeItem. Use edge + delta for a drag, or rect for a
programmatic size.
export type ResizeItemInput<T = unknown> = {
layout: GridLayout<T>
itemId: string
/**
* Edge or corner being dragged. Required when `delta` is used; optional
* with `rect`, where it only informs edge snapping.
*/
edge?: GridResizeEdge
/** Pixel movement of the dragged edge. Ignored when `rect` is given. */
delta?: GridPoint
/** Requested rectangle. Missing fields default to the current values. */
rect?: Partial<GridRect>
options?: SolveOptions
}
SolveOptions
type · core/solver/shared.ts:39
Tuning shared by every solver: minimum gap, snapping, and tracing. Every field
is optional.
export type SolveOptions = {
/** Minimum distance kept between neighbors. Default `0`. */
gap?: number
/** Distance within which edges attract. Default `24`. */
snapDistance?: number
/**
* When `false`, alignment snapping is skipped so the item tracks the
* requested position exactly. Bounds, gap, and constraint rules still
* apply. Default `true`.
*/
snap?: boolean
/** Receives one event per solve describing which strategy produced the result. */
onTrace?: TraceCallback
}
SolveResult
type · core/solver/shared.ts:71
Outcome of a solve. layout is always a complete, independent copy.
export type SolveResult<T = unknown> = {
/** `false` means the request could not be honored; `layout` then equals the input. */
accepted: boolean
/** The layout after the operation. */
layout: GridLayout<T>
/**
* The active item as it appears in `layout`. When `accepted` is false this
* is the rejected candidate, useful for showing where a drop would land.
*/
item: GridItem<T>
/** Which strategy produced this result. */
strategy: SolveStrategy
/** True when siblings moved or resized to make room. */
shiftedSiblings: boolean
}
type · core/solver/transfer.ts:18
Input for transferItem: move an item out of one layout and into another at a
pointer location.
export type TransferItemInput<T = unknown> = {
/** Layout the item currently lives in. */
source: GridLayout<T>
/** Layout the item is dropped into. */
target: GridLayout<T>
itemId: string
/** Pointer location in the target's canvas coordinates. */
pointer: GridPoint
/**
* Size the item should take in the target. Defaults to the source size
* scaled by the ratio of the two canvases' inner areas.
*/
size?: Partial<GridSize>
options?: SolveOptions
}
TransferResult
type · core/solver/transfer.ts:39
Result of transferItem: both layouts after the operation, the item as placed
in the target, and the strategy used. When accepted is false both layouts
equal their inputs.
export type TransferResult<T = unknown> = {
accepted: boolean
/** Source layout without the item. Equals the input when rejected. */
source: GridLayout<T>
/** Target layout with the item. Equals the input when rejected. */
target: GridLayout<T>
item: GridItem<T>
strategy: SolveStrategy
shiftedSiblings: boolean
}