-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5168 from kobotoolbox/task-1154-avatar-component
[TASK-1154] Update Avatar component
- Loading branch information
Showing
16 changed files
with
158 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,60 @@ | ||
import React from 'react'; | ||
import type {ComponentStory, ComponentMeta} from '@storybook/react'; | ||
|
||
import Avatar from './avatar'; | ||
import type {AvatarSize} from './avatar'; | ||
|
||
const avatarSizes: AvatarSize[] = ['s', 'm', 'l']; | ||
|
||
export default { | ||
title: 'common/Avatar', | ||
component: Avatar, | ||
argTypes: { | ||
username: {type: 'string'}, | ||
size: { | ||
options: avatarSizes, | ||
control: {type: 'select'}, | ||
}, | ||
isUsernameVisible: {type: 'boolean'}, | ||
}, | ||
} as ComponentMeta<typeof Avatar>; | ||
|
||
const Template: ComponentStory<typeof Avatar> = (args) => <Avatar {...args} />; | ||
|
||
export const Primary = Template.bind({}); | ||
Primary.args = { | ||
username: 'Leszek', | ||
size: avatarSizes[0], | ||
isUsernameVisible: true, | ||
}; | ||
|
||
// We want to test how the avatar colors look like with some ~random usernames. | ||
const bulkUsernames = [ | ||
// NATO phonetic alphabet (https://en.wikipedia.org/wiki/NATO_phonetic_alphabet) | ||
'Alfa', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', | ||
'India', 'Juliett', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', | ||
'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'Xray', | ||
'Yankee', 'Zulu', | ||
// Top 100 most popular names in the world (https://forebears.io/earth/forenames) | ||
'Maria', 'Nushi', 'Mohammed', 'Jose', 'Muhammad', 'Mohamed', 'Wei', 'Mohammad', | ||
'Ahmed', 'Yan', 'Ali', 'John', 'David', 'Li', 'Abdul', 'Ana', 'Ying', 'Michael', | ||
'Juan', 'Anna', 'Mary', 'Jean', 'Robert', 'Daniel', 'Luis', 'Carlos', 'James', | ||
'Antonio', 'Joseph', 'Hui', 'Elena', 'Francisco', 'Hong', 'Marie', 'Min', 'Lei', | ||
'Yu', 'Ibrahim', 'Peter', 'Fatima', 'Aleksandr', 'Richard', 'Xin', 'Bin', | ||
'Paul', 'Ping', 'Lin', 'Olga', 'Sri', 'Pedro', 'William', 'Rosa', 'Thomas', | ||
'Jorge', 'Yong', 'Elizabeth', 'Sergey', 'Ram', 'Patricia', 'Hassan', 'Anita', | ||
'Manuel', 'Victor', 'Sandra', 'Ming', 'Siti', 'Miguel', 'Emmanuel', 'Samuel', | ||
'Ling', 'Charles', 'Sarah', 'Mario', 'Joao', 'Tatyana', 'Mark', 'Rita', | ||
'Martin', 'Svetlana', 'Patrick', 'Natalya', 'Qing', 'Ahmad', 'Martha', 'Andrey', | ||
'Sunita', 'Andrea', 'Christine', 'Irina', 'Laura', 'Linda', 'Marina', 'Carmen', | ||
'Ghulam', 'Vladimir', 'Barbara', 'Angela', 'George', 'Roberto', 'Peng', | ||
]; | ||
export const BulkTest = () => ( | ||
<div style={{display: 'flex', flexWrap: 'wrap', gap: '10px'}}> | ||
{bulkUsernames.map((username) => ( | ||
<div key={username}> | ||
<Avatar size='m' username={username} isUsernameVisible/> | ||
</div> | ||
))} | ||
</div> | ||
); |
This file contains 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 |
---|---|---|
@@ -1,19 +1,48 @@ | ||
import {stringToColor} from 'jsapp/js/utils'; | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import styles from './avatar.module.scss'; | ||
|
||
export type AvatarSize = 'l' | 'm' | 's'; | ||
|
||
/** | ||
* A simple function that generates hsl color from given string. Saturation and | ||
* lightness is not random, just the hue. | ||
*/ | ||
function stringToHSL(string: string, saturation: number, lightness: number) { | ||
let hash = 0; | ||
for (let i = 0; i < string.length; i++) { | ||
hash = string.charCodeAt(i) + ((hash << 5) - hash); | ||
hash = hash & hash; | ||
} | ||
return `hsl(${(hash % 360)}, ${saturation}%, ${lightness}%)`; | ||
} | ||
|
||
interface AvatarProps { | ||
/** | ||
* First letter of the username would be used as avatar. Whole username would | ||
* be used to generate the color of the avatar. | ||
*/ | ||
username: string; | ||
/** | ||
* Username is not being displayed by default. | ||
*/ | ||
isUsernameVisible?: boolean; | ||
size: AvatarSize; | ||
} | ||
|
||
export default function Avatar(props: AvatarProps) { | ||
return ( | ||
<div className={styles.root}> | ||
<div className={styles.initials} style={{background: `#${stringToColor(props.username)}`}}> | ||
<div className={cx(styles.avatar, styles[`avatar-size-${props.size}`])}> | ||
<div | ||
className={styles.initials} | ||
style={{backgroundColor: `${stringToHSL(props.username, 80, 40)}`}} | ||
> | ||
{props.username.charAt(0)} | ||
</div> | ||
|
||
<label>{props.username}</label> | ||
{props.isUsernameVisible && | ||
<label>{props.username}</label> | ||
} | ||
</div> | ||
); | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -120,6 +120,7 @@ | |
display: flex; | ||
justify-content: flex-start; | ||
flex-wrap: wrap; | ||
gap: 10px; | ||
} | ||
|
||
.user-row { | ||
|
This file contains 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
This file contains 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
This file contains 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.