Fragiola

Guides

Resizing nodes

Give nodes resize handles on their corners and sides, set limits for the whole flow or for one node, and keep an aspect ratio.

resize() and NodeResizer let a user resize a node from its corners and sides: each drag step is a node.resize, which writes the node's width and height (and, from a top or left handle, its position) into the document. The handles' look, when they show, and which nodes have them are yours.

Node resizer
Gallery

Turn it on

resize() is a feature, and NodeResizer and ResizeHandle come with it, on the same entry:

import { NodeResizer, resize } from "@fragiola/flow-react/resize";
import { drag } from "@fragiola/flow-react/drag";
import { selection } from "@fragiola/flow-react/selection";

const features = [selection(), drag(), resize({ minWidth: 80, minHeight: 60 })] as const;

function renderNode(node: FlowNode<Types>) {
    return (
        <Flow.Node<Types> node={node} className="node group">
            {node.data.label}
            <NodeResizer className="hidden group-data-selected:block" />
        </Flow.Node>
    );
}

NodeResizer goes inside Flow.Node: a box over the node holding a ResizeHandle at each corner and side by default. Give the handles their size and look; they are placed on their corner or along their side, and carry data-position (top-left, top, …, left).

The limits

resize()'s options hold for every node; a NodeResizer's props override them for its node:

optiondefaultdoes
minWidth, minHeight10the smallest size, in flow units
maxWidth, maxHeightnonethe largest size
keepAspectRatiofalsea resize keeps the node's width-to-height ratio
<NodeResizer minWidth={120} maxWidth={360} minHeight={80} maxHeight={240} />
<NodeResizer keepAspectRatio />

A child with extent: "parent" (sub-flows) is kept inside its parent while it resizes, and a parent never shrinks past such a child.

A resizer's direction ("horizontal" or "vertical") makes its node resize along one axis only, and resize({ snap: { x, y } }) puts a resize on a grid; a drag's snapGrid does not apply to a resize. See One direction, and a grid.

Your own handles

Pass the handles you want as children: the corners only, a single grip, your own look per position. RESIZE_POSITIONS lists every position, corners first:

import { NodeResizer, RESIZE_POSITIONS, ResizeHandle } from "@fragiola/flow-react/resize";

<NodeResizer keepAspectRatio className="resizer">
    {RESIZE_POSITIONS.map((position) => (
        <ResizeHandle
            key={position}
            position={position}
            className="size-2.5 rounded-sm border border-palette-ring bg-palette-base data-[position=top]:h-1.5 data-[position=bottom]:h-1.5 data-[position=left]:w-1.5 data-[position=right]:w-1.5"
        />
    ))}
</NodeResizer>;

<NodeResizer>
    {/* aria-hidden: a pointer-only grip; with keyboard(), name it instead (below) */}
    <ResizeHandle position="bottom-right" aria-hidden className="size-3 cursor-nwse-resize" />
</NodeResizer>;

A corner is drawn above the two sides it joins, so a press on it resizes both dimensions.

One handle, one direction, and lines

A ResizeHandle takes a direction of its own ("horizontal", "vertical"), which overrides its resizer's for that handle, for a press and for the arrows: a node can grow freely from its corner while its sides each move one dimension. variant="line" marks a side's handle as a line (data-variant="line", a corner is always "handle"), so your styles draw it as a thin bar along the whole side:

<NodeResizer>
    <ResizeHandle position="bottom-right" aria-label="Resize note" className="size-2.5 bg-palette-ring" />
    <ResizeHandle position="right" variant="line" direction="horizontal" aria-label="Resize note: width" className="w-0.5 cursor-ew-resize" />
    <ResizeHandle position="bottom" variant="line" direction="vertical" aria-label="Resize note: height" className="h-0.5 cursor-ns-resize" />
</NodeResizer>

The line is a styling marker only: its structural style is a side handle's, and its look is yours (ResizeHandle).

From the keyboard

Install keyboard() beside resize() and every resize handle takes focus (tabindex="0", from the engine: without both features nothing changes in the tab order). On a focused handle, the arrows move the sides that handle moves, by the keyboard's step (5 flow units by default), Shift + arrow by its fastStep (20):

handlekeys that resize
left, rightArrow Left, Arrow Right
top, bottomArrow Up, Arrow Down
a cornerthe four arrows, each moving one of its two sides

A handle's own direction (or else its resizer's) holds for the keys too: a corner of a "horizontal" handle answers Arrow Left and Arrow Right only.

Every limit holds as for a drag: minWidth to maxHeight, the resizer's own, the aspect ratio, the parent's box and its children. At a limit the key does nothing (and the page does not scroll); an arrow across the handle's sides, or with Ctrl, Alt or ⌘, is left to the page. Each press is one final node.resize (one undo step) and a resize event with source: "keyboard".

A focused handle needs a name a screen reader can read. A default NodeResizer (no children) renders eight handles that cannot be named, all focusable once keyboard() is installed: pass your own ResizeHandle children with an aria-label each (aria-hidden on a focusable element hides what has focus):

import { keyboard } from "@fragiola/flow-react/keyboard";
import { NodeResizer, ResizeHandle, resize } from "@fragiola/flow-react/resize";

const features = [selection(), drag(), resize(), keyboard()] as const;

<NodeResizer>
    <ResizeHandle position="right" aria-label={`Width of ${node.data.title}`} className="w-1.5 focus-visible:outline-2" />
    <ResizeHandle position="bottom" aria-label={`Height of ${node.data.title}`} className="h-1.5 focus-visible:outline-2" />
    <ResizeHandle position="bottom-right" aria-label={`Size of ${node.data.title}`} className="size-2.5 focus-visible:outline-2" />
</NodeResizer>;

Say the new size in your live region from the resize event (Keyboard and accessibility).

Show the handles while selected

Render the resizer only when the node should be resizable, or hide it with a style on the node's data-selected. While a node is resized, the node and its resizer carry data-resizing.

[data-flow-part="node"]:not([data-selected]) [data-flow-part="resizer"] {
    display: none;
}

[data-flow-part="resize-handle"] {
    width: 10px;
    height: 10px;
    background: var(--fl-handle-bg);
    border: 1px solid var(--fl-handle-line);
}

React to a resize

node.resize is transient during the drag and final on the release, like a drag's moves: an undo history makes one step of it, and the engine's resize event ({ nodeId, width, height }) tells the final size (source: "pointer" for a drag, "keyboard" for an arrow). The steps of a resize are its transient node.resize commands, seen by onNodesChange and model.subscribe; the root's onResizeStart, onResizeEnd and onResizeCancel frame them (Root).

engine.subscribe((event) => {
    if (event.type === "resize") save(event.nodeId, event.width, event.height);
});

Code resizes a node with the same command. It needs no feature: node.resize is the core's, and resize() is its gesture.

model.run("node.resize", { nodeId: "note", width: 240, height: 160 });