TippyJS-React: how to open context-menu on right-click

2.4k Views Asked by At

I try to do right-click menu using tippyjs-react:

<Tippy
  placement={'right-start'}
  content={someConent}
  trigger={'manual'}
>
  {children}
</Tippy>

I know that I need to add the event listener with 'contextmenu' to children and use tippyInstance.show() method, but how can I get access to the instance?

1

There are 1 best solutions below

0
On BEST ANSWER
import React, { useState } from "react";
import Tippy from "@tippyjs/react";
import "tippy.js/dist/tippy.css"; // optional
import "./styles.css";

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <MyTippy />
    </div>
  );
}

const MyTippy = () => {
  const [visible, setVisible] = useState(false);
  return (
    <Tippy
      onClickOutside={() => setVisible(false)}
      visible={visible}
      placement="right-start"
      content={<span>Tooltip</span>}
    >
      <button
        onContextMenu={(e) => {
          e.preventDefault();
          setVisible(true);
        }}
      >
        My button
      </button>
    </Tippy>
  );
};

Edit compassionate-curran-t3n82