Skip to main content
Version: 4.3

Web Components

JointJS works inside a shadow root, so a diagram can be wrapped in a web component (a custom element with its own shadow DOM). The same applies to the frameworks and libraries that render into a shadow root, such as Lit, Stencil, Angular with ViewEncapsulation.ShadowDom, or Salesforce Lightning Web Components.

JointJS​

The custom element below attaches a shadow root and renders a paper into it. Click an element or the blank area, hover over an element, or drag the end of the link to another element.

<diagram-element></diagram-element>


Styles of the page do not reach into a shadow root. The paper adds its own stylesheet inside its element, so a diagram built with @joint/core needs no extra CSS. Styles of your own (e.g. for the container of the paper) go inside the shadow root, for example in a <style> element as in the example above.

JointJS+​

JointJS+ components rendered inside the shadow root need joint-plus.css in the shadow root too. Add it with a <link> element:

const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/path/to/joint-plus.css';
shadowRoot.appendChild(link);

Alternatively, adopt it with the adoptedStyleSheets API. A constructable stylesheet is created once and shared by all instances of the component, so the CSS is parsed only once, and no element is added to the shadow root:

// Vite: the `?inline` suffix imports the CSS as a string.
import jointPlusCSS from '@joint/plus/joint-plus.css?inline';

const jointPlusSheet = new CSSStyleSheet();
jointPlusSheet.replaceSync(jointPlusCSS);

// In `connectedCallback()`:
shadowRoot.adoptedStyleSheets = [jointPlusSheet];

The example below renders a paper, an ui.Inspector and an ui.Tooltip inside the shadow root. Hover over an element to show its tooltip, and click it to edit it in the inspector.

import { dia, shapes, ui } from '@joint/plus';

import './styles.css';

// Styles of the component. Page styles do not reach into the shadow root.
const css = `
    :host { position: relative; display: block; font-family: system-ui, sans-serif; }
    .paper { position: absolute; inset: 0; }
    .inspector {
        position: absolute; right: 12px; top: 12px; width: 240px; height: 200px;
        border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; overflow: hidden;
    }
`;

class DiagramElement extends HTMLElement {
    connectedCallback() {
        const shadowRoot = this.attachShadow({ mode: 'open' });
        shadowRoot.innerHTML = `
            <style>${css}</style>
            <div class="paper"></div>
            <div class="inspector"></div>
        `;

        // The JointJS+ stylesheet must be loaded inside the shadow root too.
        const stylesheet = document.createElement('link');
        stylesheet.rel = 'stylesheet';
        stylesheet.href = 'https://docs.jointjs.com/lib/4.3/joint-plus.css';
        shadowRoot.prepend(stylesheet);

        const graph = new dia.Graph({}, { cellNamespace: shapes });
        const paper = new dia.Paper({
            el: shadowRoot.querySelector('.paper'),
            model: graph,
            cellViewNamespace: shapes,
            width: '100%',
            height: '100%',
            gridSize: 10,
            background: { color: '#f8fafc' }
        });

        const rect = (x, y, text) => new shapes.standard.Rectangle({
            position: { x, y },
            size: { width: 120, height: 60 },
            attrs: {
                root: { 'data-tooltip': `Click to edit "${text}"` },
                body: { fill: '#fff', stroke: '#e2e8f0', rx: 12, ry: 12 },
                label: { text, fill: '#0f172a', fontSize: 14 }
            }
        });

        const source = rect(40, 60, 'Source');
        const target = rect(40, 220, 'Target');
        const link = new shapes.standard.Link({
            source: { id: source.id },
            target: { id: target.id }
        });
        graph.addCells([source, target, link]);

        // The tooltip renders inside the shadow root, where the stylesheet applies.
        new ui.Tooltip({
            rootTarget: paper.el,
            container: shadowRoot.querySelector('.paper'),
            target: '[data-tooltip]',
            direction: 'right',
            padding: 10
        });

        const inspectorEl = shadowRoot.querySelector('.inspector');
        let inspector = null;

        const openInspector = (cell) => {
            if (inspector) inspector.remove();
            inspector = new ui.Inspector({
                cell,
                inputs: {
                    attrs: {
                        label: {
                            text: { type: 'content-editable', label: 'Label' }
                        },
                        body: {
                            fill: {
                                type: 'color-palette',
                                label: 'Fill',
                                options: [
                                    { content: '#ffffff' },
                                    { content: '#dbeafe' },
                                    { content: '#d1fae5' },
                                    { content: '#fef3c7' }
                                ]
                            }
                        }
                    }
                }
            });
            inspectorEl.appendChild(inspector.el);
            inspector.render();
        };

        paper.on('element:pointerclick', (elementView) => openInspector(elementView.model));
        openInspector(source);
    }
}

// The sandbox may run this file again, and a name can be defined only once.
if (!customElements.get('diagram-element')) {
    customElements.define('diagram-element', DiagramElement);
}


Stay in the know

Be where thousands of diagramming enthusiasts meet

Star us on GitHub