JointJS Port Hover CSS Styling

by Roman Bruckner

HTML

<html>

  <head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.6.3/joint.css" />
  </head>

  <body>
    <!-- content -->
    <div id="paper"></div>

    <!-- dependencies -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.6.3/joint.js"></script>

  </body>

</html>

CSS

.joint-port rect:hover {
  fill: orange;
}

.joint-port [magnet="my-magnet"] {
  cursor: grab;
}

JavaScript

var graph = new joint.dia.Graph({}, {
  cellNamespace: joint.shapes
});

const paper = new joint.dia.Paper({
  el: document.getElementById('paper'),
  width: 800,
  height: 900,
  model: graph,
  cellViewNamespace: joint.shapes,
  gridSize: 10,
  async: true,
  sorting: joint.dia.Paper.sorting.APPROX,
  snapLinks: true,
  linkPinning: false,
  magnetThreshold: 'onleave',
  defaultLink: () => new joint.shapes.standard.Link()
});

// Example

var el1 = new joint.shapes.standard.Rectangle({
  position: {
    x: 10,
    y: 10
  },
  size: {
    width: 100,
    height: 150
  },
  ports: {
    groups: {
      right: {
        position: 'right',
        attrs: {
          portRect: {
            width: 20,
            height: 20,
            fill: 'red',
            stroke: '#333',
            strokeWidth: 2,
            magnet: true
          }
        },
        markup: [{
          tagName: 'rect',
          selector: 'portBody',
          groupSelector: 'portRect'
        }],
      }
    },
    items: [{
      id: 'p1',
      group: 'right',
    }, {
      id: 'p2',
      group: 'right',
      attrs: {
        portRect: {
           magnet: 'my-magnet' // use custom name to disable the default styling
        }
      }
    }]
  },

});


graph.resetCells([el1]);

paper.unfreeze();