Skip to content

Commit 61564e1

Browse files
Make sure lib is only one file
relative imports wont work with anywidget
1 parent 203eb84 commit 61564e1

File tree

7 files changed

+56
-503
lines changed

7 files changed

+56
-503
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "egraph-visualizer",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/saulshanabrook/egraph-visualizer.git"
@@ -13,6 +13,7 @@
1313
"preview": "vite preview"
1414
},
1515
"dependencies": {
16+
"@anywidget/types": "0.2.0",
1617
"@headlessui/react": "2.1.3",
1718
"@heroicons/react": "2.1.5",
1819
"@xyflow/react": "12.1.1",
@@ -26,7 +27,6 @@
2627
},
2728
"devDependencies": {
2829
"@eslint/js": "^9.9.0",
29-
"@jupyter-widgets/base": "6.0.10",
3030
"@types/node": "22.5.0",
3131
"@types/react": "^18.3.3",
3232
"@types/react-dom": "^18.3.0",
@@ -45,6 +45,6 @@
4545
"files": [
4646
"dist"
4747
],
48-
"main": "dist/dom.js",
48+
"main": "dist/index.js",
4949
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
5050
}

src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { use, useState } from "react";
22
import "./App.css";
33
import Monaco from "./Monaco";
4-
import Visualizer from "./Visualizer";
4+
import { Visualizer } from "./Visualizer";
55
import DefaultCode from "/examples/manual/homepage.json?raw";
66

77
function App() {

src/Visualizer.tsx

+43-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import {
3030
} from "@xyflow/react";
3131

3232
import "@xyflow/react/dist/style.css";
33+
import { AnyModel } from "@anywidget/types";
34+
import { createRoot } from "react-dom/client";
3335
// Elk has a *huge* amount of options to configure. To see everything you can
3436
// tweak check out:
3537
//
@@ -701,7 +703,7 @@ function LayoutFlow({
701703
);
702704
}
703705

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

@@ -731,4 +733,43 @@ function Visualizer({ egraph, height = null, resize = false }: { egraph: string;
731733
);
732734
}
733735

734-
export default Visualizer;
736+
// Put these both in one file, so its emitted as a single chunk and anywidget doesn't have to import another file
737+
738+
/// Render anywidget model to the given element
739+
// Must be named `render` to work as an anywidget module
740+
// https://anywidget.dev/en/afm/#lifecycle-methods
741+
// eslint-disable-next-line react-refresh/only-export-components
742+
export function render({ model, el }: { el: HTMLElement; model: AnyModel }) {
743+
const root = createRoot(el);
744+
function render() {
745+
startTransition(() => {
746+
root.render(<Visualizer egraph={model.get("egraph")} height="600px" resize />);
747+
});
748+
}
749+
render();
750+
model.on("change:egraph", render);
751+
752+
return () => {
753+
model.off("change:egraph", render);
754+
root.unmount();
755+
};
756+
}
757+
758+
/// Mount the visualizer to the given element
759+
/// Call `render` to render a new egraph
760+
/// Call `unmount` to unmount the visualizer
761+
// eslint-disable-next-line react-refresh/only-export-components
762+
export function mount(element: HTMLElement): { render: (egraph: string) => void; unmount: () => void } {
763+
const root = createRoot(element);
764+
765+
function render(egraph: string) {
766+
startTransition(() => {
767+
root.render(<Visualizer egraph={egraph} />);
768+
});
769+
}
770+
771+
function unmount() {
772+
root.unmount();
773+
}
774+
return { render, unmount };
775+
}

src/anywidget.tsx

-24
This file was deleted.

src/dom.tsx

-23
This file was deleted.

vite.config.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ export default defineConfig({
88
base: "./",
99
build: {
1010
lib: {
11-
entry: [resolve(__dirname, "src/anywidget.tsx"), resolve(__dirname, "src/dom.tsx")],
11+
entry: [resolve(__dirname, "src/Visualizer.tsx")],
1212
formats: ["es"],
1313
},
1414
rollupOptions: {
1515
input: {
1616
main: resolve(__dirname, "index.html"),
17-
anywidget: resolve(__dirname, "src/anywidget.tsx"),
18-
dom: resolve(__dirname, "src/dom.tsx"),
17+
index: resolve(__dirname, "src/Visualizer.tsx"),
1918
},
19+
// allow extension of entry signatures so Visualizer.tsx can be outputed as index.js and not include any imports
20+
preserveEntrySignatures: "allow-extension",
2021
},
2122
},
2223

0 commit comments

Comments
 (0)