Skip to content

Commit 3a83a04

Browse files
authored
fix avatar label sizes (#2548)
1 parent 030e1a0 commit 3a83a04

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

demo/src/screens/__tests__/__snapshots__/AvatarScreen.spec.js.snap

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,10 @@ exports[`AvatarScreen renders screen 1`] = `
456456
undefined,
457457
[
458458
{
459-
"fontSize": 16,
459+
"fontFamily": "System",
460+
"fontSize": 14,
461+
"fontWeight": "700",
462+
"lineHeight": 20,
460463
},
461464
{
462465
"backgroundColor": "transparent",
@@ -678,7 +681,10 @@ exports[`AvatarScreen renders screen 1`] = `
678681
undefined,
679682
[
680683
{
681-
"fontSize": 16,
684+
"fontFamily": "System",
685+
"fontSize": 14,
686+
"fontWeight": "700",
687+
"lineHeight": 20,
682688
},
683689
{
684690
"backgroundColor": "transparent",
@@ -2344,7 +2350,10 @@ exports[`AvatarScreen renders screen 1`] = `
23442350
undefined,
23452351
[
23462352
{
2347-
"fontSize": 16,
2353+
"fontFamily": "System",
2354+
"fontSize": 14,
2355+
"fontWeight": "700",
2356+
"lineHeight": 20,
23482357
},
23492358
{
23502359
"backgroundColor": "transparent",
@@ -2567,7 +2576,10 @@ exports[`AvatarScreen renders screen 1`] = `
25672576
undefined,
25682577
[
25692578
{
2570-
"fontSize": 16,
2579+
"fontFamily": "System",
2580+
"fontSize": 14,
2581+
"fontWeight": "700",
2582+
"lineHeight": 20,
25712583
},
25722584
{
25732585
"backgroundColor": "transparent",
@@ -2969,7 +2981,10 @@ exports[`AvatarScreen renders screen 1`] = `
29692981
undefined,
29702982
[
29712983
{
2972-
"fontSize": 16,
2984+
"fontFamily": "System",
2985+
"fontSize": 14,
2986+
"fontWeight": "700",
2987+
"lineHeight": 20,
29732988
},
29742989
{
29752990
"backgroundColor": "transparent",

src/components/avatar/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,8 @@ const Avatar = forwardRef<any, AvatarProps>((props: AvatarProps, ref: React.Forw
262262
}, [_baseContainerStyle, containerStyle]);
263263

264264
const textStyle = useMemo(() => {
265-
const fontSizeToImageSizeRatio = 0.32;
266-
const fontSize = size * fontSizeToImageSizeRatio;
267-
return [{fontSize}, initialsStyle, {color: labelColor}];
265+
const typography = AvatarHelper.getInitialsTypography(size);
266+
return [typography, initialsStyle, {color: labelColor}];
268267
}, [size, initialsStyle, labelColor]);
269268

270269
const textContainerStyle = useMemo(() => {

src/helpers/AvatarHelper.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import _ from 'lodash';
22
import URL from 'url-parse';
33
import Colors from '../style/colors';
4+
import {Typography} from 'style';
5+
6+
export function getInitialsTypography(size: number) {
7+
let typography;
8+
switch (true) {
9+
case size < 24:
10+
typography = {fontSize: 10};
11+
break;
12+
case size < 34:
13+
typography = Typography.text90BO;
14+
break;
15+
case size < 54:
16+
typography = Typography.text80BO;
17+
break;
18+
case size < 70:
19+
typography = Typography.text60BO;
20+
break;
21+
case size < 100:
22+
typography = Typography.text50H;
23+
break;
24+
default:
25+
case size >= 100:
26+
typography = Typography.text40H;
27+
break;
28+
}
29+
return typography;
30+
}
431

532
export function hashStringToNumber(str?: string) {
633
let hash = 5381;

src/helpers/__tests__/AvatarHelper.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import Colors from '../../../src/style/colors';
1+
import {Colors, Typography} from '../../../src/style';
22

33
describe('services/AvatarService', () => {
44
let uut;
55
beforeEach(() => {
66
uut = require('../../../src/helpers/AvatarHelper');
77
});
88

9+
it('should getInitialsTypography', () => {
10+
expect(uut.getInitialsTypography(12)).toEqual({fontSize: 10});
11+
expect(uut.getInitialsTypography(16)).toEqual({fontSize: 10});
12+
expect(uut.getInitialsTypography(20)).toEqual({fontSize: 10});
13+
expect(uut.getInitialsTypography(28)).toEqual(Typography.text90BO);
14+
expect(uut.getInitialsTypography(40)).toEqual(Typography.text80BO);
15+
expect(uut.getInitialsTypography(48)).toEqual(Typography.text80BO);
16+
expect(uut.getInitialsTypography(60)).toEqual(Typography.text60BO);
17+
expect(uut.getInitialsTypography(80)).toEqual(Typography.text50H);
18+
expect(uut.getInitialsTypography(120)).toEqual(Typography.text40H);
19+
expect(uut.getInitialsTypography(200)).toEqual(Typography.text40H);
20+
});
21+
922
it('should getAvatarColors', () => {
1023
const avatarColors = uut.getAvatarColors();
1124
expect(avatarColors).toEqual([

0 commit comments

Comments
 (0)