UNPKG

2.02 kBJavaScriptView Raw
1const lastFocusedViewOnPageKeyName = '__lastFocusedViewOnPage';
2export const accessibilityBlurEvent = 'accessibilityBlur';
3export const accessibilityFocusEvent = 'accessibilityFocus';
4export const accessibilityFocusChangedEvent = 'accessibilityFocusChanged';
5export const accessibilityPerformEscapeEvent = 'accessibilityPerformEscape';
6/**
7 * Send notification when accessibility focus state changes.
8 * If either receivedFocus or lostFocus is true, 'accessibilityFocusChanged' is send with value true if element received focus
9 * If receivedFocus, 'accessibilityFocus' is send
10 * if lostFocus, 'accessibilityBlur' is send
11 *
12 * @param {View} view
13 * @param {boolean} receivedFocus
14 * @param {boolean} lostFocus
15 */
16export function notifyAccessibilityFocusState(view, receivedFocus, lostFocus) {
17 if (!receivedFocus && !lostFocus) {
18 return;
19 }
20 view.notify({
21 eventName: accessibilityFocusChangedEvent,
22 object: view,
23 value: !!receivedFocus,
24 });
25 if (receivedFocus) {
26 if (view.page) {
27 view.page[lastFocusedViewOnPageKeyName] = new WeakRef(view);
28 }
29 view.notify({
30 eventName: accessibilityFocusEvent,
31 object: view,
32 });
33 }
34 else if (lostFocus) {
35 view.notify({
36 eventName: accessibilityBlurEvent,
37 object: view,
38 });
39 }
40}
41export function getLastFocusedViewOnPage(page) {
42 try {
43 const lastFocusedViewRef = page[lastFocusedViewOnPageKeyName];
44 if (!lastFocusedViewRef) {
45 return null;
46 }
47 const lastFocusedView = lastFocusedViewRef.deref();
48 if (!lastFocusedView) {
49 return null;
50 }
51 if (!lastFocusedView.parent || lastFocusedView.page !== page) {
52 return null;
53 }
54 return lastFocusedView;
55 }
56 catch {
57 // ignore
58 }
59 finally {
60 delete page[lastFocusedViewOnPageKeyName];
61 }
62 return null;
63}
64//# sourceMappingURL=accessibility-common.js.map
\No newline at end of file