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
Enhances a component with draggable behavior.
DndScroller
A wrapper component for the Scroller
component that is conditionally rendered based on the dragging state.
A flag indicating whether the scroll area is enabled. If set to false, the scroll functionality is disabled.
Height of the scrollable area in pixels.
The stack order of the scrollable area. An element with a higher z-index is drawn in front of an element with a lower one.
The minimum strength (speed) of the scrolling action. Higher values will make the scroll action more sensitive.
A multiplier that increases the scroll speed. Higher values will result in faster scrolling.
Reference to the scrollable container DOM element.
Additional props for the scroll area div element.
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.