apps/docs/solid/quickstart.mdx
import {Story} from '/snippets/story.mdx'; import Intro from '/snippets/quickstart/intro.mdx';
<Intro />The @dnd-kit/solid package provides a set of SolidJS hooks and components that you can use to build drag and drop interfaces. It is a thin SolidJS integration layer built on top of the vanilla library, so all of the same concepts are shared and can be used. You can refer to the vanilla documentation of these concepts, such as plugins, modifiers, and sensors.
Before getting started, make sure you install @dnd-kit/solid in your project:
yarn add @dnd-kit/solid
pnpm add @dnd-kit/solid
bun add @dnd-kit/solid
Let's get started by creating draggable elements. The useDraggable hook requires a unique id, and returns a ref callback that you can attach to any element.
import {useDraggable} from '@dnd-kit/solid';
function Draggable() {
const {ref} = useDraggable({id: 'draggable'});
return (
<button ref={ref}>
Draggable
</button>
);
}
To create drop targets, use the useDroppable hook.
import {useDroppable} from '@dnd-kit/solid';
function Droppable(props) {
const {ref, isDropTarget} = useDroppable({id: props.id});
return (
<div ref={ref} style={{width: '300px', height: '300px'}}>
{props.children}
</div>
);
}
Wrap your draggable and droppable elements in a DragDropProvider to enable drag and drop interactions.
import {createSignal} from 'solid-js';
import {DragDropProvider, useDraggable, useDroppable} from '@dnd-kit/solid';
function Draggable() {
const {ref} = useDraggable({id: 'draggable'});
return <button ref={ref}>Draggable</button>;
}
function Droppable(props) {
const {ref, isDropTarget} = useDroppable({id: 'droppable'});
return <div ref={ref}>{props.children}</div>;
}
function App() {
const [parent, setParent] = createSignal(undefined);
return (
<DragDropProvider
onDragEnd={(event) => {
if (event.canceled) return;
setParent(event.operation.target?.id);
}}
>
{parent() == null ? <Draggable /> : null}
<Droppable id="droppable">
{parent() === 'droppable' ? <Draggable /> : null}
</Droppable>
</DragDropProvider>
);
}