1

我尝试使用tippyjs-react做右键菜单:

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

我知道我需要将带有“contextmenu”的事件侦听器添加到孩子并使用tippyInstance.show() 方法,但是我怎样才能访问该实例?

4

1 回答 1

2
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>
  );
};

编辑富有同情心的curran-t3n82

于 2020-10-22T05:08:27.547 回答