Skip to content

Commit b46968f

Browse files
feat: Render Mantine BlockNoteView without MantineProvider (#2029)
* Added `withMantineProvider` prop * Ran `pnpm gen` * Made separate component instead of boolean flag * Updated naming * Made `BlockNoteView` render `MantineProvider` only if it's not already within a `MantineContext`
1 parent d3db99d commit b46968f

File tree

12 files changed

+55
-45
lines changed

12 files changed

+55
-45
lines changed

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@
140140
"y-partykit": "^0.0.33",
141141
"yjs": "^13.6.27"
142142
}
143-
}
143+
}

examples/03-ui-components/03-formatting-toolbar-block-type-items/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
"@vitejs/plugin-react": "^4.3.1",
2727
"vite": "^5.3.4"
2828
}
29-
}
29+
}

examples/06-custom-schema/01-alert-block/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
"@vitejs/plugin-react": "^4.3.1",
2727
"vite": "^5.3.4"
2828
}
29-
}
29+
}

examples/06-custom-schema/04-pdf-file-block/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
"@vitejs/plugin-react": "^4.3.1",
2727
"vite": "^5.3.4"
2828
}
29-
}
29+
}

examples/06-custom-schema/05-alert-block-full-ux/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
"@vitejs/plugin-react": "^4.3.1",
2727
"vite": "^5.3.4"
2828
}
29-
}
29+
}

examples/09-ai/01-minimal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
"@vitejs/plugin-react": "^4.3.1",
3030
"vite": "^5.3.4"
3131
}
32-
}
32+
}

examples/09-ai/02-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
"@vitejs/plugin-react": "^4.3.1",
3535
"vite": "^5.3.4"
3636
}
37-
}
37+
}

examples/09-ai/03-custom-ai-menu-items/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"@vitejs/plugin-react": "^4.3.1",
3232
"vite": "^5.3.4"
3333
}
34-
}
34+
}

examples/09-ai/04-with-collaboration/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"@vitejs/plugin-react": "^4.3.1",
3232
"vite": "^5.3.4"
3333
}
34-
}
34+
}

packages/mantine/src/BlockNoteView.tsx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
useBlockNoteContext,
1111
usePrefersColorScheme,
1212
} from "@blocknote/react";
13-
import { MantineProvider } from "@mantine/core";
14-
import React, { useCallback } from "react";
13+
import { MantineContext, MantineProvider } from "@mantine/core";
14+
import React, { useCallback, useContext } from "react";
1515
import {
1616
applyBlockNoteCSSVariablesFromTheme,
1717
removeBlockNoteCSSVariables,
@@ -20,11 +20,6 @@ import {
2020
import { components } from "./components.js";
2121
import "./style.css";
2222

23-
const mantineTheme = {
24-
// Removes button press effect
25-
activeClassName: "",
26-
};
27-
2823
export const BlockNoteView = <
2924
BSchema extends BlockSchema,
3025
ISchema extends InlineContentSchema,
@@ -76,34 +71,43 @@ export const BlockNoteView = <
7671
[defaultColorScheme, theme],
7772
);
7873

74+
const mantineContext = useContext(MantineContext);
75+
7976
const finalTheme =
8077
typeof theme === "string"
8178
? theme
8279
: defaultColorScheme !== "no-preference"
8380
? defaultColorScheme
8481
: "light";
8582

86-
return (
83+
const view = (
8784
<ComponentsContext.Provider value={components}>
88-
<MantineProvider
89-
theme={mantineTheme}
90-
// Scopes Mantine CSS variables to only the editor, as proposed here:
91-
// https://github.com/orgs/mantinedev/discussions/5685
92-
cssVariablesSelector=".bn-mantine"
93-
// This gets the element to set `data-mantine-color-scheme` on. This
94-
// element needs to already be rendered, so we can't set it to the
95-
// editor container element. Instead, we set it to `undefined` and set it
96-
// manually in `BlockNoteViewRaw`.
97-
getRootElement={() => undefined}
98-
>
99-
<BlockNoteViewRaw
100-
data-mantine-color-scheme={finalTheme}
101-
className={mergeCSSClasses("bn-mantine", className || "")}
102-
theme={typeof theme === "object" ? undefined : theme}
103-
{...rest}
104-
ref={ref}
105-
/>
106-
</MantineProvider>
85+
<BlockNoteViewRaw
86+
data-mantine-color-scheme={finalTheme}
87+
className={mergeCSSClasses("bn-mantine", className || "")}
88+
theme={typeof theme === "object" ? undefined : theme}
89+
{...rest}
90+
ref={ref}
91+
/>
10792
</ComponentsContext.Provider>
10893
);
94+
95+
if (mantineContext) {
96+
return view;
97+
}
98+
99+
return (
100+
<MantineProvider
101+
// Scopes Mantine CSS variables to only the editor, as proposed here:
102+
// https://github.com/orgs/mantinedev/discussions/5685
103+
cssVariablesSelector=".bn-mantine"
104+
// This gets the element to set `data-mantine-color-scheme` on. This
105+
// element needs to already be rendered, so we can't set it to the
106+
// editor container element. Instead, we set it to `undefined` and set it
107+
// manually in `BlockNoteViewRaw`.
108+
getRootElement={() => undefined}
109+
>
110+
{view}
111+
</MantineProvider>
112+
);
109113
};

0 commit comments

Comments
 (0)