Drag Handle extension
Have you ever wanted to drag nodes around your editor? Well, we did too—so here’s an extension for that.
The DragHandle
extension allows you to easily handle dragging nodes around in the editor. You can define custom render functions, placement, and more.
Install
Set up access to Tiptap's private repository
Integrate this pro extension by registering for a free Tiptap account and following our access guide to Tiptap’s private repository.
npm install @tiptap-pro/extension-drag-handle
Settings
render
Renders an element that is positioned with tippy.js. This is the element that will be displayed as the handle when dragging a node around.
DragHandle.configure({
render: () => {
const element = document.createElement('div')
// Use as a hook for CSS to insert an icon
element.classList.add('custom-drag-handle')
return element
},
})
tippyOptions
Options for tippy.js. You can pass any options that are available in the tippy.js documentation.
Default: undefined
DragHandle.configure({
tippyOptions: {
placement: 'left',
},
})
onNodeChange
Returns a node or null when a node is hovered over. This can be used to highlight the node that is currently hovered over.
Default: undefined
DragHandle.configure({
onNodeChange: ({ node, editor }) => {
if (!node) {
selectedNode = null
return
}
// Do something with the node
selectedNode = node
},
})
Commands
lockDragHandle()
Locks the draghandle in place and visibility. If the drag handle was visible, it will remain visible until unlocked. If it was hidden, it will remain hidden until unlocked.
This can be useful if you want to have a menu inside of the drag handle and want it to remain visible whether the drag handle is moused over or not.
editor.commands.lockDragHandle()
unlockDragHandle()
Unlocks the draghandle. Resets to default visibility and behavior.
editor.commands.unlockDragHandle()
toggleDragHandle()
Toggle draghandle lock state. If the drag handle is locked, it will be unlocked and vice versa.
editor.commands.toggleDragHandle()