Skip to content

Commit 85409f5

Browse files
committed
docs(cdk/drag-drop): add tabs example (#29517)
Adds an example that shows how to add drag&drop support to a `mat-tab-group`. Fixes #13572. (cherry picked from commit 67f9a29)
1 parent 2c76917 commit 85409f5

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,13 @@ item will be moved into the new index, otherwise it will keep its current positi
251251

252252
<!-- example(cdk-drag-drop-sort-predicate) -->
253253

254-
### Reordering table rows
255-
Angular Material provides seamless integration of drag-and-drop functionality into tables,
256-
by adding the `cdkDropList` directive to your mat-table and handling the `(cdkDropListDropped)`
257-
event, you can enable drag-and-drop interactions within your table. This allows users to reorder
258-
rows or perform other custom actions with ease.
254+
### Integrations with Angular Material
255+
The CDK's drag&drop functionality can be integrated with different parts of Angular Material.
259256

257+
#### Sortable table
258+
This example shows how you can set up a table which supports re-ordering of tabs.
260259
<!-- example(cdk-drag-drop-table) -->
260+
261+
#### Sortable tabs
262+
Example of how to add sorting support to a `mat-tab-group`.
263+
<!-- example(cdk-drag-drop-tabs) -->

src/components-examples/cdk/drag-drop/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ng_module(
1515
"//src/cdk/portal",
1616
"//src/material/icon",
1717
"//src/material/table",
18+
"//src/material/tabs",
1819
],
1920
)
2021

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.example-drag-tabs.cdk-drop-list-dragging {
2+
pointer-events: none;
3+
}
4+
5+
.example-drag-tabs-preview.cdk-drag-animating {
6+
transition: all 250ms cubic-bezier(0, 0, 0.2, 1);
7+
}
8+
9+
.mat-mdc-tab.example-drag-tabs-preview {
10+
outline: dashed 1px #ccc;
11+
outline-offset: 4px;
12+
}
13+
14+
.example-drag-tabs .cdk-drag-placeholder {
15+
opacity: 0.5;
16+
}
17+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<mat-tab-group
2+
cdkDropList
3+
cdkDropListOrientation="horizontal"
4+
(cdkDropListDropped)="drop($event)"
5+
cdkDropListElementContainer=".mat-mdc-tab-labels"
6+
class="example-drag-tabs"
7+
[(selectedIndex)]="selectedTabIndex"
8+
animationDuration="0">
9+
@for (tab of tabs; track $index) {
10+
<mat-tab>
11+
<ng-template mat-tab-label>
12+
<span
13+
cdkDrag
14+
cdkDragPreviewClass="example-drag-tabs-preview"
15+
cdkDragRootElement=".mat-mdc-tab">{{tab}}</span>
16+
</ng-template>
17+
18+
<h3>Content for {{tab}}</h3>
19+
20+
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quidem perspiciatis in delectus
21+
reprehenderit, molestias ullam nostrum odit, modi consequatur harum beatae? Sapiente
22+
voluptatibus illo natus assumenda hic quasi dolor et laborum veniam! Molestiae architecto
23+
nesciunt est quo nisi? Nostrum repellendus quibusdam laudantium? Optio architecto explicabo
24+
labore sapiente cum alias nobis!
25+
</mat-tab>
26+
}
27+
</mat-tab-group>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {Component, ViewEncapsulation} from '@angular/core';
2+
import {CdkDrag, CdkDragDrop, CdkDropList, moveItemInArray} from '@angular/cdk/drag-drop';
3+
import {MatTabsModule} from '@angular/material/tabs';
4+
5+
/**
6+
* @title Drag&Drop tabs
7+
*/
8+
@Component({
9+
selector: 'cdk-drag-drop-tabs-example',
10+
templateUrl: 'cdk-drag-drop-tabs-example.html',
11+
styleUrl: 'cdk-drag-drop-tabs-example.css',
12+
standalone: true,
13+
imports: [CdkDrag, CdkDropList, MatTabsModule],
14+
encapsulation: ViewEncapsulation.None,
15+
})
16+
export class CdkDragDropTabsExample {
17+
protected tabs = ['One', 'Two', 'Three', 'Four', 'Five'];
18+
protected selectedTabIndex = 0;
19+
20+
drop(event: CdkDragDrop<string[]>) {
21+
const prevActive = this.tabs[this.selectedTabIndex];
22+
moveItemInArray(this.tabs, event.previousIndex, event.currentIndex);
23+
this.selectedTabIndex = this.tabs.indexOf(prevActive);
24+
}
25+
}

src/components-examples/cdk/drag-drop/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export {CdkDragDropSortingExample} from './cdk-drag-drop-sorting/cdk-drag-drop-s
1717
export {CdkDragDropSortPredicateExample} from './cdk-drag-drop-sort-predicate/cdk-drag-drop-sort-predicate-example';
1818
export {CdkDragDropTableExample} from './cdk-drag-drop-table/cdk-drag-drop-table-example';
1919
export {CdkDragDropMixedSortingExample} from './cdk-drag-drop-mixed-sorting/cdk-drag-drop-mixed-sorting-example';
20+
export {CdkDragDropTabsExample} from './cdk-drag-drop-tabs/cdk-drag-drop-tabs-example';

0 commit comments

Comments
 (0)