React Diagram - Customize the Diagram Component

This article contains code samples that demonstrate how to customize the Diagram UI component and its items.

Change the Component's Background Color

Override the following CSS rule to change the Diagram component's background color:

styles.css
  • #diagram .dxdi-canvas .grid {
  • fill: lightGray !important;
  • }

Change the Diagram's background color

Change the Color of Connector Points

Add the following CSS rules to change the color of connector points:

styles.css
  • .dx-diagram .dxdi-control .dxdi-canvas .connection-point:not(.selector) {
  • stroke: red;
  • }
  • .dx-diagram .dxdi-control .dxdi-canvas .connection-point:not(.selector).active {
  • fill: red;
  • }

Change the connector points color

Customize the Context Menu's Items

The example below demonstrates how to show default and custom commands in the context menu depending on the selected item:

Code
  • import React from 'react';
  • import Diagram from 'devextreme-react/diagram';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.diagramRef = React.createRef();
  • this.onSelectionChanged = this.onSelectionChanged.bind(this);
  • this.onCustomCommand = this.onCustomCommand.bind(this);
  • }
  • onSelectionChanged(e) {
  • // Displays the "showGrid" and "snapToGrid" commands when a user selects no items
  • if (e.items.length === 0)
  • e.component.option("contextMenu.commands", ["showGrid", "snapToGrid"]);
  • else
  • // Displays the "fontName", "fontSize", and "selectShapes" commands when a user selects a shape
  • if (e.items[0].itemType === "shape")
  • e.component.option("contextMenu.commands", ["fontName", "fontSize", {name: "selectShapes", text: "Select All Shapes"}]);
  • else
  • // Displays the "connectorLineStart", "connectorLineEnd", and "selectConnectors" commands when a user selects a connector
  • e.component.option("contextMenu.commands", ["connectorLineStart", "connectorLineEnd", {name: "selectConnectors", text: "Select All Connectors"}]);
  • }
  • onCustomCommand(e) {
  • if (e.name == "selectShapes") {
  • var shapes = e.component.getItems().filter(function(item) {
  • return (item.itemType === "shape")
  • });
  • e.component.setSelectedItems(shapes);
  • }
  • if (e.name == "selectConnectors") {
  • var connectors = e.component.getItems().filter(function(item) {
  • return (item.itemType === "connector")
  • });
  • e.component.setSelectedItems(connectors);
  • }
  • }
  • render() {
  • return (
  • <Diagram id="diagram" ref={this.diagramRef} onSelectionChanged={this.onSelectionChanged} onCustomCommand={this.onCustomCommand}>
  • </Diagram>
  • );
  • }
  • }
  • export default App;

Specify a Command's Location on the Main Toolbar

Use the location property to set the location of a command or separator on the main toolbar. This property accepts one of the following values:

  • center
    Places the command in the center of the main toolbar.

  • before
    Places the command before the central element(s).

  • after
    Places the command after the central element(s).

The Diagram UI component displays commands with the same location property value in the order in which you listed them. When the main toolbar cannot accommodate all commands, the component places excess commands in the overflow menu.

The example below demonstrates how to customize the main toolbar:

App.js
  • import React from 'react';
  • import Diagram, { HistoryToolbar, ViewToolbar, MainToolbar, Command, Toolbox } from 'devextreme-react/diagram';
  • import { confirm } from 'devextreme/ui/dialog';
  • import 'whatwg-fetch';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.diagramRef = React.createRef();
  • }
  •  
  • onCustomCommand(e) {
  • if(e.name === 'clear') {
  • var result = confirm('Are you sure you want to clear the diagram? This action cannot be undone.', 'Warning');
  • result.then(
  • function(dialogResult) {
  • if(dialogResult) {
  • e.component.import('');
  • }
  • }
  • );
  • }
  • }
  • componentDidMount() {
  • var diagram = this.diagramRef.current.instance();
  • fetch('data/diagram-flow.json')
  • .then(function(response) {
  • return response.json();
  • })
  • .then(function(json) {
  • diagram.import(JSON.stringify(json));
  • })
  • .catch(function() {
  • throw 'Data Loading Error';
  • });
  • }
  • render() {
  • return (
  • <Diagram id="diagram" ref={this.diagramRef} onCustomCommand={this.onCustomCommand}>
  • <MainToolbar visible={true}>
  • <Command name="undo" location="before" />
  • <Command name="redo" location="before" />
  • <Command name="separator" location="before" />
  • <Command name="copy" location="center" />
  • <Command name="paste" location="center" />
  • <Command name="cut" location="center" />
  • <Command name="separator" location="after" />
  • <Command name="clear" icon="clearsquare" text="Clear Diagram" location="after" />
  • </MainToolbar>
  • <Toolbox visibility="hidden" />
  • <HistoryToolbar visible={false} />
  • <ViewToolbar visible={false} />
  • </Diagram>
  • );
  • }
  • }
  • export default App;

Customize main toolbar