Features
- Drag and drop blocks for content movement and insertion within the editor.
- Automatic scrolling when dragging blocks near viewport edges.
- File drop support for inserting media blocks.
- Visual drop indicators and drag handles.
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.
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.
enableScroller optional boolean
Enables automatic scrolling when dragging blocks near viewport edges.
- Default:
false
- Default:
scrollerProps optional Partial<ScrollerProps>
Props for the
Scroller
component whenenableScroller
is true.onDropFiles optional function
Handler for file drop events.
dropTarget optional { id: string | null; line: DropLineDirection }
The current drop target state containing both the target element id and drop line direction.
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.
element TElement
The node to be dragged.
type optional string
The type of drag item.
- Default:
'block'
- Default:
nodeRef any
The ref of the node to be dragged.
orientation optional 'horizontal' | 'vertical'
The orientation of drag and drop.
- Default:
'vertical'
- Default:
canDropNode optional (args: { dragEntry: NodeEntry; dragItem: DragItemNode; dropEntry: NodeEntry; editor: PlateEditor }) => boolean
Callback to determine if a node can be dropped at the current location.
preview optional previewOptions
The preview options for the dragged node.
drag optional dragOptions
The drag options for the dragged node.
drop optional dropOptions
The drop options for the dragged node.
onDropHandler optional string
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.
nodeRef any
Reference to the node being dragged.
element TElement
The node to which the drop line is attached.
dropLine string
Current value of the drop line.
onChangeDropLine function
Callback when drop line changes.
onDropHandler object
Callback that intercepts drop handling.
- Returns
true
to prevent default behavior - Returns
false
to run default behavior after
- Returns
useDropLine
Returns the current drop line state for an element.