useDndObserver
A Solid hook that creates and returns a DndObserver instance for observing collisions between Draggable and Droppable instances.
The observer is automatically destroyed when the component is disposed.
Usage
/** @jsxImportSource solid-js */
import { render } from 'solid-js/web';
import { DndObserverContext, useDndObserver } from 'dragdoll-solid';
function App() {
const observer = useDndObserver({
onEnter: ({ draggable, droppable }) => {
console.log('Draggable entered droppable');
},
onLeave: ({ draggable, droppable }) => {
console.log('Draggable left droppable');
},
});
return (
<DndObserverContext.Provider value={observer}>
{/* Your draggables and droppables */}
</DndObserverContext.Provider>
);
}
render(() => <App />, document.getElementById('root')!);Signature
function useDndObserver<T extends CollisionData = CollisionData>(
settings?: MaybeAccessor<UseDndObserverSettings<T> | undefined>,
): Accessor<DndObserver<T> | null>;Parameters
settings
type settings = MaybeAccessor<UseDndObserverSettings<T> | undefined>;Configuration settings for the DndObserver.
Settings can be a plain object or an accessor function returning the settings object. When provided as an accessor, the hook reacts to changes automatically via Solid's fine-grained tracking.
collisionDetector
type collisionDetector = CollisionDetector<T>;A factory function that receives the DndObserver instance and returns a CollisionDetector. By default, if undefined, a default CollisionDetector will be created.
IMPORTANT
The DndObserver will be recreated whenever this setting changes. If you provide the setting via an accessor, make sure the returned function reference is stable when no actual change is intended.
See the CollisionDetector docs for subclassing examples.
- Optional.
- Default is
undefined.
onStart
type onStart = (event: DndObserverStartEvent<T>) => void;A callback function that is called when a draggable starts being dragged.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onMove
type onMove = (event: DndObserverMoveEvent<T>) => void;A callback function that is called when a draggable is moved.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onEnter
type onEnter = (event: DndObserverEnterEvent<T>) => void;A callback function that is called when a draggable enters a droppable.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onLeave
type onLeave = (event: DndObserverLeaveEvent<T>) => void;A callback function that is called when a draggable leaves a droppable.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onCollide
type onCollide = (event: DndObserverCollideEvent<T>) => void;A callback function that is called when a draggable collides with a droppable.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onEnd
type onEnd = (event: DndObserverEndEvent<T>) => void;A callback function that is called when a draggable stops being dragged.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onAddDraggables
type onAddDraggables = (event: DndObserverAddDraggablesEvent<T>) => void;A callback function that is called when a draggable is added to the observer.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onRemoveDraggables
type onRemoveDraggables = (event: DndObserverRemoveDraggablesEvent<T>) => void;A callback function that is called when a draggable is removed from the observer.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onAddDroppables
type onAddDroppables = (event: DndObserverAddDroppablesEvent<T>) => void;A callback function that is called when a droppable is added to the observer.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onRemoveDroppables
type onRemoveDroppables = (event: DndObserverRemoveDroppablesEvent<T>) => void;A callback function that is called when a droppable is removed from the observer.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
onDestroy
type onDestroy = (event: DndObserverDestroyEvent<T>) => void;A callback function that is called when the observer is destroyed.
IMPORTANT
Callbacks do not need special handling. The hook stores a reference to the latest callback internally.
- Optional.
- Default is
undefined.
Return Value
type returnValue = Accessor<DndObserver<T> | null>;Returns an accessor to the DndObserver instance, or null if not yet initialized. Access with observer().
Types
UseDndObserverSettings
// Import
import type { UseDndObserverSettings } from 'dragdoll-solid';
// Interface
interface UseDndObserverSettings<T extends CollisionData = CollisionData> {
collisionDetector?: DndObserverOptions<T>['collisionDetector'];
onStart?: DndObserverEventCallbacks<T>['start'];
onMove?: DndObserverEventCallbacks<T>['move'];
onEnter?: DndObserverEventCallbacks<T>['enter'];
onLeave?: DndObserverEventCallbacks<T>['leave'];
onCollide?: DndObserverEventCallbacks<T>['collide'];
onEnd?: DndObserverEventCallbacks<T>['end'];
onAddDraggables?: DndObserverEventCallbacks<T>['addDraggables'];
onRemoveDraggables?: DndObserverEventCallbacks<T>['removeDraggables'];
onAddDroppables?: DndObserverEventCallbacks<T>['addDroppables'];
onRemoveDroppables?: DndObserverEventCallbacks<T>['removeDroppables'];
onDestroy?: DndObserverEventCallbacks<T>['destroy'];
}Notes
- The observer is destroyed automatically when the component is disposed.
- Creating a new
collisionDetectorreference triggers recreation of the whole observer. - Callbacks can be supplied directly or via accessor functions for reactive composition.
- SSR-safe: returns
() => nullon the server.