apps/docs/extend/plugins.mdx
Plugins are a powerful way to extend the core functionality of @dnd-kit. They can be used to add new features, modify existing behavior, or react to drag and drop operations.
Several plugins are included by default when creating a new DragDropManager:
The plugins option accepts either an array or a function that receives the default plugins.
Use the function form to add plugins to the defaults or configure existing ones, without replacing them:
import {DragDropManager} from '@dnd-kit/dom';
const manager = new DragDropManager({
plugins: (defaults) => [...defaults, MyPlugin],
});
import {DragDropManager, Feedback} from '@dnd-kit/dom';
const manager = new DragDropManager({
plugins: (defaults) => [
...defaults,
Feedback.configure({ dropAnimation: null }),
],
});
Pass an array to fully replace the default plugins:
const manager = new DragDropManager({
plugins: [
MyPlugin.configure({ delay: 500 }),
AutoScroller,
]
});
To create a custom plugin, extend the Plugin class:
import {Plugin} from '@dnd-kit/abstract';
interface MyPluginOptions {
delay?: number;
}
class MyPlugin extends Plugin {
constructor(manager, options?: MyPluginOptions) {
super(manager, options);
this.registerEffect(() => {
const {monitor} = this.manager;
const cleanup = monitor.addEventListener('dragstart', (event) => {
console.log('Drag started:', event.operation.source.id);
});
return cleanup;
});
}
public customMethod() {
if (this.disabled) return;
// Custom functionality
}
}
The base class for all plugins:
<ParamField path="manager" type="DragDropManager" required> Reference to the drag and drop manager instance. </ParamField> <ParamField path="options" type="PluginOptions"> Optional configuration options for the plugin. </ParamField>disabled: Whether the plugin is currently disabledoptions: Current plugin optionsenable(): Enable the plugindisable(): Disable the pluginisDisabled(): Check if plugin is disabledconfigure(options): Update plugin optionsdestroy(): Clean up plugin resourcesregisterEffect(callback): Register a reactive effectconfigure(options): Create a configured plugin descriptorconst configuredPlugin = MyPlugin.configure({
delay: 500
});
const manager = new DragDropManager({
plugins: [configuredPlugin]
});
Plugins can register effects that automatically clean up:
class MyPlugin extends Plugin {
constructor(manager) {
super(manager);
this.registerEffect(() => {
const interval = setInterval(() => {
// Do something periodically
}, 100);
return () => clearInterval(interval);
});
}
}
destroy methodthis.disabled before performing operations