-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathVideoTile.tsx
More file actions
494 lines (461 loc) · 16 KB
/
VideoTile.tsx
File metadata and controls
494 lines (461 loc) · 16 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import {
DirectionalHint,
Icon,
IconButton,
IContextualMenuProps,
IStyle,
mergeStyles,
Persona,
Stack,
Text
} from '@fluentui/react';
import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
import { useIdentifiers } from '../identifiers';
import { ComponentLocale, useLocale } from '../localization';
import { useTheme } from '../theming';
import { BaseCustomStyles, CustomAvatarOptions, OnRenderAvatarCallback } from '../types';
import { CallingTheme } from '../theming';
import { RaisedHand } from '../types';
import { RaisedHandIcon } from './assets/RaisedHandIcon';
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
import { ParticipantState } from '../types';
import {
disabledVideoHint,
displayNameStyle,
iconContainerStyle,
overlayContainerStyles,
rootStyles,
videoContainerStyles,
videoHint,
tileInfoContainerStyle,
participantStateStringStyles
} from './styles/VideoTile.styles';
import { getVideoTileOverrideColor } from './utils/videoTileStylesUtils';
import { pinIconStyle } from './styles/VideoTile.styles';
import useLongPress from './utils/useLongPress';
import { moreButtonStyles } from './styles/VideoTile.styles';
import { raiseHandContainerStyles } from './styles/VideoTile.styles';
/* @conditional-compile-remove(reaction) */
import { ReactionResources } from '../types/ReactionTypes';
/**
* Strings of {@link VideoTile} that can be overridden.
* @beta
*/
export interface VideoTileStrings {
participantStateRinging: string;
participantStateHold: string;
}
/**
* Fluent styles for {@link VideoTile}.
*
* @public
*/
export interface VideoTileStylesProps extends BaseCustomStyles {
/** Styles for video container. */
videoContainer?: IStyle;
/** Styles for container overlayed on the video container. */
overlayContainer?: IStyle;
/** Styles for displayName on the video container. */
displayNameContainer?: IStyle;
}
/**
* Props for {@link VideoTile}.
*
* @public
*/
export interface VideoTileProps {
/** React Child components. Child Components will show as overlay component in the VideoTile. */
children?: React.ReactNode;
/**
* Allows users to pass in an object contains custom CSS styles.
* @Example
* ```
* <VideoTile styles={{ root: { background: 'blue' } }} />
* ```
*/
styles?: VideoTileStylesProps;
/** user id for the VideoTile placeholder. */
userId?: string;
/** Component with the video stream. */
renderElement?: JSX.Element | null;
/* @conditional-compile-remove(reaction) */
/**
* Overlay component responsible for rendering reaction
*/
overlay?: JSX.Element | null;
/** Determines if the video is mirrored or not. */
isMirrored?: boolean;
/** Custom render Component function for no video is available. Render a Persona Icon if undefined. */
onRenderPlaceholder?: OnRenderAvatarCallback;
/**
* Show label on the VideoTile
* @defaultValue true
*/
showLabel?: boolean;
/**
* Whether to display a mute icon beside the user's display name.
* @defaultValue true
*/
showMuteIndicator?: boolean;
/**
* Whether the video is muted or not.
*/
isMuted?: boolean;
/**
* If true, the video tile will show the pin icon.
*/
isPinned?: boolean;
/**
* Display Name of the Participant to be shown in the label.
* @remarks `displayName` is used to generate avatar initials if `initialsName` is not provided.
*/
displayName?: string;
/**
* Name of the participant used to generate initials. For example, a name `John Doe` will display `JD` as initials.
* @remarks `displayName` is used if this property is not specified.
*/
initialsName?: string;
/**
* Minimum size of the persona avatar in px.
* The persona avatar is the default placeholder shown when no video stream is available.
* For more information see https://developer.microsoft.com/en-us/fluentui#/controls/web/persona
* @defaultValue 32px
*/
personaMinSize?: number;
/**
* Maximum size of the personal avatar in px.
* The persona avatar is the default placeholder shown when no video stream is available.
* For more information see https://developer.microsoft.com/en-us/fluentui#/controls/web/persona
* @defaultValue 100px
*/
personaMaxSize?: number;
/** Optional property to set the aria label of the video tile if there is no available stream. */
noVideoAvailableAriaLabel?: string;
/** Whether the participant in the videoTile is speaking. Shows a speaking indicator (border). */
isSpeaking?: boolean;
/** Whether the participant is raised hand. Show a indicator (border) and icon with order */
raisedHand?: RaisedHand;
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
/**
* The call connection state of the participant.
* For example, `Hold` means the participant is on hold.
*/
participantState?: ParticipantState;
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
strings?: VideoTileStrings;
/**
* Display custom menu items in the VideoTile's contextual menu.
* Uses Fluent UI ContextualMenu.
* An ellipses icon will be displayed to open the contextual menu if this prop is defined.
*/
contextualMenu?: IContextualMenuProps;
/**
* Callback triggered by video tile on touch and hold.
*/
onLongTouch?: () => void;
/* @conditional-compile-remove(spotlight) */
/**
* If true, the video tile will show the spotlighted icon.
*/
isSpotlighted?: boolean;
/* @conditional-compile-remove(reaction) */
/**
* Reactions resources' url and metadata.
*/
reactionResources?: ReactionResources;
}
// Coin max size is set to PersonaSize.size100
const DEFAULT_PERSONA_MAX_SIZE_PX = 100;
// Coin min size is set PersonaSize.size32
const DEFAULT_PERSONA_MIN_SIZE_PX = 32;
const DefaultPlaceholder = (props: CustomAvatarOptions): JSX.Element => {
const { text, noVideoAvailableAriaLabel, coinSize, hidePersonaDetails } = props;
return (
<Stack className={mergeStyles({ position: 'absolute', height: '100%', width: '100%' })}>
<Stack styles={defaultPersonaStyles}>
{coinSize && (
<Persona
coinSize={coinSize}
hidePersonaDetails={hidePersonaDetails}
text={text ?? ''}
initialsTextColor="white"
aria-label={noVideoAvailableAriaLabel ?? ''}
showOverflowTooltip={false}
/>
)}
</Stack>
</Stack>
);
};
const defaultPersonaStyles = { root: { margin: 'auto', maxHeight: '100%' } };
const videoTileMoreMenuIconProps = { iconName: undefined, style: { display: 'none' } };
const videoTileMoreMenuProps = {
directionalHint: DirectionalHint.topLeftEdge,
isBeakVisible: false,
styles: { container: { maxWidth: '8rem' } }
};
const VideoTileMoreOptionsButton = (props: {
contextualMenu?: IContextualMenuProps;
canShowContextMenuButton: boolean;
}): JSX.Element => {
const { contextualMenu, canShowContextMenuButton } = props;
if (!contextualMenu) {
return <></>;
}
const optionsIcon = canShowContextMenuButton ? 'VideoTileMoreOptions' : undefined;
return (
<IconButton
data-ui-id="video-tile-more-options-button"
styles={moreButtonStyles}
menuIconProps={videoTileMoreMenuIconProps}
menuProps={{ ...videoTileMoreMenuProps, ...contextualMenu }}
iconProps={{ iconName: optionsIcon }}
/>
);
};
/**
* A component to render the video stream for a single call participant.
*
* Use with {@link GridLayout} in a {@link VideoGallery}.
*
* @public
*/
export const VideoTile = (props: VideoTileProps): JSX.Element => {
const {
children,
displayName,
initialsName,
isMirrored,
isMuted,
/* @conditional-compile-remove(spotlight) */
isSpotlighted,
isPinned,
onRenderPlaceholder,
renderElement,
/* @conditional-compile-remove(reaction) */
overlay: reactionOverlay,
showLabel = true,
showMuteIndicator = true,
styles,
userId,
noVideoAvailableAriaLabel,
isSpeaking,
raisedHand,
personaMinSize = DEFAULT_PERSONA_MIN_SIZE_PX,
personaMaxSize = DEFAULT_PERSONA_MAX_SIZE_PX,
contextualMenu
} = props;
const [isHovered, setIsHovered] = useState<boolean>(false);
const [isFocused, setIsFocused] = useState<boolean>(false);
// need to set a default otherwise the resizeObserver will get stuck in an infinite loop.
const [personaSize, setPersonaSize] = useState<number>(1);
const videoTileRef = useRef<HTMLDivElement>(null);
const locale = useLocale();
const theme = useTheme();
const isVideoRendered = !!renderElement;
const observer = useRef(
new ResizeObserver((entries): void => {
const { width, height } = entries[0].contentRect;
const personaCalcSize = Math.min(width, height) / 3;
// we only want to set the persona size if it has changed
if (personaCalcSize !== personaSize) {
setPersonaSize(Math.max(Math.min(personaCalcSize, personaMaxSize), personaMinSize));
}
})
);
useLayoutEffect(() => {
if (videoTileRef.current) {
observer.current.observe(videoTileRef.current);
}
const currentObserver = observer.current;
return () => currentObserver.disconnect();
}, [videoTileRef]);
const useLongPressProps = useMemo(() => {
return {
onLongPress: () => {
props.onLongTouch?.();
},
touchEventsOnly: true
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.onLongTouch]);
const longPressHandlers = useLongPress(useLongPressProps);
const hoverHandlers = useMemo(() => {
return {
onMouseEnter: () => setIsHovered(true),
onMouseLeave: () => setIsHovered(false),
onFocus: () => setIsFocused(true),
onBlur: () => setIsFocused(false)
};
}, []);
const placeholderOptions = {
userId,
text: initialsName ?? displayName,
noVideoAvailableAriaLabel,
coinSize: personaSize,
styles: defaultPersonaStyles,
hidePersonaDetails: true
};
const videoHintWithBorderRadius = mergeStyles(videoHint, { borderRadius: theme.effects.roundedCorner4 });
const tileInfoStyle = useMemo(
() =>
mergeStyles(
isVideoRendered ? videoHintWithBorderRadius : disabledVideoHint,
getVideoTileOverrideColor(isVideoRendered, theme, 'neutralPrimary'),
styles?.displayNameContainer
),
[isVideoRendered, videoHintWithBorderRadius, theme, styles?.displayNameContainer]
);
const ids = useIdentifiers();
const canShowLabel = showLabel && (displayName || (showMuteIndicator && isMuted));
const participantStateString = participantStateStringTrampoline(props, locale);
const canShowContextMenuButton = isHovered || isFocused;
let raisedHandBackgroundColor = '';
const callingPalette = (theme as unknown as CallingTheme).callingPalette;
raisedHandBackgroundColor = callingPalette.raiseHandGold;
return (
<Stack
data-ui-id={ids.videoTile}
className={mergeStyles(
rootStyles,
{
background: theme.palette.neutralLighter,
borderRadius: theme.effects.roundedCorner4
},
(isSpeaking || raisedHand) && {
'&::after': {
content: `''`,
position: 'absolute',
border: `0.25rem solid ${isSpeaking ? theme.palette.themePrimary : raisedHandBackgroundColor}`,
borderRadius: theme.effects.roundedCorner4,
width: '100%',
height: '100%',
pointerEvents: 'none'
}
},
styles?.root
)}
{...longPressHandlers}
>
<div ref={videoTileRef} style={{ width: '100%', height: '100%' }} {...hoverHandlers} data-is-focusable={true}>
{isVideoRendered ? (
<Stack
className={mergeStyles(
videoContainerStyles,
isMirrored && { transform: 'scaleX(-1)' },
styles?.videoContainer
)}
>
{renderElement}
</Stack>
) : (
<Stack
className={mergeStyles(videoContainerStyles, {
opacity:
participantStateString ||
/* @conditional-compile-remove(PSTN-calls) */ props.participantState === 'Idle'
? 0.4
: 1
})}
>
{onRenderPlaceholder ? (
onRenderPlaceholder(userId ?? '', placeholderOptions, DefaultPlaceholder)
) : (
<DefaultPlaceholder {...placeholderOptions} />
)}
</Stack>
)}
{
/* @conditional-compile-remove(reaction) */
reactionOverlay
}
{(canShowLabel || participantStateString) && (
<Stack horizontal className={tileInfoContainerStyle} tokens={tileInfoContainerTokens}>
<Stack horizontal className={tileInfoStyle}>
{canShowLabel && (
<Text
className={mergeStyles(displayNameStyle)}
title={displayName}
style={{ color: participantStateString ? theme.palette.neutralSecondary : 'inherit' }}
data-ui-id="video-tile-display-name"
>
{displayName}
</Text>
)}
{participantStateString && (
<Text className={mergeStyles(participantStateStringStyles(theme))}>
{bracketedParticipantString(participantStateString, !!canShowLabel)}
</Text>
)}
{showMuteIndicator && isMuted && (
<Stack className={mergeStyles(iconContainerStyle)}>
<Icon iconName="VideoTileMicOff" />
</Stack>
)}
{
/* @conditional-compile-remove(spotlight) */
isSpotlighted && (
<Stack className={mergeStyles(iconContainerStyle)}>
<Icon iconName="VideoTileSpotlighted" />
</Stack>
)
}
{isPinned && (
<Stack className={mergeStyles(iconContainerStyle)}>
<Icon iconName="VideoTilePinned" className={mergeStyles(pinIconStyle)} />
</Stack>
)}
<VideoTileMoreOptionsButton
contextualMenu={contextualMenu}
canShowContextMenuButton={canShowContextMenuButton}
/>
</Stack>
</Stack>
)}
{children && (
<Stack className={mergeStyles(overlayContainerStyles, styles?.overlayContainer)}>{children}</Stack>
)}
{raisedHand && (
<Stack
horizontal={true}
tokens={{ childrenGap: '0.2rem' }}
className={raiseHandContainerStyles(theme, !canShowLabel)}
>
<Stack.Item>
<Text>{raisedHand.raisedHandOrderPosition}</Text>
</Stack.Item>
<Stack.Item>
<RaisedHandIcon />
</Stack.Item>
</Stack>
)}
</div>
</Stack>
);
};
const participantStateStringTrampoline = (props: VideoTileProps, locale: ComponentLocale): string | undefined => {
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
const strings = { ...locale.strings.videoTile, ...props.strings };
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
return props.participantState === 'EarlyMedia' || props.participantState === 'Ringing'
? strings?.participantStateRinging
: props.participantState === 'Hold'
? strings?.participantStateHold
: undefined;
return undefined;
};
const tileInfoContainerTokens = {
// A horizontal Stack sets the left margin to 0 for all it's children.
// We need to allow the children to set their own margins
childrenGap: 'none'
};
const bracketedParticipantString = (participantString: string, withBrackets: boolean): string => {
return withBrackets ? `(${participantString})` : participantString;
};