Skip to content

build: fix not being able to test MDC density in RTL #21398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/dev-app/dev-app/dev-app-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@
</mat-nav-list>
<button mat-button tabindex="-1" (click)="start.close()">CLOSE</button>
</mat-sidenav>
<main>
<mat-toolbar color="primary">
<!--
Note that the setup with the directionality and density is a little convoluted, but it's
organized this way so that we can test MDC's density styles in both LTR and RTL. Their mixins
output styles in the form of `[dir='rtl'] .density-min .foo` which means that the `dir` has to
be one level above the density class in the DOM. At the same time, we want the density to apply
to the toolbar while always keeping it in LTR at the same time.
-->
<main [attr.dir]="dir.value" [ngClass]="getDensityClass()">
<!-- The toolbar should always be in the LTR direction -->
<mat-toolbar color="primary" dir="ltr">
<button mat-icon-button (click)="start.open('mouse')">
<mat-icon>menu</mat-icon>
</button>
Expand Down Expand Up @@ -54,7 +62,7 @@ <h1>Angular Material Demos</h1>
</div>
</mat-toolbar>

<div [attr.dir]="dir.value" class="demo-content mat-app-background">
<div [ngClass]="getDensityClass()" class="demo-content mat-app-background">
<ng-content></ng-content>
</div>
</main>
Expand Down
13 changes: 2 additions & 11 deletions src/dev-app/dev-app/dev-app-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export class DevAppLayout {
@Inject(Directionality) public dir: DevAppDirectionality, cdr: ChangeDetectorRef,
@Inject(DOCUMENT) private _document: Document) {
dir.change.subscribe(() => cdr.markForCheck());
this.updateDensityClasses();
try {
const isDark = localStorage.getItem(isDarkThemeKey);
if (isDark != null) {
Expand Down Expand Up @@ -189,21 +188,13 @@ export class DevAppLayout {
/** Selects the next possible density scale. */
selectNextDensity() {
this.currentDensityIndex = this.getNextDensityIndex();
this.updateDensityClasses();
}

/**
* Updates the density classes on the host element. Applies a unique class for
* a given density scale, so that the density styles are conditionally applied.
*/
updateDensityClasses() {
for (let i = 0; i < this.densityScales.length; i++) {
const className = `demo-density-${this.densityScales[i]}`;
if (i === this.currentDensityIndex) {
this._document.body.classList.add(className);
} else {
this._document.body.classList.remove(className);
}
}
getDensityClass() {
return `demo-density-${this.densityScales[this.currentDensityIndex]}`;
}
}