Skip to content

Commit ecdca2c

Browse files
committed
chore: Make accessibility demo page focus goes to header after clicking on links
1 parent dcf3b27 commit ecdca2c

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,
@@ -15,7 +16,10 @@ export class AccessibilityHome {}
1516
encapsulation: ViewEncapsulation.None,
1617
})
1718
export class AccessibilityDemo {
19+
currentComponent: string = '';
20+
1821
@ViewChild('maincontent') mainContent: ElementRef;
22+
@ViewChild('header') sectionHeader: ElementRef;
1923

2024
navItems = [
2125
{name: 'Home', route: '.'},
@@ -43,8 +47,17 @@ export class AccessibilityDemo {
4347
{name: 'Tooltip', route: 'tooltip'},
4448
];
4549

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

0 commit comments

Comments
 (0)