主题
BlockNote 提供了浅色和深色主题。默认情况下,主题会根据用户的系统偏好自动选择,但你也可以强制使用明亮或暗黑模式。
当使用 Mantine 时,你拥有额外的主题功能:
- 针对浅色和深色模式自定义颜色方案,包括高亮颜色
- 更改字体、阴影、边框和边框半径
- 通过编程方式或使用 CSS 变量覆盖来自定义主题
本页主要关注使用 Mantine 带来的额外功能,但强制浅色/暗黑模式部分适用于所有 UI 库。
CSS 变量
主题由一组 CSS 变量组成,可以通过覆盖这些变量来更改编辑器主题。
以下是你可以设置的每个主题 CSS 变量,值取自默认浅色主题:
--bn-colors-editor-text: #3f3f3f;
--bn-colors-editor-background: #ffffff;
--bn-colors-menu-text: #3f3f3f;
--bn-colors-menu-background: #ffffff;
--bn-colors-tooltip-text: #3f3f3f;
--bn-colors-tooltip-background: #efefef;
--bn-colors-hovered-text: #3f3f3f;
--bn-colors-hovered-background: #efefef;
--bn-colors-selected-text: #ffffff;
--bn-colors-selected-background: #3f3f3f;
--bn-colors-disabled-text: #afafaf;
--bn-colors-disabled-background: #efefef;
--bn-colors-shadow: #cfcfcf;
--bn-colors-border: #efefef;
--bn-colors-side-menu: #cfcfcf;
--bn-colors-highlights-gray-text: #9b9a97;
--bn-colors-highlights-gray-background: #ebeced;
--bn-colors-highlights-brown-text: #64473a;
--bn-colors-highlights-brown-background: #e9e5e3;
--bn-colors-highlights-red-text: #e03e3e;
--bn-colors-highlights-red-background: #fbe4e4;
--bn-colors-highlights-orange-text: #d9730d;
--bn-colors-highlights-orange-background: #f6e9d9;
--bn-colors-highlights-yellow-text: #dfab01;
--bn-colors-highlights-yellow-background: #fbf3db;
--bn-colors-highlights-green-text: #4d6461;
--bn-colors-highlights-green-background: #ddedea;
--bn-colors-highlights-blue-text: #0b6e99;
--bn-colors-highlights-blue-background: #ddebf1;
--bn-colors-highlights-purple-text: #6940a5;
--bn-colors-highlights-purple-background: #eae4f2;
--bn-colors-highlights-pink-text: #ad1a72;
--bn-colors-highlights-pink-background: #f4dfeb;
--bn-font-family:
"Inter", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Open Sans",
"Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans",
"Droid Sans", "Helvetica Neue", sans-serif;
--bn-border-radius: 6px;在 .bn-container[data-color-scheme] 选择器上设置这些变量将覆盖默认的浅色和深色主题。若要分别覆盖浅色和深色主题,使用 .bn-container[data-color-scheme="light"] 和 .bn-container[data-color-scheme="dark"] 选择器。
编程配置
你还可以使用 BlockNoteView 的 theme 属性 来设置主题 CSS 变量。传入一个 Theme 对象会用该对象中的值覆盖浅色和深色主题的 CSS 变量。
/**
* 一对前景色和背景色
*/
type CombinedColor = Partial<{
text: string;
background: string;
}>;
/**
* 一个配色方案
*/
type ColorScheme = Partial<{
editor: CombinedColor;
menu: CombinedColor;
tooltip: CombinedColor;
hovered: CombinedColor;
selected: CombinedColor;
disabled: CombinedColor;
shadow: string;
border: string;
sideMenu: string;
highlights: Partial<{
gray: CombinedColor;
brown: CombinedColor;
red: CombinedColor;
orange: CombinedColor;
yellow: CombinedColor;
green: CombinedColor;
blue: CombinedColor;
purple: CombinedColor;
pink: CombinedColor;
}>;
}>;
/**
* 一个主题
*/
type Theme = Partial<{
colors: ColorScheme;
borderRadius: number;
fontFamily: string;
}>;下面的演示创建了与上个示例中的红色主题相同的效果,但这次是通过在 BlockNoteView 中使用 theme 属性设置的:
浅色和深色主题
你也可以通过传入以下对象类型,分别覆盖浅色和深色主题的 CSS 变量:
type LightAndDarkThemes = {
light: Theme;
dark: Theme;
};强制浅色/暗黑模式
通过向 theme 属性传入 "light" 或 "dark" 字符串(而非 Theme 对象),你还可以强制 BlockNote 始终使用明亮或暗黑主题。
如果你想对编辑器设置更复杂的样式,请参见 覆盖 CSS。