skip to content

In the Angular CDK, what do cdkDragHandle, cdkDragPreview and cdkDragPlaceholder each customise when a playlist row is dragged?

level: middleimportance: should knowfreq 33%

answer

  1. where the drag may start
  2. what follows the pointer
  3. what marks the landing slot
  4. ng-template replaces a clone
  5. inserted into the body by default

basics

~20 s

In the Angular CDK, cdkDragHandle limits where a drag may start, a cdkDragPreview template replaces the floating copy that follows the pointer, and a cdkDragPlaceholder template replaces the clone that marks where the row will land.

solid answer

~50 s

All three are helper directives inside a `cdkDrag` element. Once a row contains a `cdkDragHandle`, only the handle starts a drag, so a volume slider or the title text in the row behaves normally; a row may have several handles, and `cdkDragHandleDisabled` turns one off. While a row in a sortable list is dragged, the CDK hides the original element and shows two stand-ins: the **preview**, which follows the pointer and defaults to a clone of the row, and the **placeholder**, which sits in the list at the landing slot and also defaults to a clone. An `ng-template` with `cdkDragPreview` or `cdkDragPlaceholder` replaces either. A custom preview does not copy the row's size unless you set `matchSize`, and by default the preview is inserted into the page body, so it loses inherited styles unless `cdkDragPreviewContainer` says otherwise.

code

html · 16 lines
html
<ol cdkDropList cdkDropListLockAxis="y" class="playlist" (cdkDropListDropped)="drop($event)">
  @for (track of tracks(); track track.id) {
    <li cdkDrag cdkDragBoundary=".playlist" [cdkDragStartDelay]="{touch: 250, mouse: 0}">
      <mat-icon cdkDragHandle>drag_indicator</mat-icon>
      <span>{{ track.title }}</span>
      <input type="range" [value]="track.volume" aria-label="Track volume" />

      <ng-template cdkDragPreview [matchSize]="true">
        <div class="track-card">{{ track.title }} · {{ track.artist }}</div>
      </ng-template>
      <ng-template cdkDragPlaceholder>
        <div class="track-gap"></div>
      </ng-template>
    </li>
  }
</ol>

go deeper

for a junior

Recall what each helper changes: the handle restricts where a drag starts, the preview follows the pointer, the placeholder marks the landing slot in the list.

for a middle

Explain the drag lifecycle: the original row is hidden, two clones stand in, and templates replace them. Know matchSize and why a body-level preview loses inherited styles.

for a senior

Show judgment on preview placement, global against parent, and on touch ergonomics: handles and a touch-only start delay so long lists still scroll on phones.

for a principal

Weigh how much drag chrome a design system should standardise, such as shared preview and placeholder styles, against letting each list tune its own handles and delays.

## Three helpers for one dragged row In the Angular CDK's `@angular/cdk/drag-drop`, the `cdkDrag` directive makes an element draggable. Three **helper directives**, placed *inside* that element, change how the drag looks and where it may begin: | Directive | Placed on | Customises | Default without it | |---|---|---|---| | `cdkDragHandle` | Any descendant of the `cdkDrag` element | Where a drag may start | The whole element starts a drag | | `cdkDragPreview` | An `ng-template` | The element that follows the pointer | A clone of the dragged element | | `cdkDragPlaceholder` | An `ng-template` | The element shown at the landing slot in the list | A clone of the dragged element | ## Drag handles A playlist row often holds more than a title: a play button, a volume slider, a menu. If the whole row starts a drag, a user trying to move the slider moves the row instead. Adding **`cdkDragHandle`** to one element in the row, such as a grip icon, means **only the handle** starts a drag. A row may contain several handles, and `cdkDragHandleDisabled` disables one of them. The CDK adds the `.cdk-drag-handle` class to the handle element. ## What happens to the row during a drag When a row in a `cdkDropList` is picked up, the CDK does three things: 1. It **hides the original element** and moves it out of the list. It is not removed, so Angular still owns it, and it is put back in its old DOM position at the end of the drag. 2. It inserts a **placeholder** in the row's slot. As the pointer moves, the placeholder moves through the list to show where the row will land. Its class is `.cdk-drag-placeholder`. 3. It creates a **preview** that follows the pointer. Its class is `.cdk-drag-preview`. Both stand-ins default to a clone of the row. The clone has its `id` attribute removed so the page never holds two elements with the same id, which also means CSS aimed at that id does not style the clone. ## Custom previews Put **`cdkDragPreview`** on an `ng-template` inside the row to show something else under the pointer, such as a compact card with the track title and artist. Two details catch people: - A custom preview is **not resized** to match the row, because the CDK makes no assumptions about its content. Set the template's **`matchSize`** input to `true` to give it the row's size. - By default the preview is inserted into the page **`<body>`** (or the closest shadow root). The **`cdkDragPreviewContainer`** input controls this: `'global'` is the default, `'parent'` inserts it next to the dragged element, and an `ElementRef` or `HTMLElement` names a container. `'global'` avoids clipping by `overflow: hidden` and stacking problems, but the preview **loses inherited styles**, so a row styled through its parent looks unstyled while dragged. `'parent'` keeps those styles, at the risk of clipping and of disturbing `:nth-child` selectors and flex layouts. `cdkDragPreviewClass` adds classes to the preview without writing a template. ## Custom placeholders Put **`cdkDragPlaceholder`** on an `ng-template` to replace the landing-slot clone, typically with an empty bar of the row's height that reads as a gap. A placeholder exists only while the item is inside a drop list; a freely dragged element with no list has none. ## Styling the stand-ins The helpers pair with CSS classes the CDK adds, so most drag polish is plain CSS: - `.cdk-drag-preview`: give the floating copy a shadow and a fixed width so it reads as lifted. - `.cdk-drag-placeholder`: fade it or draw a dashed outline so the landing slot is obvious. - `.cdk-drag-animating`: a `transition` on `transform` lets the released row settle smoothly. - `.cdk-drop-list-dragging .cdk-drag:not(.cdk-drag-placeholder)`: a `transition` here animates the other rows sliding aside. Because a global preview lives under `<body>`, rules that depend on an ancestor, such as `.playlist li`, no longer match it; write preview rules against `.cdk-drag-preview` itself or a class added through `cdkDragPreviewClass`. ## Related inputs a row often needs - **`cdkDragLockAxis`** set to `'x'` or `'y'` restricts movement to one axis; `cdkDropListLockAxis` sets it for every item in a list. - **`cdkDragBoundary`** takes a CSS selector (or an element) and keeps the dragged item inside the nearest matching ancestor. - **`cdkDragStartDelay`** waits a number of milliseconds of pointer hold before a drag starts, or takes `{touch, mouse}` to delay only touch drags so the page still scrolls under a finger. - **`cdkDragDisabled`** turns one row off, for example a track that is still loading.

  • A row looks correct in the list but appears unstyled while dragged. What is the likely cause, and what are the fixes?
    The preview is inserted into the page body by default, so styles inherited from the list or scoped to an ancestor no longer apply, and a clone loses its `id`, so id selectors miss it. Style `.cdk-drag-preview` globally, add classes with `cdkDragPreviewClass`, or set `cdkDragPreviewContainer` to `'parent'`, accepting the risk of clipping by `overflow: hidden`.
  • On phones, scrolling a long playlist keeps picking up rows. Which cdkDrag input helps, and how would you set it?
    `cdkDragStartDelay` makes the user hold the pointer down for some milliseconds before the drag starts. It accepts a number or an object with separate `touch` and `mouse` values, so `{touch: 250, mouse: 0}` keeps mouse drags instant while a quick swipe scrolls the page. A dedicated `cdkDragHandle` reduces accidental drags further.

  • A custom cdkDragPreview is automatically resized to match the dragged row.
  • cdkDragHandle must be the only interactive element inside the cdkDrag row.
  • The placeholder and the preview are the same element moving between two places.
  • By default the preview is rendered inside the drop list, so it inherits its styles.
  • cdkDragPlaceholder also shows a landing marker for a freely dragged element.