Skip to content

Commit a3af613

Browse files
fix(Avatar): Adjust icon size in sync with Avatar Size (#317)
* fix(Avatar): icon size changes along with avatar size * fix(Icon): remove deprecated onPress prop Co-authored-by: Marcus Notheis <[email protected]>
1 parent ee37009 commit a3af613

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
lines changed

packages/main/src/components/Avatar/Avatar.jss.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,56 @@ const styles = ({ parameters }: JSSTheme) => ({
3131
// biggest avatar; 7rem
3232
sizeXL: {
3333
...size('7rem'),
34-
fontSize: '2.75rem'
34+
fontSize: '3rem',
35+
'& ui5-icon': {
36+
width: '3rem',
37+
height: '3rem'
38+
}
3539
},
3640
// 5rem
3741
sizeL: {
3842
...size('5rem'),
39-
fontSize: '2rem'
43+
fontSize: '2.25rem',
44+
'& ui5-icon': {
45+
width: '2.25rem',
46+
height: '2.25rem'
47+
}
4048
},
4149
// 4rem
4250
sizeM: {
4351
...size('4rem'),
44-
fontSize: '1.75rem'
52+
fontSize: '1.5rem',
53+
'& ui5-icon': {
54+
width: '1.5rem',
55+
height: '1.5rem'
56+
}
4557
},
4658
// 3rem
4759
sizeS: {
4860
...size('3rem'),
49-
fontSize: '1.125rem'
61+
fontSize: '1.125rem',
62+
'& ui5-icon': {
63+
width: '1.125rem',
64+
height: '1.125rem'
65+
}
5066
},
5167
// 2rem
5268
sizeXS: {
5369
...size('2rem'),
54-
fontSize: '0.75rem'
55-
}
70+
fontSize: '1rem',
71+
'& ui5-icon': {
72+
width: '1rem',
73+
height: '1rem'
74+
}
75+
},
76+
sizeCustom: ({ customFontSize, customDisplaySize }) => ({
77+
...size(customDisplaySize),
78+
fontSize: customFontSize,
79+
'& ui5-icon': {
80+
width: customFontSize,
81+
height: customFontSize
82+
}
83+
})
5684
});
5785

5886
export default styles;

packages/main/src/components/Avatar/Avatar.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ describe('Avatar', () => {
3232
<Avatar size={AvatarSize.Custom} customDisplaySize="5rem" customFontSize="2rem" initials="JD" />
3333
);
3434
const el = wrapper.find(Avatar).getDOMNode() as HTMLElement;
35-
expect(el.style.fontSize).toEqual('2rem');
36-
expect(el.style.width).toEqual('5rem');
37-
expect(el.style.height).toEqual('5rem');
38-
expect(el.style.lineHeight).toEqual('5rem');
35+
const computedStyle = getComputedStyle(el);
36+
expect(computedStyle.fontSize).toEqual('2rem');
37+
expect(computedStyle.width).toEqual('5rem');
38+
expect(computedStyle.height).toEqual('5rem');
39+
expect(computedStyle.lineHeight).toEqual('5rem');
3940
});
4041

4142
test('Custom Size Defined but wrong size prop', () => {

packages/main/src/components/Avatar/__snapshots__/Avatar.test.tsx.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
exports[`Avatar Avatar - size: Custom shape: Circle 1`] = `
44
<span
5-
class="Avatar--avatar- Avatar--circle-"
6-
style="font-size: 1.125rem; width: 3rem; height: 3rem; line-height: 3rem;"
5+
class="Avatar--avatar- Avatar--sizeCustom- Avatar--sizeCustom-d0- Avatar--circle-"
76
tabindex="0"
87
/>
98
`;
109

1110
exports[`Avatar Avatar - size: Custom shape: Square 1`] = `
1211
<span
13-
class="Avatar--avatar-"
14-
style="font-size: 1.125rem; width: 3rem; height: 3rem; line-height: 3rem;"
12+
class="Avatar--avatar- Avatar--sizeCustom- Avatar--sizeCustom-d0-"
1513
tabindex="0"
1614
/>
1715
`;

packages/main/src/components/Avatar/index.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Event } from '@ui5/webcomponents-react-base/lib/Event';
22
import { usePassThroughHtmlProps } from '@ui5/webcomponents-react-base/lib/usePassThroughHtmlProps';
33
import { AvatarShape } from '@ui5/webcomponents-react/lib/AvatarShape';
44
import { AvatarSize } from '@ui5/webcomponents-react/lib/AvatarSize';
5-
import React, { CSSProperties, FC, forwardRef, Ref, useCallback } from 'react';
5+
import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo } from 'react';
66
import { createUseStyles } from 'react-jss';
77
import { CommonProps } from '../../interfaces/CommonProps';
88
import { JSSTheme } from '../../interfaces/JSSTheme';
@@ -39,38 +39,34 @@ const Avatar: FC<AvatarPropTypes> = forwardRef((props: AvatarPropTypes, ref: Ref
3939
tooltip,
4040
slot
4141
} = props;
42+
const classes = useStyles({ customDisplaySize, customFontSize });
4243

43-
const classes = useStyles();
44-
45-
const cssClasses = [classes.avatar];
46-
const inlineStyle: CSSProperties = {};
47-
if (size === AvatarSize.Custom) {
48-
inlineStyle.fontSize = customFontSize;
49-
inlineStyle.width = customDisplaySize;
50-
inlineStyle.height = customDisplaySize;
51-
inlineStyle.lineHeight = customDisplaySize;
52-
} else {
53-
cssClasses.push(classes[`size${size}`]);
54-
}
44+
const cssClasses = [classes.avatar, classes[`size${size}`]];
5545

5646
if (shape === AvatarShape.Circle) {
5747
cssClasses.push(classes.circle);
5848
}
5949

60-
if (image) {
61-
inlineStyle.backgroundImage = `url(${image})`;
62-
}
50+
const inlineStyle = useMemo(() => {
51+
const internalStyle: CSSProperties = {};
52+
if (image) {
53+
internalStyle.backgroundImage = `url(${image})`;
54+
}
6355

64-
if (onClick) {
65-
inlineStyle.cursor = 'pointer';
66-
}
56+
if (onClick) {
57+
internalStyle.cursor = 'pointer';
58+
}
59+
60+
if (style) {
61+
Object.assign(internalStyle, style);
62+
}
63+
64+
return internalStyle;
65+
}, [image, onClick, style]);
6766

6867
if (className) {
6968
cssClasses.push(className);
7069
}
71-
if (style) {
72-
Object.assign(inlineStyle, style);
73-
}
7470

7571
const handleKeyDown = useCallback(
7672
(e) => {

packages/main/src/webComponents/Icon/demo.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
component: Icon
1010
};
1111

12-
export const defaultStory = () => <Icon name={text('src', 'add')} onPress={action('onPress')} />;
12+
export const defaultStory = () => <Icon name={text('src', 'add')} />;
1313

1414
defaultStory.story = {
1515
name: 'Default story'

0 commit comments

Comments
 (0)