Comments (2)
You can use getContent
to return your DOM element. https://g6-next.antv.antgroup.com/api/plugins/contextmenu
from g6.
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)
- 如何恢复被批量更新的自定义节点的样式 HOT 2
- 树图删除中间的某个节点并保留他的子节点 HOT 2
- 5.0版本动态添加元素边,拖拽和缩放画布之后边错位,如果在新增边也是错位的 HOT 1
- Custom React Node with zoom-canvas and drag-canvas displaces the node. HOT 4
- 树图TreeGraph首次render时先触发了afterrender事件 HOT 1
- [v5] Calling render while an existing render is happening causes force layout to flash HOT 1
- gForce 布局,没有设置 preset 依然显示 HOT 1
- Cannot read properties of undefined (reading 'Graph') HOT 2
- Method not implemented. 报错。 HOT 4
- [V5] label 使用padding并且折行的情况下,background宽度不能覆盖全部文字内容 HOT 1
- 【5.x】边选中有偏差 HOT 1
- 【V5】5.0.9 combo updateComboData 更新 icon/badge 等不生效
- cursor 状态不会自动变化 HOT 1
- dagre布局下的combo,如何设置固定宽度,高度由内容自动撑开,或者有api实现节点换行吗
- V5.0.9 自定义节点展开 收缩有问题 HOT 2
- 【V5】树布局中,节点高度不固定的情况下,getHeight按节点实际高度设置,会出现节点布局重叠问题
- 树布局中如何在getWidth中获取节点的宽高
- [V5] hover-activate behavior makes expand/collapse combo breaking HOT 1
- [V5]拖动Custom React Node后,连线错乱 HOT 1
- 【V5】5.0.10 click-select behavior 双击节点后 选择失效 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from g6.