Skip to content

Commit

Permalink
add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 7, 2019
1 parent b7a6f91 commit e216ec8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
31 changes: 31 additions & 0 deletions docs/src/pages/components/avatars/FallbackAvatars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
import { deepOrange } from '@material-ui/core/colors';

const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
'& > *': {
margin: theme.spacing(1),
},
},
orange: {
color: theme.palette.getContrastText(deepOrange[500]),
backgroundColor: deepOrange[500],
},
}));

export default function FallbackAvatars() {
const classes = useStyles();

return (
<div className={classes.root}>
<Avatar alt="Remy Sharp" src="/broken-image.jpg" className={classes.orange}>
B
</Avatar>
<Avatar alt="Remy Sharp" src="/broken-image.jpg" className={classes.orange} />
<Avatar src="/broken-image.jpg" />
</div>
);
}
33 changes: 33 additions & 0 deletions docs/src/pages/components/avatars/FallbackAvatars.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
import { deepOrange } from '@material-ui/core/colors';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
display: 'flex',
'& > *': {
margin: theme.spacing(1),
},
},
orange: {
color: theme.palette.getContrastText(deepOrange[500]),
backgroundColor: deepOrange[500],
},
}),
);

export default function FallbackAvatars() {
const classes = useStyles();

return (
<div className={classes.root}>
<Avatar alt="Remy Sharp" src="/broken-image.jpg" className={classes.orange}>
B
</Avatar>
<Avatar alt="Remy Sharp" src="/broken-image.jpg" className={classes.orange} />
<Avatar src="/broken-image.jpg" />
</div>
);
}
10 changes: 10 additions & 0 deletions docs/src/pages/components/avatars/avatars.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ Icon avatars are created by passing an icon as `children`.
If you need square or rounded avatars, use the `variant` prop.

{{"demo": "pages/components/avatars/VariantAvatars.js"}}

## Fallbacks

The component will fallback if there is an error loading the avatar image, in this order, to:

- the provided children
- the first letter of tha `alt` text
- a generic avatar icon

{{"demo": "pages/components/avatars/FallbackAvatars.js"}}
6 changes: 3 additions & 3 deletions packages/material-ui/src/Avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export const styles = theme => ({
},
/* Styles applied to the fallback icon */
fallback: {
width: '80%',
height: '80%',
width: '75%',
height: '75%',
},
});

Expand Down Expand Up @@ -126,7 +126,7 @@ const Avatar = React.forwardRef(function Avatar(props, ref) {
{...imgProps}
/>
);
} else if (childrenProp) {
} else if (childrenProp != null) {
children = childrenProp;
} else if (hasImg && alt) {
children = alt[0];
Expand Down
1 change: 1 addition & 0 deletions test/utils/createDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const whitelist = [
// required for fake getComputedStyle
'CSSStyleDeclaration',
'Element',
'Image',
'HTMLElement',
'HTMLInputElement',
'Performance',
Expand Down

0 comments on commit e216ec8

Please sign in to comment.