-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement default avatar based on user name
- Loading branch information
Showing
6 changed files
with
88 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
name: DefaultAvatar | ||
route: /DefaultAvatar | ||
menu: Misc | ||
--- | ||
|
||
import { Playground, Props } from 'docz'; | ||
import { DefaultAvatar } from '@cosmotech/ui'; | ||
|
||
# Help Menu | ||
|
||
The aim of this component is to display an avatar for users that didn't define one, while offering a specific one, whose color is based on the user's name. | ||
|
||
## Props | ||
|
||
<Props of={DefaultAvatar} /> | ||
|
||
## Basic Usage | ||
|
||
<Playground> | ||
<div> | ||
<DefaultAvatar userName="Emma Michelet" /> | ||
<DefaultAvatar userName="Elena Sasova" /> | ||
<DefaultAvatar userName="Tristan Huet" /> | ||
<DefaultAvatar userName="Nicolas Borde" /> | ||
<DefaultAvatar userName="Jérémy Reynard" /> | ||
</div> | ||
</Playground> |
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,53 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Avatar, makeStyles } from '@material-ui/core'; | ||
|
||
function stringToColor(str) { | ||
let hash = 0; | ||
|
||
for (let i = 0; i < str.length; ++i) { | ||
hash = str.charCodeAt(i) + ((hash << 5) - hash); | ||
} | ||
|
||
let color = '#'; | ||
for (let i = 0; i < 3; ++i) { | ||
const value = (hash >> (i * 8)) & 0xff; | ||
color += `00${value.toString(16)}`.slice(-2); | ||
} | ||
|
||
return color; | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
avatar: { | ||
width: (props) => `${props.size}px`, | ||
height: (props) => `${props.size}px`, | ||
fontSize: (props) => theme.typography.pxToRem(props.size / 2), | ||
backgroundColor: (props) => props.avatarBackgroundColor, | ||
color: (props) => theme.palette.getContrastText(props.avatarBackgroundColor), | ||
}, | ||
})); | ||
|
||
export const DefaultAvatar = ({ userName, size }) => { | ||
const avatarBackgroundColor = stringToColor(userName); | ||
const letter = userName.charAt(0).toUpperCase(); | ||
|
||
const classes = useStyles({ avatarBackgroundColor, size }); | ||
|
||
return <Avatar className={classes.avatar}>{letter}</Avatar>; | ||
}; | ||
|
||
DefaultAvatar.propTypes = { | ||
/** | ||
* User name to get first letter and compute avatar's color | ||
*/ | ||
userName: PropTypes.string.isRequired, | ||
/** | ||
* Icon size in pixels | ||
*/ | ||
size: PropTypes.int, | ||
}; | ||
|
||
DefaultAvatar.defaultProps = { | ||
size: 24, | ||
}; |
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 @@ | ||
export { DefaultAvatar } from './DefaultAvatar'; |
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