Skip to content
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

[Avatar] Use inner shadow for border #4261

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ purple500,

const style = {margin: 5};

/**
* Examples of `Avatar` using an image, [Font Icon](/#/components/font-icon), [SVG Icon](/#/components/svg-icon)
* and "Letter" (string), with and without custom colors at the default size (`40dp`) and an alternate size (`30dp`).
*/
const AvatarExampleSimple = () => (
<List>
<ListItem
disabled={true}
leftAvatar={
<Avatar src="images/uxceo-128.jpg" />
<Avatar src="images/kerem-128.jpg" />
}
>
Image Avatar
Expand All @@ -30,7 +34,7 @@ const AvatarExampleSimple = () => (
disabled={true}
leftAvatar={
<Avatar
src="images/uxceo-128.jpg"
src="images/kerem-128.jpg"
size={30}
style={style}
/>
Expand Down
4 changes: 0 additions & 4 deletions docs/src/app/components/pages/components/Avatar/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import AvatarExampleSimple from './ExampleSimple';
import avatarExampleSimpleCode from '!raw!./ExampleSimple';
import avatarCode from '!raw!material-ui/Avatar/Avatar';

const description = 'Examples of `Avatar` using an image, [Font Icon](/#/components/font-icon), ' +
'[SVG Icon](/#/components/svg-icon) and "Letter" (string), with and without custom colors.';

const AvatarsPage = () => (
<div>
<Title render={(previousTitle) => `Avatar - ${previousTitle}`} />
<MarkdownElement text={avatarReadmeText} />
<CodeExample
code={avatarExampleSimpleCode}
title="Examples"
description={description}
>
<AvatarExampleSimple />
</CodeExample>
Expand Down
50 changes: 30 additions & 20 deletions src/Avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,41 @@ function getStyles(props, context) {
backgroundColor,
color,
size,
src,
} = props;

const {avatar} = context.muiTheme;

const styles = {
root: {
position: 'absolute',
color: color || avatar.color,
backgroundColor: backgroundColor || avatar.backgroundColor,
userSelect: 'none',
display: 'inline-block',
textAlign: 'center',
lineHeight: `${size}px`,
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: size / 2,
borderRadius: '50%',
height: size,
width: size,
},
image: {
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%',
borderRadius: '50%',
},
border: {
position: 'absolute',
top: 0,
left: 0,
borderRadius: '50%',
boxShadow: `inset 0px 0px 0px 0.5px ${avatar.borderColor}`,
height: '100%',
width: '100%',
},
icon: {
color: color || avatar.color,
width: size * 0.6,
Expand All @@ -32,17 +49,6 @@ function getStyles(props, context) {
},
};

if (src && avatar.borderColor) {
Object.assign(styles.root, {
background: `url(${src})`,
backgroundSize: size,
backgroundOrigin: 'border-box',
border: `solid 1px ${avatar.borderColor}`,
height: size - 2,
width: size - 2,
});
}

return styles;
}

Expand Down Expand Up @@ -106,11 +112,15 @@ class Avatar extends Component {

if (src) {
return (
<div
{...other}
style={prepareStyles(Object.assign(styles.root, style))}
className={className}
/>
<div style={prepareStyles(Object.assign(styles.root, style))}>
<img
style={prepareStyles(styles.image)}
{...other}
src={src}
className={className}
/>
<div style={prepareStyles(styles.border)}></div>
</div>
);
} else {
return (
Expand Down
6 changes: 3 additions & 3 deletions src/Avatar/Avatar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ describe('<Avatar />', () => {
);

assert.notOk(!wrapper.contains(testChildren), 'should not contain the children');
assert.ok(wrapper.is('div'), 'should be a div');
assert.ok(wrapper.is({style: {background: 'url(face.jpg)'}}), 'should set background url');
assert.ok(wrapper.is('img'), 'should be an image');
assert.ok(wrapper.is({src: 'face.jpg'}), 'should have the src passed into props');

wrapper.setProps({src: 'meow.jpg'});
assert.ok(wrapper.is({style: {background: 'url(meow.jpg)'}}), 'should have changed the background url');
assert.ok(wrapper.is({src: 'meow.jpg'}), 'should have changed the src');
});
});