Skip to content

Commit aa94d72

Browse files
committed
Made BlockNoteView render MantineProvider only if it's not already within a MantineContext
1 parent 7cfdd68 commit aa94d72

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

docs/content/docs/getting-started/mantine.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,3 @@ bun add @blocknote/core @blocknote/react @blocknote/mantine
2323
To use BlockNote with Mantine, you can import `BlockNoteView` from `@blocknote/mantine` and the stylesheet from `@blocknote/mantine/style.css`.
2424

2525
<Example name="basic/minimal" />
26-
27-
## Usage within a Mantine app
28-
29-
By default, the [`BlockNoteView`](/docs/getting-started/editor-setup#render-the-editor) component from `@blocknote/mantine` will be wrapped in a [`MantineProvider`](https://mantine.dev/theming/mantine-provider/) context. However, if you're already using Mantine for your application UI, this context will already be provided.
30-
31-
Therefore, `@blocknote/mantine` also includes a `BaseBlockNoteView` component which expects to be rendered in an existing `MantineProvider` context and does not include its own. It takes the same props as the regular `BlockNoteView`.

packages/mantine/src/BlockNoteView.tsx

Lines changed: 25 additions & 37 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,7 +20,7 @@ import {
2020
import { components } from "./components.js";
2121
import "./style.css";
2222

23-
export const BaseBlockNoteView = <
23+
export const BlockNoteView = <
2424
BSchema extends BlockSchema,
2525
ISchema extends InlineContentSchema,
2626
SSchema extends StyleSchema,
@@ -71,14 +71,16 @@ export const BaseBlockNoteView = <
7171
[defaultColorScheme, theme],
7272
);
7373

74+
const mantineContext = useContext(MantineContext);
75+
7476
const finalTheme =
7577
typeof theme === "string"
7678
? theme
7779
: defaultColorScheme !== "no-preference"
7880
? defaultColorScheme
7981
: "light";
8082

81-
return (
83+
const view = (
8284
<ComponentsContext.Provider value={components}>
8385
<BlockNoteViewRaw
8486
data-mantine-color-scheme={finalTheme}
@@ -89,37 +91,23 @@ export const BaseBlockNoteView = <
8991
/>
9092
</ComponentsContext.Provider>
9193
);
92-
};
9394

94-
export const BlockNoteView = <
95-
BSchema extends BlockSchema,
96-
ISchema extends InlineContentSchema,
97-
SSchema extends StyleSchema,
98-
>(
99-
props: Omit<
100-
React.ComponentProps<typeof BlockNoteViewRaw<BSchema, ISchema, SSchema>>,
101-
"theme"
102-
> & {
103-
theme?:
104-
| "light"
105-
| "dark"
106-
| Theme
107-
| {
108-
light: Theme;
109-
dark: Theme;
110-
};
111-
},
112-
) => (
113-
<MantineProvider
114-
// Scopes Mantine CSS variables to only the editor, as proposed here:
115-
// https://github.com/orgs/mantinedev/discussions/5685
116-
cssVariablesSelector=".bn-mantine"
117-
// This gets the element to set `data-mantine-color-scheme` on. This
118-
// element needs to already be rendered, so we can't set it to the
119-
// editor container element. Instead, we set it to `undefined` and set it
120-
// manually in `BlockNoteViewRaw`.
121-
getRootElement={() => undefined}
122-
>
123-
<BaseBlockNoteView {...props} />
124-
</MantineProvider>
125-
);
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+
);
113+
};

0 commit comments

Comments
 (0)