I’m trying to embed a notebook cell in a resizable React Component.
I have difficulties making the notebook cell chart responsive to the React Component Size.
Here is the code that is roughly working (displaying the chart but not resizing it)…
import React from "react";
import ReactDOM from "react-dom";
import { Runtime, Inspector, Library } from "@observablehq/runtime";
import define from "@maliky/3-filtres-pour-les-donnees-des-mooc-v3";
import { ResizableBox } from "react-resizable";
const mountNode = document.getElementById("react-root");
var runtime = new Runtime();
class App extends React.Component {
constructor(props) {
super(props);
this.state = { animationRef: React.createRef() };
}
componentDidMount() {
runtime.module(define, name => {
if (name === "viewof link_strength") {
return new Inspector.into(document.querySelector("#obs1"))();
/* return new Inspector.into(this.state.animationRef)(); */
}
});
}
render() {
return (
<div id="root">
<ResizableBox width={400} height={300}>
<div id="obs1"></div>
{/* <div ref={this.state.animationRef}></div> */}
</ResizableBox>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
you may notice that I need to add parentheses at the end of new Inspector.into(...)I’m not sure why. I don’t see this in other people’s code I’ve read. Edit: Checked again the docs… The reason is that I’m returning new Inspector.into(foo) and it is different from returning new Inspector(foo) new Inspector.into(foo)(baz) Is returning a new Inspector(foo) into the baz DOM element, but if baz is empty – i.e. new Inspector.into(foo)() – then, I guess, it does the same as new Inspector(foo)
where the svg attributes are a translation of DOM.svg(dim.W, dim.H)
But, maybe it is the way I am building and thinking my charts that is not correct.
Could you please tell me what you think about my approach to set the basics elements for my chart ?
… I know this is a stupid question, I’m sorry. There’s still basic stuff I don’t get about viewBox too although I am checking the MDN docs.
Edit:
I finally go the iframe technique working thanks to bgchen and his answer to my previous question and it does enable the chart to resize base on its containers’s width.
All there is to do is really to change the link of the import define to match your chart with js at the end.
endEdit: