Skip to content

Commit ce9d8ca

Browse files
crisbetoandrewseguin
authored andcommitted
fix(material/sidenav): end position sidenav tab order not matching visual order (#18101)
We project all sidenavs before the content in the DOM since we can't know ahead of time what their position will be. This is problematic when the drawer is in the end position, because the visual order of the content no longer matches the tab order. These changes fix the issue by moving the sidenav after the content manually when it's set to `end` and then moving it back if it's set to `start` again. A couple of notes: 1. We could technically do this with content projection, but it would only work when the `position` value is static (e.g. `position="end"`). I did it this way so we can cover the case where it's data bound. 2. Currently the focus trap anchors are set around the sidenav, but that's problematic when we're moving the element around since the anchors will be left at their old positions. To avoid adding extra logic for moving the anchors, I've moved the focus trap to be inside the sidenav. Here's what the DOM looks like at the moment: ```html <container> <anchor/> <sidenav>Content</sidenav> <anchor/> </container> ``` And this is what I've changed it to: ```html <container> <sidenav> <anchor/> Content <anchor/> </sidenav> </container ``` Fixes #15247. (cherry picked from commit c489f37)
1 parent c48742e commit ce9d8ca

File tree

4 files changed

+230
-8
lines changed

4 files changed

+230
-8
lines changed

src/material/sidenav/drawer.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<div class="mat-drawer-inner-container" cdkScrollable>
1+
<div class="mat-drawer-inner-container" cdkScrollable #content>
22
<ng-content></ng-content>
33
</div>

src/material/sidenav/drawer.spec.ts

+175-1
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,144 @@ describe('MatDrawer', () => {
661661
expect(scrollable).toBeTruthy();
662662
expect(scrollable.getElementRef().nativeElement).toBe(content.nativeElement);
663663
});
664+
665+
describe('DOM position', () => {
666+
it('should project start drawer before the content', () => {
667+
const fixture = TestBed.createComponent(BasicTestApp);
668+
fixture.componentInstance.position = 'start';
669+
fixture.detectChanges();
670+
671+
const allNodes = getDrawerNodesArray(fixture);
672+
const drawerIndex = allNodes.indexOf(fixture.nativeElement.querySelector('.mat-drawer'));
673+
const contentIndex = allNodes.indexOf(
674+
fixture.nativeElement.querySelector('.mat-drawer-content'),
675+
);
676+
677+
expect(drawerIndex)
678+
.withContext('Expected drawer to be inside the container')
679+
.toBeGreaterThan(-1);
680+
expect(contentIndex)
681+
.withContext('Expected content to be inside the container')
682+
.toBeGreaterThan(-1);
683+
expect(drawerIndex)
684+
.withContext('Expected drawer to be before the content')
685+
.toBeLessThan(contentIndex);
686+
});
687+
688+
it('should project end drawer after the content', () => {
689+
const fixture = TestBed.createComponent(BasicTestApp);
690+
fixture.componentInstance.position = 'end';
691+
fixture.detectChanges();
692+
693+
const allNodes = getDrawerNodesArray(fixture);
694+
const drawerIndex = allNodes.indexOf(fixture.nativeElement.querySelector('.mat-drawer'));
695+
const contentIndex = allNodes.indexOf(
696+
fixture.nativeElement.querySelector('.mat-drawer-content'),
697+
);
698+
699+
expect(drawerIndex)
700+
.withContext('Expected drawer to be inside the container')
701+
.toBeGreaterThan(-1);
702+
expect(contentIndex)
703+
.withContext('Expected content to be inside the container')
704+
.toBeGreaterThan(-1);
705+
expect(drawerIndex)
706+
.withContext('Expected drawer to be after the content')
707+
.toBeGreaterThan(contentIndex);
708+
});
709+
710+
it(
711+
'should move the drawer before/after the content when its position changes after being ' +
712+
'initialized at `start`',
713+
() => {
714+
const fixture = TestBed.createComponent(BasicTestApp);
715+
fixture.componentInstance.position = 'start';
716+
fixture.detectChanges();
717+
718+
const drawer = fixture.nativeElement.querySelector('.mat-drawer');
719+
const content = fixture.nativeElement.querySelector('.mat-drawer-content');
720+
721+
let allNodes = getDrawerNodesArray(fixture);
722+
const startDrawerIndex = allNodes.indexOf(drawer);
723+
const startContentIndex = allNodes.indexOf(content);
724+
725+
expect(startDrawerIndex)
726+
.withContext('Expected drawer to be inside the container')
727+
.toBeGreaterThan(-1);
728+
expect(startContentIndex)
729+
.withContext('Expected content to be inside the container')
730+
.toBeGreaterThan(-1);
731+
expect(startDrawerIndex)
732+
.withContext('Expected drawer to be before the content on init')
733+
.toBeLessThan(startContentIndex);
734+
735+
fixture.componentInstance.position = 'end';
736+
fixture.detectChanges();
737+
allNodes = getDrawerNodesArray(fixture);
738+
739+
expect(allNodes.indexOf(drawer))
740+
.withContext('Expected drawer to be after content when position changes to `end`')
741+
.toBeGreaterThan(allNodes.indexOf(content));
742+
743+
fixture.componentInstance.position = 'start';
744+
fixture.detectChanges();
745+
allNodes = getDrawerNodesArray(fixture);
746+
747+
expect(allNodes.indexOf(drawer))
748+
.withContext('Expected drawer to be before content when position changes back to `start`')
749+
.toBeLessThan(allNodes.indexOf(content));
750+
},
751+
);
752+
753+
it(
754+
'should move the drawer before/after the content when its position changes after being ' +
755+
'initialized at `end`',
756+
() => {
757+
const fixture = TestBed.createComponent(BasicTestApp);
758+
fixture.componentInstance.position = 'end';
759+
fixture.detectChanges();
760+
761+
const drawer = fixture.nativeElement.querySelector('.mat-drawer');
762+
const content = fixture.nativeElement.querySelector('.mat-drawer-content');
763+
764+
let allNodes = getDrawerNodesArray(fixture);
765+
const startDrawerIndex = allNodes.indexOf(drawer);
766+
const startContentIndex = allNodes.indexOf(content);
767+
768+
expect(startDrawerIndex).toBeGreaterThan(-1, 'Expected drawer to be inside the container');
769+
expect(startContentIndex).toBeGreaterThan(
770+
-1,
771+
'Expected content to be inside the container',
772+
);
773+
expect(startDrawerIndex).toBeGreaterThan(
774+
startContentIndex,
775+
'Expected drawer to be after the content on init',
776+
);
777+
778+
fixture.componentInstance.position = 'start';
779+
fixture.detectChanges();
780+
allNodes = getDrawerNodesArray(fixture);
781+
782+
expect(allNodes.indexOf(drawer)).toBeLessThan(
783+
allNodes.indexOf(content),
784+
'Expected drawer to be before content when position changes to `start`',
785+
);
786+
787+
fixture.componentInstance.position = 'end';
788+
fixture.detectChanges();
789+
allNodes = getDrawerNodesArray(fixture);
790+
791+
expect(allNodes.indexOf(drawer)).toBeGreaterThan(
792+
allNodes.indexOf(content),
793+
'Expected drawer to be after content when position changes back to `end`',
794+
);
795+
},
796+
);
797+
798+
function getDrawerNodesArray(fixture: ComponentFixture<any>): HTMLElement[] {
799+
return Array.from(fixture.nativeElement.querySelector('.mat-drawer-container').childNodes);
800+
}
801+
});
664802
});
665803

666804
describe('MatDrawerContainer', () => {
@@ -949,6 +1087,41 @@ describe('MatDrawerContainer', () => {
9491087
expect(spy).toHaveBeenCalled();
9501088
subscription.unsubscribe();
9511089
}));
1090+
1091+
it('should position the drawers before/after the content in the DOM based on their position', fakeAsync(() => {
1092+
const fixture = TestBed.createComponent(DrawerContainerTwoDrawerTestApp);
1093+
fixture.detectChanges();
1094+
1095+
const drawerDebugElements = fixture.debugElement.queryAll(By.directive(MatDrawer));
1096+
const [start, end] = drawerDebugElements.map(el => el.componentInstance);
1097+
const [startNode, endNode] = drawerDebugElements.map(el => el.nativeElement);
1098+
const contentNode = fixture.nativeElement.querySelector('.mat-drawer-content');
1099+
const allNodes: HTMLElement[] = Array.from(
1100+
fixture.nativeElement.querySelector('.mat-drawer-container').childNodes,
1101+
);
1102+
const startIndex = allNodes.indexOf(startNode);
1103+
const endIndex = allNodes.indexOf(endNode);
1104+
const contentIndex = allNodes.indexOf(contentNode);
1105+
1106+
expect(start.position).toBe('start');
1107+
expect(end.position).toBe('end');
1108+
expect(contentIndex)
1109+
.withContext('Expected content to be inside the container')
1110+
.toBeGreaterThan(-1);
1111+
expect(startIndex)
1112+
.withContext('Expected start drawer to be inside the container')
1113+
.toBeGreaterThan(-1);
1114+
expect(endIndex)
1115+
.withContext('Expected end drawer to be inside the container')
1116+
.toBeGreaterThan(-1);
1117+
1118+
expect(startIndex)
1119+
.withContext('Expected start drawer to be before content')
1120+
.toBeLessThan(contentIndex);
1121+
expect(endIndex)
1122+
.withContext('Expected end drawer to be after content')
1123+
.toBeGreaterThan(contentIndex);
1124+
}));
9521125
});
9531126

9541127
/** Test component that contains an MatDrawerContainer but no MatDrawer. */
@@ -971,7 +1144,7 @@ class DrawerContainerTwoDrawerTestApp {
9711144
@Component({
9721145
template: `
9731146
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
974-
<mat-drawer #drawer="matDrawer" position="start"
1147+
<mat-drawer #drawer="matDrawer" [position]="position"
9751148
(opened)="open()"
9761149
(openedStart)="openStart()"
9771150
(closed)="close()"
@@ -997,6 +1170,7 @@ class BasicTestApp {
9971170
closeStartCount = 0;
9981171
backdropClickedCount = 0;
9991172
hasBackdrop: boolean | null = null;
1173+
position = 'start';
10001174

10011175
@ViewChild('drawer') drawer: MatDrawer;
10021176
@ViewChild('drawerButton') drawerButton: ElementRef<HTMLButtonElement>;

src/material/sidenav/drawer.ts

+50-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {DOCUMENT} from '@angular/common';
2222
import {
2323
AfterContentChecked,
2424
AfterContentInit,
25+
AfterViewInit,
2526
ChangeDetectionStrategy,
2627
ChangeDetectorRef,
2728
Component,
@@ -153,13 +154,19 @@ export class MatDrawerContent extends CdkScrollable implements AfterContentInit
153154
changeDetection: ChangeDetectionStrategy.OnPush,
154155
encapsulation: ViewEncapsulation.None,
155156
})
156-
export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestroy {
157+
export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy {
157158
private _focusTrap: FocusTrap;
158159
private _elementFocusedBeforeDrawerWasOpened: HTMLElement | null = null;
159160

160161
/** Whether the drawer is initialized. Used for disabling the initial animation. */
161162
private _enableAnimations = false;
162163

164+
/** Whether the view of the component has been attached. */
165+
private _isAttached: boolean;
166+
167+
/** Anchor node used to restore the drawer to its initial position. */
168+
private _anchor: Comment | null;
169+
163170
/** The side that the drawer is attached to. */
164171
@Input()
165172
get position(): 'start' | 'end' {
@@ -168,7 +175,12 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
168175
set position(value: 'start' | 'end') {
169176
// Make sure we have a valid value.
170177
value = value === 'end' ? 'end' : 'start';
171-
if (value != this._position) {
178+
if (value !== this._position) {
179+
// Static inputs in Ivy are set before the element is in the DOM.
180+
if (this._isAttached) {
181+
this._updatePositionInParent(value);
182+
}
183+
172184
this._position = value;
173185
this.onPositionChanged.emit();
174186
}
@@ -293,6 +305,9 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
293305
// tslint:disable-next-line:no-output-on-prefix
294306
@Output('positionChanged') readonly onPositionChanged = new EventEmitter<void>();
295307

308+
/** Reference to the inner element that contains all the content. */
309+
@ViewChild('content') _content: ElementRef<HTMLElement>;
310+
296311
/**
297312
* An observable that emits when the drawer mode changes. This is used by the drawer container to
298313
* to know when to when the mode changes so it can adapt the margins on the content.
@@ -448,13 +463,20 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
448463

449464
/** Whether focus is currently within the drawer. */
450465
private _isFocusWithinDrawer(): boolean {
451-
const activeEl = this._doc?.activeElement;
466+
const activeEl = this._doc.activeElement;
452467
return !!activeEl && this._elementRef.nativeElement.contains(activeEl);
453468
}
454469

455-
ngAfterContentInit() {
470+
ngAfterViewInit() {
471+
this._isAttached = true;
456472
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
457473
this._updateFocusTrapState();
474+
475+
// Only update the DOM position when the sidenav is positioned at
476+
// the end since we project the sidenav before the content by default.
477+
if (this._position === 'end') {
478+
this._updatePositionInParent('end');
479+
}
458480
}
459481

460482
ngAfterContentChecked() {
@@ -472,6 +494,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
472494
this._focusTrap.destroy();
473495
}
474496

497+
this._anchor?.remove();
498+
this._anchor = null;
475499
this._animationStarted.complete();
476500
this._animationEnd.complete();
477501
this._modeChanged.complete();
@@ -567,6 +591,28 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
567591
this._focusTrap.enabled = this.opened && this.mode !== 'side';
568592
}
569593
}
594+
595+
/**
596+
* Updates the position of the drawer in the DOM. We need to move the element around ourselves
597+
* when it's in the `end` position so that it comes after the content and the visual order
598+
* matches the tab order. We also need to be able to move it back to `start` if the sidenav
599+
* started off as `end` and was changed to `start`.
600+
*/
601+
private _updatePositionInParent(newPosition: 'start' | 'end') {
602+
const element = this._elementRef.nativeElement;
603+
const parent = element.parentNode!;
604+
605+
if (newPosition === 'end') {
606+
if (!this._anchor) {
607+
this._anchor = this._doc.createComment('mat-drawer-anchor')!;
608+
parent.insertBefore(this._anchor!, element);
609+
}
610+
611+
parent.appendChild(element);
612+
} else if (this._anchor) {
613+
this._anchor.parentNode!.insertBefore(element, this._anchor);
614+
}
615+
}
570616
}
571617

572618
/**

tools/public_api_guard/material/sidenav.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { AfterContentChecked } from '@angular/core';
88
import { AfterContentInit } from '@angular/core';
9+
import { AfterViewInit } from '@angular/core';
910
import { AnimationEvent as AnimationEvent_2 } from '@angular/animations';
1011
import { AnimationTriggerMetadata } from '@angular/animations';
1112
import { BooleanInput } from '@angular/cdk/coercion';
@@ -48,7 +49,7 @@ export const MAT_DRAWER_DEFAULT_AUTOSIZE: InjectionToken<boolean>;
4849
export function MAT_DRAWER_DEFAULT_AUTOSIZE_FACTORY(): boolean;
4950

5051
// @public
51-
export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestroy {
52+
export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy {
5253
constructor(_elementRef: ElementRef<HTMLElement>, _focusTrapFactory: FocusTrapFactory, _focusMonitor: FocusMonitor, _platform: Platform, _ngZone: NgZone, _interactivityChecker: InteractivityChecker, _doc: any, _container?: MatDrawerContainer | undefined);
5354
readonly _animationEnd: Subject<AnimationEvent_2>;
5455
readonly _animationStarted: Subject<AnimationEvent_2>;
@@ -61,6 +62,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
6162
_closeViaBackdropClick(): Promise<MatDrawerToggleResult>;
6263
// (undocumented)
6364
_container?: MatDrawerContainer | undefined;
65+
_content: ElementRef<HTMLElement>;
6466
get disableClose(): boolean;
6567
set disableClose(value: BooleanInput);
6668
// (undocumented)
@@ -71,7 +73,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
7173
// (undocumented)
7274
ngAfterContentChecked(): void;
7375
// (undocumented)
74-
ngAfterContentInit(): void;
76+
ngAfterViewInit(): void;
7577
// (undocumented)
7678
ngOnDestroy(): void;
7779
readonly onPositionChanged: EventEmitter<void>;

0 commit comments

Comments
 (0)