Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to display label conditionally on hover. #207

Open
dipin-anand opened this issue Sep 9, 2024 · 0 comments
Open

Trying to display label conditionally on hover. #207

dipin-anand opened this issue Sep 9, 2024 · 0 comments

Comments

@dipin-anand
Copy link

Hi, I am trying to display the label component conditionally onHover. But as soon as I hover over the displayed label element which is a button, it keeps triggering the onMouseEnter and onMouseLeave which keeps changing the onHoverState. Since the button component(label) is a child of the svg, it should have ideally not triggered the state changes. I have tried stop propogation and prevent default but nothing seems to work. Here is a code snippet.

const App = () => {
  const [isHovering, setIsHovering] = useState(false);
  console.log(isHovering);

  return (
    <div
      style={{
        height: "500px",
        margin: "50px",
      }}
    >
      <ArcherContainer strokeColor="red">
        <div style={rootStyle}>
          <ArcherElement
            id="root"
            relations={[
              {
                targetId: "element2",
                targetAnchor: "top",
                sourceAnchor: "bottom",
                style: {
                  strokeDasharray: "5,5",
                  strokeColor: isHovering ? "green" : "blue",
                },

                label: (
                  <>
                    {isHovering && (
                      <Box width="100px" height={"100px"}>
                        <IconButton aria-label="delete" color="primary">
                          <DeleteIcon />
                        </IconButton>
                      </Box>
                    )}
                  </>
                ),

                domAttributes: {
                  // The hovering style could be achieved with CSS as well
                  onDoubleClick: () => {
                    console.log("you focused me!");
                  },
                  onMouseEnter: (e) => {
                    e.preventDefault();
                    e.stopPropagation();

                    setIsHovering(true);
                  },
                  onMouseLeave: (e) => {
                    e.preventDefault();
                    e.stopPropagation();
                    setIsHovering(false);
                  },
                  // The click however needs a props, obviously
                  onClick: () => {
                    // setIsHovering(true);
                  },
                  onKeyDown: (e: React.KeyboardEvent) => {
                    console.log(e);
                  },
                },
                hitSlop: 30,
                cursor: "grab",
              },
            ]}
          >
            <div style={boxStyle}>Hover my arrow</div>
          </ArcherElement>
        </div>

        <div style={rowStyle}>
          <ArcherElement
            id="element2"
            relations={[
              {
                targetId: "element3",
                targetAnchor: "left",
                sourceAnchor: "right",
                style: {
                  strokeColor: "blue",
                  strokeWidth: 1,
                },
                label: (
                  <div
                    style={{
                      marginTop: "-20px",
                    }}
                  >
                    Arrow 2
                  </div>
                ),
              },
            ]}
          >
            <div style={boxStyle}>Element 2</div>
          </ArcherElement>

          </div>
      </ArcherContainer>
    </div>
  );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant