Drag & Drop
'use client';
import React from 'react';
import { Plate } from '@udecode/plate/react';
import { editorPlugins } from '@/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/components/editor/use-create-editor';
import { Editor, EditorContainer } from '@/components/plate-ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = useCreateEditor({
plugins: [...editorPlugins],
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Installation
npm install @udecode/plate-dnd @udecode/plate-node-id react-dnd react-dnd-html5-backend
Usage
import { DndPlugin } from '@udecode/plate-dnd';
import { NodeIdPlugin } from '@udecode/plate-node-id';
const plugins = [
// ...otherPlugins,
NodeIdPlugin,
DndPlugin,
];
Then, wrap your plugin components with Draggable.
Plugins
DndPlugin
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
Components
withDraggable
DndScroller
A wrapper component for the Scroller
component that is conditionally rendered based on the dragging state.
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.
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.