-
Notifications
You must be signed in to change notification settings - Fork 28
/
ScreenReader.tsx
89 lines (81 loc) · 2.24 KB
/
ScreenReader.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { ExceptionCodes, PluginListenerHandle } from '@capacitor/core';
import { ScreenReader } from '@capacitor/screen-reader';
import {
IonButtons,
IonContent,
IonHeader,
IonPage,
IonMenuButton,
IonTitle,
IonToolbar,
IonButton,
IonInput,
IonList,
IonItem,
IonLabel,
useIonViewDidEnter,
useIonViewDidLeave,
} from '@ionic/react';
import React, { useState } from 'react';
import { createEventTargetValueExtractor } from '../utils/dom';
const ScreenReaderPage: React.FC = () => {
let handler: PluginListenerHandle;
const [sentence, setSentence] = useState('Hello World?');
useIonViewDidEnter(() => {
handler = ScreenReader.addListener(
'screenReaderStateChange',
({ value }: any) => alert(`State Change! Screen Reader on? ${value}`),
);
});
useIonViewDidLeave(() => {
handler.remove();
});
const isVoiceOverEnabled = async () => {
try {
const { value: enabled } = await ScreenReader.isEnabled();
alert(`Screen Reader on? ${enabled}`);
} catch (e) {
if (e.code === ExceptionCodes.UNSUPPORTED_BROWSER) {
console.warn(
'Unsupported in the browser! Handling this in my own way...',
);
} else {
throw e;
}
}
};
const speak = async () => {
await ScreenReader.speak({ value: sentence });
};
const handleSpeakInputChange = createEventTargetValueExtractor(setSentence);
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton />
</IonButtons>
<IonTitle>Screen Reader</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonList>
<IonItem>
<IonLabel>Screen Reader</IonLabel>
<IonButton expand="block" onClick={isVoiceOverEnabled}>
TalkBack/VoiceOver Enabled?
</IonButton>
</IonItem>
<IonItem>
<IonLabel position="stacked">Sentence</IonLabel>
<IonInput value={sentence} onInput={handleSpeakInputChange} />
<IonButton onClick={speak} slot="end">
Speak
</IonButton>
</IonItem>
</IonList>
</IonContent>
</IonPage>
);
};
export default ScreenReaderPage;