0

I have the responsive mat-sidenav which generated with Angular Material 6. Open/close button for navigation panel work excellent, but then navigation panel is open pannel obscure part of the content.

How to make the content part like a rubber:

<!--CONTENT-->
<main>
    <router-outlet></router-outlet>
</main>

I mean when the navigation bar is closed then the content width is 100% of the page. But when navigation bar is opened then content compressed to 85% (My navigation bar have width: 15%)

This is template:

<mat-sidenav-container class="sidenav-container">
    <mat-sidenav
        mode="side"
        #drawer
        class="sidenav"
        [(opened)]="isOpened"
        fixedInViewport="true"
        [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
        [mode]="(isHandset$ | async) ? 'over' : 'side'"
        [opened]="(isHandset$ | async)"
    >
        <mat-nav-list>
            <span
                *ngFor="let link of links"
                routerLinkActive="active"
            >
                <a mat-list-item [routerLink]="[link.url]">
                    {{link.name}}
                </a>
            </span>
            <span style="position: absolute; bottom: 1px; width: 100%">
                <a mat-list-item class="waves-effect waves-orange" (click)="logout($event)">
                    Выйти
                </a>
            </span>
        </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content>
        <mat-toolbar color="primary">

            <button mat-icon-button (click)="drawer.toggle()">
                <mat-icon>menu</mat-icon>
            </button>
            <span style="margin-left: 45%">Oasis Bot Manager</span>
        </mat-toolbar>
    </mat-sidenav-content>
</mat-sidenav-container>

<!--CONTENT-->
<main [ngStyle]="{'width': isOpened ? '85%' : '100%'}">
    <router-outlet></router-outlet>
</main>

TypeScript side:

export class SiteLayoutComponent {
    links = [
        {url: '/vds_list', name: 'Статистика'},
        {url: '/profile', name: 'Личный кабинет'}
    ];

    isOpened;

    isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
        .pipe(
            map(result => result.matches)
        );

    constructor(private breakpointObserver: BreakpointObserver,
                private auth: AuthService,
                private router: Router) {
    }

    logout(event: Event) {
        event.preventDefault();
        this.auth.logout();
        this.router.navigate(['/login']);
    }
}

I'm trying bind width of content through [ngStyle] to [(opened)]="isOpened" state of mat-sidenav.

But not work. How to resolve this situation?

Pavel
  • 2,005
  • 5
  • 36
  • 68

1 Answers1

1

Your content area needs to be inside mat-sidenav-container below the toolbar:

<mat-sidenav-container class="sidenav-container">
    <mat-sidenav
        mode="side"
        #drawer
        class="sidenav"
        [(opened)]="isOpened"
        fixedInViewport="true"
        [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
        [mode]="(isHandset$ | async) ? 'over' : 'side'"
        [opened]="(isHandset$ | async)"
    >
        <mat-nav-list>
            <span
                *ngFor="let link of links"
                routerLinkActive="active"
            >
                <a mat-list-item [routerLink]="[link.url]">
                    {{link.name}}
                </a>
            </span>
            <span style="position: absolute; bottom: 1px; width: 100%">
                <a mat-list-item class="waves-effect waves-orange" (click)="logout($event)">
                    Выйти
                </a>
            </span>
        </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content style="display:flex; flex-direction:column;">
        <mat-toolbar color="primary">

            <button mat-icon-button (click)="drawer.toggle()">
                <mat-icon>menu</mat-icon>
            </button>
            <span style="margin-left: 45%">Oasis Bot Manager</span>
        </mat-toolbar>
        <!--CONTENT-->
        <main>
            <router-outlet></router-outlet>
        </main>
    </mat-sidenav-content>
</mat-sidenav-container>
G. Tranter
  • 16,766
  • 1
  • 48
  • 68