-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathParticipantItem.tsx
More file actions
213 lines (201 loc) · 6.37 KB
/
ParticipantItem.tsx
File metadata and controls
213 lines (201 loc) · 6.37 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
ContextualMenu,
DirectionalHint,
Icon,
IContextualMenuItem,
IStyle,
mergeStyles,
Persona,
PersonaPresence,
PersonaSize,
Stack
} from '@fluentui/react';
import React, { useMemo, useRef, useState } from 'react';
import { useIdentifiers } from '../identifiers';
import { useLocale } from '../localization';
import { useTheme } from '../theming';
import { BaseCustomStyles, OnRenderAvatarCallback } from '../types';
import {
iconContainerStyle,
iconStyles,
meContainerStyle,
menuButtonContainerStyle,
participantItemContainerStyle
} from './styles/ParticipantItem.styles';
/**
* Fluent styles for {@link ParticipantItem}.
*
* @public
*/
export interface ParticipantItemStyles extends BaseCustomStyles {
/** Styles for the avatar. */
avatar?: IStyle;
/** Styles for the (You) string. */
me?: IStyle;
/** Styles for the container of the icon. */
iconContainer?: IStyle;
/** Styles for the menu. */
menu?: IStyle;
}
/**
* Strings of {@link ParticipantItem} that can be overridden.
*
* @public
*/
export interface ParticipantItemStrings {
/** String shown when participant is me */
isMeText: string;
/** String shown when hovering over menu button */
menuTitle: string;
/** Label for the remove button in participant menu */
removeButtonLabel: string;
/** Label for the sharing icon in participant state stack */
sharingIconLabel: string;
/** Label for the muted icon in participant state stack */
mutedIconLabel: string;
}
/**
* Props for {@link ParticipantItem}.
*
* @public
*/
export interface ParticipantItemProps {
/** Unique User ID of the participant. This `userId` is available in the `onRenderAvatar` callback function */
userId?: string;
/** Name of participant. */
displayName: string;
/** Optional indicator to show participant is the user. */
me?: boolean;
/** Optional callback returning a JSX element to override avatar. */
onRenderAvatar?: OnRenderAvatarCallback;
/** Optional array of IContextualMenuItem for contextual menu. */
menuItems?: IContextualMenuItem[];
/** Optional callback returning a JSX element rendered on the right portion of the ParticipantItem. Intended for adding icons. */
onRenderIcon?: (props?: ParticipantItemProps) => JSX.Element | null;
/** Optional PersonaPresence to show participant presence. This will not have an effect if property avatar is assigned. */
presence?: PersonaPresence;
/**
* Allows users to pass in an object contains custom CSS styles.
* @Example
* ```
* <ParticipantItem styles={{ root: { background: 'blue' } }} />
* ```
*/
styles?: ParticipantItemStyles;
/**
* Optional strings to override in component
*/
strings?: Partial<ParticipantItemStrings>;
}
/**
* Component to render a calling or chat participant.
*
* Displays the participant's avatar, displayName and status as well as optional icons and context menu.
*
* @public
*/
export const ParticipantItem = (props: ParticipantItemProps): JSX.Element => {
const { userId, displayName, onRenderAvatar, menuItems, onRenderIcon, presence, styles, me } = props;
const [itemHovered, setItemHovered] = useState<boolean>(false);
const [menuHidden, setMenuHidden] = useState<boolean>(true);
const containerRef = useRef<HTMLDivElement>(null);
const theme = useTheme();
const localeStrings = useLocale().strings.participantItem;
const ids = useIdentifiers();
const isMeText = props.strings?.isMeText ?? localeStrings.isMeText;
const menuTitle = props.strings?.menuTitle ?? localeStrings.menuTitle;
const avatarOptions = {
text: displayName,
size: PersonaSize.size32,
presence: presence,
initialsTextColor: 'white'
};
const avatar = onRenderAvatar ? (
onRenderAvatar(userId ?? '', avatarOptions)
) : (
<Persona
className={mergeStyles(
{
// Prevents persona text from being vertically truncated if a global line height is less than 1.15.
lineHeight: '1.15rem'
},
styles?.avatar
)}
{...avatarOptions}
/>
);
const meTextStyle = useMemo(
() => mergeStyles(meContainerStyle, { color: theme.palette.neutralTertiary }, styles?.me),
[theme.palette.neutralTertiary, styles?.me]
);
const contextualMenuStyle = useMemo(
() => mergeStyles({ background: theme.palette.neutralLighterAlt }, styles?.menu),
[theme.palette.neutralLighterAlt, styles?.menu]
);
const infoContainerStyle = useMemo(
() => mergeStyles(iconContainerStyle, { color: theme.palette.neutralTertiary }, styles?.iconContainer),
[theme.palette.neutralTertiary, styles?.iconContainer]
);
const menuButton = useMemo(
() => (
<Stack
horizontal={true}
horizontalAlign="end"
className={mergeStyles(menuButtonContainerStyle)}
title={menuTitle}
data-ui-id={ids.participantItemMenuButton}
>
<Icon
iconName={itemHovered ? 'ParticipantItemOptionsHovered' : 'ParticipantItemOptions'}
className={iconStyles}
/>
</Stack>
),
[itemHovered, menuTitle, ids.participantItemMenuButton]
);
const onDismissMenu = (): void => {
setItemHovered(false);
setMenuHidden(true);
};
return (
<div
ref={containerRef}
role={'menuitem'}
data-is-focusable={true}
className={mergeStyles(participantItemContainerStyle, styles?.root)}
onMouseEnter={() => setItemHovered(true)}
onMouseLeave={() => setItemHovered(false)}
onClick={() => {
setItemHovered(true);
setMenuHidden(false);
}}
>
<Stack
horizontal
className={mergeStyles({ width: `calc(100% - ${menuButtonContainerStyle.width})`, alignItems: 'center' })}
>
{avatar}
{me && <Stack className={meTextStyle}>{isMeText}</Stack>}
<Stack horizontal className={mergeStyles(infoContainerStyle)}>
{onRenderIcon && onRenderIcon(props)}
</Stack>
</Stack>
{menuItems && menuItems.length > 0 && (
<>
{menuButton}
<ContextualMenu
items={menuItems}
hidden={menuHidden}
target={containerRef}
onItemClick={onDismissMenu}
onDismiss={onDismissMenu}
directionalHint={DirectionalHint.bottomRightEdge}
className={contextualMenuStyle}
/>
</>
)}
</div>
);
};