Overview
TheuseDeleteLayers hook is a specialized Liveblocks mutation that handles the deletion of currently selected layers from the collaborative canvas. It removes layers from both the layers map and the layer IDs list, then clears the selection state.
Usage
Parameters
This hook does not accept any parameters.Return Value
A mutation function that deletes all currently selected layers from the canvas storage and clears the selection. This operation is added to the undo/redo history.
How It Works
- Gets Current Selection: Reads the current user’s selected layer IDs from their presence state
- Removes Layers: Iterates through selected layer IDs and:
- Deletes each layer from the
layersLiveMap - Finds and removes the layer ID from the
layerIdsLiveList
- Deletes each layer from the
- Clears Selection: Sets the user’s selection to an empty array
- History Tracking: Adds the operation to history for undo/redo support
Real-World Examples
Delete Button in Selection Tools
Keyboard Shortcut Handler
Type Definitions
The hook relies on Liveblocks storage types:Dependencies
The mutation re-runs when theselection array changes, ensuring it always operates on the current selection.
The hook uses
useSelf to read the current user’s selection and useMutation from Liveblocks to create a collaborative mutation that syncs across all connected clients.Best Practices
- Bind this hook to common keyboard shortcuts (Delete, Backspace) for better UX
- Only show delete UI when there are selected layers (
selection.length > 0) - Combine with confirmation dialogs for important or bulk deletions
- The operation includes
{ addToHistory: true }to support undo/redo functionality