在鼠标悬停时专注于弹出活动。

huangapple go评论124阅读模式
英文:

Focusing on Popper activated on MouseOver

问题

我的Popper在鼠标悬停时激活,并在鼠标移出时停用)。

我的弹出窗口包含一个用户可以点击的链接。

问题是,一旦我移出弹出窗口,它就会消失,我无法找到一种方法让它保持打开,以便用户可以点击链接。

以下是简化的代码:

import "./styles.css";
import React, { useState } from "react";
import { Box, Popper } from "@mui/material";

export default function App() {
  const [el, setEl] = useState<(EventTarget & Element) | null>(null);
  const [inBox, setBox] = useState(false);
  const handle1 = (e: React.MouseEvent) => {
    if (inBox) return;
    console.log("enter");
    setEl(e.currentTarget);
  };

  const handle2 = () => {
    if (inBox) return;
    console.log("close");
    setEl(null);
  };

  const open = !!el;

  return (
    <Box>
      <Box
        component="div"
        sx={{
          backgroundColor: "red",
          width: "100px",
          height: "100px"
        }}
        onMouseOver={(e) => handle1(e)}
        onMouseOut={handle2}
      />
      {open && (
        <Popper open={open} anchorEl={el} placement="bottom-start">
          <Box
            sx={{
              backgroundColor: "green",
              width: "170px",
              height: "70px"
            }}
            onMouseOver={() => {
              console.log("ENTERING");
              setBox(true);
            }}
            onMouseOut={() => {
              setBox(false);
            }}
          />
        </Popper>
      )}
      <Box
        onMouseEnter={handle1}
        onMouseLeave={handle2}
        sx={{
          backgroundColor: "blue",
          width: "100px",
          height: "100px"
        }}
      />
    </Box>
  );
}

查看代码

英文:

I have a Popper activated on mouseOver and deactivated on mouseOut).

My popper contains a link for a user to click on.

The problem is that as soon as I move out of the popper, it disappears and I cannot figure out a way to keep it open for a user to click a link.

The simplified code is below:

import &quot;./styles.css&quot;;
import React, { useState } from &quot;react&quot;;
import { Box, Popper } from &quot;@mui/material&quot;;
export default function App() {
const [el, setEl] = useState&lt;(EventTarget &amp; Element) | null&gt;(null);
const [inBox, setBox] = useState(false);
const handle1 = (e: React.MouseEvent) =&gt; {
if (inBox) return;
console.log(&quot;enter&quot;);
setEl(e.currentTarget);
};
const handle2 = () =&gt; {
if (inBox) return;
console.log(&quot;close&quot;);
setEl(null);
};
const open = !!el;
return (
&lt;Box&gt;
&lt;Box
component=&quot;div&quot;
sx={{
backgroundColor: &quot;red&quot;,
width: &quot;100px&quot;,
height: &quot;100px&quot;
}}
onMouseOver={(e) =&gt; handle1(e)}
onMouseOut={handle2}
/&gt;
{open &amp;&amp; (
&lt;Popper open={open} anchorEl={el} placement=&quot;bottom-start&quot;&gt;
&lt;Box
sx={{
backgroundColor: &quot;green&quot;,
width: &quot;170px&quot;,
height: &quot;70px&quot;
}}
onMouseOver={() =&gt; {
console.log(&quot;ENTERING&quot;);
setBox(true);
}}
onMouseOut={() =&gt; {
setBox(false);
}}
/&gt;
&lt;/Popper&gt;
)}
&lt;Box
onMouseEnter={handle1}
onMouseLeave={handle2}
sx={{
backgroundColor: &quot;blue&quot;,
width: &quot;100px&quot;,
height: &quot;100px&quot;
}}
/&gt;
&lt;/Box&gt;
);
}

答案1

得分: 1

我并没有完全理解你想要的内容。但我猜你想要在鼠标悬停在"Popper"显示框上时显示另一个框,并在鼠标移出时隐藏它,对吗?

import React, { useRef, RefObject, useEffect, useState } from "react";

const Popper = () => {
  const divEl1 = useRef<HTMLDivElement>();
  const divEl2 = useRef<HTMLDivElement>();
  const [isOver, setIsOver] = useState<boolean>(false);

  useEffect(() => {
    const { current } = divEl1;
    if (current) {
      current.addEventListener("mouseover", () => setIsOver(true));
      current.addEventListener("mouseout", () => setIsOver(false));
      
      return () => {
        current.removeEventListener("mouseover", () => setIsOver(true));
        current.removeEventListener("mouseout", () => setIsOver(false));
      };
    }
  }, [divEl1]);

  return (
    <div>
      <div ref={divEl1 as RefObject<HTMLDivElement>}>Popper</div>
      <div style={{ display: isOver ? "block" : "none" }} ref={divEl2 as RefObject<HTMLDivElement>}>
        Show/NotShow
      </div>
    </div>
  );
};

export default Popper;

请注意,我已经添加了"mouseout"事件监听器,并在适当的时候移除了它们,以确保正常工作。

英文:

I didn't perfectly understand what you want. But, I guess you want to do that mouseover at popper show Box & mouseout on Box disappear box? Right?

import React, { useRef, RefObject, useEffect } from &quot;react&quot;;
const Popper = () =&gt; {
const divEl1 = useRef&lt;HTMLDivElement&gt;();
const divEl2 = useRef&lt;HTMLDivElement&gt;();
const [isOver,setIsOver] = useState&lt;boolean&gt;(false);
useEffect(() =&gt; {
const { current } = divEl1;
if(current) {
current.addEventListener(&quot;mouseover&quot;, () =&gt;  setIsOver(true));
current.removeEventListener(&quot;mouseover&quot;, () =&gt; setIsOver(true));
}
}, [divEl1]);
useEffect(() =&gt; {
const { current } = divEl2;
if(current) {
current.addEventListener(&quot;mouseout&quot;, () =&gt;  setIsOver(false));
current.removeEventListener(&quot;mouseout&quot;, () =&gt; setIsOver(false));
}
}, [divEl2]);
return (
&lt;div &gt;
&lt;div ref={divEl1 as RefObject&lt;HTMLDivElement&gt;}&gt;
Popper
&lt;/div&gt;
&lt;div style={{display : isOver ? &quot;block&quot; : &quot;none&quot;}} ref={divEl2 as RefObject&lt;HTMLDivElement&gt;}&gt;
Show/NotShow
&lt;/div&gt;
&lt;/div&gt;
)
}

huangapple
  • 本文由 发表于 2023年6月22日 03:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定