apps/docs/solid/hooks/use-droppable.mdx
import {CodeSandbox} from '/snippets/sandbox.mdx'; import {draggableStyles, droppableStyles} from '/snippets/code.mdx';
<CodeSandbox template="solid" files={{ 'App.tsx': {code: app, hidden: true}, 'Droppable.tsx': {code, active: true}, 'styles.css': {code: draggableStyles + '\n\n' + droppableStyles + '\n\n' + containerStyles, hidden: true}, }} height={350} previewHeight={400} hero />
The useDroppable hook requires a unique id and returns a ref callback.
import {useDroppable} from '@dnd-kit/solid';
function Droppable(props) {
const {ref, isDropTarget} = useDroppable({id: props.id});
return (
<div ref={ref} data-highlight={isDropTarget()}>
{props.children}
</div>
);
}
export const app = ` import {createSignal} from 'solid-js'; import {DragDropProvider, useDraggable} from '@dnd-kit/solid'; import {Droppable} from './Droppable'; import './styles.css';
function Draggable() { const {ref} = useDraggable({id: 'draggable'}); return <button ref={ref} class="btn">draggable</button>; }
export default function App() { const [parent, setParent] = createSignal(undefined); return ( <DragDropProvider onDragEnd={(event) => { if (event.canceled) return; setParent(event.operation.target?.id); }} > <div class="container"> {parent() == null ? <Draggable /> : null} <Droppable> {parent() === 'droppable' ? <Draggable /> : null} </Droppable> </div> </DragDropProvider> ); } `.trim();
export const code = ` import {useDroppable} from '@dnd-kit/solid';
export function Droppable(props) { const {ref, isDropTarget} = useDroppable({id: 'droppable'});
return ( <div ref={ref} class={isDropTarget() ? "droppable active" : "droppable"}> {props.children} </div> ); } `.trim();
export const containerStyles = .container { display: grid; grid-template-columns: 2fr 1fr; grid-gap: 20px; align-items: center; max-width: 700px; margin: 0 auto; }.trim();