-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathActivityRow.tsx
More file actions
198 lines (169 loc) · 7.73 KB
/
ActivityRow.tsx
File metadata and controls
198 lines (169 loc) · 7.73 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { hooks } from 'botframework-webchat-api';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { forwardRef, memo, useCallback, useMemo, useRef } from 'react';
import SpeakActivity from '../Activity/Speak';
import useActiveDescendantId from '../providers/TranscriptFocus/useActiveDescendantId';
import useFocusByActivityKey from '../providers/TranscriptFocus/useFocusByActivityKey';
import useGetDescendantIdByActivityKey from '../providers/TranscriptFocus/useGetDescendantIdByActivityKey';
import ScreenReaderText from '../ScreenReaderText';
import { android } from '../Utils/detectBrowser';
import FocusTrap from './FocusTrap';
import useActivityAccessibleName from './useActivityAccessibleName';
import { useStyleToEmotionObject } from '../hooks/internal/styleToEmotionObject';
import type { WebChatActivity } from 'botframework-webchat-core';
import type { MouseEventHandler, PropsWithChildren } from 'react';
import { useRefFrom } from 'use-ref-from';
import {
TranscriptFocusContent,
TranscriptFocusContentActiveDescendant,
TranscriptFocusContentOverlay,
TranscriptFocusIndicator
} from './TranscriptFocus';
const ROOT_STYLE = {
color: 'transparent',
height: 1,
overflow: 'hidden',
position: 'absolute',
top: 0,
whiteSpace: 'nowrap',
width: 1
};
const { useActivityKeysByRead, useGetHasAcknowledgedByActivityKey, useGetKeyByActivity } = hooks;
type ActivityRowProps = PropsWithChildren<{ activity: WebChatActivity }>;
const ActivityRow = forwardRef<HTMLElement, ActivityRowProps>(({ activity, children }, ref) => {
const [activeDescendantId] = useActiveDescendantId();
const [readActivityKeys] = useActivityKeysByRead();
const bodyRef = useRef<HTMLDivElement>();
const focusByActivityKey = useFocusByActivityKey();
const getKeyByActivity = useGetKeyByActivity();
// TODO: [P2] #2858 We should use core/definitions/speakingActivity for this predicate instead
const shouldSpeak = activity.channelData?.speak;
const [accessibleName] = useActivityAccessibleName(activity, bodyRef);
const activityKey = getKeyByActivity(activity);
const acknowledged = useGetHasAcknowledgedByActivityKey()(activityKey);
const activityKeyRef = useRefFrom<string>(activityKey);
const descendantId = useGetDescendantIdByActivityKey()(activityKey);
const descendantLabelId = `webchat__basic-transcript__active-descendant-label--${activityKey}`;
const rootClassName = useStyleToEmotionObject()(ROOT_STYLE) + '';
const isActiveDescendant = descendantId === activeDescendantId;
const read = readActivityKeys.includes(activityKey);
const focusSelf = useCallback<(withFocus?: boolean) => void>(
(withFocus?: boolean) => focusByActivityKey(activityKeyRef.current, withFocus),
[activityKeyRef, focusByActivityKey]
);
// When a child of the activity receives focus, notify the transcript to set the `aria-activedescendant` to this activity.
const handleDescendantFocus: () => void = useCallback(() => focusSelf(false), [focusSelf]);
// When receive Escape key from descendant, focus back to the activity.
const handleLeaveFocusTrap = useCallback(() => focusSelf(), [focusSelf]);
// When the user press UP/DOWN arrow keys, we put a visual focus indicator around the focused activity.
// We should do the same for mouse, when the user click on the activity, we should also put a visual focus indicator around the activity.
// We are doing it in event capture phase to prevent descendants from stopping event propagation to us.
const handleMouseDownCapture: MouseEventHandler = useCallback(() => focusSelf(false), [focusSelf]);
const focusTrapChildren = useMemo(
() => (
<div className="webchat__basic-transcript__activity-body" ref={bodyRef}>
{children}
</div>
),
[bodyRef, children]
);
const activityIdRef = useRefFrom(activity.id);
const handleFormData = useCallback(
(event: FormDataEvent & { target: HTMLFormElement }) => {
const { webchatIncludeActivityId, webchatIncludeActivityKey } = event.target.dataset;
if (webchatIncludeActivityId) {
event.formData.set(webchatIncludeActivityId, activityIdRef.current ?? '');
}
if (webchatIncludeActivityKey) {
event.formData.set(webchatIncludeActivityKey, activityKeyRef.current);
}
},
[activityKeyRef, activityIdRef]
);
const prevArticleRef = useRef<HTMLElement>(null);
const wrappedRef = useCallback(
(el: HTMLElement | null) => {
if (prevArticleRef.current) {
prevArticleRef.current.removeEventListener('formdata', handleFormData);
}
if (el) {
el.addEventListener('formdata', handleFormData);
}
prevArticleRef.current = el;
if (ref) {
if (typeof ref === 'function') {
ref(el);
} else {
ref.current = el;
}
}
},
[handleFormData, ref]
);
return (
// TODO: [P2] Add `aria-roledescription="message"` for better AX, need localization strings.
/* TODO: [P1] File a crbug for TalkBack. It should not able to read the content twice when scanning. */
/* The following <div> is designed for active descendant only.
We want to prevent screen reader from scanning the content that is authored only for active descendant.
The specific content should only read when user press UP/DOWN arrow keys to change `aria-activedescendant`.
However, Android TalkBack 12.1 is buggy when the there is an element with ID of one of the `aria-activedescendant` potential candidates,
TalkBack will narrate the message content twice (i.e. content of `bodyRef`), regardless whether the ID is currently set as `aria-activedescendant` or not.
As Android does not support active descendant, we are hiding the whole DOM element altogether. */
<TranscriptFocusContent
className={classNames('webchat__basic-transcript__activity', {
'webchat__basic-transcript__activity--acknowledged': acknowledged,
'webchat__basic-transcript__activity--read': read
})}
focused={isActiveDescendant}
onMouseDownCapture={handleMouseDownCapture}
// When NVDA is in browse mode, using up/down arrow key to "browse" will dispatch "click" and "mousedown" events for <article> element (inside <LiveRegionActivity>).
ref={wrappedRef}
tag="article"
>
<FocusTrap
onFocus={handleDescendantFocus}
onLeave={handleLeaveFocusTrap}
targetClassName="webchat__basic-transcript__activity-focus-target"
>
{focusTrapChildren}
</FocusTrap>
{shouldSpeak && (
// TODO: Should build `webChatActivitySchema`.
<SpeakActivity activity={activity as WebChatActivity & { channelData: { speechSynthesisUtterance?: any } }} />
)}
<TranscriptFocusContentOverlay>
{!android && (
<TranscriptFocusContentActiveDescendant
aria-labelledby={descendantLabelId}
className="webchat__basic-transcript__activity-active-descendant"
// "id" is required for "aria-labelledby"
id={descendantId}
role="article"
>
<h6 className={rootClassName} id={descendantLabelId}>
{accessibleName}
</h6>
</TranscriptFocusContentActiveDescendant>
)}
<TranscriptFocusIndicator type="content" />
</TranscriptFocusContentOverlay>
</TranscriptFocusContent>
);
});
ActivityRow.defaultProps = {
children: undefined
};
ActivityRow.displayName = 'ActivityRow';
ActivityRow.propTypes = {
// PropTypes cannot fully capture TypeScript type.
// @ts-ignore
activity: PropTypes.shape({
channelData: PropTypes.shape({
speak: PropTypes.bool,
'webchat:fallback-text': PropTypes.string
})
}).isRequired,
children: PropTypes.any
};
export default memo(ActivityRow);