BACK
Use casesDrag handler — mouseDown → mouseMove → mouseUp
Use cases · 10

Drag handler — mouseDown → mouseMove → mouseUp

Pattern

DnD, sliders, panel resizing, drawing — they're all described by one formula: "after mousedown, listen to mousemove until mouseup happens." In imperative code this is always a tangle of flags and handlers. In RxJS it's a single pipeline.

The operators and their roles

  • switchMap — for each mousedown we create a new sub-stream of moves.
  • takeUntil(mouseUp$) — that sub-stream lives exactly until the button is released.
  • map — compute the delta from the start point.

What we achieve

Events between gestures are ignored automatically. No manual removeEventListener — RxJS cleans up the subscription itself.

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...