/** * `NodeGraph/GraphPort` — four sides, and a crowded node. * * The `.mp-graph` wrapper matters: every rule in `css/graph.css` is scoped * under the canvas root, so a port rendered outside it gets the inline * fallbacks (a round dot at the painted size) but NOT the `::after` hit * region. Wrapping here is what makes the debug toggle below tell the truth. * * EXPECTED in this page's console, once per port: * * [React Flow]: Handle: No node id found. Make sure to only use a Handle * inside a custom Node. * * `useNodeId()` returns null outside a mounted canvas, and these stories show * the port off canvas on purpose — that is the whole subject. The warning is * React Flow telling the truth about the setup, not a defect in GraphPort, and * it goes away in `NodeGraph/NodeGraph` (task 1.11) where the same ports render * inside real nodes. Do not silence it with a fake node context: that would * hide the same warning when it is real. */ import { Switch } from "@multiplatform.one/forms"; import { Preset, Surface } from "@multiplatform.one/theme"; import type { Meta, StoryObj } from "@storybook/react-native-web-vite"; import { useState } from "react"; import { XStack, YStack } from "tamagui"; // React Flow's own sheet FIRST: it carries `.react-flow__handle-left/right/ // top/bottom`, the rules that translate a handle onto the node edge, and // nothing in this package reproduces them. Without it a port renders at its // static position and lands on top of its own label. Task 1.11 does this once // inside `NodeGraphWeb`; a story that renders ports off canvas has to do it // itself or it shows a layout the product never has. import "@xyflow/react/dist/style.css"; import "../../css/graph.css"; import { getHandlePosition, Position, ReactFlowProvider } from "../react-flow/xy"; import type { HandleBounds, InternalNodeBase } from "../react-flow/xy"; import { GraphNode } from "./GraphNode"; import { GraphPort } from "./GraphPort"; import { paintedPortSize, portGeometry, portHitSize } from "./handleGeometry"; import type { GraphNodePort, GraphNodeSizeKnob, GraphPortSide } from "./nodeMeasure"; import { SizableText } from "@multiplatform.one/components"; /** The size token whose side of `$true` reads each table row. */ const rowToken = { small: "$3", medium: "$4", large: "$5" } as const; const sides: GraphPortSide[] = ["left", "right", "top", "bottom"]; const crowded: GraphNodePort[] = [ { id: "rows", side: "left", direction: "in", label: "rows" }, { id: "schema", side: "left", direction: "in", label: "schema" }, { id: "opts", side: "left", direction: "in", label: "options" }, { id: "ok", side: "right", direction: "out", label: "ok" }, { id: "warn", side: "right", direction: "out", label: "warning" }, { id: "err", side: "right", direction: "out", label: "error" }, ]; const meta: Meta = { title: "NodeGraph/GraphPort", component: GraphPort, parameters: { status: { type: "beta" }, nativeGallery: { exempt: { from: "native", reason: "Every story here mounts a React Flow handle, a DOM element, outside a canvas, because the port off canvas is the subject. On device the port renders inside the canvas's WebView.", readInstead: "NodeGraph → NodeGraph, where the same ports render inside real nodes", }, }, docs: { description: { component: "A port is painted only — it draws a dot and declares a hit region, and decides nothing about whether a connection is legal. The painted dot is 8/10/12 by the size token and NEVER 44 (H1); the 44pt target is a ::after pseudo-element that grows outward (H2), so the rendered edge endpoint stays where the 8px dot put it. The circle survives borderRadius:none, because a square port reads as a resize grip.", }, }, }, argTypes: { side: { control: "inline-radio", options: sides }, direction: { control: "inline-radio", options: ["in", "out"] }, coarsePointer: { control: "boolean", description: "Touch surface — hit becomes 44" }, }, decorators: [ (Story) => ( ), ], }; export default meta; type Story = StoryObj; /** * Four sides, three size stops, both pointer kinds. The numbers under each dot * are the two that must never be conflated: painted, then hit. */ export const Main: Story = { render: () => ( {([true, false] as const).map((coarsePointer) => ( {coarsePointer ? "coarse pointer (touch)" : "fine pointer (mouse)"} {(["small", "medium", "large"] as const satisfies readonly GraphNodeSizeKnob[]).map( (size) => ( size {size} {sides.map((side) => ( {side} {paintedPortSize(size)} / {portHitSize({ size, coarsePointer })} ))} ), )} ))} ), }; /** * Six ports, three a side. H4's floor is what keeps this node tall enough for * six real targets; without it the discs would overlap and the wrong port * would win the drop. The radius toggle proves the circle is immune to the * knob at `none`, where a square dot would read as a resize grip. */ export const Crowded: Story = { render: () => { const [coarsePointer, setCoarsePointer] = useState(true); const [squareKnob, setSquareKnob] = useState(false); return ( {/* CASCADES, on purpose. `cascade={false}` starts from `defaultKnobs` and merges only the overrides, so it REPLACES the whole knob set — which silently discarded every Storybook global and made this page unfuzzable: the MPO-191 sweep measured a 10px dot and a 1px ring at every size and borderWidth stop, because the toggle's own Preset had reset them. Cascading merges the toggle onto the ambient knobs instead, which is what a demo of ONE override should do. */} ( )} /> ( )} /> ); }, }; /** * The 44pt region made visible, and the endpoint law made checkable by eye. * * Two things a unit spec cannot do. jsdom and happy-dom cannot resolve a * pseudo-element at all — `getComputedStyle(el, "::after")` returns an empty * declaration — so `GraphPort.spec.tsx` reads the RULE TEXT and says so. Here * the browser resolves it for real: the debug toggle paints the `::after` * itself, so what you see outlined is the element the finger will hit, not a * div drawn beside it. * * The readout underneath is the same `getHandlePosition` call the spec makes, * run in the browser, at hit 8 and at hit 44. The two rows must print the same * point. Under element growth they would differ by 18 on x — which is the * number design §7.1 gives for why the region is a pseudo-element. */ const hitDebugClass = "mp-graph-hit-debug"; /** * Story-local, and it touches no `.react-flow__` selector — `mp-graph-port` is * this package's own class, so `checkGraphVarsCoverage()` stays satisfied and * the debug paint cannot leak into the shipped sheet. */ const hitDebugCss = ` .${hitDebugClass} .mp-graph-port::after { background: currentColor; opacity: 0.14; outline: 1px dashed var(--mpo-graph-guide-color, currentColor); } `; function EndpointRow({ label, painted, hit }: { label: string; painted: number; hit: number }) { const geometry = portGeometry({ painted, hit, side: "left", nodeWidth: 190, nodeHeight: 72, offset: 36, }); const node = { id: "n1", position: { x: 300, y: 120 }, data: {}, measured: { width: 190, height: 72 }, internals: { positionAbsolute: { x: 300, y: 120 } }, } as unknown as InternalNodeBase; const handle = { id: "p1", type: "source", nodeId: "n1", position: Position.Left, ...geometry.box, } as unknown as HandleBounds; const point = getHandlePosition(node, handle, Position.Left); return ( {label} box {geometry.box.width} region {geometry.region.width} endpoint {point.x} , {point.y} ); } export const HitTarget: Story = { render: () => { const [debug, setDebug] = useState(true); return ( {/* biome-ignore lint/security/noDangerouslySetInnerHtml: story-local debug paint */}