-
Notifications
You must be signed in to change notification settings - Fork 888
refactor(site): Normalize avatar components #5860
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
6 commits
Select commit
Hold shift + click to select a range
27ca826
refactor(site): Normalize avatar components
BrunoQuaresma 72bfda0
Fix template layout size
BrunoQuaresma 1cb58da
No need for sm styles
BrunoQuaresma 900622e
Add storybook for Avatar
BrunoQuaresma 454a1f5
Fix fmt
BrunoQuaresma 61b5f95
Improvements nd fixes
BrunoQuaresma 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
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,61 @@ | ||
import { Story } from "@storybook/react" | ||
import { Avatar, AvatarIcon, AvatarProps } from "./Avatar" | ||
import PauseIcon from "@material-ui/icons/PauseOutlined" | ||
|
||
export default { | ||
title: "components/Avatar", | ||
component: Avatar, | ||
} | ||
|
||
const Template: Story<AvatarProps> = (args: AvatarProps) => <Avatar {...args} /> | ||
|
||
export const Letter = Template.bind({}) | ||
Letter.args = { | ||
children: "Coder", | ||
} | ||
|
||
export const LetterXL = Template.bind({}) | ||
LetterXL.args = { | ||
children: "Coder", | ||
size: "xl", | ||
} | ||
|
||
export const LetterDarken = Template.bind({}) | ||
LetterDarken.args = { | ||
children: "Coder", | ||
colorScheme: "darken", | ||
} | ||
|
||
export const Image = Template.bind({}) | ||
Image.args = { | ||
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4", | ||
} | ||
|
||
export const ImageXL = Template.bind({}) | ||
ImageXL.args = { | ||
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4", | ||
size: "xl", | ||
} | ||
|
||
export const MuiIcon = Template.bind({}) | ||
MuiIcon.args = { | ||
children: <PauseIcon />, | ||
} | ||
|
||
export const MuiIconDarken = Template.bind({}) | ||
MuiIconDarken.args = { | ||
children: <PauseIcon />, | ||
colorScheme: "darken", | ||
} | ||
|
||
export const MuiIconXL = Template.bind({}) | ||
MuiIconXL.args = { | ||
children: <PauseIcon />, | ||
size: "xl", | ||
} | ||
|
||
export const AvatarIconDarken = Template.bind({}) | ||
AvatarIconDarken.args = { | ||
children: <AvatarIcon src="/icon/database.svg" />, | ||
colorScheme: "darken", | ||
} |
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,77 @@ | ||
// This is the only place MuiAvatar can be used | ||
// eslint-disable-next-line no-restricted-imports -- Read above | ||
import MuiAvatar, { | ||
AvatarProps as MuiAvatarProps, | ||
} from "@material-ui/core/Avatar" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import { FC } from "react" | ||
import { combineClasses } from "util/combineClasses" | ||
import { firstLetter } from "./firstLetter" | ||
|
||
export type AvatarProps = MuiAvatarProps & { | ||
size?: "md" | "xl" | ||
colorScheme?: "light" | "darken" | ||
fitImage?: boolean | ||
} | ||
|
||
export const Avatar: FC<AvatarProps> = ({ | ||
size = "md", | ||
colorScheme = "light", | ||
fitImage, | ||
className, | ||
children, | ||
...muiProps | ||
}) => { | ||
const styles = useStyles() | ||
|
||
return ( | ||
<MuiAvatar | ||
{...muiProps} | ||
className={combineClasses([ | ||
className, | ||
styles[size], | ||
styles[colorScheme], | ||
fitImage && styles.fitImage, | ||
])} | ||
> | ||
{/* If the children is a string, we always want to render the first letter */} | ||
{typeof children === "string" ? firstLetter(children) : children} | ||
</MuiAvatar> | ||
) | ||
} | ||
|
||
/** | ||
* Use it to make an img element behaves like a MaterialUI Icon component | ||
*/ | ||
export const AvatarIcon: FC<{ src: string }> = ({ src }) => { | ||
const styles = useStyles() | ||
return <img src={src} alt="" className={styles.avatarIcon} /> | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
// Size styles | ||
// Just use the default value from theme | ||
md: {}, | ||
xl: { | ||
width: theme.spacing(6), | ||
height: theme.spacing(6), | ||
fontSize: theme.spacing(3), | ||
}, | ||
// Colors | ||
// Just use the default value from theme | ||
light: {}, | ||
darken: { | ||
background: theme.palette.divider, | ||
color: theme.palette.text.primary, | ||
}, | ||
// Avatar icon | ||
avatarIcon: { | ||
maxWidth: "50%", | ||
}, | ||
// Fit image | ||
fitImage: { | ||
"& .MuiAvatar-img": { | ||
objectFit: "contain", | ||
}, | ||
}, | ||
})) |
File renamed without changes.
File renamed without changes.
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
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 |
---|---|---|
@@ -1,71 +1,50 @@ | ||
import Avatar from "@material-ui/core/Avatar" | ||
import Link from "@material-ui/core/Link" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import { Avatar } from "components/Avatar/Avatar" | ||
import { FC, PropsWithChildren } from "react" | ||
import { Link as RouterLink } from "react-router-dom" | ||
import { firstLetter } from "../../util/firstLetter" | ||
import { | ||
TableCellData, | ||
TableCellDataPrimary, | ||
TableCellDataSecondary, | ||
} from "../TableCellData/TableCellData" | ||
import { Stack } from "components/Stack/Stack" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
|
||
export interface AvatarDataProps { | ||
title: string | ||
subtitle?: string | ||
highlightTitle?: boolean | ||
link?: string | ||
src?: string | ||
avatar?: React.ReactNode | ||
} | ||
|
||
export const AvatarData: FC<PropsWithChildren<AvatarDataProps>> = ({ | ||
title, | ||
subtitle, | ||
link, | ||
highlightTitle, | ||
src, | ||
avatar, | ||
}) => { | ||
const styles = useStyles() | ||
|
||
if (!avatar) { | ||
avatar = <Avatar>{firstLetter(title)}</Avatar> | ||
avatar = <Avatar src={src}>{title}</Avatar> | ||
} | ||
|
||
return ( | ||
<div className={styles.root}> | ||
<div className={styles.avatarWrapper}>{avatar}</div> | ||
<Stack spacing={1.5} direction="row" alignItems="center"> | ||
{avatar} | ||
|
||
{link ? ( | ||
<Link to={link} underline="none" component={RouterLink}> | ||
<TableCellData> | ||
<TableCellDataPrimary highlight={highlightTitle}> | ||
{title} | ||
</TableCellDataPrimary> | ||
{subtitle && ( | ||
<TableCellDataSecondary>{subtitle}</TableCellDataSecondary> | ||
)} | ||
</TableCellData> | ||
</Link> | ||
) : ( | ||
<TableCellData> | ||
<TableCellDataPrimary highlight={highlightTitle}> | ||
{title} | ||
</TableCellDataPrimary> | ||
{subtitle && ( | ||
<TableCellDataSecondary>{subtitle}</TableCellDataSecondary> | ||
)} | ||
</TableCellData> | ||
)} | ||
</div> | ||
<Stack spacing={0}> | ||
<span className={styles.title}>{title}</span> | ||
{subtitle && <span className={styles.subtitle}>{subtitle}</span>} | ||
</Stack> | ||
</Stack> | ||
) | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
root: { | ||
display: "flex", | ||
alignItems: "center", | ||
title: { | ||
color: theme.palette.text.primary, | ||
fontWeight: 600, | ||
}, | ||
avatarWrapper: { | ||
marginRight: theme.spacing(1.5), | ||
|
||
subtitle: { | ||
fontSize: 12, | ||
color: theme.palette.text.secondary, | ||
lineHeight: "140%", | ||
marginTop: 2, | ||
maxWidth: 540, | ||
}, | ||
})) |
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
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
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.
This is not really important, just a thought but would it make sense to make
AvatarData
takeAvatar
props and pass them along rather than acceptavatar
? My reasoning is that it seems unfortunate that there are two ways of doing things like:and
Or alternatively we could always make it so we always have to pass an
avatar
, either way works so there is one way of doing things.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.
AvatarData
is a bad name for what it does and not a good abstraction. Wondering ifHeading
orDataHeading
would be better with the following interface: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.
Definitely agree!