Skip to content

Conversation

@samuelgja
Copy link
Contributor

@samuelgja samuelgja commented Aug 13, 2025

Description

This PR refactors joint-react and related packages to fully support the upcoming @joint/plus-react integration.
The most important change is the introduction of controlled mode in GraphProvider, allowing React state to directly control elements and links instead of relying solely on dia.Graph.

🚩 Major Change: Controlled Mode in GraphProvider

  • GraphProvider can now run in controlled or uncontrolled mode:
    • Uncontrolled mode (default): elements and links are added once on mount, and dia.Graph is the single source of truth.
    • Controlled mode (new): if onElementsChange and/or onLinksChange are provided, React state becomes the source of truth.
      • Updates to the graph trigger the callbacks.
      • Developers can synchronize elements/links with Redux, Zustand, or plain React useState.
  • This enables workflows such as:
    • React-driven graph state (e.g., Redux-managed elements/links).
    • Two-way binding between dia.Graph and external state.
    • Fine-grained testing and debugging with predictable state transitions.

Other Key Changes

- Dependency updates

  • Updated eslint, typescript, @types/react, @types/react-dom, and related plugins.
  • Adjusted peerDependencies for modern React/TypeScript.

- Graph & Store

  • createStore.destroy() now takes a flag to avoid destroying external graphs.
  • createStoreData returns structured UpdateResult (diffIds, areElementsChanged, areLinksChanged) instead of raw sets.

- Contexts

  • New: PaperProviderConfigContext for custom paper rendering (e.g., PaperScroller).
  • New: RenderElementContext for React render function sharing.
  • PaperContext now stores:
    • elementViews instead of raw SVG nodes.
    • paperHTMLElement ref for container replacement.

- Hooks

  • Removed use-create-paper and use-paper-element-renderer.
  • Added use-element-views for managing ElementView lifecycle.
  • Simplified use-measure-node-size (no more single-measurement restriction).
  • use-remove-cell renamed to use-remove-element.

- Rendering pipeline

  • PaperProvider renders via elementViews instead of recordOfSVGElements.
  • Added resize and event dispatch via mvc.Listener.

- Tests & Stories

  • Added tests for controlled mode in GraphProvider.
  • Updated storybook examples to use explicit React element types.
  • Added Redux tutorial story demonstrating controlled integration.
  • Removed legacy overwriteDefaultPaperElement tests.

- Utilities

  • New makeOptions helper to filter out undefined values.
  • Unified setElements / setLinks under cell-utilities.
  • Added smarter updateGraph logic for replacing/updating cells.

Motivation and Context

The introduction of controlled mode marks a major step forward:
Developers can now choose whether the graph should be the single source of truth, or whether React should manage state and sync changes into the graph.

This bridges the gap between JointJS internals and React ecosystem practices, enabling seamless integration with external state managers like Redux, better testability, and predictable React rendering.


Screenshots

N/A — changes are internal and behavioral, no direct UI differences.

@samuelgja samuelgja marked this pull request as draft August 13, 2025 09:49
@samuelgja
Copy link
Contributor Author

Still draft, we should maybe merge after plus react package will be done / merged :)

@samuelgja samuelgja self-assigned this Aug 13, 2025
@samuelgja samuelgja requested a review from kumilingus August 13, 2025 09:50
…pdates

- Added a new utility function `updateGraph` to handle the addition and update of cells in the graph without removing existing elements.
- Introduced helper functions `getType` and `getAttributes` to retrieve cell attributes based on the cell type.
- Enhanced the handling of cell types to replace cells when their type changes.
- Updated the `getCellId` function in link utilities for consistency in ID retrieval.
- Modified the subscriber handler to support batch notifications and updated the subscribe method signature.
- Updated dependencies in `yarn.lock` to include new packages and versions for improved functionality and compatibility.
@samuelgja samuelgja force-pushed the feat/sync-with-joint-plus branch from e9b5d41 to 9e0df9d Compare September 2, 2025 08:24
…tions for improved readability and maintainability
…ncy resolutions and type definitions

fix(joint-react): update highlighter components to use optional forwardedRef
… to use a centralized event mapper

- Introduced PAPER_EVENTS_MAPPER to map event names to their corresponding joint events and handlers.
- Replaced the switch-case structure in handleEvent with a more maintainable approach using the event mapper.
- Updated event handlers to accept parameters and return structured objects for easier access to event data.
- Added handlePaperEvents function to set up event listeners on the paper instance using the new mapping.
…ook decorators

- Modified ESLint configuration to remove additional hooks setting.
- Enhanced Storybook decorators to support args and improve type safety.
- Refactored GraphProvider to utilize useImperativeApi for better lifecycle management.
- Removed deprecated useHighlighter hook and replaced its functionality with useImperativeApi.
- Cleaned up PaperProvider and Paper components for better performance and readability.
- Introduced new utility functions in object-utilities for managing options and dependencies.
- Added tests for useImperativeApi to ensure proper initialization and cleanup.
- Updated the PaperEvents interface to include a `graph` parameter in all event handler signatures.
- Modified the PAPER_EVENTS_MAPPER to accommodate the new `graph` parameter in event handlers.
- Adjusted the handlePaperEvents function to accept a `graph` argument and pass it to the event handlers.
- Updated unit tests to reflect changes in event handler signatures and ensure proper functionality with the new `graph` parameter.
…r element manipulation

- Deleted the useUpdateElement hook from the codebase.
- Introduced useCellActions hook to manage setting and removing elements and links in the graph.
- Updated various components and stories to utilize the new useCellActions hook instead of the deprecated useUpdateElement.
- Enhanced the updateCell function to handle both adding and updating cells in the graph.
- Added comprehensive tests for the useCellActions hook to ensure correct functionality for setting and removing elements and links.
@samuelgja samuelgja closed this Oct 15, 2025
@samuelgja
Copy link
Contributor Author

closing, many commits. Here is cleaned and updated PR: #3077

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