-
Notifications
You must be signed in to change notification settings - Fork 362
feat: add avatar group, user avatar group components and badge refinements #3378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| import React, { useCallback, useMemo } from 'react'; | ||
| import { StyleSheet, View } from 'react-native'; | ||
|
|
||
| import { UserResponse } from 'stream-chat'; | ||
|
|
||
| import { Avatar } from './Avatar'; | ||
|
|
||
| import { iconSizes } from './constants'; | ||
|
|
||
| import { UserAvatar } from './UserAvatar'; | ||
|
|
||
| import { useTheme } from '../../../contexts/themeContext/ThemeContext'; | ||
| import { PeopleIcon } from '../../../icons/PeopleIcon'; | ||
| import { primitives } from '../../../theme'; | ||
| import { BadgeCount } from '../BadgeCount'; | ||
| import { OnlineIndicator } from '../OnlineIndicator'; | ||
|
|
||
| export type AvatarGroupProps = { | ||
| /** | ||
| * The size of the avatar group. | ||
| */ | ||
| size: 'lg' | 'xl'; | ||
| /** | ||
| * The items to display in the avatar group. | ||
| */ | ||
| items: React.ReactNode[]; | ||
| }; | ||
|
|
||
| const sizes = { | ||
| lg: { | ||
| width: 40, | ||
| height: 40, | ||
| }, | ||
| xl: { | ||
| width: 64, | ||
| height: 64, | ||
| }, | ||
| }; | ||
|
|
||
| const buildForTwo = (items: React.ReactNode[]) => { | ||
| return ( | ||
| <> | ||
| <View style={styles.topStart}>{items[0]}</View> | ||
| <View style={styles.bottomEnd}>{items[1]}</View> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| const buildForThree = (items: React.ReactNode[]) => { | ||
| return ( | ||
| <> | ||
| <View style={styles.topCenter}>{items[0]}</View> | ||
| <View style={styles.bottomStart}>{items[1]}</View> | ||
| <View style={styles.bottomEnd}>{items[2]}</View> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| const buildForFour = (items: React.ReactNode[]) => { | ||
| return ( | ||
| <> | ||
| <View style={styles.topStart}>{items[0]}</View> | ||
| <View style={styles.topEnd}>{items[1]}</View> | ||
| <View style={styles.bottomStart}>{items[2]}</View> | ||
| <View style={styles.bottomEnd}>{items[3]}</View> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const AvatarGroup = (props: AvatarGroupProps) => { | ||
| const { size, items = [] } = props; | ||
| const { | ||
| theme: { semantics }, | ||
| } = useTheme(); | ||
|
|
||
| const avatarSize = size === 'lg' ? 'sm' : 'lg'; | ||
| const badgeCountSize = size === 'lg' ? 'xs' : 'md'; | ||
|
|
||
| const buildForOne = useCallback( | ||
| (item: React.ReactNode) => { | ||
| return buildForTwo([ | ||
| <Avatar | ||
| key={'people-icon'} | ||
| placeholder={ | ||
| <PeopleIcon | ||
| stroke={semantics.avatarTextDefault} | ||
| height={iconSizes[avatarSize]} | ||
| width={iconSizes[avatarSize]} | ||
| /> | ||
| } | ||
| size={avatarSize} | ||
| />, | ||
| item, | ||
| ]); | ||
| }, | ||
| [semantics.avatarTextDefault, avatarSize], | ||
| ); | ||
|
|
||
| const buildForMore = useCallback( | ||
| (items: React.ReactNode[]) => { | ||
| const remainingItems = items.length - 2; | ||
| return ( | ||
| <> | ||
| <View style={styles.topStart}>{items[0]}</View> | ||
| <View style={styles.topEnd}>{items[1]}</View> | ||
| <View style={styles.bottomCenter}> | ||
| <BadgeCount size={badgeCountSize} count={`+${remainingItems}`} /> | ||
| </View> | ||
| </> | ||
| ); | ||
| }, | ||
| [badgeCountSize], | ||
| ); | ||
|
|
||
| const renderItems = useMemo(() => { | ||
| const length = items.length; | ||
| if (length === 1) { | ||
| return buildForOne(items[0]); | ||
| } | ||
| if (length === 2) { | ||
| return buildForTwo(items); | ||
| } | ||
| if (length === 3) { | ||
| return buildForThree(items); | ||
| } | ||
| if (length === 4) { | ||
| return buildForFour(items); | ||
| } | ||
| return buildForMore(items); | ||
| }, [buildForMore, buildForOne, items]); | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <View style={[sizes[size]]}>{renderItems}</View> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| export type UserAvatarGroupProps = Pick<AvatarGroupProps, 'size'> & { | ||
| /** | ||
| * The users to display in the avatar group. | ||
| */ | ||
| users: UserResponse[]; | ||
| /** | ||
| * Whether to show the online indicator. | ||
| */ | ||
| showOnlineIndicator?: boolean; | ||
| }; | ||
|
|
||
| export const UserAvatarGroup = ({ | ||
| users, | ||
| showOnlineIndicator = true, | ||
| size, | ||
| }: UserAvatarGroupProps) => { | ||
| const styles = useUserAvatarGroupStyles(); | ||
| const userAvatarSize = size === 'lg' ? 'sm' : 'lg'; | ||
| const onlineIndicatorSize = size === 'xl' ? 'xl' : 'lg'; | ||
| return ( | ||
| <View testID='user-avatar-group'> | ||
| <AvatarGroup | ||
| size={size} | ||
| items={users.map((user) => ( | ||
| <View key={user.id} style={styles.userAvatarWrapper}> | ||
| <UserAvatar user={user} size={userAvatarSize} showBorder={true} /> | ||
| </View> | ||
| ))} | ||
| /> | ||
| {showOnlineIndicator ? ( | ||
| <View style={styles.onlineIndicatorWrapper}> | ||
| <OnlineIndicator online={true} size={onlineIndicatorSize} /> | ||
| </View> | ||
| ) : null} | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const useUserAvatarGroupStyles = () => { | ||
| const { | ||
| theme: { semantics }, | ||
| } = useTheme(); | ||
|
|
||
| return useMemo( | ||
| () => | ||
| StyleSheet.create({ | ||
| userAvatarWrapper: { | ||
| borderWidth: 2, | ||
| borderColor: semantics.borderCoreOnAccent, | ||
| borderRadius: primitives.radiusMax, | ||
| }, | ||
| onlineIndicatorWrapper: { | ||
| position: 'absolute', | ||
| right: 0, | ||
| top: 0, | ||
| }, | ||
| }), | ||
| [semantics], | ||
| ); | ||
| }; | ||
|
|
||
| // TODO V9: Add theming support here. | ||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| padding: 2, | ||
| }, | ||
| topStart: { | ||
| position: 'absolute', | ||
| top: 0, | ||
| left: 0, | ||
| }, | ||
| bottomEnd: { | ||
| position: 'absolute', | ||
| bottom: 0, | ||
| right: 0, | ||
| }, | ||
| topEnd: { | ||
| position: 'absolute', | ||
| top: 0, | ||
| right: 0, | ||
| }, | ||
| bottomStart: { | ||
| position: 'absolute', | ||
| bottom: 0, | ||
| left: 0, | ||
| }, | ||
| topCenter: { | ||
| alignItems: 'center', | ||
| }, | ||
| bottomCenter: { | ||
| position: 'absolute', | ||
| bottom: 0, | ||
| alignSelf: 'center', | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like the fact that we ignore/intentionally omit theming for the new components. Going forward, please make sure that all new components are theme compatible. And also make sure that these are added to the new avatars/.