Skip to main content

UI States

A common use case is the requirement to temporarily paint a set of nodes/groups/ports and/or edges with some set of styles. The surface supports this through the concept of UI States.

UI states are shipped as part of the @jsplumbtoolkit/browser-ui package.

First, an example. This is a snippet from the view parameter passed to a render call:

toolkitInstance.render(someElement, {

... other view content...

paintStyle:{ "strokeStyle":"#00FF00" },
endpointStyle: { fillStyle:"#00EE00" }

Here we have declared a state called mintyGreenState. When this state is active, all nodes of type default to which it applies will have a CSS class of mintyGreen added, and all edges of type default will be painted with a stroke style of #00FF00 (for the eagle-eyed, not quite minty green, it's true). All ports - which are represented via endpoints - will have a fill style of #00EE00.

The states themselves are listed inside the view on a render call, but note also the plugins parameter in the code snippet above. If you're using Typescript or ES6 you can also strongly type this declaration:

import { UIStatesPlugin } from "@jsplumbtoolkit/browser-ui"

toolkitInstance.render(someElement, {

plugins:[ UIStatesPlugin.type ]

Note also that edges and ports will be assigned the CSS class of mintyGreen. So you need to ensure that any CSS rules related to UI states are specific enough that you do not accidentally apply node/group styles to a connection.


When you call updateEdge, updateNode or updateGroup on a Toolkit instance, any edge, node or group states that are active for the given object will be cancelled and it will revert to its default state (albeit with the new data you provided).


UI States are very useful, but they operate in a "push" rather than a "pull" mode, ie. it is up to the user to tell the surface what state some object should be in. A related concept for edges that works in "push" mode is Property Mapping

Applying UI States

You can apply a state to any of the following:


UI states are manipulated by invoking methods on the UI states plugin, not by invoking methods on the Surface. The code snippets for below include a reference to a plugin variable, which is retrieved from the Surface.

If you're using Javascript, you can retrieve the plugin with this call:

var plugin = surface.getPlugin("ui-states")

If you're using Typescript, you can use this call instead:

import { UiStatesPlugin } from "@jsplumbtoolkit/browser-ui"

const plugin = surface.getPlugin<UiStatesPlugin>(UiStatesPlugin.type)

Applying state to the entire dataset

plugin.activateState("mintyGreenState") // in the absence of a second argument, the state is applied to the entire dataset.

Applying state to a Toolkit's current selection

plugin.activateState("mintyGreenState", toolkit.getSelection())

Applying state to an ad-hoc selection

const selection =["node1", "node2"])
plugin.activateState("mintyGreenState", selection)

Applying state to a path

const path = toolkit.getPath({source:"node1", target:"node2"})
plugin.activateState("mintyGreenState", path)

Methods available

These methods are exposed on the UIStatesPlugin.

  • activateState(stateId, [target])

Activate the given state, optionally on the given target. If target is not supplied then the state will be applied to the entire dataset.

  • deactivateState(stateId, [target])

Deactivate the given state, optionally on the given target. If target is not supplied then the state deactivation will be applied to the entire dataset.

  • resetState()

Deactivates all states across the entire dataset.

Wildcard states

In the above example, the state specification applied to all nodes, groups, edges and ports that have a type of default. Should you wish to indicate that a state can be applied to any object, you can use a wildcard:


In this example, which contains only cssClass directives (remember that the cssClass will be added to all nodes, groups, ports and edges), when mintyGreenState is activated, all objects will get the mintyGreen CSS class. But objects of type aSpecificType will get the FOO CSS class also.