Skip to content

fix(behavior): restore node zIndex after drag-element ends#7636

Open
Mnikley wants to merge 1 commit into
antvis:v5from
Mnikley:fix/drag-hover-zindex-ordering
Open

fix(behavior): restore node zIndex after drag-element ends#7636
Mnikley wants to merge 1 commit into
antvis:v5from
Mnikley:fix/drag-hover-zindex-ordering

Conversation

@Mnikley

@Mnikley Mnikley commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Summary

drag-element.onDragStart calls graph.frontElement(target), which sets the dragged node's zIndex to max(all_elements) + 1. This elevated zIndex persists after drag ends - there is no reset.

When hover-activate later fires on the same node, computeZIndex recalculates edge zIndex as Math.max(source.zIndex, target.zIndex) - 1. Because the dragged node's zIndex is now elevated, its edges get a zIndex higher than non-neighbor nodes (still at 0), causing edges to render on top of unrelated nodes.

The problem compounds: each subsequent drag of any node further escalates the global max zIndex.

Fix

  • Store original zIndex values before frontElement() in onDragStart
  • Restore them via setElementZIndex() in onDragEnd
  • frontElement() is still called during drag so the dragged node renders above others while dragging

Reproduction

JSFiddle: https://jsfiddle.net/z03jrefa/5/

  1. Hover "Hub" — edges render behind bystander nodes ✅
  2. Drag "Hub", release
  3. Hover "Hub" again — edges now render on top of bystander nodes ❌

Snapshot tests

Some snapshot tests will need regeneration — they previously captured the buggy elevated zIndex state after drag-end. The new snapshots reflect the corrected behavior.

Test plan

  • Added unit test verifying getElementZIndex() returns original value after drag cycle
  • Regenerate affected snapshot baselines (drag-element, z-index, bubble-sets, hull, click-select-drag-node, element-combo-drag, drag-element-combo)

drag-element calls graph.frontElement() on drag start, which elevates
the node's zIndex to max(all) + 1. This value persisted after drag end,
causing hover-activate to compute edge zIndex higher than non-neighbor
nodes, rendering edges on top of unrelated nodes.

- Store original zIndex values before frontElement() in onDragStart
- Restore them via setElementZIndex() in onDragEnd
- Add unit test verifying zIndex restoration after drag cycle

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to store and restore the zIndex of elements during drag operations in G6, ensuring that elements return to their original rendering layer after being brought to the front. While the implementation includes new tests and logic to handle target elements, the review feedback correctly identifies a significant limitation: the current approach only captures the zIndex of immediate targets and fails to account for the recursive zIndex adjustments made to combos and their descendants, which will likely result in persistent rendering issues for hierarchical structures.

Comment on lines +394 to +400
private storeOriginalZIndices(ids: ID[]) {
const { graph } = this.context;
this.originalZIndices = {};
ids.forEach((id) => {
this.originalZIndices[id] = graph.getElementZIndex(id);
});
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The storeOriginalZIndices method currently only captures the zIndex of the immediate elements being dragged (this.target). However, graph.frontElement (called on line 276) has significant side effects when a combo is involved: it recursively elevates the zIndex of the entire hierarchy, including the top-level ancestor, all descendants, and internal edges (as seen in graph.ts lines 1763-1775).

Because these related elements are not captured here, their elevated zIndex will persist after the drag ends. This means the rendering issue described in the PR (edges appearing on top of other nodes) will still occur for any nodes or edges within a dragged combo's subtree. You should ensure that all elements modified by frontElement have their original zIndex stored and restored.

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

Successfully merging this pull request may close these issues.

1 participant