Skip to content

diagram: convert drawing components to function components - #722

Merged
bpowers merged 1 commit into
mainfrom
drawing-function-components
Jun 7, 2026
Merged

bpowers merged 1 commit into
mainfrom
drawing-function-components

Conversation

@bpowers

@bpowers bpowers commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

Converts the twelve React.PureComponent classes under src/diagram/drawing/ (everything except Canvas) to memo'd function components, continuing the front-end cleanup after #721. The intent is zero behavior change; the byte-identical Rust-vs-TS SVG parity test (tests/svg-rendering.test.ts) plus the existing geometry suites are the regression net.

Highlights:

  • Trivial leaves (Arrowhead, Group, Cloud, Alias, Aux, Stock, Module, Flow, Connector): mechanical conversion. Handlers passed to memo'd children (Label, Arrowhead) are wrapped in useCallback so React.memo actually holds during drags; handlers attached only to DOM elements stay plain closures.
  • Sparkline: the hand-rolled recache()/cachedSeries instance-field cache becomes a useMemo (now correctly keyed on width/height too, fixing a latent staleness bug).
  • Label: transient pointer-gesture fields (pointerId/inMove) become refs — deliberately not state, so a drag never re-renders the label.
  • EditableLabel: the constructor-created Slate editor becomes a useState lazy initializer; Canvas mounts it per edit session, so init-once-per-mount semantics are preserved.
  • Connector: static methods move to module-level functions (isStraightLine, intersectElementStraight, connectorBounds), matching how the rest of the file's geometry already lives; tests import them directly.
  • docs/dev/typescript.md: now prefers function components, replacing the stale class-components-by-default guidance.

Canvas stays a class on purpose: its hooks migration is sequenced after the canvas-interaction.ts state migration (gated on e2e gesture tests, tech-debt #65) to avoid migrating it twice.

Test plan

  • Full pre-commit suite (Rust fmt/clippy/tests, TS lint/build/tsc/tests, WASM build, pysimlin) passes.
  • @simlin/diagram: 72 suites / 1023 tests green, including the five-model byte-identical SVG parity test that exercises every converted component through renderToString.
  • Two independent adversarial review passes (hunk-by-hunk diff audit, handler-identity/pointer-capture analysis, StrictMode double-invoke check) found no material issues.

Convert the twelve PureComponent classes under drawing/ (everything but
Canvas) to memo'd function components. None of them held real state:
Sparkline's hand-rolled recache()/cachedSeries instance-field cache
becomes a useMemo (now correctly keyed on width/height too, not just
series identity), Label's transient pointer-gesture fields become refs
(deliberately not state -- a drag must not re-render the label), and
EditableLabel's constructor-created Slate editor becomes a useState lazy
initializer with the same init-once-per-mount semantics.

Handlers passed to memo'd children (Label, Arrowhead) are wrapped in
useCallback so React.memo actually holds; handlers attached only to DOM
elements stay as plain closures. Connector's static methods move to
module-level functions (isStraightLine, intersectElementStraight,
connectorBounds), matching how the rest of the file's geometry already
lives; tests now import them directly. Canvas stays a class for now --
its hooks migration is sequenced after the canvas-interaction.ts state
migration (gated on e2e gesture tests) to avoid migrating it twice.

The SVG parity test (byte-identical Rust-vs-TS rendering) plus the
existing geometry suites are the regression net; render output is
unchanged. docs/dev/typescript.md now prefers function components,
replacing the stale class-components-by-default guidance.
@claude

claude Bot commented Jun 7, 2026

Copy link
Copy Markdown

Reviewed the PR. The class-to-function-component conversion looks correct and behavior-preserving:

  • useCallback is applied exactly where handlers cross a React.memo boundary (Label/Arrowhead); plain closures elsewhere.
  • Static Connector.* methods become module-level functions and the test imports are updated accordingly; no other callers exist.
  • The Sparkline useMemo correctly includes width/height in its dep array (the latent staleness fix called out in the PR description).
  • Label uses useRef for transient pointer state so drags don't re-render.
  • EditableLabel's useState lazy initializer preserves the constructor's init-once semantics; Canvas conditionally renders it per edit session so mount/unmount cycles still bound the editor lifetime.
  • Coordinate inlinings in Stock/Module/Aux/Flow indicator and sparkline transforms are arithmetically equivalent to the previous element.x + w/2 - 1 etc. forms.

No findings.

Overall correctness: correct.

@bpowers
bpowers merged commit d6e0d7d into main Jun 7, 2026
11 of 13 checks passed
@bpowers
bpowers deleted the drawing-function-components branch June 7, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant