Code Monkey home page Code Monkey logo

Comments (2)

hustcc avatar hustcc commented on July 20, 2024 1

You can use getContent to return your DOM element. https://g6-next.antv.antgroup.com/api/plugins/contextmenu

from g6.

RackweLLizm avatar RackweLLizm commented on July 20, 2024 1

Maybe it will be useful to others. ContextMenu example prepared with React 18 on Antv G6 v5.
ContextMenu React Component.

import { Dropdown, Menu } from "antd";
import SubMenu from "antd/es/menu/SubMenu";
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";

const SorguYapContextMenu = ({ x, y, id, targetType, onClose }) => {
    console.log("id, click element id", id);
    console.log("targetType node,edge or canvas", targetType);
    const [visible, setVisible] = useState(true);
    useEffect(() => {
        const handleClickOutside = (event) => {
            if (!event.target.closest('.g6-contextmenu')) {
                setTimeout(() => {
                    setVisible(false);
                    onClose();
                }, 150)
            }
        };

        const timer = setTimeout(() => {
            document.addEventListener('mousedown', handleClickOutside);
        }, 0);

        return () => {
            clearTimeout(timer);
            document.removeEventListener('mousedown', handleClickOutside);
        };
    }, [onClose]);



    const menu = (): JSX.Element => {
        if (targetType === "node") {
            return <Menu triggerSubMenuAction={"hover"} onClick={(e) => console.log("click", e)}>
                <Menu.Item key="1">node 1</Menu.Item>
                <Menu.Item key="2">Option 2</Menu.Item>
                <Menu.Item key="3">Option 3</Menu.Item>
                <SubMenu onTitleClick={(e) => e.domEvent.stopPropagation()} key="sub1" title="Submenu 1">
                    <Menu.Item key="5">Option 5</Menu.Item>
                    <Menu.Item key="6">Option 6</Menu.Item>
                </SubMenu>
            </Menu>
        }
        else if (targetType === "edge") {
            return <Menu triggerSubMenuAction={"hover"} onClick={(e) => console.log("click", e)}>
                <Menu.Item key="1">edge 1</Menu.Item>
                <Menu.Item key="2">Option 2</Menu.Item>
                <Menu.Item key="3">Option 3</Menu.Item>
                <SubMenu onTitleClick={(e) => e.domEvent.stopPropagation()} key="sub1" title="Submenu 1">
                    <Menu.Item key="5">Option 5</Menu.Item>
                    <Menu.Item key="6">Option 6</Menu.Item>
                </SubMenu>
            </Menu>
        }
        else if (targetType === "canvas") {
            return <Menu triggerSubMenuAction={"hover"} onClick={(e) => console.log("click", e)}>
                <Menu.Item key="1">canvas 1</Menu.Item>
                <Menu.Item key="2">Option 2</Menu.Item>
                <Menu.Item key="3">Option 3</Menu.Item>
                <SubMenu onTitleClick={(e) => e.domEvent.stopPropagation()} key="sub1" title="Submenu 1">
                    <Menu.Item key="5">Option 5</Menu.Item>
                    <Menu.Item key="6">Option 6</Menu.Item>
                </SubMenu>
            </Menu>
        }
        return <></>;
    }

    return createPortal(
        <Dropdown overlay={menu} visible={visible}>
            <div className="g6-contextmenu" style={{ position: 'absolute', left: x, top: y, zIndex: 1000 }} />
        </Dropdown>,
        document.body
    );
};
export default SorguYapContextMenu

Graph Plugins ContextMenu

            plugins: [
                {
                    type: 'contextmenu',
                    trigger: 'contextmenu', // 'click' or 'contextmenu'
                    getContent: (e) => {
                        const container = document.createElement('div');
                        document.body.appendChild(container);
                        const root = createRoot(container);

                        const closeContextMenu = () => {
                            root.unmount();
                            if (container.parentNode) {
                                container.parentNode.removeChild(container);
                            }
                        };
                        console.log("targetType",e.targetType)

                        root.render(<SorguYapContextMenu x={e.client.x} y={e.client.y} id={e.target.id} targetType={e.targetType} onClose={closeContextMenu} />);

                        return container;
                    },
                    enable: 'always',
                },
]

from g6.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.