Skip to content

Bug/Avatar badge position #370

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 2 commits into from
Feb 12, 2019
Merged
Changes from 1 commit
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
48 changes: 28 additions & 20 deletions src/components/avatar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Text from '../text';
import Image from '../image';
import AnimatedImage from '../../animations/animatedImage';


export const STATUS_MODES = {
ONLINE: 'ONLINE',
OFFLINE: 'OFFLINE',
AWAY: 'AWAY',
NONE: 'NONE'
NONE: 'NONE',
};

/**
Expand Down Expand Up @@ -105,15 +104,15 @@ export default class Avatar extends BaseComponent {
/**
* Press handler
*/
onPress: PropTypes.func
onPress: PropTypes.func,
};

static defaultProps = {
animate: false,
backgroundColor: Colors.dark80,
size: 50,
labelColor: Colors.dark10,
status: STATUS_MODES.NONE
status: STATUS_MODES.NONE,
};

generateStyles() {
Expand Down Expand Up @@ -141,14 +140,23 @@ export default class Avatar extends BaseComponent {
return badgeColor;
}

getBadgePosition() {
const {size} = this.props;
const radius = size / 2;
const x = Math.sqrt(radius ** 2 * 2);
const y = x - radius;
const shift = Math.sqrt(y ** 2 / 2) - 13.5 / 2; // 13.5 = hard coded badge size
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the size of the pimple to a const at the top of the file and reuse it.
this way you don't need to add comments explaining what you're doing...

return {top: shift, right: shift};
}

renderBadge() {
const {testID, isOnline, status} = this.props;
const badgeColor = this.getBadgeColor(isOnline, status);
if (badgeColor === null) {
return false;
}
return (
<View style={this.styles.onlineBadge} testID={`${testID}.onlineBadge`}>
<View style={[this.styles.onlineBadge, this.getBadgePosition()]} testID={`${testID}.onlineBadge`}>
<View style={[this.styles.onlineBadgeInner, {backgroundColor: badgeColor}]} />
</View>
);
Expand Down Expand Up @@ -176,7 +184,7 @@ export default class Avatar extends BaseComponent {
onImageLoadError,
testID,
imageProps,
imageStyle
imageStyle,
} = this.props;
const hasImage = !_.isUndefined(imageSource);
const ImageContainer = animate ? AnimatedImage : Image;
Expand Down Expand Up @@ -205,7 +213,9 @@ export default class Avatar extends BaseComponent {

return (
<Container style={[this.styles.container, containerStyle]} testID={testID} onPress={onPress}>
<View style={[this.styles.initialsContainer, {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}>
<View
style={[this.styles.initialsContainer, {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}
>
<Text numberOfLines={1} style={[this.styles.initials, {color}]}>
{label}
</Text>
Expand All @@ -218,7 +228,7 @@ export default class Avatar extends BaseComponent {
}
}

function createStyles({size, labelColor, imageSource}) {
function createStyles({size, labelColor}) {
const borderRadius = size / 2;
const fontSizeToImageSizeRatio = 0.32;
const styles = StyleSheet.create({
Expand All @@ -227,38 +237,38 @@ function createStyles({size, labelColor, imageSource}) {
justifyContent: 'center',
width: size,
height: size,
borderRadius
borderRadius,
},
initialsContainer: {
...StyleSheet.absoluteFillObject,
alignItems: 'center',
justifyContent: 'center',
borderRadius
borderRadius,
},
initialsContainerWithInset: {
top: 1,
right: 1,
bottom: 1,
left: 1
left: 1,
},
/*eslint-disable*/
initials: {
fontSize: size * fontSizeToImageSizeRatio,
color: labelColor,
backgroundColor: 'transparent'
backgroundColor: 'transparent',
},
/*eslint-enable*/
defaultImage: {
width: size,
height: size,
borderRadius
borderRadius,
},
image: {
...StyleSheet.absoluteFillObject,
position: 'absolute',
width: size,
height: size,
borderRadius
borderRadius,
},
onlineBadge: {
height: 13.5,
Expand All @@ -267,18 +277,16 @@ function createStyles({size, labelColor, imageSource}) {
borderRadius: 999,
backgroundColor: Colors.white,
position: 'absolute',
right: imageSource ? -1.5 : 0,
top: 4.5
},
onlineBadgeInner: {
flex: 1,
borderRadius: 999
borderRadius: 999,
// backgroundColor: Colors.green30,
},
fixAbsolutePosition: {
position: undefined,
left: undefined,
bottom: undefined
bottom: undefined,
},
ribbon: {
position: 'absolute',
Expand All @@ -287,8 +295,8 @@ function createStyles({size, labelColor, imageSource}) {
backgroundColor: Colors.blue30,
borderRadius: BorderRadiuses.br100,
paddingHorizontal: 6,
paddingVertical: 3
}
paddingVertical: 3,
},
});

return styles;
Expand Down