JointJS: Custom Attribute To Set CSS Property

by Roman Bruckner

HTML

<html>

  <head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.7.4/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.7.4/joint.js"></script>

  </body>

</html>

CSS

.joint-element:hover .circle-body {
    fill: var(--color);
}

.joint-element:hover .circle-icon {
    stroke: white;
}

JavaScript

const namespace = { ...joint.shapes };
const graph = new joint.dia.Graph({}, { cellNamespace: namespace });
const paper = new joint.dia.Paper({
    el: document.getElementById('paper'),
    width: 800,
    height: 600,
    model: graph,
    async: true,
    sorting: joint.dia.Paper.sorting.APPROX,
    cellViewNamespace: namespace,
});


const el1 = new joint.shapes.standard.Rectangle({
    position: { x: 100, y: 100 },
    size: { width: 100, height: 200 },
    ports: {
        groups: {
            'top': {
                position: {
                    name: 'top',
                },
                attrs: {
                    labelText: {
                        transform: 'rotate(-90)  translate(-15, 0)',
                        textAnchor: 'end',
                        textVerticalAnchor: 'middle',
                    }
                },
                label: {
                    position: {
                        name: 'manual',
                    }
                }
            },
            'bottom': {
                position: {
                    name: 'bottom',
                },
                attrs: {
                    labelText: {
                        transform: 'rotate(-90)  translate(15, 0)',
                        textAnchor: 'start',
                        textVerticalAnchor: 'middle',
                    }
                },
                label: {
                    position: 'manual'
                }
            }
        },
        items: [{
            id: 'in1',
            group: 'top',
            attrs: {
                text: {
                    text: 'Top 1'
                }
            }
        }, {
            id: 'in2',
            group: 'top',
            attrs: {
                text: {
                    text: 'Top 2'
                }
            }
        }, {
            id: 'out1',
            group: 'bottom',
            attrs: {
                text: {
                    text: 'Bottom 1'
       ...