'use client';
import * as React from 'react';
import { Plate, usePlateEditor } from 'platejs/react';
import { EditorKit } from '@/components/editor/editor-kit';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = usePlateEditor({
plugins: EditorKit,
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Features
- Apply font styling to selected text including size, family, color, background color, and weight.
- Supports custom font families, sizes, colors, and weights.
Plugins
FontBackgroundColorPlugin
: Control background color withbackground-color
styleFontColorPlugin
: Control font color withcolor
styleFontFamilyPlugin
: Change font family using inline elements withfont-family
styleFontSizePlugin
: Control font size with CSS class orfont-size
styleFontWeightPlugin
: Control font weight withfont-weight
style
Kit Usage
Installation
The fastest way to add font styling functionality is with the FontKit
, which includes pre-configured font plugins with their Plate UI components.
'use client';
import type { PlatePluginConfig } from 'platejs/react';
import {
FontBackgroundColorPlugin,
FontColorPlugin,
FontFamilyPlugin,
FontSizePlugin,
} from '@platejs/basic-styles/react';
import { KEYS } from 'platejs';
const options = {
inject: { targetPlugins: [KEYS.p] },
} satisfies PlatePluginConfig;
export const FontKit = [
FontColorPlugin.configure({
inject: {
...options.inject,
nodeProps: {
defaultNodeValue: 'black',
},
},
}),
FontBackgroundColorPlugin.configure(options),
FontSizePlugin.configure(options),
FontFamilyPlugin.configure(options),
];
- Includes all font plugins (
FontColorPlugin
,FontBackgroundColorPlugin
,FontSizePlugin
,FontFamilyPlugin
) with sensible defaults.
Add Kit
Add the kit to your plugins:
import { createPlateEditor } from 'platejs/react';
import { FontKit } from '@/components/editor/plugins/font-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...FontKit,
],
});
Manual Usage
Installation
pnpm add @platejs/basic-styles
Add Plugins
Include the font plugins in your Plate plugins array when creating the editor.
import {
FontBackgroundColorPlugin,
FontColorPlugin,
FontFamilyPlugin,
FontSizePlugin,
} from '@platejs/basic-styles/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
FontColorPlugin,
FontBackgroundColorPlugin,
FontFamilyPlugin,
FontSizePlugin,
],
});
Configure Plugins
You can configure individual font plugins with custom options and target elements.
import {
FontColorPlugin,
FontBackgroundColorPlugin,
FontSizePlugin,
FontFamilyPlugin,
} from '@platejs/basic-styles/react';
import { KEYS } from 'platejs';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
FontColorPlugin.configure({
inject: {
nodeProps: {
defaultNodeValue: 'black',
},
targetPlugins: [KEYS.p],
},
}),
FontSizePlugin.configure({
inject: {
targetPlugins: [KEYS.p],
},
}),
FontBackgroundColorPlugin.configure({
inject: {
targetPlugins: [KEYS.p],
},
}),
FontFamilyPlugin.configure({
inject: {
targetPlugins: [KEYS.p],
},
}),
],
});
inject.nodeProps.defaultNodeValue
: Sets the default font color value.inject.targetPlugins
: Specifies which element types can receive font styling (affects HTML parsing).
Add Toolbar Button
You can add FontColorToolbarButton
and FontSizeToolbarButton
to your Toolbar to control font color and size.
Plugins
FontBackgroundColorPlugin
Plugin for font background color formatting. Applies background-color
style to selected text.
FontColorPlugin
Plugin for font color formatting. Applies color
style to selected text.
FontFamilyPlugin
Plugin for font family formatting. Applies font-family
style to selected text.
FontSizePlugin
Plugin for font size formatting. Applies font-size
style to selected text.
FontWeightPlugin
Plugin for font weight formatting. Applies font-weight
style to selected text.
Transforms
tf.backgroundColor.addMark
Set the font background color mark on the selected text.
tf.color.addMark
Set the font color mark on the selected text.
tf.fontFamily.addMark
Set the font family mark on the selected text.
tf.fontSize.addMark
Set the font size mark on the selected text.
tf.fontWeight.addMark
Set the font weight mark on the selected text.
API
toUnitLess
Convert a font size value to a unitless value.
On This Page
FeaturesPluginsKit UsageInstallationAdd KitManual UsageInstallationAdd PluginsConfigure PluginsAdd Toolbar ButtonPluginsFontBackgroundColorPluginFontColorPluginFontFamilyPluginFontSizePluginFontWeightPluginTransformstf.backgroundColor.addMarktf.color.addMarktf.fontFamily.addMarktf.fontSize.addMarktf.fontWeight.addMarkAPItoUnitLess