diff --git a/packages/g6/__tests__/demos/index.ts b/packages/g6/__tests__/demos/index.ts
index 9d48b690c0b..accb02e85de 100644
--- a/packages/g6/__tests__/demos/index.ts
+++ b/packages/g6/__tests__/demos/index.ts
@@ -141,6 +141,7 @@ export { pluginTimebar } from './plugin-timebar';
export { pluginToolbarBuildIn } from './plugin-toolbar-build-in';
export { pluginToolbarIconfont } from './plugin-toolbar-iconfont';
export { pluginTooltip } from './plugin-tooltip';
+export { pluginTooltipDual } from './plugin-tooltip-dual';
export { pluginWatermark } from './plugin-watermark';
export { pluginWatermarkImage } from './plugin-watermark-image';
export { theme } from './theme';
diff --git a/packages/g6/__tests__/demos/plugin-tooltip-dual.ts b/packages/g6/__tests__/demos/plugin-tooltip-dual.ts
new file mode 100644
index 00000000000..b2e9ca90a8c
--- /dev/null
+++ b/packages/g6/__tests__/demos/plugin-tooltip-dual.ts
@@ -0,0 +1,61 @@
+import type { IElementEvent, Tooltip } from '@antv/g6';
+import { Graph } from '@antv/g6';
+
+export const pluginTooltipDual: TestCase = async (context) => {
+ const graph = new Graph({
+ ...context,
+ data: {
+ nodes: [
+ { id: 'node1', style: { x: 100, y: 100 } },
+ { id: 'node2', style: { x: 200, y: 200 } },
+ ],
+ edges: [{ id: 'edge', source: 'node1', target: 'node2' }],
+ },
+ node: {
+ style: {
+ labelText: (d) => d.id,
+ },
+ },
+ plugins: [
+ function () {
+ return {
+ key: 'tooltip-click',
+ type: 'tooltip',
+ trigger: 'click',
+ getContent: (evt: any, items: any[]) => {
+ return `
click ${items[0].id}
`;
+ },
+ onOpenChange: (open: boolean) => {
+ const tooltip = this.getPluginInstance('tooltip-hover') as Tooltip;
+ if (tooltip && open) tooltip.hide();
+ },
+ };
+ },
+ function () {
+ return {
+ key: 'tooltip-hover',
+ type: 'tooltip',
+ trigger: 'hover',
+ enable: (e: IElementEvent) => {
+ const tooltip = this.getPluginInstance('tooltip-click') as Tooltip;
+ // @ts-expect-error access private property
+ return e.target.id !== tooltip.currentTarget;
+ },
+ getContent: (evt: any, items: any[]) => {
+ return `hover ${items[0].id}
`;
+ },
+ onOpenChange: (open: boolean) => {
+ const tooltip = this.getPluginInstance('tooltip-click') as Tooltip;
+ if (tooltip && open) {
+ tooltip.hide();
+ }
+ },
+ };
+ },
+ ],
+ });
+
+ await graph.render();
+
+ return graph;
+};
diff --git a/packages/g6/src/plugins/tooltip.ts b/packages/g6/src/plugins/tooltip.ts
index d60678783a2..d9539a99c0d 100644
--- a/packages/g6/src/plugins/tooltip.ts
+++ b/packages/g6/src/plugins/tooltip.ts
@@ -39,6 +39,12 @@ export interface TooltipOptions
* @defaultValue true
*/
enable?: boolean | ((event: IElementEvent) => boolean);
+ /**
+ * 显示隐藏的回调
+ *
+ * Callback executed when visibility of the tooltip card is changed
+ */
+ onOpenChange: (open: boolean) => void;
}
/**
@@ -162,9 +168,7 @@ export class Tooltip extends BasePlugin {
// click the same item twice, tooltip will be hidden
if (this.currentTarget === id) {
this.hide(event);
- this.currentTarget = null;
} else {
- this.currentTarget = id;
this.show(event);
}
};
@@ -190,7 +194,6 @@ export class Tooltip extends BasePlugin {
*/
public onPointerLeave = (event: IElementEvent) => {
this.hide(event);
- this.currentTarget = null;
};
/**
* 移动画布
@@ -200,7 +203,6 @@ export class Tooltip extends BasePlugin {
*/
public onCanvasMove = (event: IElementEvent) => {
this.hide(event);
- this.currentTarget = null;
};
private onPointerEnter = (event: IElementEvent) => {
@@ -280,6 +282,7 @@ export class Tooltip extends BasePlugin {
}),
};
}
+ this.options.onOpenChange?.(true);
this.tooltipElement.update({
...this.tooltipStyleProps,
x,
@@ -301,7 +304,9 @@ export class Tooltip extends BasePlugin {
public hide = (event?: IElementEvent) => {
// if e is undefined, hide the tooltip, external call
if (!event) {
+ this.options.onOpenChange?.(false);
this.tooltipElement?.hide();
+ this.currentTarget = null;
return;
}
if (!this.tooltipElement) return;
@@ -310,7 +315,9 @@ export class Tooltip extends BasePlugin {
const {
client: { x, y },
} = event;
+ this.options.onOpenChange?.(false);
this.tooltipElement.hide(x, y);
+ this.currentTarget = null;
};
private get tooltipStyleProps() {
diff --git a/packages/site/examples/plugin/tooltip/demo/dual.js b/packages/site/examples/plugin/tooltip/demo/dual.js
new file mode 100644
index 00000000000..cdfbd42665e
--- /dev/null
+++ b/packages/site/examples/plugin/tooltip/demo/dual.js
@@ -0,0 +1,64 @@
+import { Graph } from '@antv/g6';
+
+const graph = new Graph({
+ container: 'container',
+ data: {
+ nodes: [
+ { id: '0', data: { label: 'node-0', description: 'This is node-0.' } },
+ { id: '1', data: { label: 'node-1', description: 'This is node-1.' } },
+ { id: '2', data: { label: 'node-2', description: 'This is node-2.' } },
+ { id: '3', data: { label: 'node-3', description: 'This is node-3.' } },
+ { id: '4', data: { label: 'node-4', description: 'This is node-4.' } },
+ { id: '5', data: { label: 'node-5', description: 'This is node-5.' } },
+ ],
+ edges: [
+ { source: '0', target: '1', data: { description: 'This is edge from node 0 to node 1.' } },
+ { source: '0', target: '2', data: { description: 'This is edge from node 0 to node 2.' } },
+ { source: '0', target: '3', data: { description: 'This is edge from node 0 to node 3.' } },
+ { source: '0', target: '4', data: { description: 'This is edge from node 0 to node 4.' } },
+ { source: '0', target: '5', data: { description: 'This is edge from node 0 to node 5.' } },
+ ],
+ },
+ layout: {
+ type: 'grid',
+ },
+ plugins: [
+ function () {
+ return {
+ key: 'tooltip-click',
+ type: 'tooltip',
+ trigger: 'click',
+ getContent: (evt, items) => {
+ return `click ${items[0].id}
`;
+ },
+ onOpenChange: (open) => {
+ const tooltip = this.getPluginInstance('tooltip-hover');
+ if (tooltip && open) tooltip.hide();
+ },
+ };
+ },
+ function () {
+ return {
+ key: 'tooltip-hover',
+ type: 'tooltip',
+ trigger: 'hover',
+ enable: (e) => {
+ const tooltip = this.getPluginInstance('tooltip-click');
+ return e.target.id !== tooltip.currentTarget;
+ },
+ getContent: (evt, items) => {
+ return `hover ${items[0].id}
`;
+ },
+ onOpenChange: (open) => {
+ const tooltip = this.getPluginInstance('tooltip-click');
+ if (tooltip && open) {
+ tooltip.hide();
+ }
+ },
+ };
+ },
+ ],
+ behaviors: ['drag-canvas', 'drag-element'],
+});
+
+graph.render();
diff --git a/packages/site/examples/plugin/tooltip/demo/meta.json b/packages/site/examples/plugin/tooltip/demo/meta.json
index 6a65073308f..f475c062494 100644
--- a/packages/site/examples/plugin/tooltip/demo/meta.json
+++ b/packages/site/examples/plugin/tooltip/demo/meta.json
@@ -19,6 +19,14 @@
"en": "Show on click"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*ErhbR7ErhRwAAAAAAAAAAAAADmJ7AQ/original"
+ },
+ {
+ "filename": "dual.js",
+ "title": {
+ "zh": "鼠标移入和点击同一元素时显示不同的提示框",
+ "en": "Dual tooltips"
+ },
+ "screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*DCZ_SbSHGnYAAAAAAAAAAAAADmJ7AQ/original"
}
]
}