Skip to content

Commit 203eb84

Browse files
Make anywidget more flexible
1 parent 84aa693 commit 203eb84

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "egraph-visualizer",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/saulshanabrook/egraph-visualizer.git"

src/Visualizer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ function LayoutFlow({
701701
);
702702
}
703703

704-
function Visualizer({ egraph }: { egraph: string }) {
704+
function Visualizer({ egraph, height = null, resize = false }: { egraph: string; height?: string | null; resize?: boolean }) {
705705
const [outerElem, setOuterElem] = useState<HTMLDivElement | null>(null);
706706
const [innerElem, setInnerElem] = useState<HTMLDivElement | null>(null);
707707

@@ -713,7 +713,7 @@ function Visualizer({ egraph }: { egraph: string }) {
713713
}
714714
}, [rootElem]);
715715
return (
716-
<div className="w-full h-full" ref={setRootElem}>
716+
<div className={`w-full ${resize ? "resize-y" : ""}`} style={{ height: height || "100%" }} ref={setRootElem}>
717717
{/* Hidden node to measure text size */}
718718
<div className="invisible absolute">
719719
<ENode outerRef={setOuterElem} innerRef={setInnerElem} />

src/anywidget.tsx

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import { useSyncExternalStore } from "react";
21
import { createRoot } from "react-dom/client";
32
import "./index.css";
43
import { DOMWidgetModel } from "@jupyter-widgets/base";
54
import Visualizer from "./Visualizer.tsx";
6-
7-
// eslint-disable-next-line react-refresh/only-export-components
8-
function ModelApp({ model }: { model: DOMWidgetModel }) {
9-
const egraph: string = useSyncExternalStore(
10-
(callback) => {
11-
model.on("change:egraph", callback);
12-
return () => model.off("change:egraph", callback);
13-
},
14-
() => model.get("egraph")
15-
);
16-
return <Visualizer egraph={egraph} />;
17-
}
5+
import { startTransition } from "react";
186

197
function render({ model, el }: { el: HTMLElement; model: DOMWidgetModel }) {
208
const root = createRoot(el);
21-
root.render(<ModelApp model={model} />);
22-
return () => root.unmount();
9+
const height = model.has("height") ? model.get("height") : "600px";
10+
function render() {
11+
startTransition(() => {
12+
root.render(<Visualizer egraph={model.get("egraph")} height={height} resize />);
13+
});
14+
}
15+
render();
16+
model.on("change:egraph", render);
17+
18+
return () => {
19+
model.off("change:egraph", render);
20+
root.unmount();
21+
};
2322
}
2423

2524
export default { render };

0 commit comments

Comments
 (0)