Skip to content

Commit 23173c3

Browse files
committed
D2 interactivity example
1 parent 721ace7 commit 23173c3

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

packages/docs/src/content/docs/start-here/interactivity.mdx

+94-2
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,103 @@ document.querySelectorAll(".vizdom.ants").forEach((container) => {
450450
</TabItem>
451451
</Tabs>
452452

453+
### D2 Example
454+
455+
**Try** hower nodes.
456+
457+
<Tabs>
458+
<TabItem label="Diagram">
459+
```d2 strategy=inline darkScheme=false graphFormat=dagre class=shadow svgo=false
460+
direction: right
461+
a -> b -> c -> d -> e
462+
```
463+
</TabItem>
464+
<TabItem label="Markdown">
465+
````md
466+
```d2 strategy=inline darkScheme=false graphFormat=dagre class=shadow svgo=false
467+
direction: right
468+
a -> b -> c -> d -> e
469+
```
470+
````
471+
</TabItem>
472+
<TabItem label="JS">
473+
474+
```ts
475+
import { json, alg } from "@dagrejs/graphlib";
476+
477+
// interactivity for d2 diagrams
478+
document.querySelectorAll(".d2.shadow").forEach((container) => {
479+
const data = container.getAttribute("data-beoe")
480+
? JSON.parse(container.getAttribute("data-beoe")!)
481+
: null;
482+
483+
if (!data) return;
484+
const graph = json.read(data);
485+
486+
function clear() {
487+
container
488+
.querySelectorAll("g[id]")
489+
.forEach((node) => node.classList.remove("shadow"));
490+
}
491+
492+
function highlight(id: string) {
493+
container
494+
.querySelectorAll("g[id]")
495+
.forEach((node) => node.classList.add("shadow"));
496+
alg.postorder(graph, [id]).forEach((node) => {
497+
container
498+
.querySelector(`#${CSS.escape(node)}`)
499+
?.classList.remove("shadow");
500+
graph.outEdges(node)?.forEach(({ name }) => {
501+
container
502+
.querySelector(`#${CSS.escape(name!)}`)
503+
?.classList.remove("shadow");
504+
});
505+
});
506+
}
507+
508+
// highlight on hover
509+
let currentHover: string | null = null;
510+
container.addEventListener("mouseover", (e) => {
511+
// @ts-expect-error
512+
const node = e.target?.closest(".shape");
513+
514+
if (node) {
515+
const id = node.parentElement.getAttribute("id");
516+
if (currentHover == id) return;
517+
clear();
518+
highlight(id);
519+
currentHover = id;
520+
} else {
521+
if (currentHover == null) return;
522+
clear();
523+
currentHover = null;
524+
}
525+
});
526+
});
527+
```
528+
</TabItem>
529+
<TabItem label="CSS">
530+
531+
```css
532+
.d2[data-beoe].shadow {
533+
.shadow {
534+
opacity: 0.4;
535+
}
536+
537+
.shape {
538+
cursor: default;
539+
}
540+
}
541+
```
542+
</TabItem>
543+
</Tabs>
544+
453545
### Notes
454546

455-
- JS and CSS for those examples are not included in Vizdom Rehype plugin, but feel free to copy (from this website) and modify them
547+
- JS and CSS for those examples are not included in Rehype plugin, but feel free to copy (from this website) and modify them
456548
- Graphviz Rehype plugin also provides option for `data` (`graphFormat=dagre`), but ids in SVG are not consistent with ids in JSON. So it doesn't really work
457-
- [Request to support JSON representation for D2](https://github.com/terrastruct/d2/discussions/2224)
549+
- [There are some issues with D2](https://github.com/terrastruct/d2/discussions/2224)
458550

459551
### TODO
460552

0 commit comments

Comments
 (0)