Skip to content

Commit 4147cb6

Browse files
crisbetoannieyw
authored andcommitted
build: fix not being able to test MDC density in RTL (#21398)
Our dev app is set up so that the density styles are set on the `body` while the `dir` is set only around the content. The problem is that MDC's mixins output density styles for RTL in the format `[dir='rtl'] .density .foo` which means that the directionality has to be a level above the density class or we won't get an accurate representation of how the components look. This threw me off for a while earlier today, because I noticed that the MDC slide toggle didn't behave correctly in RTL. These changes fix the issue by moving the `dir` a level up and applying the density class in a couple of places. (cherry picked from commit 69b0a72)
1 parent f9a166d commit 4147cb6

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/dev-app/dev-app/dev-app-layout.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@
2222
</mat-nav-list>
2323
<button mat-button tabindex="-1" (click)="start.close()">CLOSE</button>
2424
</mat-sidenav>
25-
<main>
26-
<mat-toolbar color="primary">
25+
<!--
26+
Note that the setup with the directionality and density is a little convoluted, but it's
27+
organized this way so that we can test MDC's density styles in both LTR and RTL. Their mixins
28+
output styles in the form of `[dir='rtl'] .density-min .foo` which means that the `dir` has to
29+
be one level above the density class in the DOM. At the same time, we want the density to apply
30+
to the toolbar while always keeping it in LTR at the same time.
31+
-->
32+
<main [attr.dir]="dir.value" [ngClass]="getDensityClass()">
33+
<!-- The toolbar should always be in the LTR direction -->
34+
<mat-toolbar color="primary" dir="ltr">
2735
<button mat-icon-button (click)="start.open('mouse')">
2836
<mat-icon>menu</mat-icon>
2937
</button>
@@ -54,7 +62,7 @@ <h1>Angular Material Demos</h1>
5462
</div>
5563
</mat-toolbar>
5664

57-
<div [attr.dir]="dir.value" class="demo-content mat-app-background">
65+
<div [ngClass]="getDensityClass()" class="demo-content mat-app-background">
5866
<ng-content></ng-content>
5967
</div>
6068
</main>

src/dev-app/dev-app/dev-app-layout.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ export class DevAppLayout {
114114
@Inject(Directionality) public dir: DevAppDirectionality, cdr: ChangeDetectorRef,
115115
@Inject(DOCUMENT) private _document: Document) {
116116
dir.change.subscribe(() => cdr.markForCheck());
117-
this.updateDensityClasses();
118117
try {
119118
const isDark = localStorage.getItem(isDarkThemeKey);
120119
if (isDark != null) {
@@ -189,21 +188,13 @@ export class DevAppLayout {
189188
/** Selects the next possible density scale. */
190189
selectNextDensity() {
191190
this.currentDensityIndex = this.getNextDensityIndex();
192-
this.updateDensityClasses();
193191
}
194192

195193
/**
196194
* Updates the density classes on the host element. Applies a unique class for
197195
* a given density scale, so that the density styles are conditionally applied.
198196
*/
199-
updateDensityClasses() {
200-
for (let i = 0; i < this.densityScales.length; i++) {
201-
const className = `demo-density-${this.densityScales[i]}`;
202-
if (i === this.currentDensityIndex) {
203-
this._document.body.classList.add(className);
204-
} else {
205-
this._document.body.classList.remove(className);
206-
}
207-
}
197+
getDensityClass() {
198+
return `demo-density-${this.densityScales[this.currentDensityIndex]}`;
208199
}
209200
}

0 commit comments

Comments
 (0)