question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add optional "class" attribute to nodes and links - improved styling and interactivity

See original GitHub issue

In the discussion of the color attribute in the README, you state “However, I haven’t found the balance between ease of use and expressiveness yet.” - I propose adding an optional “class” attribute to allow users to specify domain-specific CSS classes for specific nodes and links. This will allow for maximum expressiveness through the use of CSS when embedding the results in HTML without overwhelming the smcat API with numerous additional properties. This will also allow JavaScript to locate specific nodes by class efficiently.

Context

I would like the ability to have more flexible, custom, per-node styling and interactivity when using smcat within a javascript-based web application. By adding a custom “class” attribute to nodes and links, this can be supported without adding multiple new properties to the existing smcat API. Using per-node and per-link classes supports both: (1) custom CSS-based styling of specific node(s) and (2) the ability to attach custom handlers (e.g. onmouseover, onclick, etc) by selecting specific node(s) efficiently using getElementsByClassName.

Proposed Behavior

I propose that in the 2 places where the color attribute is currently supported (nodes and links), that a new class attribute also be added. This attribute will take a string and append it to the existing class (either “node” or “edge”) in the generated SVG.

An example of what this might look like in practice is available at: https://htmlpreview.github.io/?https://github.com/pangaeatech/state-machine-cat/blob/master/docs/class_example.html

Current Behavior

Currently, the only styling available is through the use of the (limited) color attribute.

Considered alternatives

Currently, the only way to achieve this is by knowing the order in which items will be rendered into the svg and using the existing id properties (e.g. “#node1”, “#edge2”, etc) for both CSS and for selecting items using getElementById - this is both very inefficient as it requires each node or edge to be handled individually instead of as a group and it is also very “brittle” in that the order of nodes is not guaranteed by the API and could potentially change with new versions of smcat.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sverweijcommented, Jan 30, 2021

Surprisingly, I’m not able to even get it to yield invalid svg using that on https://sverweij.gitlab.io/state-machine-cat/ – I tried a few different variations, but all of them show up as properly escaped in the DOM of the page.

Browsers are quite unforgiving for svg embedded in html, I’ve noticed. When confronted with the separate svg, though (either from the webpage or the cli), they’ll start to eek out. In the next version the ‘class’ attribute is properly html escaped (the other attributes, like ‘color’ and ‘label’ already were) and both the smcat language and the json schema for the internal representation only allow ^[a-zA-Z0-9_\\.\\- ]*$ - which seems reasonable (and which is exactly what you proposed as well). If there’s need for more it’s easy to expand.

(published as state-machine-cat@7.4.0-beta-2 on npm and available on beta test site as well)

1reaction
sverweijcommented, Jan 27, 2021

Hi @mwaddell - sounds like a good idea. I’ve added it to the smcat language and the output modules for dot (and hence svg) => see the linked PR (WIP). It still needs a bit of documentation and a round-trip (json => smcat), but the smcat => svg (and hence smcat => json => dot => svg) work. I might still tweak some of the auto generated class names to grow a prefix and change around little details, but …

… you can try it out right now at either

=> Let me know what you think - all feedback welcome

Read more comments on GitHub >

github_iconTop Results From Across the Web

Selectors - W3C
A simple selector is either a type selector or universal selector followed immediately by zero or more attribute selectors, ID selectors, or pseudo-classes, ......
Read more >
Python's property(): Add Managed Attributes to Your Classes
In this step-by-step tutorial, you'll learn how to create managed attributes, also known as properties, using Python's property() in your custom classes.
Read more >
optional - CSS: Cascading Style Sheets - MDN Web Docs
The :optional CSS pseudo-class represents any , , or element that does not have the required attribute set on it.
Read more >
Attributes - Graphviz
Instructions to customise the layout of Graphviz [nodes](/docs/nodes), [edges](/docs/edges), [graphs](/docs/graph), subgraphs, ...
Read more >
How To Modify Attributes, Classes, and Styles in the DOM
In this tutorial, you will learn how to further alter the DOM by modifying styles, classes, and other attributes of HTML element nodes....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found