Skip to content

Commit 39c7b81

Browse files
tinayuangaojelbourn
authored andcommitted
chore: make accessibility demo page focus goes to header after clicking on links (#7188)
1 parent 13d0160 commit 39c7b81

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/demo-app/a11y/a11y.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ <h1>Accessibility Demo</h1>
22

33
<nav>
44
<a (click)="skipNavigation()" href="javascript:void(0)">Skip navigation</a>
5-
<a *ngFor="let navItem of navItems" [routerLink]="[navItem.route]">
5+
<a *ngFor="let navItem of navItems" [routerLink]="[navItem.route]" (click)="skipNavigation()">
66
{{navItem.name}}
77
</a>
88
</nav>
99

10-
<div #maincontent tabindex="0">
10+
<div #maincontent tabindex="-1">
11+
<h1 #header tabindex="-1" *ngIf="!!currentComponent">{{currentComponent}} Demo</h1>
1112
<router-outlet></router-outlet>
1213
</div>

src/demo-app/a11y/a11y.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Component, ElementRef, ViewChild, ViewEncapsulation} from '@angular/core';
2+
import {NavigationEnd, Router} from '@angular/router';
23

34
@Component({
45
moduleId: module.id,
@@ -16,7 +17,10 @@ export class AccessibilityHome {}
1617
preserveWhitespaces: false,
1718
})
1819
export class AccessibilityDemo {
20+
currentComponent: string = '';
21+
1922
@ViewChild('maincontent') mainContent: ElementRef;
23+
@ViewChild('header') sectionHeader: ElementRef;
2024

2125
navItems = [
2226
{name: 'Home', route: '.'},
@@ -44,8 +48,17 @@ export class AccessibilityDemo {
4448
{name: 'Tooltip', route: 'tooltip'},
4549
];
4650

51+
constructor(private router: Router) {
52+
router.events.subscribe(event => {
53+
let nav = this.navItems.find(navItem => {
54+
let fragments = (event as NavigationEnd).url.split('/');
55+
return fragments[fragments.length - 1] === navItem.route;
56+
});
57+
this.currentComponent = nav ? nav.name : '';
58+
});
59+
}
60+
4761
skipNavigation() {
48-
this.mainContent.nativeElement.scrollIntoView();
49-
this.mainContent.nativeElement.focus();
62+
(this.currentComponent ? this.sectionHeader : this.mainContent).nativeElement.focus();
5063
}
5164
}

0 commit comments

Comments
 (0)