Skip to content

Commit

Permalink
Merge pull request #83 from Atrue/v3
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkfranz authored Mar 5, 2024
2 parents 81a272c + 3acb0f2 commit a3b0b7e
Show file tree
Hide file tree
Showing 13 changed files with 15,195 additions and 11,164 deletions.
246 changes: 214 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ cytoscape-popper

## Description

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))
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:
- Floating UI - [demo](https://cytoscape.github.io/cytoscape.js-popper), [usage](#usage-with-floating-ui)
- Popper.js@2 - [demo](https://cytoscape.github.io/cytoscape.js-popper/demo-popper.html), [usage](#usage-with-popperjs--deprecated-)
- Tippy.JS - [demo](https://cytoscape.github.io/cytoscape.js-popper/demo-tippy.html), [usage](#usage-with-tippyjs)


## Migration from v2

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](#usage-with-floating-ui) or [Popper.js](#usage-with-popperjs--deprecated-) sections.


Popper.js allows you to dynamically align a div, e.g. a tooltip, to another element in the page. This extension allows you to use Popper.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.

## Dependencies

* Cytoscape.js ^3.2.0
* Popper.js ^2.0.0


## Usage instructions
Expand All @@ -28,49 +41,184 @@ ES import:

```js
import cytoscape from 'cytoscape';
import popper from 'cytoscape-popper';
import cytoscapePopper from 'cytoscape-popper';

cytoscape.use( popper );
function popperFactory(ref, content, opts) {
// See integration sections
}

cytoscape.use( cytoscapePopper(popperFactory) );
```

CommonJS require:

```js
let cytoscape = require('cytoscape');
let popper = require('cytoscape-popper');
let cytoscapePopper = require('cytoscape-popper');

function popperFactory(ref, content, opts) {
// See integration sections
}

cytoscape.use( popper ); // register extension
cytoscape.use( cytoscapePopper(popperFactory) ); // register extension
```

AMD:

```js
require(['cytoscape', 'cytoscape-popper'], function( cytoscape, popper ){
popper( cytoscape ); // register extension
function popperFactory(ref, content, opts) {
// See integration sections
}
// register extension
popper(popperFactory)(cytoscape);
});
```

Plain HTML/JS has the extension registered for you automatically, because no `require()` is needed.

## API

This extension exposes two functions, `popper()` and `popperRef()`. These functions are defined for both the core and for elements, so you can call `cy.popper()` or `ele.popper()` for example.
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 )` : Get a [Popper Instance](https://popper.js.org/docs/v2/constructors/) 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.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 )` : Get a [Popper virtual element](https://popper.js.org/docs/v2/virtual-elements/) (aka `Popper reference object` in Popper v1) 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.
`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](http://js.cytoscape.org/#notation/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](http://js.cytoscape.org/#notation/position) Cytoscape [bounding box dimensions](http://js.cytoscape.org/#eles.renderedBoundingBox) 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 Popper [options object](https://popper.js.org/docs/v2/constructors/#options). You may use this to override Popper options.
- `popper` : The `PopperOptions` object. These options are used in provided `popperFactory`.

## Usage with @floating-ui

### Initializing

``` js
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, opts).then(({x, y}) => {
Object.assign(content.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
update();
return { update };
}

cytoscape.use(cytoscapePopper(popperFactory));
```

### `popper()` example

``` js
// 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

``` js
// 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 })
});
```

### Sticky `popper()` example

```js
let 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);
```

## Usage with Popper.js (deprecated)

### Initializing

``` js
import cytoscape from 'cytoscape';
import cytoscapePopper from 'cytoscape-popper';
import { createPopper } from '@popperjs/core';

cytoscape.use(cytoscapePopper(createPopper));
```

### `popper()` example

``` js
// create a basic popper on the first node
let popper1 = cy.nodes()[0].popper({
content: () => {
Expand Down Expand Up @@ -138,46 +286,80 @@ node.on('position', update);

cy.on('pan zoom resize', update);
```
Note that for Popper v2 the update method is asynchronous and returns a promise. See [Manual update](https://popper.js.org/docs/v2/lifecycle/#manual-update).
### Usage with Tippy.js

## Usage with Tippy.js

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.

### Initializing

```js
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()`](https://atomiks.github.io/tippyjs/v6/methods/#destroy) instead of `hide()` where possible.

```js
let node = cy.nodes().first();

let ref = node.popperRef(); // used only for positioning

// A dummy element must be passed as tippy only accepts dom element(s) as the target
// https://atomiks.github.io/tippyjs/v6/constructor/#target-types
let dummyDomEle = document.createElement('div');

let tip = new tippy(dummyDomEle, { // tippy props:
getReferenceClientRect: ref.getBoundingClientRect, // https://atomiks.github.io/tippyjs/v6/all-props/#getreferenceclientrect
trigger: 'manual', // mandatory, we cause the tippy to show programmatically.

// your own custom props
// content prop can be used when the target is a single element https://atomiks.github.io/tippyjs/v6/constructor/#prop
var tip = node.popper({
content: () => {
let content = document.createElement('div');

content.innerHTML = 'Tippy content';

return content;
}
},
});

tip.show();
```

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

## v2 changes
This version of cytoscape-popper has been updated to use Popper 2 and be compatible with Tippy 6. Thus, it is no longer compatible with Popper v1/Tippy v5. If your application needs Popper v1/Tippy v5, use the latest v1 version of cytoscape-popper instead. Cytoscape-popper v1 dependencies are Cytoscape.js ^3.2.0 and Popper.js ^1.12.0.
## Typescript

The cytoscape-popper api has not changed in v2, but you may need to update your code if it references Popper/Tippy. See [Migrating to Popper 2](https://popper.js.org/docs/v2/migration-guide/) and [Tippy Migration Guide](https://github.com/atomiks/tippyjs/blob/master/MIGRATION_GUIDE.md#5x-to-6x).
This extension exports empty `PopperInstance` and `PopperOptions` interfaces allows to extend them according to the final Popper implementation.

`@floating-ui` example:
```ts
import { ComputePositionConfig } from '@floating-ui/dom';

declare module 'cytoscape-popper' {
interface PopperOptions extends ComputePositionConfig {
}
interface PopperInstance {
update(): void;
}
}

```

## Build targets

Expand Down
Loading

0 comments on commit a3b0b7e

Please sign in to comment.