export class AppComponent implements OnInit, AfterContentInit {
@ViewChild('button') button: ElementRef;
constructor(private interactivityChecker: InteractivityChecker) {}
ngOnInit() {
console.log(this.interactivityChecker.isDisabled(this.button.nativeElement));
console.log(this.interactivityChecker.isFocusable(this.button.nativeElement));
console.log(this.interactivityChecker.isTabbable(this.button.nativeElement));
console.log(this.interactivityChecker.isVisible(this.button.nativeElement));
}
}
打開VoiceOver看看,目前確定朗讀程式沒反應@@
say() {
this.liveAnnouncer.announce('hello~~', 'polite');
}
startMonitor() {
this.monitorLog += `<<< start monitoring element\n`;
this.focusMonitor.monitor(this.container.nativeElement, this.renderer2, false).subscribe(mode => {
this.monitorLog += `element focused by ${mode}\n`;
});
}
stopMonitor() {
this.focusMonitor.stopMonitoring(this.container.nativeElement);
this.monitorLog += `>>> stop monitoring element\n`;
}
export class AppComponent implements OnInit, AfterViewInit, AfterViewChecked {
// 找出所有的MatButton
@ViewChildren(MatButton) buttons: QueryList<MatButton>;
listKeyManager: FocusKeyManager<MatButton>;
@HostListener('keydown', ['$event'])
keydown($event: KeyboardEvent) {
// 監聽鍵盤事件並依照AS移動按鈕focus狀態
if ($event.code === 'KeyA') {
this.listKeyManager.setPreviousItemActive();
} else if ($event.code === 'KeyS') {
this.listKeyManager.setNextItemActive();
}
}
ngAfterViewInit() {
// 建立FocusKeyManager, withWrap的話, 會形成一個focus的範圍區段
this.listKeyManager = new FocusKeyManager(this.buttons).withWrap();
// active到最後一個
this.listKeyManager.setLastItemActive();
}
}
<button (click)="display = !display" #button>TEST</button>
<div cdkTrapFocus cdkTrapFocusAutoCapture="true" *ngIf="display">
<input type="text" cdkFocusRegionStart>
<input type="text" >
<input type="text" cdkFocusInitial>
<input type="text" >
<input type="text" cdkFocusRegionEnd>
</div>
- 可以減少使用 FocusMonitor
- focus時會加上class: cdk-focused
- 依照focuse不同的類型會加上不同class
- cdk-mouse-focused
- cdk-keyboard-focused
- cdk-program-focused
- cdk-touch-focused
- 注意需要tabindex=?
<div class="demo-focusable" tabindex="0" cdkMonitorElementFocus>
<p>div with element focus monitored</p>
<button>1</button><button>2</button>
</div>
<div class="demo-focusable" tabindex="0" cdkMonitorSubtreeFocus>
<p>div with subtree focus monitored</p>
<button>1</button><button>2</button>
</div>