$ npm install cytoscape-popper
Popper allows you to dynamically align a div, e.g. a tooltip, to another element in the page. This extension allows you to use any popper library on Cytoscape elements. This allows you to create DOM elements positioned on or around Cytoscape elements. It is useful for tooltips and overlays, for example.
Integration examples:
Since Popper.js
has become @floating-ui
(https://floating-ui.com/docs/migration) and the API has changed a lot it becomes harder to support both versions
(for example TippyJS, that supports only the previous version), so instead of depending on a specific external version
this extension allows users to use any Popper library with providing popperFactory
function during initializing.
See FloatingUI or Popper.js sections.
Download the library:
npm install cytoscape-popper
,bower install cytoscape-popper
, orImport the library as appropriate for your project:
ES import:
import cytoscape from 'cytoscape';
import cytoscapePopper from 'cytoscape-popper';
function popperFactory(ref, content, opts) {
// See integration sections
}
cytoscape.use( cytoscapePopper(popperFactory) );
CommonJS require:
let cytoscape = require('cytoscape');
let cytoscapePopper = require('cytoscape-popper');
function popperFactory(ref, content, opts) {
// See integration sections
}
cytoscape.use( cytoscapePopper(popperFactory) ); // register extension
AMD:
require(['cytoscape', 'cytoscape-popper'], function( cytoscape, popper ){
function popperFactory(ref, content, opts) {
// See integration sections
}
// register extension
popper(popperFactory)(cytoscape);
});
This extension exposes popper()
and popperRef()
functions and provided popperFactory()
. These functions are defined for both the core and for elements, so you can call cy.popper()
or ele.popper()
for example.
Each function takes an options object, as follows:
cy.popper( options )
or ele.popper( options )
: Make a PopperInstance
for the specified core Cytoscape instance or the specified element. This is useful for positioning a div relative to or on top of a core instance or element.
cy.popperRef( options )
or ele.popperRef( options )
: Make a PopperInstance
for the specified core Cytoscape instance or the specified element. A Popper virtual element is useful only for positioning, as it represent the target rather than the content. This is useful for cases where you want to create a new Popper instance manually via Popper constructor createPopper()
or where you need to pass a popperRef
object to another library like Tippy.js.
options
content
: The HTML content of the popper. May be a DOM Element
reference or a function that returns one.renderedPosition
: A function that can be used to override the rendered Cytoscape position of the Popper target. This option is mandatory when using Popper on the core. For an element, the centre of its bounding box is used by default.renderedDimensions
: A function that can be used to override the rendered Cytoscape bounding box dimensions considered for the popper target (i.e. cy
or ele
). It defines only the effective width and height (bb.w
and bb.h
) of the Popper target. This option is more often useful for elements rather than for the core.popper
: The PopperOptions
object. These options are used in provided popperFactory
.import cytoscape from 'cytoscape';
import cytoscapePopper from 'cytoscape-popper';
import {
computePosition,
flip,
shift,
limitShift,
} from '@floating-ui/dom';
function popperFactory(ref, content, opts) {
// see https://floating-ui.com/docs/computePosition#options
const popperOptions = {
// matching the default behaviour from Popper@2
// https://floating-ui.com/docs/migration#configure-middleware
middleware: [
flip(),
shift({limiter: limitShift()})
],
...opts,
}
function update() {
computePosition(ref, content, popperOptions).then(({x, y}) => {
Object.assign(content.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
update();
return { update };
}
cytoscape.use(cytoscapePopper(popperFactory));
popper()
example// create a basic popper on the first node
let popper1 = cy.nodes()[0].popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Popper content';
document.body.appendChild(div);
return div;
}
});
// create a basic popper on the core with custom options
let popper2 = cy.popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Popper content';
document.body.appendChild(div);
return div;
},
renderedPosition: () => ({ x: 100, y: 200 }),
popper: {
placement: 'bottom',
} // @flaoting-ui options (https://floating-ui.com/docs/middleware)
});
popperRef()
example// create a basic popper ref for the first node
let popperRef1 = cy.nodes()[0].popperRef();
// create a basic popper ref on the core
let popperRef2 = cy.popperRef({
renderedPosition: () => ({ x: 200, y: 300 })
});
popper()
examplelet node = cy.nodes().first();
let popper = node.popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Sticky Popper content';
document.body.appendChild( div );
return div;
}
});
let update = () => {
popper.update();
};
node.on('position', update);
cy.on('pan zoom resize', update);
import cytoscape from 'cytoscape';
import cytoscapePopper from 'cytoscape-popper';
import { createPopper } from '@popperjs/core';
cytoscape.use(cytoscapePopper(createPopper));
popper()
example// create a basic popper on the first node
let popper1 = cy.nodes()[0].popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Popper content';
document.body.appendChild(div);
return div;
},
popper: {} // my popper options here
});
// create a basic popper on the core
let popper2 = cy.popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Popper content';
document.body.appendChild(div);
return div;
},
renderedPosition: () => ({ x: 100, y: 200 }),
popper: {} // my popper options here
});
popperRef()
example// create a basic popper ref for the first node
let popperRef1 = cy.nodes()[0].popperRef();
// create a basic popper ref on the core
let popperRef2 = cy.popperRef({
renderedPosition: () => ({ x: 200, y: 300 })
});
popper()
examplelet node = cy.nodes().first();
let popper = node.popper({
content: () => {
let div = document.createElement('div');
div.innerHTML = 'Sticky Popper content';
document.body.appendChild( div );
return div;
}
});
let update = () => {
popper.update();
};
node.on('position', update);
cy.on('pan zoom resize', update);
This extension can also be used to enable Tippy.js tooltip functionality with Cytoscape. Any version of Tippy that is compatible with Popper v2 is compatible with this extension.
import cytoscape from 'cytoscape';
import popper from 'cytoscape-popper';
import tippy from 'tippy.js';
function tippyFactory(ref, content){
// Since tippy constructor requires DOM element/elements, create a placeholder
var dummyDomEle = document.createElement('div');
var tip = tippy( dummyDomEle, {
getReferenceClientRect: ref.getBoundingClientRect,
trigger: 'manual', // mandatory
// dom element inside the tippy:
content: content,
// your own preferences:
arrow: true,
placement: 'bottom',
hideOnClick: false,
sticky: "reference",
// if interactive:
interactive: true,
appendTo: document.body // or append dummyDomEle to document.body
} );
return tip;
}
cytoscape.use(cytoscapePopper(tippyFactory));
The creation of many Tippy
instances at once has performance implications, especially for large graphs. Create each instance on demand, e.g. on tap
. Use destroy()
instead of hide()
where possible.
let node = cy.nodes().first();
var tip = node.popper({
content: () => {
let content = document.createElement('div');
content.innerHTML = 'Tippy content';
return content;
},
});
tip.show();
Refer to Tippy.js documentation for more details.
This extension exports empty PopperInstance
and PopperOptions
interfaces allows to extend them according to the final Popper implementation.
@floating-ui
example:
import { ComputePositionConfig } from '@floating-ui/dom';
declare module 'cytoscape-popper' {
interface PopperOptions extends ComputePositionConfig {
}
interface PopperInstance {
update(): void;
}
}
npm run test
: Run Mocha tests in ./test
npm run build
: Build ./src/**
into cytoscape-popper.js
npm run watch
: Automatically build on changes with live reloading (N.b. you must already have an HTTP server running)npm run dev
: Automatically build on changes with live reloading with webpack dev servernpm run lint
: Run eslint on the sourceN.b. all builds use babel, so modern ES features can be used in the src
.
This project is set up to automatically be published to npm and bower. To publish:
npm run build:release
git commit -am "Build for release"
npm version major|minor|patch
git push && git push --tags
npm publish .
bower register cytoscape-popper https://github.com/cytoscape/cytoscape.js-popper.git
© 2010 - cnpmjs.org x YWFE | Home | YWFE