Primitives / State

Box: move and resize within limits

A box that a pointer drags (the Move Behavior in interaction reports the deltas) or a key nudges is held in the Model, and these functions turn a delta into the next box. They are pure, so a key and a drag obey the same limits and the Model is the only owner:

  • Box.moveBy(box, dx, dy, { bounds?, snap? }) and Box.moveEach(boxes, ids, dx, dy, limits) for several boxes at once. A group keeps its shape: the delta is limited so the whole group stays inside bounds, rather than each box stopping alone. snap lands the first id on the grid, on the axes that move, and the rest keep their distance from it.
  • Box.resize(box, handle, dx, dy, { bounds?, min?, max?, aspect?, snap? }) moves the edges a handle (n, e, se, ...) owns and keeps the others. The grid takes the moving edge first and the limits still win; with aspect an edge grows the other side about the box's middle and a corner follows the edge that moved further.
  • Box.moveKey(press, { step?, largeStep? }): Alt with an arrow, Shift for the larger step (ten steps by default). Box.resizeKey(press, handle, options): an arrow moves the handle's own edges (an east handle reads Left and Right only), and Home and End take its axes to the smallest and largest size the limits allow. Both are Option, none for another key or a press with Control or Meta; read them from a Pressed or an attribute-handler key, not a Mount.

A function that changes nothing returns the box it was given, so a nudge against the edge records no undo step (see History.push's equals). moveBy and moveEach refuse a delta that is not finite; resize reads an infinite one as "as far as the limits allow", which is what resizeKey sends for Home and End. Announcing the result ("Card moved to 40, 20") is the parent's: say it through LiveAnnounce in the same transition.

ts
import { Option } from 'effect'
import type { KeyPress } from 'foldkit-primitives/events'
import { Box } from 'foldkit-primitives/state'

const area = { left: 0, top: 0, right: 100, bottom: 50 }
const card = { x: 10, y: 10, width: 20, height: 10 }

const press: KeyPress = {
  key: 'ArrowRight',
  repeat: false,
  ctrl: false,
  shift: true,
  alt: true,
  meta: false,
}
const nudged = Option.map(Box.moveKey(press, { step: 100 }), ({ dx, dy }) =>
  Box.moveBy(card, dx, dy, { bounds: area }),
)
const grown = Box.resize(card, 'se', 500, 500, { bounds: area, aspect: 2 })

Locale and SelectionSet

Locale keeps one string, read from navigator.language at init with the configured default as fallback; SetLocale switches it. It does not follow later changes.

SelectionSet keeps string ids in first-selection order: Select keeps an existing id's position, Deselect removes, Toggle re-appends, ReplaceAll replaces (deduplicated), Clear empties. isSelected(model, id) reads membership. It stores ids only; it neither loads rows nor persists. For a selection a view's slots must reflect, with aria-selected and Shift ranges, see Selection in interaction.