# p5-wrapper/react architecture

> Architecture diagram of the GitHub repository p5-wrapper/react, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/p5-wrapper/react
- Repository: https://github.com/p5-wrapper/react

## Overview

##### Main Components and Architecture

a) Core Components Layer:
- ReactP5Wrapper (main component)
- ReactP5WrapperGuard (error boundary handling)
- ReactP5WrapperWithSketch (sketch implementation)

b) Contract Layer:
- Input Props definitions
- Canvas Instance management
- Sketch type definitions
- Wrapper interfaces

c) Utils Layer:
- Canvas instance management utilities
- Props comparison utilities
- Error handling utilities

**┌─────────────────────────────────────────────────────────────┐**
- React Application                        │
- ┬─────────────────────────────────┘
- ↓
**┌─────────────────────────────────────────────────────────────┐**
- ReactP5Wrapper Component                  │
- ┤
- ┌─────────────────┐    ┌──────────────────┐    ┌─────────┐ │
- Error Boundary   │    │Sketch Container   │    │Fallback │ │
- (WrapperGuard)   │    │(WithSketch)      │    │UI       │ │
- ┘    └──────────────────┘    └─────────┘ │
- ┬─────────────────────────────────┘
- ↓
**┌─────────────────────────────────────────────────────────────┐**
- Canvas Management                        │
- ┬────────────────────────┬──────────────────┤
- Create Instance │Update Instance         │Remove Instance    │
- ┴────────────────────────┴──────────────────┘
- ↓
**┌─────────────────────────────────────────────────────────────┐**
- P5.js Instance                       │
- ┘

##### Data Flow Indicators

- Props flow from React application to P5 wrapper
- Canvas instance lifecycle management
- Error propagation paths
- State updates and re-renders

##### Implementation Details

- Instance mode P5.js usage
- React 18+ compatibility
- Peer dependency requirements
- TypeScript generics support
- Child component rendering capabilities

##### Integration Points

Connection points for:
- External P5.js plugins
- React application integration
- Custom sketch implementations
- Props updating mechanism

- The component's architecture
- How data flows through the system
- Where and how P5.js integrates with React
- Error handling and fallback mechanisms
- The relationship between different parts of the system

Remember to maintain a balance between showing enough detail to understand the system while keeping it clear and readable. The focus should be on the component's role as a bridge between React and P5.js.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph React["React Application Layer"]
        App["React App"]
    end

    subgraph Core["Core Components Layer"]
        ReactP5["ReactP5Wrapper"]:::component
        Guard["ReactP5WrapperGuard"]:::error
        WithSketch["ReactP5WrapperWithSketch"]:::component
    end

    subgraph Contracts["Contract Layer"]
        InputProps["Input Props"]:::contract
        CanvasRef["Canvas Instance Ref"]:::contract
        SketchDef["Sketch Definitions"]:::contract
        WrapperInt["Wrapper Interface"]:::contract
        P5Canvas["P5 Canvas Instance"]:::contract
    end

    subgraph Utils["Utilities Layer"]
        CreateCanvas["Create Canvas Instance"]:::util
        UpdateCanvas["Update Canvas Instance"]:::util
        RemoveCanvas["Remove Canvas Instance"]:::util
        PropsEqual["Props Comparison"]:::util
        ErrorHandler["Error Handling"]:::util
    end

    subgraph P5["P5.js Layer"]
        P5Instance["P5.js Instance"]:::external
    end

    App --> ReactP5
    ReactP5 --> Guard
    Guard --> WithSketch
    WithSketch --> CreateCanvas
    WithSketch --> UpdateCanvas
    WithSketch --> RemoveCanvas
    
    CreateCanvas --> P5Instance
    UpdateCanvas --> P5Instance
    RemoveCanvas --> P5Instance

    WithSketch -.-> InputProps
    WithSketch -.-> CanvasRef
    WithSketch -.-> SketchDef
    WithSketch -.-> WrapperInt
    WithSketch -.-> P5Canvas
    
    Guard -.-> ErrorHandler
    WithSketch -.-> PropsEqual

    %% Styles
    classDef component fill:#3498db,stroke:#2980b9,color:white
    classDef error fill:#e74c3c,stroke:#c0392b,color:white
    classDef contract fill:#f1c40f,stroke:#f39c12,color:black
    classDef util fill:#2ecc71,stroke:#27ae60,color:white
    classDef external fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Click Events
    click ReactP5 "https://github.com/P5-wrapper/react/blob/master/src/components/ReactP5Wrapper.tsx"
    click Guard "https://github.com/P5-wrapper/react/blob/master/src/components/ReactP5WrapperGuard.tsx"
    click WithSketch "https://github.com/P5-wrapper/react/blob/master/src/components/ReactP5WrapperWithSketch.tsx"
    click InputProps "https://github.com/P5-wrapper/react/blob/master/src/contracts/InputProps.ts"
    click CanvasRef "https://github.com/P5-wrapper/react/blob/master/src/contracts/CanvasInstanceRef.ts"
    click SketchDef "https://github.com/P5-wrapper/react/blob/master/src/contracts/Sketch.ts"
    click WrapperInt "https://github.com/P5-wrapper/react/blob/master/src/contracts/Wrapper.ts"
    click P5Canvas "https://github.com/P5-wrapper/react/blob/master/src/contracts/P5CanvasInstance.ts"
    click CreateCanvas "https://github.com/P5-wrapper/react/blob/master/src/utils/createCanvasInstance.ts"
    click UpdateCanvas "https://github.com/P5-wrapper/react/blob/master/src/utils/updateCanvasInstance.ts"
    click RemoveCanvas "https://github.com/P5-wrapper/react/blob/master/src/utils/removeCanvasInstance.ts"
    click PropsEqual "https://github.com/P5-wrapper/react/blob/master/src/utils/propsAreEqual.ts"
    click ErrorHandler "https://github.com/P5-wrapper/react/blob/master/src/utils/logErrorBoundaryError.ts"
```
