Primitives / Interaction

Pressing and dragging

Press turns pointer and keyboard activation of one element into one fact. Foldkit's declarative pointer attributes carry no button, pointer id, or click detail, so Press.events is a Mount that reports what the element saw (PointerDown, PointerUp, PointerCancelled, KeyDown, KeyUp, KeyCancelled, Clicked), and the Bundle's update decides: primary button only, one pointer at a time, pointerleave and pointercancel cancel, Enter and Space with a repeat ignored, a click with detail 0 (keyboard on a native control, or assistive technology) counts, and the ghost click that follows a touch is ignored inside a window of clickSuppressionMs that a Command on Effect's clock closes. Enter and Space are default-prevented on the element, so a native control does not also click and Space does not scroll. Activation is the OutMessage Pressed { pointerType, modifiers }, and the placement must handle it:

ts
import { Press } from 'foldkit-primitives/interaction'

const Button = Bundle.declare(Press.bundle, 'saveButton')
const pressed = CardPage.assemble(
  CardPage.at(Button, {
    args: { clickSuppressionMs: 50 },
    onOut: () => model => ({ model, commands: [save(model)] }),
  }),
)
const Activate = Press.behavior(Button)(CardSlots)<CardModel, CardMessage>({
  target: 'save',
  disabled: model => model.saving,
})

The Behavior attaches the Mount to the target slot, writes data-pressed while the element is down for styling, and marks a disabled target aria-disabled, which the Mount reads at event time, so no remount is needed. A disabled element refuses new presses (a click, a pointer down, a key down) but still hears a press that began while it was enabled end: its release or key up arrives as a cancel (PointerCancelled, KeyCancelled), so the press ends without activating. That matters for a control that disables itself mid-press, such as a stepper at its bound. The Model slice is { pressed, pointerId, key, suppressing, generation }; only pressed is meant for a view. modifiers is { shiftKey, ctrlKey, metaKey, altKey } as the pointer release, the key release or the click that completed the press reported them, so a range or a toggle needs no Mount of its own. A placement is one pressed state for one element: Pressed says how it was pressed, not which of several rows under a shared container was.

LongPress is holding for thresholdMs. It reads the same facts Press.events reports, so it needs no Mount of its own; the threshold is a Command on Effect's clock carrying a generation, and a release before it fires makes its Elapsed a no-op. LongPressed { pointerType } is the OutMessage, required at placement. The Behavior writes data-holding while down. Press and LongPress on one slot are refused by the resolver, since both would mount PressEvents; a slot takes one of them.

SwipeActions is actions behind a row, with a way in that needs no pointer. Its Model holds which side is open (start is revealed by moving the row toward the end, right in English); fullSwipe, flick and full are its args. SwipeActions.decide(model, release, args) is the rule for a SwipeEnded (from foldkit-primitives/dom's Swipe, which is not part of this Bundle): past fullSwipe of the row it runs the first action when full is set, reported as FullSwiped { side } after the row closes; otherwise a flick opens the side it points to or closes the row when it points back, and a slower release opens a side once it is past half of that side's actions. A side with no actions never opens, and right to left swaps the sides. The row's offset while a finger moves it is the parent's draft from Swiped. SwipeActions.keys({ open, rtl, toMessage }) is a Behaviors.Keys route for the row's keydown owner: the arrow toward the end of the row opens start (the other arrow, end), the opposite arrow closes an open side, Escape closes, and a press that would change nothing is declined, so Escape on a closed row still reaches the dialog around it. Draw the actions behind a closed row inert from open, so Tab skips them, and say the result through LiveAnnounce.

Repeat is a held stepper: the + and - of a number field. A mouse or Enter or Space steps as it goes down, and holding on steps again every intervalMs once delayMs has passed. A finger or a pen steps on a tap, or once held, so a scroll that starts on the button never steps; a click no pointer made (a screen reader's) steps once, and the click that follows a press does not step again. Stepped { count, input } is an OutMessage per step and Ended { count } one when a hold's steps stop (a single step has no end of its own), so a parent can fold a hold's steps into one History group and announce once. When update reaches a bound it sends Repeat.Message.Stopped() and the rest of that press steps no more. The delay and interval run on Effect's clock, so TestClock drives them, and a release makes a late timer a no-op.

ts
import { Repeat } from 'foldkit-primitives/interaction'

const More = Bundle.declare(Repeat.bundle, 'more')
const Stepper = SlotView.over(StepperSlots)<StepperModel, StepperMessage>()
const Holds = Repeat.behavior(More)(Stepper)({
  target: 'more',
  disabled: model => model.value >= 100,
})

Repeat.behavior(Declared)(binder)({ target, disabled? }) takes the same SlotView.over binder as Press.behavior, so the options are typed from it with nothing annotated, and the target supplies the activation obligation (bare slots with the generics named, (Slots)<Input, Message>({ target }), also still work). It attaches Press.events and writes data-holding; disabled marks the target aria-disabled, which refuses new presses and leaves a hold already under way to end on release. It shares Press's Mount, so a slot takes one of Press, LongPress and Repeat. The pointer leaving the button cancels the press, as it does for Press, so a hold ends there (Ended) and coming back with the button still down does not resume it: the next hold is a new press. A Repeat button may disable itself at its bound while held; its release still ends the hold.

Move is pointer movement as facts, a Mount in foldkit-primitives/dom: a primary-button pointer down captures the pointer and reports MoveStarted, each move reports Moved { deltaX, deltaY } from where it went down, and up reports MoveEnded { completed: true }. Cancel, lost capture, or Escape reports completed: false: the Escape is the drag's alone (it closes no dialog around it), is heard on the document only while a pointer is down, and the Model, which kept the box where it began, drops the draft. A second pointer and a secondary button are ignored; capture is released with the Mount. Move.behavior(Slots)<Input, Message>({ handle, toMessage }) attaches it to a Draggable slot and maps each fact into the view's Messages; a drag's meaning (a threshold, a snap, a reorder) is the parent's update.

The keyboard's way to do the same is two Behaviors.Keys routes for the handle's keydown owner. Move.nudge({ step?, largeStep?, toMessage }) takes Alt with an arrow (Shift for the larger step), and Move.resizeKeys(handle, { toMessage }) takes a resize handle's arrows, Home and End; each hands toMessage a { dx, dy }. update applies it with Box.moveBy, Box.moveEach or Box.resize from state, so a key and a drag obey the same bounds, grid, size limits and aspect ratio, and says the result through LiveAnnounce. Plain arrows are not taken, and a second route on one chord is a mixins:key-conflict.

ts
import { Move } from 'foldkit-primitives/interaction'
import { Box } from 'foldkit-primitives/state'

type Nudged = { readonly dx: number; readonly dy: number }

// Attach with Behaviors.Keys.behavior(...)({ slot: 'handle', routes: [nudge] }).
const nudge = Move.nudge<undefined, Nudged>({ step: 2, toMessage: delta => delta })

const card = { x: 10, y: 10, width: 20, height: 10 }
const area = { left: 0, top: 0, right: 100, bottom: 50 }
const applyNudge = ({ dx, dy }: Nudged) => Box.moveBy(card, dx, dy, { bounds: area })

Targets is which marked descendant of a container the pointer is over, and which one was pressed, as one Mount on the container rather than one per item. A descendant is marked by an attribute holding its id (a canvas's data-composition-node, a table's data-row), and the nearest marked ancestor of the event's target, inside the container, is the one reported: TargetHovered { id } once per change, null when there is none or the pointer leaves, and TargetPressed { id, shiftKey, altKey, ctrlKey, metaKey } on a click. preventDefault: true stops a press's default, such as a link navigating inside an editor's canvas. The Mount is Targets({ attribute, preventDefault }) in foldkit-primitives/dom; Targets.behavior(Slots)<Input, Message>({ container, attribute, preventDefault?, toMessage }) attaches it. targetOf(container, from, attribute) is the pure lookup.

PointerDrag is dragging one marked descendant onto another, marked the way Targets marks them. A primary press on one that moves more than DRAG_THRESHOLD (4px) starts a drag (DragStarted { id }; a finger's press starts one once held still HOLD_TO_DRAG_MS, 400ms, while one that moves HOLD_SLOP, 8px, first is a scroll, so a list of marked rows still scrolls under a finger and the page is kept still only once the drag is on); then, once per change, DraggedOver { over } says which other marked descendant the pointer is over and in which third of its box, { id, zone: 'before' | 'inside' | 'after' }, or null. Releasing is DragDropped { id, over }, and Escape, a cancelled pointer, or a button found released mid-drag is DragCancelled { id }. An Escape that cancels is heard on the way down and goes no further, so the focused element's own Escape (a canvas's deselect) does not also run; one pressed before the press became a drag ends the press and goes on to the page (a dialog closing on it), as it does for Marquee before the lasso starts. It follows the pointer that pressed and ignores a second one; the element under it is found by position, so a touch or a pen, which the browser captures to where it went down, drags too, once the marked elements have touch-action: none so a finger drags rather than scrolls. The click a drop ends with is swallowed, so a Targets on the same container does not also press. A press in contenteditable text selects text and starts no drag. It writes no roles, tabindex, or keys, so it sits beside a tree's or a listbox's own; for the keyboard's way to move an item, see Reorder below. Boxes are measured as the pointer moves and never kept; an element drawn as display: contents is measured by its first child (boxOf). The Mount is PointerDrag({ attribute }) in foldkit-primitives/dom; PointerDrag.behavior(Slots)<Input, Message>({ container, attribute, toMessage }) attaches it, and zoneOf(box, y) is the pure split.

A drag may also land somewhere other than among its own: with targets: [{ attribute: 'data-composition-node', within: '#page' }], what is dragged is still one of the container's marked descendants (a palette's tiles), and over is an element marked by that attribute inside the one within selects (the page's nodes). Several entries are several regions (a page and its layers), each with its own attribute. Its own tiles, and a matching element outside its region, are then over nothing. within is looked for nearest first, under the container's closest ancestor that holds a match, so two editors on one page each drop onto their own page. Such a drag's DraggedOver and DragDropped also carry region: whether the pointer is inside that element, so over: null with region: true is the empty space of one of the regions (an empty page, the space below its last node), and with false, elsewhere.

With gaps: true, a pointer over a container's own space (its padding, the gap between two children) is not over the container: the place is between its children, before the first whose middle along their flow (a column, a row, a right-to-left row) is past the pointer, else after the last, found by halving so that only a few of the children of that one container are measured. The children are the elements marked by attribute whose nearest marked ancestor is the container; one marked by part is a place inside a node, not a child. Past the last child of the whole region, over is null. The release is hit-tested again, so the place is where the pointer let go. Without it a container is one place, by thirds.

A place can be a named part of what it is over: with part: 'data-composition-region', an element marked by both attribute (or targets.attribute) and part is reported as { id, zone: 'inside', part }, whatever third of it the pointer is in. An editor draws a node's empty Region that way, so a drop can name the Region it goes into.

Edge scrolling. A list longer than its box cannot be dragged past the visible rows unless something scrolls it. edgeScroll: {} (or { edge, speed }) on PointerDrag, and on Marquee, scrolls the nearest ancestor that can scroll the way the pointer pulls, on a frame clock, while the press is held in its last edge pixels (default 40, at most a third of the box) at up to speed pixels a second (default 600), less the nearer the strip's inner side. A box at its end hands over to the next ancestor and then the page; held at the window's edge over a box that can still scroll that way, the box goes before the window does. Only a box the pointer is over is pulled (not one it is beside, above or below, over another panel), and only once it has stayed in the strip for 150ms, so a pointer passing through scrolls nothing. Nothing above a position: fixed box is, and the page is not while it or its body has overflow: hidden or clip (a scroll lock's, under a modal). After every step the place under the still pointer is reported again (DraggedOver), so the list moving is a place changing. Each step is scrollTo with behavior: 'instant', so a page's own scroll-behavior: smooth does not glide it; under prefers-reduced-motion: reduce the scroll moves in jumps every 150ms by what the same time would have moved it, not in a glide. The frame loop runs only while something scrolls, and ends with the drag, a cancel, or the Mount. The scrolling code is loaded when a Mount that asks for it starts, so a page whose drags never scroll does not carry it. PointerDrag scrolls nothing unless asked; Reorder asks for it itself (edgeScroll: false turns it off).

Following the pointer. A drag under way writes where the pointer is on the document root, so something that follows it needs no Message and no draw per move: --fk-drag-x and --fk-drag-y (viewport pixels, from the press and then each move) and data-fk-drag="mouse", "touch" or "pen" (DRAGGING names the three). All are removed on a drop, a cancel and when the Mount ends. DragPreview() in foldkit-primitives/dom is the Mount for what a drag carries: on an element the view draws with popover="manual" while the drag lasts, it shows it in the top layer, above every panel and clear of an ancestor's contain or transform, and leaves the rest to a rule such as position: fixed; top: 0; left: 0; transform: translate(var(--fk-drag-x), var(--fk-drag-y)) (physical top and left, since the pointer's coordinates are), pointer-events: none, and a lift above a finger under :root[data-fk-drag="touch"]. A browser without popovers leaves the element in the page flow. It settles with no animation, so reduced motion has nothing to turn off.