Loading...
Files
components/demo.tsx
'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>
);
}
Kit Usage
Installation
The fastest way to add block placeholders is with the BlockPlaceholderKit
, which includes the pre-configured BlockPlaceholderPlugin
.
'use client';
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';
export const BlockPlaceholderKit = [
BlockPlaceholderPlugin.configure({
options: {
className:
'before:absolute before:cursor-text before:text-muted-foreground/80 before:content-[attr(placeholder)]',
placeholders: {
[KEYS.p]: 'Type something...',
},
query: ({ path }) => {
return path.length === 1;
},
},
}),
];
Add Kit
import { createPlateEditor } from 'platejs/react';
import { BlockPlaceholderKit } from '@/components/editor/plugins/block-placeholder-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...BlockPlaceholderKit,
],
});
Manual Usage
Installation
Block placeholders are included in the core Plate package.
import { BlockPlaceholderPlugin } from 'platejs/react';
Add Plugin
import { BlockPlaceholderPlugin } from 'platejs/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
BlockPlaceholderPlugin,
],
});
Configure Plugin
Configure placeholders for different block types:
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';
BlockPlaceholderPlugin.configure({
options: {
placeholders: {
[KEYS.p]: 'Type something...',
[KEYS.h1]: 'Enter heading...',
[KEYS.blockquote]: 'Enter quote...',
[KEYS.codeBlock]: 'Enter code...',
},
},
});
Advanced Configuration
Customize appearance and visibility rules:
import { KEYS } from 'platejs';
import { BlockPlaceholderPlugin } from 'platejs/react';
BlockPlaceholderPlugin.configure({
options: {
className: 'before:absolute before:cursor-text before:opacity-30 before:content-[attr(placeholder)]',
placeholders: {
[KEYS.p]: 'Type something...',
},
query: ({ path }) => {
// Only show placeholders in root-level blocks
return path.length === 1;
},
},
});
placeholders
: Map of block types to placeholder text.className
: CSS classes applied to blocks with placeholders.query
: Function to determine which blocks should show placeholders.
Plugins
BlockPlaceholderPlugin
Plugin for displaying placeholder text in empty blocks.
The plugin shows placeholders when all of these conditions are met:
- The block is empty (contains no content)
- The editor is not empty (has other content)
- The editor is focused
- The block matches the query function
- The block type matches a key in the placeholders map