Skip to content

Commit 2e0c300

Browse files
committed
improve the web component example
1 parent 4718d37 commit 2e0c300

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ Which will start a vite server with a BlockNote editor instance including the av
7878

7979
#### Usage within a shadow dom root
8080

81-
This project uses `styled components` to define styles. This means that styles are, by default, injected onto the page header. To be able to use styles onto a shadow dom root it is necessary to use our `ShadowDomWrapper` component targeting the root for the styles.
81+
This project uses `styledComponents` to define styles. This means that styles are, by default, injected onto the page header. To be able to use styles onto a shadow dom root it is necessary to use our `ShadowDomWrapper` component targeting the root for the styles.
8282

8383
```tsx
84-
<ShadowDomWrapper target={this.targetHtmlElementOrShadowRoot}>
84+
<ShadowDomWrapper target={targetHtmlElementOrShadowRoot}>
8585
<MyBlockNoteView />
8686
</ShadowDomWrapper>
8787
```

src/op-blocknote.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { createRoot } from 'react-dom/client';
33
import App from './App';
44

55
import mantineStyles from '@blocknote/mantine/style.css?url';
6+
import { ShadowDomWrapper } from '../lib';
67

78
class BlockNoteElement extends HTMLElement {
89
private mount: HTMLDivElement;
10+
private reactRoot: ReturnType<typeof createRoot> | null = null;
911

1012
constructor() {
1113
super();
@@ -21,15 +23,26 @@ class BlockNoteElement extends HTMLElement {
2123
}
2224

2325
connectedCallback() {
24-
const root = createRoot(this.mount);
25-
26-
root.render(this.BlockNoteReactContainer());
26+
this.reactRoot = createRoot(this.mount);
27+
28+
this.reactRoot.render(
29+
React.createElement(
30+
ShadowDomWrapper,
31+
{ target: this.mount },
32+
React.createElement(App)
33+
)
34+
);
2735
}
2836

29-
BlockNoteReactContainer() {
30-
return React.createElement(App);
37+
disconnectedCallback() {
38+
if (this.reactRoot) {
39+
this.reactRoot.unmount();
40+
this.reactRoot = null;
41+
}
3142
}
3243
}
3344

34-
customElements.define('op-block-note', BlockNoteElement);
45+
if (!customElements.get('op-block-note')) {
46+
customElements.define('op-block-note', BlockNoteElement);
47+
}
3548

0 commit comments

Comments
 (0)