Skip to content

Commit 5ce6a33

Browse files
docs: AI playground styling fixes (#1800)
* Scoped Mantine styles in AI playground demo * Fixed playground not setting Mantine dark mode from system theme * Updated dark theme e2e snaps
1 parent 3e94d1a commit 5ce6a33

26 files changed

+86
-70
lines changed

examples/09-ai/02-playground/App.tsx

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import { createGroq } from "@ai-sdk/groq";
33
import { createMistral } from "@ai-sdk/mistral";
44
import { createOpenAI } from "@ai-sdk/openai";
55
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
6+
import "@mantine/core/styles.css";
67
import { BlockNoteEditor, filterSuggestionItems } from "@blocknote/core";
78
import "@blocknote/core/fonts/inter.css";
89
import { en } from "@blocknote/core/locales";
910
import { BlockNoteView } from "@blocknote/mantine";
10-
import "@mantine/core/styles.css";
1111
import "@blocknote/mantine/style.css";
1212
import {
1313
FormattingToolbar,
1414
FormattingToolbarController,
1515
SuggestionMenuController,
1616
getDefaultReactSlashMenuItems,
1717
getFormattingToolbarItems,
18+
useBlockNoteContext,
1819
useCreateBlockNote,
20+
usePrefersColorScheme,
1921
} from "@blocknote/react";
2022
import {
2123
AIMenuController,
@@ -136,74 +138,88 @@ export default function App() {
136138

137139
const stream = useStore(ai.options, (state) => state.stream);
138140

141+
const themePreference = usePrefersColorScheme();
142+
const existingContext = useBlockNoteContext();
143+
144+
const theme =
145+
existingContext?.colorSchemePreference ||
146+
(themePreference === "no-preference" ? "light" : themePreference);
147+
139148
return (
140-
<MantineProvider>
141-
<div>
142-
<Fieldset legend="Model settings" style={{ maxWidth: "500px" }}>
143-
<BasicAutocomplete
144-
error={model === "unknown-model" ? "Unknown model" : undefined}
145-
value={modelString}
146-
onChange={setModelString}
147-
/>
148-
<RadioGroupComponent
149-
label="Data format"
150-
items={[
151-
{ name: "HTML", description: "HTML", value: "html" },
152-
{
153-
name: "JSON",
154-
description: "JSON (experimental)",
155-
value: "json",
156-
},
157-
{
158-
name: "Markdown",
159-
description: "Markdown (experimental)",
160-
value: "markdown",
161-
},
162-
]}
163-
value={dataFormat}
164-
onChange={(value) => {
165-
const dataFormat =
166-
value === "markdown"
167-
? llmFormats._experimental_markdown
168-
: value === "json"
169-
? llmFormats._experimental_json
170-
: llmFormats.html;
171-
ai.options.setState({
172-
dataFormat,
173-
});
174-
setDataFormat(value);
175-
}}
176-
/>
177-
178-
<Switch
179-
checked={stream}
180-
onChange={(e) => ai.options.setState({ stream: e.target.checked })}
181-
label="Streaming"
182-
/>
183-
</Fieldset>
184-
185-
<BlockNoteView
186-
editor={editor}
187-
formattingToolbar={false}
188-
slashMenu={false}
189-
>
190-
{/* Add the AI Command menu to the editor */}
191-
<AIMenuController />
192-
193-
{/* We disabled the default formatting toolbar with `formattingToolbar=false`
149+
<div>
150+
<MantineProvider
151+
cssVariablesSelector=".model-settings"
152+
getRootElement={() => undefined}
153+
>
154+
<div className="model-settings" data-mantine-color-scheme={theme}>
155+
<Fieldset legend="Model settings" style={{ maxWidth: "500px" }}>
156+
<BasicAutocomplete
157+
error={model === "unknown-model" ? "Unknown model" : undefined}
158+
value={modelString}
159+
onChange={setModelString}
160+
/>
161+
<RadioGroupComponent
162+
label="Data format"
163+
items={[
164+
{ name: "HTML", description: "HTML", value: "html" },
165+
{
166+
name: "JSON",
167+
description: "JSON (experimental)",
168+
value: "json",
169+
},
170+
{
171+
name: "Markdown",
172+
description: "Markdown (experimental)",
173+
value: "markdown",
174+
},
175+
]}
176+
value={dataFormat}
177+
onChange={(value) => {
178+
const dataFormat =
179+
value === "markdown"
180+
? llmFormats._experimental_markdown
181+
: value === "json"
182+
? llmFormats._experimental_json
183+
: llmFormats.html;
184+
ai.options.setState({
185+
dataFormat,
186+
});
187+
setDataFormat(value);
188+
}}
189+
/>
190+
191+
<Switch
192+
checked={stream}
193+
onChange={(e) =>
194+
ai.options.setState({ stream: e.target.checked })
195+
}
196+
label="Streaming"
197+
/>
198+
</Fieldset>
199+
</div>
200+
</MantineProvider>
201+
202+
<BlockNoteView
203+
editor={editor}
204+
formattingToolbar={false}
205+
slashMenu={false}
206+
>
207+
{/* Add the AI Command menu to the editor */}
208+
<AIMenuController />
209+
210+
{/* We disabled the default formatting toolbar with `formattingToolbar=false`
194211
and replace it for one with an "AI button" (defined below).
195212
(See "Formatting Toolbar" in docs)
196213
*/}
197-
<FormattingToolbarWithAI />
214+
<FormattingToolbarWithAI />
198215

199-
{/* We disabled the default SlashMenu with `slashMenu=false`
216+
{/* We disabled the default SlashMenu with `slashMenu=false`
200217
and replace it for one with an AI option (defined below).
201218
(See "Suggestion Menus" in docs)
202219
*/}
203-
<SuggestionMenuWithAI editor={editor} />
204-
</BlockNoteView>
205-
</div>
206-
</MantineProvider>
220+
<SuggestionMenuWithAI editor={editor} />
221+
</BlockNoteView>
222+
</div>
207223
);
208224
}
209225

playground/src/main.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { usePrefersColorScheme } from "@blocknote/react";
12
import { AppShell, MantineProvider, ScrollArea } from "@mantine/core";
23
import React from "react";
34
import { createRoot } from "react-dom/client";
@@ -31,8 +32,15 @@ function Root() {
3132
// },
3233
// // "root:hover": { background: "blue" },
3334
// });
35+
36+
const themePreference = usePrefersColorScheme();
37+
3438
return (
35-
<MantineProvider>
39+
<MantineProvider
40+
forceColorScheme={
41+
themePreference === "no-preference" ? undefined : themePreference
42+
}
43+
>
3644
<AppShell
3745
navbar={
3846
window.location.search.includes("hideMenu")
@@ -46,14 +54,6 @@ function Root() {
4654
// header={<Header height={60} p="xs">
4755
// {/* Header content */}
4856
// </Header>}
49-
styles={() => ({
50-
main: {
51-
backgroundColor: "white",
52-
// theme.colorScheme === "dark"
53-
// ? theme.colors.dark[8]
54-
// : theme.colors.gray[0],
55-
},
56-
})}
5757
>
5858
{window.location.search.includes("hideMenu") ? undefined : (
5959
<AppShell.Navbar p="xs">
-1.96 KB
Loading
-2.43 KB
Loading
-2.3 KB
Loading
-1.32 KB
Loading
-273 Bytes
Loading
-442 Bytes
Loading
-908 Bytes
Loading
-895 Bytes
Loading

0 commit comments

Comments
 (0)