Fragiola

API: primitives (@fragiola/flow-react, @fragiola/flow-vue, @fragiola/flow-svelte, @fragiola/flow-angular)

The flow ref

A flow ref gives the model and the engine of a root to code outside it, such as a toolbar beside the flow, a menu or a test.

A flow ref is how code outside Flow.Root reaches its model and engine: pass it as the root's flowRef, and ref.current is { model, engine } while the root is mounted, null otherwise. In Vue it is :flow-ref, and flow.value holds them; in Svelte, flowRef={flow} and flow.current, as here; in Angular, [flowRef]="flow" and flow.current, a signal's value. Inside the root, useFlow is simpler.

useFlowRef

useFlowRef<T, F>() keeps a ref in the component and re-renders it when the root mounts or unmounts, so flow.current may be read in render (a toolbar next to the flow) or in handlers.

import { Flow, useFlowRef } from "@fragiola/flow-react";

export function Editor() {
    const flow = useFlowRef<Types, typeof features>();
    return (
        <>
            <button type="button" disabled={!flow.current} onClick={() => flow.current?.engine.run("fit-view")}>
                Fit
            </button>
            <Flow.Root<Types, typeof features> flowRef={flow} features={features} defaultNodes={nodes}>
                …
            </Flow.Root>
        </>
    );
}

createFlowRef

createFlowRef<T, F>() makes one outside a component (a module, a test). It does not re-render anything when it attaches.

const flow = createFlowRef<Types, typeof features>();
// <Flow.Root flowRef={flow} …>
flow.current?.model.run("node.add", { type: "step", position: { x: 0, y: 0 }, data: { label: "New" } });

Types

FlowRef<T, F>, the ref:

fieldtypedescription
currentFlowRefValue<T, F> | nullthe root's model and engine while it is mounted

FlowRefValue<T, F>, what current holds:

fieldtypedescription
modelFlowModel<T, F>the root's model (model)
engineFlowEngine<T, F>the root's engine (engine)

In Vue, a FlowRef<T, F> holds the same FlowRefValue<T, F> in value, not current (Vue components). In Svelte, a FlowRef<T, F> holds it in current, as React's (Svelte components).