Skip to content

Commit

Permalink
Illustrate accessing the view
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-matt-hillsdon committed Mar 6, 2024
1 parent 819752d commit 2575e70
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/editor/codemirror/reactWidgetExtension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import { useCallback } from "react";
import { supportedLanguages, useSettings } from "../../settings/settings";
import { PortalFactory } from "./CodeMirror";

interface ExampleReactComponentProps {
view: EditorView;
}

/**
* An example react component that we use inside a CodeMirror widget as
* a proof of concept.
*/
const ExampleReactComponent = () => {
const ExampleReactComponent = ({ view }: ExampleReactComponentProps) => {
console.log("We have access to the view here", view);

// This is a weird thing to do in a CodeMirror widget but proves the point that
// we can use React features to communicate with the rest of the app.
const [settings, setSettings] = useSettings();
Expand Down Expand Up @@ -50,9 +56,12 @@ class ExampleReactBlockWidget extends WidgetType {
super();
}

toDOM() {
toDOM(view: EditorView) {
const dom = document.createElement("div");
this.portalCleanup = this.createPortal(dom, <ExampleReactComponent />);
this.portalCleanup = this.createPortal(
dom,
<ExampleReactComponent view={view} />
);
return dom;
}

Expand Down

0 comments on commit 2575e70

Please sign in to comment.