Custom Drag Setup Example
Most apps wrap the playlist in ClipInteractionProvider and are done. This page does the same job by hand, with its own DragDropProvider, which is what you need when clips share the drag context with something else of yours.
Loading...
About This Example
- Drag a clip header to move the clip; it stops at its neighbours
- Drag a clip edge to trim it
- Untick the first box: the trim handles go away and headers no longer drag
- Tick the second box: trim handles widen from 8px to 24px, and a touch has to hold for 250ms before it drags
How it is wired
The drag behaviour comes from three pieces of the library placed in your own provider. The clips are told what they may do by ClipInteractionOptions; Waveform takes no prop for it.
const sensors = useDragSensors({ touchOptimized });
const arrangement = useArrangement();
const { samplesPerPixel } = useZoom();
const { sampleRate } = useAudioSettings();
const { onDragStart, onDragMove, onDragEnd } = useClipDragHandlers();
// Memoized: a list built in the JSX is a new `modifiers` prop on every render.
const modifiers = useMemo(
() => [
RestrictToHorizontalAxis,
// The arrangement's tracks: a move stops at a clip still loading
ClipCollisionModifier.configure({
tracks: () => arrangement.getTracks(),
samplesPerPixel,
sampleRate,
}),
],
[arrangement, samplesPerPixel, sampleRate]
);
<DragDropProvider
sensors={sensors}
onDragStart={onDragStart}
onDragMove={onDragMove}
onDragEnd={onDragEnd}
modifiers={modifiers}
>
<ClipInteractionOptions enabled={interactive} touchOptimized={touchOptimized}>
<WaveformEditor />
</ClipInteractionOptions>
</DragDropProvider>This is shortened: the page also selects the track of a clip when its drag starts. The collision modifier takes a function for tracks, so the list is not rebuilt each time the tracks change. The full walkthrough, with snap-to-grid and annotation dragging, is in the Custom Drag Setup guide.
Audio Credits: "Ubiquitous" by Albert Kader — Minimal Techno stems from the Cambridge Music Technology multitrack library. Licensed under CC BY 4.0.