Coder Social home page Coder Social logo

angular-reactflow-examples's Introduction

AngularFlow

This project demonstrates how to use React and Reactflow with Angular. These examples are compatible with Angular 14+ and React 17+. The examples are based on the Reactflow examples.

React Component Directive

The react-component directive allows you to use React components in Angular templates. Logic is handled in the React component, while the Angular template is used to pass data to the React component.

Warning: This directive is a standalone directive. If you want to use it in your project, you need to modify the code to fit your needs.

Import Reactflow styles

@import '@reactflow/core/dist/style.css';
@import '@reactflow/controls/dist/style.css';
@import '@reactflow/minimap/dist/style.css';

React Component Wrapper

Just pass react component and props to the react-component directive. The directive will create a wrapper component that will render the react component.

<div id="root">
  <div
    [reactComponent]="ReactFlowSubFlows"
    (onEvent)="onEvent($event)"
    [props]="props"
    class="react-flow-container"
  ></div>
</div>

Example Implementation for Older Angular Versions

An example implementation for older Angular versions based react-component.directive.ts file.

import { ApplicationRef, Directive, ElementRef, inject, Injector, Input, OnChanges, OnInit, OnDestroy } from '@angular/core';
import { ComponentProps, createElement, ElementType, React } from 'react';
import { createRoot } from 'react-dom/client';

@Directive({
	// eslint-disable-next-line @angular-eslint/directive-selector
	selector: '[reactComponent]',
})
export class ReactComponentDirective<Comp extends ElementType> implements OnChanges, OnDestroy {
	@Input() public reactComponent: Comp;
	@Input() public props: ComponentProps<Comp>;
	@Input() public children: any;

	private root: any;

	constructor(private appRef: ApplicationRef, private elementRef: ElementRef) {
		this.root = createRoot(this.elementRef.nativeElement);
	}

	public ngOnChanges(): void {
		this.root.render(
			createElement(
				this.reactComponent,
				this.props,
				this.children?.map((child: any) =>
					createElement(child as React.FC<any>, {
						key: child.name,
						...child.props,
					})
				)
			)
		);
	}

	public ngOnDestroy(): void {
		this.root.unmount();
	}
}

angular-reactflow-examples's People

Contributors

relliv avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

mohankrishna225

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.