Skip to content

Commit e8b291f

Browse files
committed
add more examples
1 parent c0570a6 commit e8b291f

File tree

8 files changed

+30860
-9135
lines changed

8 files changed

+30860
-9135
lines changed

README.md

Lines changed: 119 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ cytoscape-popper
55

66
## Description
77

8-
A Cytoscape.js extension for integrating [Popper.js](https://popper.js.org/) ([demo](https://cytoscape.github.io/cytoscape.js-popper)) ([tippy demo](https://cytoscape.github.io/cytoscape.js-popper/demo-tippy.html))
8+
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.
9+
10+
Integration examples:
11+
- Floating UI - [demo](https://cytoscape.github.io/cytoscape.js-popper), [usage](#usage-with-floating-ui)
12+
- Popper.js@2 - [demo](https://cytoscape.github.io/cytoscape.js-popper/demo-popper.html), [usage](#usage-with-popperjs--deprecated-)
13+
- Tippy.JS - [demo](https://cytoscape.github.io/cytoscape.js-popper/demo-tippy.html), [usage](#usage-with-tippyjs)
14+
15+
16+
## Migration from v2
17+
18+
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
19+
(for example TippyJS, that supports only the previous version), so instead of depending on a specific external version
20+
this extension allows users to use any Popper library with providing `popperFactory` function during initializing.
21+
22+
See [FloatingUI](#usage-with-floating-ui) or [Popper.js](#usage-with-popperjs--deprecated-) sections.
23+
924

10-
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 (mostly `@floating-ui` (previously `Popper.js`) or `Tippy.js`) 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.
1125

1226
## Dependencies
1327

@@ -30,8 +44,7 @@ import cytoscape from 'cytoscape';
3044
import cytoscapePopper from 'cytoscape-popper';
3145

3246
function popperFactory(ref, content, opts) {
33-
// use floating-ui or Tippy here. See demo
34-
return {}
47+
// See integration sections
3548
}
3649

3750
cytoscape.use( cytoscapePopper(popperFactory) );
@@ -44,8 +57,7 @@ let cytoscape = require('cytoscape');
4457
let cytoscapePopper = require('cytoscape-popper');
4558

4659
function popperFactory(ref, content, opts) {
47-
// use floating-ui or Tippy here. See demo
48-
return {}
60+
// See integration sections
4961
}
5062

5163
cytoscape.use( cytoscapePopper(popperFactory) ); // register extension
@@ -56,16 +68,13 @@ AMD:
5668
```js
5769
require(['cytoscape', 'cytoscape-popper'], function( cytoscape, popper ){
5870
function popperFactory(ref, content, opts) {
59-
// use floating-ui or Tippy here. See demo
60-
return {}
71+
// See integration sections
6172
}
6273
// register extension
6374
popper(popperFactory)(cytoscape);
6475
});
6576
```
6677

67-
Plain HTML/JS has the extension registered for you automatically, because no `require()` is needed.
68-
6978
## API
7079

7180
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.
@@ -84,15 +93,30 @@ Each function takes an options object, as follows:
8493

8594
## Usage with @floating-ui
8695

87-
8896
### Initializing
8997

9098
``` js
9199
import cytoscape from 'cytoscape';
92100
import cytoscapePopper from 'cytoscape-popper';
93-
import {computePosition} from '@floating-ui/dom';
101+
import {
102+
computePosition,
103+
flip,
104+
shift,
105+
limitShift,
106+
} from '@floating-ui/dom';
94107

95108
function popperFactory(ref, content, opts) {
109+
// see https://floating-ui.com/docs/computePosition#options
110+
const popperOptions = {
111+
// matching the default behaviour from Popper@2
112+
// https://floating-ui.com/docs/migration#configure-middleware
113+
middleware: [
114+
flip(),
115+
shift({limiter: limitShift()})
116+
],
117+
...opts,
118+
}
119+
96120
function update() {
97121
computePosition(ref, content, opts).then(({x, y}) => {
98122
Object.assign(content.style, {
@@ -138,7 +162,6 @@ let popper2 = cy.popper({
138162
renderedPosition: () => ({ x: 100, y: 200 }),
139163
popper: {
140164
placement: 'bottom',
141-
middlewares: [shift()],
142165
} // @flaoting-ui options (https://floating-ui.com/docs/middleware)
143166
});
144167
```
@@ -181,6 +204,89 @@ node.on('position', update);
181204
cy.on('pan zoom resize', update);
182205
```
183206

207+
## Usage with Popper.js (deprecated)
208+
209+
### Initializing
210+
211+
``` js
212+
import cytoscape from 'cytoscape';
213+
import cytoscapePopper from 'cytoscape-popper';
214+
import { createPopper } from '@popperjs/core';
215+
216+
cytoscape.use(cytoscapePopper(createPopper));
217+
```
218+
219+
### `popper()` example
220+
221+
``` js
222+
// create a basic popper on the first node
223+
let popper1 = cy.nodes()[0].popper({
224+
content: () => {
225+
let div = document.createElement('div');
226+
227+
div.innerHTML = 'Popper content';
228+
229+
document.body.appendChild(div);
230+
231+
return div;
232+
},
233+
popper: {} // my popper options here
234+
});
235+
236+
// create a basic popper on the core
237+
let popper2 = cy.popper({
238+
content: () => {
239+
let div = document.createElement('div');
240+
241+
div.innerHTML = 'Popper content';
242+
243+
document.body.appendChild(div);
244+
245+
return div;
246+
},
247+
renderedPosition: () => ({ x: 100, y: 200 }),
248+
popper: {} // my popper options here
249+
});
250+
```
251+
252+
### `popperRef()` example
253+
254+
``` js
255+
// create a basic popper ref for the first node
256+
let popperRef1 = cy.nodes()[0].popperRef();
257+
258+
// create a basic popper ref on the core
259+
let popperRef2 = cy.popperRef({
260+
renderedPosition: () => ({ x: 200, y: 300 })
261+
});
262+
```
263+
264+
### Sticky `popper()` example
265+
266+
```js
267+
let node = cy.nodes().first();
268+
269+
let popper = node.popper({
270+
content: () => {
271+
let div = document.createElement('div');
272+
273+
div.innerHTML = 'Sticky Popper content';
274+
275+
document.body.appendChild( div );
276+
277+
return div;
278+
}
279+
});
280+
281+
let update = () => {
282+
popper.update();
283+
};
284+
285+
node.on('position', update);
286+
287+
cy.on('pan zoom resize', update);
288+
```
289+
184290
## Usage with Tippy.js
185291

186292
This extension can also be used to enable [Tippy.js](https://github.com/atomiks/tippyjs) tooltip functionality with Cytoscape. Any version of Tippy that is compatible with Popper v2 is compatible with this extension.
@@ -237,17 +343,6 @@ tip.show();
237343

238344
Refer to [Tippy.js](https://atomiks.github.io/tippyjs/) documentation for more details.
239345

240-
## v3 changes
241-
242-
Since `Popper.js` has become `@floating-ui` and the API has changed a lot it becomes harder to support both versions
243-
(for example TippyJS, that supports only the previous version), so instead of depending on a specific external version
244-
this extension allows users to use any Popper library with providing `popperFactory` function during initializing.
245-
246-
This version dropped the external dependency and changed the initializing api from `cytoscape.use(cytoscapePopper)` to `cytoscape.use(cytoscapePopper(popperFactory))`
247-
248-
See popperFactory examples to save backward-compatibility with v2
249-
250-
251346
## Typescript
252347

253348
This extension exports empty `PopperInstance` and `PopperOptions` interfaces allows to extend them according to the final Popper implementation.

demo-popper.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE>
2+
3+
<html>
4+
5+
<head>
6+
<title>cytoscape-popper + Popper.js@2 demo</title>
7+
8+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
9+
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
10+
11+
<!-- for testing with local version of cytoscape.js -->
12+
<!--<script src="../cytoscape.js/build/cytoscape.js"></script>-->
13+
14+
<script src="https://unpkg.com/@popperjs/core@2"></script>
15+
<script src="cytoscape-popper.js"></script>
16+
17+
<style>
18+
body {
19+
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
20+
font-size: 14px
21+
}
22+
23+
#cy {
24+
position: absolute;
25+
left: 0;
26+
top: 0;
27+
bottom: 0;
28+
right: 0;
29+
z-index: 1;
30+
}
31+
32+
h1 {
33+
opacity: 0.5;
34+
font-size: 1em;
35+
font-weight: bold;
36+
}
37+
38+
.popper-div { /* just an example */
39+
border: 1px solid red;
40+
background: #fff;
41+
z-index: 9999;
42+
padding: 0.25em;
43+
pointer-events: none;
44+
}
45+
</style>
46+
47+
<script>
48+
document.addEventListener('DOMContentLoaded', function () {
49+
// Popper from @popperjs/core
50+
cytoscape.use(cytoscapePopper(Popper.createPopper));
51+
52+
var cy = window.cy = cytoscape({
53+
container: document.getElementById('cy'),
54+
55+
style: [
56+
{
57+
selector: 'node',
58+
style: {
59+
'content': 'data(id)'
60+
}
61+
},
62+
63+
{
64+
selector: 'edge',
65+
style: {
66+
'curve-style': 'bezier',
67+
'target-arrow-shape': 'triangle'
68+
}
69+
}
70+
],
71+
72+
elements: {
73+
nodes: [
74+
{ data: { id: 'a' } },
75+
{ data: { id: 'b' } }
76+
],
77+
edges: [
78+
{ data: { id: 'ab', source: 'a', target: 'b' } }
79+
]
80+
},
81+
82+
layout: {
83+
name: 'grid'
84+
}
85+
});
86+
87+
var a = cy.getElementById('a');
88+
var b = cy.getElementById('b');
89+
var ab = cy.getElementById('ab');
90+
91+
var makeDiv = function(text){
92+
var div = document.createElement('div');
93+
94+
div.classList.add('popper-div');
95+
96+
div.innerHTML = text;
97+
98+
document.body.appendChild( div );
99+
100+
return div;
101+
};
102+
103+
var popperA = a.popper({
104+
content: function(){ return makeDiv('Sticky position div'); },
105+
});
106+
107+
var updateA = function(){
108+
popperA.update();
109+
};
110+
111+
a.on('position', updateA);
112+
cy.on('pan zoom resize', updateA);
113+
114+
var popperB = b.popper({
115+
content: function(){ return makeDiv('One time position div'); },
116+
});
117+
118+
var popperAB = ab.popper({
119+
content: function(){ return makeDiv('Sticky position div'); },
120+
});
121+
122+
var updateAB = function(){
123+
popperAB.update();
124+
};
125+
126+
ab.connectedNodes().on('position', updateAB);
127+
cy.on('pan zoom resize', updateAB);
128+
});
129+
</script>
130+
</head>
131+
132+
<body>
133+
<h1>cytoscape-popper + Popper.js@2 demo</h1>
134+
<div id="cy"></div>
135+
</body>
136+
137+
</html>

demo-tippy.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<html>
44

55
<head>
6-
<title>cytoscape-popper.js tippy demo</title>
6+
<title>cytoscape-popper + TippyJS demo</title>
77

88
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
99
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
@@ -139,7 +139,7 @@
139139
</head>
140140

141141
<body>
142-
<h1>cytoscape-popper tippy demo</h1>
142+
<h1>cytoscape-popper + TippyJS demo</h1>
143143
<div id="cy"></div>
144144
</body>
145145

0 commit comments

Comments
 (0)