Skip to content

Event Handling ​

You can capture events that occur in v-network-graph by specifying the events and functions to be captured in the event-handlers attribute.

For a list of events that can be captured, see here.

Capture all events ​

If you specify "*" as the event type, all available events will be captured.

Handle user clicking on a node ​

The following is an example of specifying a handler that catches only the specified events.
In this example, the color of the badge changes when the node is clicked.

TIP

The handler for mouse events such as click are also passed a raw event object. By using this event object, it is possible to check if a modifier key (Ctrl, Alt, etc.) is pressed or not.

ts
import { EventHandlers } from "v-network-graph"
const eventHandlers: EventHandlers = {
  "node:click": ({ node, event }) => {
    if (event.ctrlKey) {
      // ...
    }
  }
}

Context menu ​

v-network-graph does not have the ability to show a context menu, but it can capture the right-clicking event ("contextmenu" event) on objects of the network graph. The handler receives the browser's MouseEvent in event. You can combine your own components or other libraries to display the menu.

Tooltip ​

By combining event handling and coordinate translation, it is possible to represent a combination of DOM elements such as tooltip.

Even though there is no edge position stored in Layout, we can still create tooltip for edges by using the node positions.

Keyboard events ​

v-network-graph does not support key events, but they can be obtained using the standard HTML/Vue ways.
Below is an example of deleting a selected node/edge using the keyup event of delete key.

Set raw event handlers to the node elements ​

This section shows how to set raw event handlers to a node when the event mechanism with event-handlers is insufficient.

In event-handlers, every node:pointerdown is followed by a node:pointerup. The node:pointerup event is fired for the node where the pointer went down, even if the pointer is released over another node or anywhere else on the page.
This is not what you want if, for example, the user drags from one node to another to pick a start point and an end point, because the end node never receives the event.

In such cases, you can access the SVG elements of each node as shown below and set your own handlers on them directly.