A wrapping grid of album covers in an Angular CDK cdkDropList sorts erratically; what does cdkDropListOrientation 'mixed' change, and what does it cost?
answer
- sorting along one axis
- transforms versus moving nodes
- rows that wrap
- the animation you give up
- the array still needs updating
basics
~20 sAn Angular CDK drop list sorts along one axis with CSS transforms, so a wrapping grid confuses it. cdkDropListOrientation 'mixed' moves DOM nodes instead, which handles wrapping but loses the sorting animation; the drop handler still updates the array.
solid answer
~40 s`cdkDropListOrientation` defaults to `'vertical'`, and `'horizontal'` is the other single-axis value. In both, the CDK computes positions along one axis and slides the other items aside with CSS `transform`, which is what animates sorting. A flex-wrap grid of album covers lays items out in two dimensions, so a single-axis calculation picks wrong indexes and items jump around. Setting `cdkDropListOrientation="mixed"` switches to a strategy that reorders by **moving the DOM nodes**, so items that wrap onto new lines sort correctly. The cost is that the sorting can no longer be animated. The CDK restores the original DOM order at the drop, so the `cdkDropListDropped` handler still has to call `moveItemInArray`, exactly as in a single-axis list.
code
html · 10 lines<div class="album-grid" cdkDropList cdkDropListOrientation="mixed"
(cdkDropListDropped)="drop($event)">
@for (album of albums(); track album.id) {
<figure cdkDrag class="album">
<img [src]="album.coverUrl" [alt]="album.title" />
<figcaption>{{ album.title }}</figcaption>
</figure>
}
</div>
<!-- .album-grid { display: flex; flex-wrap: wrap; gap: 12px; } -->go deeper
Recall the three orientation values and that 'vertical' is the default; know that 'mixed' exists for lists that wrap onto new lines.
Explain why single-axis sorting fails on a wrapping grid: one-coordinate index maths and transform-based movement, against moving DOM nodes in 'mixed'.
Show the trade-off and the unchanged contract: no sorting animation, DOM order restored at drop, moveItemInArray still required; know createDragRef and createDropListRef for elements outside templates.
Weigh layout choices against interaction quality: a fixed-column design keeps the animated single-axis strategy, while a fluid wrapping grid accepts jumps for responsiveness.
## How a drop list normally sorts In the Angular CDK, a `cdkDropList` has an **orientation**, set by **`cdkDropListOrientation`**. Its type allows three values: `'vertical'` (the default), `'horizontal'` and `'mixed'`. The first two use the same **single-axis strategy**: - When a drag starts, the CDK measures every item's position along **one axis**, top to bottom or left to right. - As the pointer moves, it works out the new index from that one coordinate. - It moves the other items aside with a CSS **`transform`**, leaving the DOM order alone. Because the movement is a transform, a CSS `transition` on `.cdk-drag` makes it **animated**. That is fast and smooth for a single column or a single row. It breaks for a layout that **wraps**, such as a flex-wrap grid of album covers: covers on the second row share x-coordinates with covers on the first row, so a one-axis calculation picks the wrong index, items slide in the wrong direction and the grid looks erratic. ## What 'mixed' changes `cdkDropListOrientation="mixed"`, added in CDK 18.1, switches the list to a different **sort strategy** that reorders items by **moving their DOM nodes**. The browser's own layout then places each cover, wrapping included, so the placeholder lands where the user expects in any row. | Aspect | `'vertical'` / `'horizontal'` | `'mixed'` | |---|---|---| | How items move during the drag | CSS transforms along one axis | DOM nodes are moved | | Layouts it handles | One column or one row | Lists that wrap onto new lines | | Sorting animation | Possible with a `transform` transition | Not available | | Your array | Unchanged until your drop handler runs | Unchanged until your drop handler runs | ## Diagnosing an erratic grid 1. Check the container's CSS. `flex-wrap: wrap`, or a CSS grid with several columns, means a two-dimensional layout. 2. Check `cdkDropListOrientation`. A missing value means `'vertical'`, which compares only the pointer's y coordinate with each item's top and bottom; `'horizontal'` compares only x. 3. Drag a cover from the first row into the second. With a single-axis strategy the placeholder jumps to an unexpected index; with `'mixed'` it follows the pointer. 4. If the covers sit inside an inner wrapper rather than directly in the drop list element, point `cdkDropListElementContainer` at the element that really holds them. 5. If the grid scrolls inside its own container, mark that container with `cdkScrollable` so the list can auto-scroll during a drag. ## What it costs, and what it does not change 1. **No sorting animation.** The angular.dev guide says it directly: with `mixed`, the list can no longer animate the sorting action. Covers jump into place instead of sliding. 2. **The data contract is the same.** The mixed strategy records each node's original neighbour and restores the DOM order when the drag ends, so Angular's list rendering stays in charge. Your `cdkDropListDropped` handler still calls `moveItemInArray` with `previousIndex` and `currentIndex`; without it the covers snap back, as in any drop list. 3. **Everything else carries over:** connected lists, enter and sort predicates, handles, previews and placeholders work as in the other orientations. ## When the directives are not enough: the DragDrop service Sometimes the elements are not in your template at all, for example tiles rendered by a third-party widget. The CDK exposes the same engine without directives: **`DragRef`** for a draggable element and **`DropListRef`** for a list, configured with methods such as `withItems`, `connectedTo` and `withOrientation('mixed')`, and reporting through observables such as `dropped`. - Historically you created them through the injectable **`DragDrop`** service, with `createDrag` and `createDropList`. - By CDK 22 those two service methods are **deprecated** (the deprecation landed in a 21.0 patch release) in favour of the standalone functions **`createDragRef(injector, element, config)`** and **`createDropListRef(injector, element)`**, which tree-shake better; the source marks the methods for removal in 23. - Refs created this way are yours to clean up: call `dispose()` when the host is destroyed. ## Choosing quickly - A single column of tracks or a single row of chips: keep the single-axis default and enjoy the animation. - A grid that wraps at different widths: use `'mixed'`, and accept jumps instead of slides. - Elements outside your templates: create the refs with the functions above and set the orientation on the `DropListRef`.
- The grid is a horizontal row that never wraps. Should you still use 'mixed'?No. A single row is exactly what `cdkDropListOrientation="horizontal"` handles: the single-axis strategy sorts with transforms, so a `transition` on `.cdk-drag` animates the movement. Reserve `'mixed'` for lists that wrap onto new lines, where losing the animation is the price of correct indexes.
- The album tiles are created by a third-party widget, so you cannot add cdkDrag to them. What does the CDK offer in version 22?Create the refs in code: `createDropListRef(injector, gridElement)` for the container and `createDragRef(injector, tileElement)` for each tile, then call `withItems` with the drag refs and `withOrientation('mixed')`, and subscribe to the list's `dropped` observable to update your data. The older `DragDrop` service methods still work in 22 but are deprecated. Call `dispose()` on every ref when the host is destroyed.
- Mixed orientation keeps the transform animation, just on two axes at once.
- With mixed orientation the CDK updates the bound array, since nodes really move.
- A wrapping grid works with 'horizontal' once flex-wrap is set on the list.
- The DragDrop service's createDrag is the current recommended API in CDK 22.
- 'mixed' only affects the preview, not how the list calculates the new index.
Assumes Angular CDK 22: mixed orientation exists since 18.1, and by 22 the DragDrop service's createDrag and createDropList are deprecated (since 21.0.6) in favour of createDragRef and createDropListRef, marked for removal in 23.