npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@foldworks/diagram

v0.1.0

Published

Compound directed-graph documents, layout strategies, scenes, and canvas interaction for Foldkit.

Readme

@foldworks/diagram

Compound directed-graph primitives for Foldkit applications. Use it to build workflow editors, state-machine visualizers, dependency graphs, or any canvas where nodes nest, connect, and sit beside free-form notes.

The package is layered so each piece can be used alone:

| Layer | Exports | | ----------- | ------------------------------------------------------------------------------------- | | Document | DiagramDocument, DocumentSchema, graph queries, validateDocument | | Operations | createDiagramOperations(policy) for immutable edits with adapter rules | | Layout | createLayeredLayout, createFreeformLayout, createSceneLayout, routeEdges | | Scene | DiagramScene, LayoutStrategy, hit testing, coordinate conversion | | Interaction | Diagram — a Foldkit submodel for selection, dragging, connecting, panning, and zoom |

Documents

A document is normalized: nodes, edges, and annotations are flat arrays. Nesting uses parentId, so a compound state, a group, or a subflow is just a node with children. Positions are relative to the parent's content origin; a node with a position is pinned, and layouts place the rest.

type DiagramDocument<NodeData, EdgeData, AnnotationData = NoteData> = {
  nodes: ReadonlyArray<{ id; parentId?; data; position?; size?; ports? }>;
  edges: ReadonlyArray<{ id; source: Endpoint; target: Endpoint; data }>;
  annotations: ReadonlyArray<{ id; parentId?; position?; size?; attachedTo?; data }>;
};

Cycles and self-loops are valid. Adapters that need a DAG opt in with allowCycles: false, or check wouldCreateCycle and topologicalOrder.

Operations and policy

createDiagramOperations returns immutable edits that return undefined when a change is rejected. The policy decides what is legal for your domain:

const operations = createDiagramOperations<StateData, TransitionData, NoteData>({
  canNest: (_document, child, parent) => parent === undefined || parent.data.kind === "compound",
  canConnect: (_document, source) => source.data.kind !== "final",
  canDelete: (node) => node.data.kind !== "initial",
});

operations.reparentNode(document, "billing", "details", { x: 0, y: 80 });
operations.removeElements(document, ["details"]); // cascades to children and edges

Layout strategies

A layout strategy is any (document) => DiagramScene. The scene holds absolute geometry keyed by document ids and never contains semantic data.

  • createLayeredLayout ranks each container independently after reversing feedback edges, orders ranks with barycenter sweeps, and sizes containers around their children. It supports Down and Right directions.
  • createFreeformLayout uses pinned positions and fills a grid for the rest.
  • createSceneLayout(config, placer) accepts your own child placer while reusing measurement, nesting, ports, and edge routing.

Edges route as straight, curved, or orthogonal paths. Parallel and opposing edges fan out, and self-loops curl off the node's corner. Use pathForPoints or smoothPathForPoints to draw them.

Interaction

Diagram is a Foldkit submodel. It owns the viewport, selection, and the active gesture; the document stays in your model and changes through OutMessages.

const Model = S.Struct({ document: MachineDocument, canvas: Diagram.Model });

// view
h.div([...Diagram.canvasAttributes({ model: model.canvas, toParentMessage }, h)], [
  h.div([h.Style({ transform: Diagram.worldTransform(model.canvas) })], [
    ...nodes.map((node) => h.div([
      ...Diagram.elementAttributes({
        model: model.canvas,
        toParentMessage,
        id: node.id,
        anchor: rectCenter(node),
      }, h),
    ], [/* content */])),
  ]),
]);

// update
Diagram.OutMessage.match(outMessage, {
  MovedElements: ({ ids, deltaX, deltaY, dropX, dropY }) => { /* pin or reparent */ },
  RequestedConnection: ({ source, x, y }) => { /* nodeAt(scene, { x, y }) */ },
  RequestedDelete: ({ ids }) => { /* operations.removeElements */ },
  ...
});

Lift Diagram.subscriptions so pointer movement and Escape are tracked while a gesture is active. maybeDragPreview and maybeConnectionPreview expose in-flight gestures so the view can re-run the layout with preview positions. Drop points are reported in world coordinates; resolve them with nodeAt, toRelative, and relativePosition.

Arrow keys nudge the selection, Shift moves ten units, Delete and Backspace request removal, and Enter activates the focused element.