Skip to content

Commit

Permalink
fix(plugins): fix tooltip with incorrect position after graph resize
Browse files Browse the repository at this point in the history
  • Loading branch information
antv committed Aug 28, 2024
1 parent c9ba7fc commit b85182e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
53 changes: 53 additions & 0 deletions packages/g6/__tests__/demos/bug-tooltip-resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Graph } from '@antv/g6';

export const bugTooltipResize: TestCase = async (context) => {
const graph = new Graph({
...context,
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node1', target: 'node3' },
{ source: 'node1', target: 'node4' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
layout: {
type: 'grid',
},
plugins: [
{
type: 'tooltip',
style: {
['.tooltip']: {
transition: 'none',
},
},
},
],
});

await graph.render();

bugTooltipResize.form = (panel) => {
let width = 500;
return [
panel.add(
{
resize: () => {
const newWidth = width === 500 ? 300 : 500;
width = newWidth;
document.querySelector<HTMLDivElement>('#container')!.style.width = `${newWidth}px`;
graph.resize();
graph.fitView();
},
},
'resize',
),
];
};

return graph;
};
1 change: 1 addition & 0 deletions packages/g6/__tests__/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { behaviorLassoSelect } from './behavior-lasso-select';
export { behaviorOptimizeViewportTransform } from './behavior-optimize-viewport-transform';
export { behaviorScrollCanvas } from './behavior-scroll-canvas';
export { behaviorZoomCanvas } from './behavior-zoom-canvas';
export { bugTooltipResize } from './bug-tooltip-resize';
export { canvasCursor } from './canvas-cursor';
export { caseDecisionTree } from './case-decision-tree';
export { caseIndentedTree } from './case-indented-tree';
Expand Down
35 changes: 18 additions & 17 deletions packages/g6/src/plugins/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
};
}
this.tooltipElement.update({
...this.tooltipStyleProps,
x,
y,
style: {
Expand Down Expand Up @@ -300,32 +301,32 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
this.tooltipElement.hide(x, y);
};

private initTooltip = () => {
private get tooltipStyleProps() {
const { canvas } = this.context;
const { center } = canvas.getBounds();
const $container = canvas.getContainer() as HTMLElement;
const { top, left } = $container.getBoundingClientRect();
const { style, position, enterable, container = { x: -left, y: -top }, title, offset } = this.options;
const [x, y] = center;
const [width, height] = canvas.getSize();

return {
x,
y,
container,
title,
bounding: { x: 0, y: 0, width, height },
position,
enterable,
offset,
style,
};
}

private initTooltip = () => {
const tooltipElement = new TooltipComponent({
className: 'tooltip',
style: {
x,
y,
container,
title,
bounding: {
x: 0,
y: 0,
width,
height,
},
position,
enterable,
offset,
style,
},
style: this.tooltipStyleProps,
});
this.container?.appendChild(tooltipElement.HTMLTooltipElement);
return tooltipElement;
Expand Down
26 changes: 26 additions & 0 deletions tests/g6/plugins/plugins-tooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from '@playwright/test';

test.describe('plugin tooltip', () => {
test('bug: tooltip should has correct position after graph resize', async ({ page }) => {
page.goto('/?Demo=bugTooltipResize&Renderer=canvas&GridLine=true&Theme=light&Animation=false');

await page.waitForSelector('.tooltip');

const clip = { x: 0, y: 0, width: 500, height: 500 };

await page.mouse.move(375, 250);

await expect(page).toHaveScreenshot({ clip });

await page.mouse.move(250, 250);

// wait for div content is 'resize'
const resize = page.getByRole('button', { name: 'resize' });

await resize?.click();

await page.mouse.move(285, 250);

await expect(page).toHaveScreenshot({ clip });
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b85182e

Please sign in to comment.