'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 drag and drop functionality is with the DndKit
, which includes the pre-configured DndPlugin
along with React DnD setup and the BlockDraggable
UI component.
'use client';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { DndPlugin } from '@platejs/dnd';
import { PlaceholderPlugin } from '@platejs/media/react';
import { BlockDraggable } from '@/components/ui/block-draggable';
export const DndKit = [
DndPlugin.configure({
options: {
enableScroller: true,
onDropFiles: ({ dragItem, editor, target }) => {
editor
.getTransforms(PlaceholderPlugin)
.insert.media(dragItem.files, { at: target, nextBlock: false });
},
},
render: {
aboveNodes: BlockDraggable,
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
}),
];
BlockDraggable
: Renders drag handles and drop indicators for blocks.
Add Kit
import { createPlateEditor } from 'platejs/react';
import { DndKit } from '@/components/editor/plugins/dnd-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...DndKit,
],
});
Manual Usage
Installation
pnpm add @platejs/dnd react-dnd react-dnd-html5-backend
Add Plugin
import { DndPlugin } from '@platejs/dnd';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
DndPlugin,
],
});
Configure Plugin
Configure drag and drop with a draggable component and DnD provider:
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { DndPlugin } from '@platejs/dnd';
DndPlugin.configure({
render: {
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
});
render.aboveNodes
: AssignsBlockDraggable
to render drag handles above blocks.render.aboveSlate
: Wraps the editor withDndProvider
andHTML5Backend
. Remove this if you already haveDndProvider
in your React tree.
Advanced Configuration
Add automatic scrolling and file drop handling:
import { DndPlugin } from '@platejs/dnd';
import { PlaceholderPlugin } from '@platejs/media/react';
DndPlugin.configure({
options: {
enableScroller: true,
onDropFiles: ({ dragItem, editor, target }) => {
editor
.getTransforms(PlaceholderPlugin)
.insert.media(dragItem.files, { at: target, nextBlock: false });
},
},
render: {
aboveNodes: BlockDraggable,
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
});
enableScroller
: Enables automatic scrolling when dragging blocks near the viewport edges.onDropFiles
: Handles file drops by inserting them as media blocks at the target location.
Plugins
DndPlugin
Plugin for drag and drop functionality within the editor.
API
focusBlockStartById
Selects the start of a block by ID and focuses the editor.
getBlocksWithId
Returns an array of blocks that have an ID.
selectBlockById
Selects a block by its ID.
Hooks
useDndNode
A custom hook that combines the useDragNode
and useDropNode
hooks to enable dragging and dropping of a node from the editor. It provides a default preview for the dragged node, which can be customized or disabled.
- Default:
'block'
- Default:
'vertical'
The node to be dragged.
The type of drag item.
The ref of the node to be dragged.
The orientation of drag and drop.
Callback to determine if a node can be dropped at the current location.
The preview options for the dragged node.
The drag options for the dragged node.
The drop options for the dragged node.
Handler called when the node is dropped.
useDragNode
A custom hook that enables dragging of a node from the editor using the useDrag
hook from react-dnd
.
useDraggable
A custom hook that enables draggable behavior for a given element. It provides a preview for the element, which can be customized or disabled.
useDropNode
A custom hook that enables dropping a node on the editor. It uses the useDrop
hook from react-dnd
to handle the drop behavior.
- Returns
true
to prevent default behavior - Returns
false
to run default behavior after
Reference to the node being dragged.
The node to which the drop line is attached.
Current value of the drop line.
Callback when drop line changes.
Callback that intercepts drop handling.
useDropLine
Returns the current drop line state for an element.