Skip to content

Commit afb0a93

Browse files
YousefEDnperez0111
andauthored
fix: shadowdom support and example (#2223)
Co-authored-by: Nick the Sick <[email protected]>
1 parent 9041e17 commit afb0a93

File tree

18 files changed

+1887
-1553
lines changed

18 files changed

+1887
-1553
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"playground": true,
3+
"docs": false,
4+
"author": "matthewlipski",
5+
"tags": ["Basic"]
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Shadow DOM
2+
3+
This example shows how to render the BlockNote editor inside a Shadow DOM.
4+
5+
**Relevant Docs:**
6+
7+
- [Editor Setup](/docs/getting-started/editor-setup)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8" />
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
<title>Shadow DOM</title>
6+
<script>
7+
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY -->
8+
</script>
9+
</head>
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="./main.tsx"></script>
13+
</body>
14+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
2+
import React from "react";
3+
import { createRoot } from "react-dom/client";
4+
import App from "./src/App.jsx";
5+
6+
const root = createRoot(document.getElementById("root")!);
7+
root.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@blocknote/example-basic-shadowdom",
3+
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
4+
"type": "module",
5+
"private": true,
6+
"version": "0.12.4",
7+
"scripts": {
8+
"start": "vite",
9+
"dev": "vite",
10+
"build:prod": "tsc && vite build",
11+
"preview": "vite preview"
12+
},
13+
"dependencies": {
14+
"@blocknote/ariakit": "latest",
15+
"@blocknote/core": "latest",
16+
"@blocknote/mantine": "latest",
17+
"@blocknote/react": "latest",
18+
"@blocknote/shadcn": "latest",
19+
"@mantine/core": "^8.3.4",
20+
"@mantine/hooks": "^8.3.4",
21+
"@mantine/utils": "^6.0.22",
22+
"react": "^19.2.1",
23+
"react-dom": "^19.2.1"
24+
},
25+
"devDependencies": {
26+
"@types/react": "^19.2.2",
27+
"@types/react-dom": "^19.2.2",
28+
"@vitejs/plugin-react": "^4.7.0",
29+
"vite": "^5.4.20"
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { BlockNoteView } from "@blocknote/mantine";
2+
import { useCreateBlockNote } from "@blocknote/react";
3+
4+
import { useEffect, useRef, useState } from "react";
5+
import { createPortal } from "react-dom";
6+
7+
import interCss from "@blocknote/core/fonts/inter.css?inline";
8+
import mantineCss from "@blocknote/mantine/style.css?inline";
9+
10+
function ShadowWrapper(props: { children: React.ReactNode }) {
11+
const host = useRef<HTMLDivElement>(null);
12+
const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);
13+
14+
useEffect(() => {
15+
if (host.current && !shadowRoot) {
16+
const root = host.current.shadowRoot || host.current.attachShadow({ mode: "open" });
17+
setShadowRoot(root);
18+
}
19+
}, [shadowRoot]);
20+
21+
return (
22+
<div ref={host}>
23+
{shadowRoot &&
24+
createPortal(
25+
<>
26+
<style>{interCss}</style>
27+
<style>{mantineCss}</style>
28+
{props.children}
29+
</>,
30+
shadowRoot as any
31+
)}
32+
</div>
33+
);
34+
}
35+
36+
export default function App() {
37+
// Creates a new editor instance.
38+
const editor = useCreateBlockNote();
39+
40+
// Renders the editor instance using a React component.
41+
return (
42+
<ShadowWrapper>
43+
<BlockNoteView editor={editor} />
44+
</ShadowWrapper>
45+
);
46+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="vite/client" />
2+
3+
declare module "*?inline" {
4+
const content: string;
5+
export default content;
6+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"useDefineForClassFields": true,
6+
"lib": [
7+
"DOM",
8+
"DOM.Iterable",
9+
"ESNext"
10+
],
11+
"allowJs": false,
12+
"skipLibCheck": true,
13+
"esModuleInterop": false,
14+
"allowSyntheticDefaultImports": true,
15+
"strict": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"module": "ESNext",
18+
"moduleResolution": "bundler",
19+
"resolveJsonModule": true,
20+
"isolatedModules": true,
21+
"noEmit": true,
22+
"jsx": "react-jsx",
23+
"composite": true
24+
},
25+
"include": [
26+
"."
27+
],
28+
"__ADD_FOR_LOCAL_DEV_references": [
29+
{
30+
"path": "../../../packages/core/"
31+
},
32+
{
33+
"path": "../../../packages/react/"
34+
}
35+
]
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
2+
import react from "@vitejs/plugin-react";
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import { defineConfig } from "vite";
6+
// import eslintPlugin from "vite-plugin-eslint";
7+
// https://vitejs.dev/config/
8+
export default defineConfig((conf) => ({
9+
plugins: [react()],
10+
optimizeDeps: {},
11+
build: {
12+
sourcemap: true,
13+
},
14+
resolve: {
15+
alias:
16+
conf.command === "build" ||
17+
!fs.existsSync(path.resolve(__dirname, "../../packages/core/src"))
18+
? {}
19+
: ({
20+
// Comment out the lines below to load a built version of blocknote
21+
// or, keep as is to load live from sources with live reload working
22+
"@blocknote/core": path.resolve(
23+
__dirname,
24+
"../../packages/core/src/"
25+
),
26+
"@blocknote/react": path.resolve(
27+
__dirname,
28+
"../../packages/react/src/"
29+
),
30+
} as any),
31+
},
32+
}));

examples/03-ui-components/13-custom-ui/src/MUISuggestionMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function MUISuggestionMenuItem(
4444

4545
const overflow = elementOverflow(
4646
itemRef.current,
47-
document.querySelector(
47+
itemRef.current.closest(
4848
`.MuiPaper-root:has([aria-label="suggestion-menu"])`,
4949
)!,
5050
);

0 commit comments

Comments
 (0)