-1

I am trying to use cytoscape with tippy but it is not showing any tool tips. It throws an error that ele.popperRef is not a function.

Here is the stackblitz link: https://stackblitz.com/edit/dagre-tippy?file=src%2Fapp%2Fapp.component.ts

I have added all the packages required which includes popper.js, tippy.js but still it does not work

Learn AspNet
  • 1,192
  • 3
  • 34
  • 74

1 Answers1

2

Check this https://stackblitz.com/edit/dagre-tippy-wgg8zz

You are not simply importing libraries properly and registering the cytoscape.js extensions.

You should register popper extension with cytoscape.use(popper);

You can use tippy.js with a function like

function makePopperWithTippy(node) {
  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 = 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
    content: () => {
      let content = document.createElement("div");

      content.innerHTML = "Tippy content";

      return content;
    }
  });

  tip.show();
}

Also, note that you don't have to use tipp.js. Just popper.js is enough actually.

function makePopper(ele) {
  // create a basic popper on the first node
  let popper1 = ele.popper({
    content: () => {
      let div = document.createElement("div");

      div.innerHTML = "Popper content";

      document.body.appendChild(div);

      return div;
    },
    popper: {} // my popper options here
  });
}

You can apply these functions below and see the tooltips. The event-based showing on and off is simple after this.

cy.elements().forEach(function(ele) {
  makePopperWithTippy(ele);
  // makePopper(ele);
});
canbax
  • 3,432
  • 1
  • 27
  • 44
  • I am trying to get tooltips on hover. The stackblitz you sent has 100 console errors and does not work on hover over each element/node. – Learn AspNet Mar 26 '21 at 12:35
  • Also, I tried using cytoscape.use(popper) but I get a console error - Can not register `popper` for `core` since `popper` already exists in the prototype and can not be overridden https://stackblitz.com/edit/dagre-tippy-test-q8ndc8?file=src/app/app.component.ts Your answer is completely different from what I am trying to do – Learn AspNet Mar 26 '21 at 12:37
  • My answer shows you how you can show tippy.js badges. After that, you can solve how to handle showing the badges with hover event – canbax Mar 26 '21 at 12:46
  • If you see the stackblitz link I sent you, I cannot even use cytoscape.use(popper) because it gives me an error that popper already exists in prototype. tippy.js badges are only working when you create new tippy variable but if you want to add tippy to the node reference, it does not work. That is the whole point. Looks like there is an issue with cytoscape popper – Learn AspNet Mar 26 '21 at 12:51
  • @LearnAspNet I suspect the "Can not register popper for core since popper already exists in the prototype" error is due to stackblitz environment calling the same thing multiple times. You cannot register an extension multiple times. Try on localhost. I also see that error but I ignored it. The extension must be registered – canbax Mar 26 '21 at 12:52
  • After you changed the code to show tippy based on separate variable and just showing that tip does not work because we want the node.tippy to have tool tips, then we will switch the hover. Can you make it work on hover on stackblitz because it does not work for me – Learn AspNet Mar 26 '21 at 12:54
  • I don't think there is such a HUGE issue with cytoscape-popper but honestly I don't like them. Actually, I might build such an extension for cytoscape.js because they all depend on totally unnecessary things. – canbax Mar 26 '21 at 12:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230411/discussion-between-learn-aspnet-and-canbax). – Learn AspNet Mar 26 '21 at 12:56