components icon indicating copy to clipboard operation
components copied to clipboard

[MdSidenav] - Sidenav Resizing Regression

Open dereekb opened this issue 9 years ago • 42 comments

Bug, feature request, or proposal:

Bug/Regression from 2.0.0-beta.8.

Also a feature request for an API to allow triggering recalculations for the MdSidenavContainer/MdDrawerContainer, or being able to update _styles manually and having the view update..

What is the expected behavior?

In the previous release 2.0.0-beta.8 the sidenav resized when the window was resized.

I have a responsive sidenav that uses Angular flex-layout for showing/hiding responsive content in the sidenav when the view changes from md to lg and vice-versa.

What is the current behavior?

In 2.0.0-beta.10 this is no longer the case. The changes made in #6189 made the sidenav update only when the sidenav is opening/closing.

Since flex-layout's changes occur without having to call open() or close() on the sidenav, the width recalculation doesn't occur.

example

What are the steps to reproduce?

Put dynamic/responsive content within a sidenav that is configured with "side".

What is the use-case or motivation for changing an existing behavior?

Being able to dynamically call a refresh of the MdSidenav/MdDrawer or MdSidenavContainer/MdDrawerContainer.

Even better would be an accessor to allow manually setting the margin of the sidenav to allow for pre-computing the _style variable and updating it when needed.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 4.3.5 Angular Material 2.0.0-beta.10

Is there anything else we should know?

This is similar to #6583.

For right now I am using the following hack in my navigation directive to workaround whenever the ObservableMedia from angular flex-layout changes:

// Timeout required for flex-layout directives to update the child views/the width.
setTimeout(() => {
            (this._container as any)._updateStyles();
            (this._container as any)._changeDetectorRef.markForCheck();
}, 0);

dereekb avatar Aug 30 '17 20:08 dereekb

+1. Same issue I have after upgrading to 2.0.0-beta.10

djfishe avatar Sep 04 '17 11:09 djfishe

Same issue after upgrading from 2.0.0-beta.8 to 2.0.0-beta.10

My workarround :

app.component.html

<md-sidenav class="main-sidenav" #sidenav mode="side" opened="false">

app.component.ts

export class AppComponent implements  AfterViewInit {

  @ViewChild('sidenav') 
  private sidenav: MdSidenav;

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.sidenav.open();
    }, 250);
  }
}

sbeaufort avatar Sep 06 '17 08:09 sbeaufort

that is exactly what i did @sbeaufort. It creates a small animation at the first page load but its ok

bleuscyther avatar Sep 06 '17 14:09 bleuscyther

I have the exact same issue.

AppField avatar Sep 10 '17 20:09 AppField

Here is my solution: HTML

<md-sidenav-container #container>
    <md-sidenav  mode="side" opened="{{showMenu}}" class="shadow"></md-sidenav>
    <div class="my-content"></div>
</md-sidenav-container>

TS

export class MyComponent implements OnInit, AfterViewInit {
    @ViewChild('container') private _container;
    public showMenu: boolean = false;
    ...
    ngAfterViewInit() {
      setTimeout(()=>{
        this.showMenu = true;
      },0);
      this._container._ngZone.onMicrotaskEmpty.subscribe(() => {
        this._container._updateStyles();
        this._container._changeDetectorRef.markForCheck();
      })
    };
    ...
}

ghwyf avatar Sep 11 '17 08:09 ghwyf

@ghwyf Thank you very much! This workaround also worked for me.

AppField avatar Sep 11 '17 13:09 AppField

Same issue for me upgrading to 2.0.0-beta.10. I used @ghwyf fix which works perfectly, thank you.

benfontan avatar Sep 20 '17 11:09 benfontan

Closing as duplicate of #1130

josephperrott avatar Sep 27 '17 22:09 josephperrott

Join the responses of @angularexample in #1130 and @ghwyf here, works for me. ngAfterViewInit seems too heavy for my application so I replace it with a specific call only when Subscription happens.

upstreamosGH avatar Sep 28 '17 11:09 upstreamosGH

@josephperrott I think the issues are about different things. The one you referenced is about opening and closing the sidenav automatically when viewport width changes. This one is about being able to resize the sidenav width while keeping it open.

Before the last update this worked with no problem. Right now, the main content is not being resized when the sidebar width is resized. Some people have shared workarounds for this, but I think it would be nice if Angular Material would support this use case which is quite popular in places like firebase.

Opened Closed
image image

adrm avatar Sep 28 '17 11:09 adrm

Reopening, sorry I misunderstood a bit there.

josephperrott avatar Sep 28 '17 20:09 josephperrott

It would be really helpful to give a guide on this. Are we going to do this with animations? Will there be a function on the sidenav (like sidenav.resize(width))?

I am on the material2-builds and use the new fixedInViewport, which is great. But when I animate the sidenav from 220px to 75px the main content section does not adjust (as it used to before).

Any comments as to what we will expect here?

axtho avatar Sep 29 '17 08:09 axtho

Same issue here.

SpiderPork avatar Oct 03 '17 07:10 SpiderPork

After a while, I was forced to come back to 2.0.0-beta.8 when sidenav works perfect.

upstreamosGH avatar Oct 03 '17 07:10 upstreamosGH

I would like to know if material team thinks this has to be fixed. If not, I will apply a workaround and stop waiting for a fix.

Thanks a lot!

adrm avatar Oct 03 '17 12:10 adrm

Same problem here after upgrading to 2.0.0-beta.10 . The fix from @ghwyf doesn't work for me with angular 4.4.4

jdazacon avatar Oct 06 '17 08:10 jdazacon

Here's my temporary workaround to fix it:

<md-sidenav-container #sideNavContainer>
  <md-sidenav #sideNav align="start" mode="{{navMode}}" opened="true"
    [@sideNavAnim] (@sideNavAnim.done)="onSideNavAnimDone($event)">
    <!-- sidenav content -->
  </md-sidenav>
  <!-- primary content -->
  <router-outlet *ngIf="sideNavAnimFinished"></router-outlet>
</md-sidenav-container>
export class MyComponent {
  navMode = 'side';
  sideNavAnimFinished = false;
  ...
  onSideNavAnimDone(event) {
    this.sideNavAnimFinished = true;
  }
}

halilibrahim avatar Oct 09 '17 04:10 halilibrahim

the example mentioned by @sbeaufort does not work well when the component has changeDetection: ChangeDetectionStrategy.OnPush. In order to make it work you need to manually detect changes to the UI, example:

ngAfterViewInit(): void {
        const size = this.drawerState === 'open' ? 220 : 75;
        setTimeout(() => {
            this.sidenav.open();
            this.cd.detectChanges();
        }, size);
    }

axtho avatar Oct 09 '17 08:10 axtho

@crisbeto is the resize function on a roadmap for sidenav? Would it not suffice to add a "closeTo" option which, when in side mode and when set, does not transform to visibility hidden but sets the drawer width to whatever was closeTo was set to?

axtho avatar Oct 10 '17 07:10 axtho

I was using @ghwyf workaround until beta.11 as well. What worked for me in beta.12 is the following:

export class MyComponent implements OnInit, AfterViewInit {
    @ViewChild('container') private _container;
    public showMenu: boolean = false;
    ...
    ngAfterViewInit() {
      setTimeout(()=>{
        this.showMenu = true;
      },0);
      this._container._ngZone.onMicrotaskEmpty.subscribe(() => {
        this._container._updateContentMargins();
        this._container._changeDetectorRef.markForCheck();
      })
    };
    ...
}

mpschaeuble avatar Oct 10 '17 08:10 mpschaeuble