Skip to content

Commit ad8494f

Browse files
ArnonZethanshar
authored andcommitted
Bug/Avatar badge position (#370)
* Fixed badge position * Refactor
1 parent 9beee67 commit ad8494f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/components/avatar/index.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import Text from '../text';
1010
import Image from '../image';
1111
import AnimatedImage from '../../animations/animatedImage';
1212

13-
1413
export const STATUS_MODES = {
1514
ONLINE: 'ONLINE',
1615
OFFLINE: 'OFFLINE',
1716
AWAY: 'AWAY',
18-
NONE: 'NONE'
17+
NONE: 'NONE',
1918
};
2019

2120
/**
@@ -105,15 +104,15 @@ export default class Avatar extends BaseComponent {
105104
/**
106105
* Press handler
107106
*/
108-
onPress: PropTypes.func
107+
onPress: PropTypes.func,
109108
};
110109

111110
static defaultProps = {
112111
animate: false,
113112
backgroundColor: Colors.dark80,
114113
size: 50,
115114
labelColor: Colors.dark10,
116-
status: STATUS_MODES.NONE
115+
status: STATUS_MODES.NONE,
117116
};
118117

119118
generateStyles() {
@@ -141,14 +140,24 @@ export default class Avatar extends BaseComponent {
141140
return badgeColor;
142141
}
143142

143+
getBadgePosition() {
144+
const {size} = this.props;
145+
const badgeSize = 13.5;
146+
const radius = size / 2;
147+
const x = Math.sqrt(radius ** 2 * 2);
148+
const y = x - radius;
149+
const shift = Math.sqrt(y ** 2 / 2) - badgeSize / 2;
150+
return {top: shift, right: shift};
151+
}
152+
144153
renderBadge() {
145154
const {testID, isOnline, status} = this.props;
146155
const badgeColor = this.getBadgeColor(isOnline, status);
147156
if (badgeColor === null) {
148157
return false;
149158
}
150159
return (
151-
<View style={this.styles.onlineBadge} testID={`${testID}.onlineBadge`}>
160+
<View style={[this.styles.onlineBadge, this.getBadgePosition()]} testID={`${testID}.onlineBadge`}>
152161
<View style={[this.styles.onlineBadgeInner, {backgroundColor: badgeColor}]} />
153162
</View>
154163
);
@@ -176,7 +185,7 @@ export default class Avatar extends BaseComponent {
176185
onImageLoadError,
177186
testID,
178187
imageProps,
179-
imageStyle
188+
imageStyle,
180189
} = this.props;
181190
const hasImage = !_.isUndefined(imageSource);
182191
const ImageContainer = animate ? AnimatedImage : Image;
@@ -205,7 +214,9 @@ export default class Avatar extends BaseComponent {
205214

206215
return (
207216
<Container style={[this.styles.container, containerStyle]} testID={testID} onPress={onPress}>
208-
<View style={[this.styles.initialsContainer, {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}>
217+
<View
218+
style={[this.styles.initialsContainer, {backgroundColor}, hasImage && this.styles.initialsContainerWithInset]}
219+
>
209220
<Text numberOfLines={1} style={[this.styles.initials, {color}]}>
210221
{label}
211222
</Text>
@@ -218,7 +229,7 @@ export default class Avatar extends BaseComponent {
218229
}
219230
}
220231

221-
function createStyles({size, labelColor, imageSource}) {
232+
function createStyles({size, labelColor}) {
222233
const borderRadius = size / 2;
223234
const fontSizeToImageSizeRatio = 0.32;
224235
const styles = StyleSheet.create({
@@ -227,38 +238,38 @@ function createStyles({size, labelColor, imageSource}) {
227238
justifyContent: 'center',
228239
width: size,
229240
height: size,
230-
borderRadius
241+
borderRadius,
231242
},
232243
initialsContainer: {
233244
...StyleSheet.absoluteFillObject,
234245
alignItems: 'center',
235246
justifyContent: 'center',
236-
borderRadius
247+
borderRadius,
237248
},
238249
initialsContainerWithInset: {
239250
top: 1,
240251
right: 1,
241252
bottom: 1,
242-
left: 1
253+
left: 1,
243254
},
244255
/*eslint-disable*/
245256
initials: {
246257
fontSize: size * fontSizeToImageSizeRatio,
247258
color: labelColor,
248-
backgroundColor: 'transparent'
259+
backgroundColor: 'transparent',
249260
},
250261
/*eslint-enable*/
251262
defaultImage: {
252263
width: size,
253264
height: size,
254-
borderRadius
265+
borderRadius,
255266
},
256267
image: {
257268
...StyleSheet.absoluteFillObject,
258269
position: 'absolute',
259270
width: size,
260271
height: size,
261-
borderRadius
272+
borderRadius,
262273
},
263274
onlineBadge: {
264275
height: 13.5,
@@ -267,18 +278,16 @@ function createStyles({size, labelColor, imageSource}) {
267278
borderRadius: 999,
268279
backgroundColor: Colors.white,
269280
position: 'absolute',
270-
right: imageSource ? -1.5 : 0,
271-
top: 4.5
272281
},
273282
onlineBadgeInner: {
274283
flex: 1,
275-
borderRadius: 999
284+
borderRadius: 999,
276285
// backgroundColor: Colors.green30,
277286
},
278287
fixAbsolutePosition: {
279288
position: undefined,
280289
left: undefined,
281-
bottom: undefined
290+
bottom: undefined,
282291
},
283292
ribbon: {
284293
position: 'absolute',
@@ -287,8 +296,8 @@ function createStyles({size, labelColor, imageSource}) {
287296
backgroundColor: Colors.blue30,
288297
borderRadius: BorderRadiuses.br100,
289298
paddingHorizontal: 6,
290-
paddingVertical: 3
291-
}
299+
paddingVertical: 3,
300+
},
292301
});
293302

294303
return styles;

0 commit comments

Comments
 (0)