[英]Angular Jest can't mock HTMLElement? "not part of the custom element registry"
[英]Ionic/Angular: Can't Focus on HTMLElement with Screen Reader
我试图强制 Ionic 5 中的屏幕阅读器专注于并宣布一个元素。
我的模板元素:
<ion-title tabindex="-1" #title id="title" role="heading" aria-level="1" class="title">My Title</ion-title>
我试图通过文档和 Viewchild 访问该元素:
@ViewChild('title', { read: ElementRef }) title: ElementRef;
const title: any = document.getElementById('title');
我尝试在此测试延迟后集中注意力:
ionViewWillEnter(): void {
setTimeout(() => {
const title: any = document.getElementById('title');
if (title) {
title.focus();
}
}, 3000);
setTimeout(() => {
this.title.nativeElement.focus();
}, 5000);
}
然而,这些都不是重点。 屏幕阅读器不会在 Android 和 iOS 中读出标题。
更新
澄清一下,我需要屏幕阅读器 position 移动到指定元素。 如前所述,将焦点放在某个元素上不会移动屏幕阅读器 position。
任何帮助将不胜感激,这是我的 Ionic 信息:
Ionic CLI : 5.4.16
Ionic Framework : @ionic/angular 5.5.3
@angular-devkit/build-angular : 0.901.15
@angular-devkit/schematics : 9.1.15
@angular/cli : 9.1.15
@ionic/angular-toolkit : 2.3.3
Capacitor:
Capacitor CLI : 2.4.2
@capacitor/core : 2.4.0
我为所有要扩展的 Ionic 页面创建了一个 BasePage。 它捕获点击和焦点事件的活动元素,并在页面再次进入时聚焦于该元素。
如果没有以前的活动元素要关注,它还会自动尝试查找 ion-title 并关注它。
这应该涵盖基本需求。 您可以调整将超时添加到焦点方法
import { Component, ElementRef, HostListener } from '@angular/core';
import { ViewDidEnter } from '@ionic/angular';
import { isNullOrUndefined } from 'util';
@Component({
template: ''
})
export class BasePage implements ViewDidEnter {
private _previouslyActiveElement: HTMLElement;
constructor(protected elementRef: ElementRef) {}
ionViewDidEnter() {
if (!isNullOrUndefined(this._previouslyActiveElement)) {
this.focusOnElement(this._previouslyActiveElement);
} else {
if (
!isNullOrUndefined(this.elementRef.nativeElement) &&
this.elementRef.nativeElement.querySelectorAll('[autofocus]').length === 0) {
const initialFocusElement: HTMLElement = <HTMLElement>this.elementRef.nativeElement.getElementsByTagName('ion-title')[0];
if (!isNullOrUndefined(initialFocusElement)) {
this.focusOnElement(initialFocusElement);
}
}
}
}
@HostListener('focusin', ['$event.target'])
@HostListener('click', ['$event.target'])
capturePreviouslyActiveElement(element: HTMLElement) {
this._previouslyActiveElement = element;
}
focusOnElement(element: HTMLElement): void {
if (isNullOrUndefined(element)) {
return;
}
try {
// Make sure it's focusable
if (isNullOrUndefined(element.getAttribute('tabindex'))) {
element.setAttribute('tabindex', '-1');
}
element.focus();
} catch (err) {
console.log('error handling aria focus ' + err);
}
}
}
我创建了stackblitz 。 这一切都按预期工作。 为了使它更明显,我添加了样式
:focus {
outline: 3px solid orange;
}
另外,我建议您添加aria-label
,因为ion-title
是内部带有 div 的元素,我不确定是否所有读者都在处理它
<ion-title tabindex="-1" #title id="title" role="heading" aria-level="1" class="title" role="heading"
aria-label="What you want to read">My Title
</ion-title>
最后,由于您将 angular 与您的 ionic 应用程序一起使用,您可以查看具有实时播音员的材料库
@Component({...})
export class MyComponent {
constructor(liveAnnouncer: LiveAnnouncer) {
liveAnnouncer.announce("Hey Google");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.