Skip to content

Commit bb2829d

Browse files
authored
docs(material/table): use CSS transition for animation (#30454)
Uses a CSS transition for the expandable rows example, instead of relying on the animations module. This will allow us to remove the animations module from the docs site.
1 parent d141f83 commit bb2829d

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

src/components-examples/material/table/table-expandable-rows/table-expandable-rows-example.css

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ tr.example-detail-row {
66
height: 0;
77
}
88

9+
tr.example-element-row {
10+
cursor: pointer;
11+
}
12+
913
tr.example-element-row:not(.example-expanded-row):hover {
1014
background: whitesmoke;
1115
}
@@ -18,9 +22,21 @@ tr.example-element-row:not(.example-expanded-row):active {
1822
border-bottom-width: 0;
1923
}
2024

21-
.example-element-detail {
25+
.example-element-detail-wrapper {
2226
overflow: hidden;
27+
display: grid;
28+
grid-template-rows: 0fr;
29+
grid-template-columns: 100%;
30+
transition: grid-template-rows 225ms cubic-bezier(0.4, 0, 0.2, 1);
31+
}
32+
33+
.example-element-detail-wrapper-expanded {
34+
grid-template-rows: 1fr;
35+
}
36+
37+
.example-element-detail {
2338
display: flex;
39+
min-height: 0;
2440
}
2541

2642
.example-element-diagram {
@@ -45,3 +61,11 @@ tr.example-element-row:not(.example-expanded-row):active {
4561
.example-element-description-attribution {
4662
opacity: 0.5;
4763
}
64+
65+
.example-toggle-button {
66+
transition: transform 225ms cubic-bezier(0.4, 0, 0.2, 1);
67+
}
68+
69+
.example-toggle-button-expanded {
70+
transform: rotate(180deg);
71+
}

src/components-examples/material/table/table-expandable-rows/table-expandable-rows-example.html

+24-21
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,40 @@
33
class="mat-elevation-z8">
44
@for (column of columnsToDisplay; track column) {
55
<ng-container matColumnDef="{{column}}">
6-
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
7-
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
6+
<th mat-header-cell *matHeaderCellDef>{{column}}</th>
7+
<td mat-cell *matCellDef="let element">{{element[column]}}</td>
88
</ng-container>
99
}
1010
<ng-container matColumnDef="expand">
1111
<th mat-header-cell *matHeaderCellDef aria-label="row actions">&nbsp;</th>
1212
<td mat-cell *matCellDef="let element">
13-
<button mat-icon-button aria-label="expand row" (click)="(expandedElement = expandedElement === element ? null : element); $event.stopPropagation()">
14-
@if (expandedElement === element) {
15-
<mat-icon>keyboard_arrow_up</mat-icon>
16-
} @else {
17-
<mat-icon>keyboard_arrow_down</mat-icon>
18-
}
13+
<button
14+
mat-icon-button
15+
aria-label="expand row"
16+
(click)="toggle(element); $event.stopPropagation()"
17+
class="example-toggle-button"
18+
[class.example-toggle-button-expanded]="isExpanded(element)">
19+
<mat-icon>keyboard_arrow_down</mat-icon>
1920
</button>
2021
</td>
2122
</ng-container>
2223

2324
<!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
2425
<ng-container matColumnDef="expandedDetail">
2526
<td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplayWithExpand.length">
26-
<div class="example-element-detail"
27-
[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
28-
<div class="example-element-diagram">
29-
<div class="example-element-position"> {{element.position}} </div>
30-
<div class="example-element-symbol"> {{element.symbol}} </div>
31-
<div class="example-element-name"> {{element.name}} </div>
32-
<div class="example-element-weight"> {{element.weight}} </div>
33-
</div>
34-
<div class="example-element-description">
35-
{{element.description}}
36-
<span class="example-element-description-attribution"> -- Wikipedia </span>
27+
<div class="example-element-detail-wrapper"
28+
[class.example-element-detail-wrapper-expanded]="isExpanded(element)">
29+
<div class="example-element-detail">
30+
<div class="example-element-diagram">
31+
<div class="example-element-position">{{element.position}}</div>
32+
<div class="example-element-symbol">{{element.symbol}}</div>
33+
<div class="example-element-name">{{element.name}}</div>
34+
<div class="example-element-weight">{{element.weight}}</div>
35+
</div>
36+
<div class="example-element-description">
37+
{{element.description}}
38+
<span class="example-element-description-attribution"> -- Wikipedia </span>
39+
</div>
3740
</div>
3841
</div>
3942
</td>
@@ -42,8 +45,8 @@
4245
<tr mat-header-row *matHeaderRowDef="columnsToDisplayWithExpand"></tr>
4346
<tr mat-row *matRowDef="let element; columns: columnsToDisplayWithExpand;"
4447
class="example-element-row"
45-
[class.example-expanded-row]="expandedElement === element"
46-
(click)="expandedElement = expandedElement === element ? null : element">
48+
[class.example-expanded-row]="isExpanded(element)"
49+
(click)="toggle(element)">
4750
</tr>
4851
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
4952
</table>

src/components-examples/material/table/table-expandable-rows/table-expandable-rows-example.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component} from '@angular/core';
2-
import {animate, state, style, transition, trigger} from '@angular/animations';
32
import {MatIconModule} from '@angular/material/icon';
43
import {MatButtonModule} from '@angular/material/button';
54
import {MatTableModule} from '@angular/material/table';
@@ -11,20 +10,23 @@ import {MatTableModule} from '@angular/material/table';
1110
selector: 'table-expandable-rows-example',
1211
styleUrl: 'table-expandable-rows-example.css',
1312
templateUrl: 'table-expandable-rows-example.html',
14-
animations: [
15-
trigger('detailExpand', [
16-
state('collapsed,void', style({height: '0px', minHeight: '0'})),
17-
state('expanded', style({height: '*'})),
18-
transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
19-
]),
20-
],
2113
imports: [MatTableModule, MatButtonModule, MatIconModule],
2214
})
2315
export class TableExpandableRowsExample {
2416
dataSource = ELEMENT_DATA;
2517
columnsToDisplay = ['name', 'weight', 'symbol', 'position'];
2618
columnsToDisplayWithExpand = [...this.columnsToDisplay, 'expand'];
2719
expandedElement: PeriodicElement | null;
20+
21+
/** Checks whether an element is expanded. */
22+
isExpanded(element: PeriodicElement) {
23+
return this.expandedElement === element;
24+
}
25+
26+
/** Toggles the expanded state of an element. */
27+
toggle(element: PeriodicElement) {
28+
this.expandedElement = this.isExpanded(element) ? null : element;
29+
}
2830
}
2931

3032
export interface PeriodicElement {

0 commit comments

Comments
 (0)