Skip to content

Commit

Permalink
Pangolin-3075: Avatar component update (#2621)
Browse files Browse the repository at this point in the history
* feat(definition.yaml): added font sizes for avatar

* feat(avatar): added function to specify font size per char length

* feat(avatar): adding logic and colors for avatar bg

* chore(tokens): ran node script for design tokens

* feat(avatar): added icon as prop for avatar

* feat(avatar): added logic for icon

* test(avatar): added new props and test

* feat(storybook): added new props to storybook

* test(vrt): added vrt tests for avatar component

* chore(docs): added documentation for avatar component

* chore(docs): fixed storybook docs for avatar

* chore(docs): added logic for icon prop in storybook

* fix(vrt): small spelling error

* chore(package.json): added lodash to deps

* chore(avatar): added css for icon scalability

* chore(changeset): added changeset

* Avatar styles refactor proposal (#2628)

* refactor(avatar): refactor styles management

* fix(avatar): fix sizes and vrts

* chore(avatar.styles): accent color update per figma feedback

* chore(avatar.styles): removed comments

---------

Co-authored-by: Carlos Cortizas <[email protected]>
  • Loading branch information
kterry1 and CarlosCortizasCT authored Oct 20, 2023
1 parent e008a64 commit fbd12e2
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 111 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-bananas-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-uikit/avatar': minor
'@commercetools-uikit/design-system': minor
---

Added color and icon props for `Avatar` component as well as updated font size/weight/color
8 changes: 2 additions & 6 deletions design-system/materials/custom-properties.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
--color-primary: hsl(175, 55%, 45%);
--color-primary-20: hsl(175, 55%, 20%);
--color-primary-25: hsl(175, 55%, 25%);
--color-primary-30: hsl(175, 55%, 33%);
--color-primary-30: hsl(175, 55%, 30%);
--color-primary-40: hsl(175, 55%, 40%);
--color-primary-85: hsl(175, 70%, 85%);
--color-primary-95: hsl(175, 90%, 95%);
Expand Down Expand Up @@ -97,6 +97,7 @@
--font-size-60: 1.5rem;
--font-size-70: 2rem;
--font-size-80: 2.5rem;
--font-size-90: 3rem;
--font-size-15: 0.9231rem;
--font-size-35: 1.0769rem;
--font-size-45: 1.2308rem;
Expand Down Expand Up @@ -217,8 +218,6 @@
--background-color-for-tag-when-hovered: hsl(232, 18%, 90%);
--background-color-for-collapsible-panel-header-icon-when-disabled: #fff;
--background-color-for-select-input-option-when-hovered: hsl(232, 18%, 98%);
--background-color-for-avatar: #213c45;
--background-color-for-avatar-when-highlighted: hsl(195, 35.2941176471%, 30%);
--background-color-for-stamp-as-positive: hsl(175, 90%, 95%);
--background-color-for-localized-input-label: #fff;
--background-color-for-localized-input-label-when-readonly: hsl(
Expand Down Expand Up @@ -512,9 +511,6 @@
--font-size-for-stamp: 0.875rem;
--font-size-for-view-switcher: 0.875rem;
--font-size-for-table: 0.875rem;
--font-size-for-avatar-as-small: 0.875rem;
--font-size-for-avatar-as-medium: 1rem;
--font-size-for-avatar-as-big: 2.5rem;
--font-size-for-localized-input-label: 1rem;
--font-size-for-content-notification: 1rem;
--font-size-for-select-input-tag: 1rem;
Expand Down
8 changes: 2 additions & 6 deletions design-system/materials/custom-properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"--color-primary": "hsl(175, 55%, 45%)",
"--color-primary-20": "hsl(175, 55%, 20%)",
"--color-primary-25": "hsl(175, 55%, 25%)",
"--color-primary-30": "hsl(175, 55%, 33%)",
"--color-primary-30": "hsl(175, 55%, 30%)",
"--color-primary-40": "hsl(175, 55%, 40%)",
"--color-primary-85": "hsl(175, 70%, 85%)",
"--color-primary-95": "hsl(175, 90%, 95%)",
Expand Down Expand Up @@ -90,6 +90,7 @@
"--font-size-60": "1.5rem",
"--font-size-70": "2rem",
"--font-size-80": "2.5rem",
"--font-size-90": "3rem",
"--font-size-15": "0.9231rem",
"--font-size-35": "1.0769rem",
"--font-size-45": "1.2308rem",
Expand Down Expand Up @@ -193,8 +194,6 @@
"--background-color-for-tag-when-hovered": "hsl(232, 18%, 90%)",
"--background-color-for-collapsible-panel-header-icon-when-disabled": "#fff",
"--background-color-for-select-input-option-when-hovered": "hsl(232, 18%, 98%)",
"--background-color-for-avatar": "#213c45",
"--background-color-for-avatar-when-highlighted": "hsl(195, 35.2941176471%, 30%)",
"--background-color-for-stamp-as-positive": "hsl(175, 90%, 95%)",
"--background-color-for-localized-input-label": "#fff",
"--background-color-for-localized-input-label-when-readonly": "hsl(232, 18%, 95%)",
Expand Down Expand Up @@ -402,9 +401,6 @@
"--font-size-for-stamp": "0.875rem",
"--font-size-for-view-switcher": "0.875rem",
"--font-size-for-table": "0.875rem",
"--font-size-for-avatar-as-small": "0.875rem",
"--font-size-for-avatar-as-medium": "1rem",
"--font-size-for-avatar-as-big": "2.5rem",
"--font-size-for-localized-input-label": "1rem",
"--font-size-for-content-notification": "1rem",
"--font-size-for-select-input-tag": "1rem",
Expand Down
11 changes: 1 addition & 10 deletions design-system/materials/internals/definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ choiceGroupsByTheme:
font-size-60: '1.5rem'
font-size-70: '2rem'
font-size-80: '2.5rem'
font-size-90: '3rem'
# These font sizes are in rems based on a root font-size of 13px and they will be removed when we replace the default theme.
font-size-15: '0.9231rem'
font-size-35: '1.0769rem'
Expand Down Expand Up @@ -559,10 +560,6 @@ decisionGroupsByTheme:
choice: color-surface
background-color-for-select-input-option-when-hovered:
choice: color-neutral-98
background-color-for-avatar:
choice: color-accent
background-color-for-avatar-when-highlighted:
choice: color-accent-30
background-color-for-stamp-as-positive:
choice: color-primary-95
background-color-for-localized-input-label:
Expand Down Expand Up @@ -1033,12 +1030,6 @@ decisionGroupsByTheme:
choice: font-size-20
font-size-for-table:
choice: font-size-20
font-size-for-avatar-as-small:
choice: font-size-20
font-size-for-avatar-as-medium:
choice: font-size-30
font-size-for-avatar-as-big:
choice: font-size-80
font-size-for-localized-input-label:
choice: font-size-30
font-size-for-content-notification:
Expand Down
17 changes: 4 additions & 13 deletions design-system/src/design-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const themes = {
colorPrimary: 'hsl(175, 55%, 45%)',
colorPrimary20: 'hsl(175, 55%, 20%)',
colorPrimary25: 'hsl(175, 55%, 25%)',
colorPrimary30: 'hsl(175, 55%, 33%)',
colorPrimary30: 'hsl(175, 55%, 30%)',
colorPrimary40: 'hsl(175, 55%, 40%)',
colorPrimary85: 'hsl(175, 70%, 85%)',
colorPrimary95: 'hsl(175, 90%, 95%)',
Expand Down Expand Up @@ -96,6 +96,7 @@ export const themes = {
fontSize60: '1.5rem',
fontSize70: '2rem',
fontSize80: '2.5rem',
fontSize90: '3rem',
fontSize15: '0.9231rem',
fontSize35: '1.0769rem',
fontSize45: '1.2308rem',
Expand Down Expand Up @@ -206,8 +207,6 @@ export const themes = {
backgroundColorForTagWhenHovered: 'hsl(232, 18%, 90%)',
backgroundColorForCollapsiblePanelHeaderIconWhenDisabled: '#fff',
backgroundColorForSelectInputOptionWhenHovered: 'hsl(232, 18%, 98%)',
backgroundColorForAvatar: '#213c45',
backgroundColorForAvatarWhenHighlighted: 'hsl(195, 35.2941176471%, 30%)',
backgroundColorForStampAsPositive: 'hsl(175, 90%, 95%)',
backgroundColorForLocalizedInputLabel: '#fff',
backgroundColorForLocalizedInputLabelWhenReadonly: 'hsl(232, 18%, 95%)',
Expand Down Expand Up @@ -433,9 +432,6 @@ export const themes = {
fontSizeForStamp: '0.875rem',
fontSizeForViewSwitcher: '0.875rem',
fontSizeForTable: '0.875rem',
fontSizeForAvatarAsSmall: '0.875rem',
fontSizeForAvatarAsMedium: '1rem',
fontSizeForAvatarAsBig: '2.5rem',
fontSizeForLocalizedInputLabel: '1rem',
fontSizeForContentNotification: '1rem',
fontSizeForSelectInputTag: '1rem',
Expand Down Expand Up @@ -566,7 +562,7 @@ const designTokens = {
colorPrimary: 'var(--color-primary, hsl(175, 55%, 45%))',
colorPrimary20: 'var(--color-primary-20, hsl(175, 55%, 20%))',
colorPrimary25: 'var(--color-primary-25, hsl(175, 55%, 25%))',
colorPrimary30: 'var(--color-primary-30, hsl(175, 55%, 33%))',
colorPrimary30: 'var(--color-primary-30, hsl(175, 55%, 30%))',
colorPrimary40: 'var(--color-primary-40, hsl(175, 55%, 40%))',
colorPrimary85: 'var(--color-primary-85, hsl(175, 70%, 85%))',
colorPrimary95: 'var(--color-primary-95, hsl(175, 90%, 95%))',
Expand Down Expand Up @@ -661,6 +657,7 @@ const designTokens = {
fontSize60: 'var(--font-size-60, 1.5rem)',
fontSize70: 'var(--font-size-70, 2rem)',
fontSize80: 'var(--font-size-80, 2.5rem)',
fontSize90: 'var(--font-size-90, 3rem)',
fontSize15: 'var(--font-size-15, 0.9231rem)',
fontSize35: 'var(--font-size-35, 1.0769rem)',
fontSize45: 'var(--font-size-45, 1.2308rem)',
Expand Down Expand Up @@ -802,9 +799,6 @@ const designTokens = {
'var(--background-color-for-collapsible-panel-header-icon-when-disabled, #fff)',
backgroundColorForSelectInputOptionWhenHovered:
'var(--background-color-for-select-input-option-when-hovered, hsl(232, 18%, 98%))',
backgroundColorForAvatar: 'var(--background-color-for-avatar, #213c45)',
backgroundColorForAvatarWhenHighlighted:
'var(--background-color-for-avatar-when-highlighted, hsl(195, 35.2941176471%, 30%))',
backgroundColorForStampAsPositive:
'var(--background-color-for-stamp-as-positive, hsl(175, 90%, 95%))',
backgroundColorForLocalizedInputLabel:
Expand Down Expand Up @@ -1174,9 +1168,6 @@ const designTokens = {
fontSizeForStamp: 'var(--font-size-for-stamp, 0.875rem)',
fontSizeForViewSwitcher: 'var(--font-size-for-view-switcher, 0.875rem)',
fontSizeForTable: 'var(--font-size-for-table, 0.875rem)',
fontSizeForAvatarAsSmall: 'var(--font-size-for-avatar-as-small, 0.875rem)',
fontSizeForAvatarAsMedium: 'var(--font-size-for-avatar-as-medium, 1rem)',
fontSizeForAvatarAsBig: 'var(--font-size-for-avatar-as-big, 2.5rem)',
fontSizeForLocalizedInputLabel:
'var(--font-size-for-localized-input-label, 1rem)',
fontSizeForContentNotification:
Expand Down
17 changes: 10 additions & 7 deletions packages/components/avatar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Example = () => (
firstName="John"
lastName="Doe"
size="s"
color="accent"
/>
);

Expand All @@ -46,10 +47,12 @@ export default Example;

## Properties

| Props | Type | Required | Default | Description |
| --------------- | -------------------------------------------------- | :------: | ------- | --------------------------------------- |
| `firstName` | `string` | | `''` | The first name of the user. |
| `lastName` | `string` | | `''` | The last name of the user. |
| `gravatarHash` | `string` || | The hashed string of the user gravatar. |
| `isHighlighted` | `boolean` | | `false` | Enhances the appearance of the avatar. |
| `size` | `union`<br/>Possible values:<br/>`'s' , 'm' , 'l'` | | `'s'` | The size of the rendered avatar. |
| Props | Type | Required | Default | Description |
| --------------- | ----------------------------------------------------------------------------- | :------: | ---------- | --------------------------------------- |
| `firstName` | `string` | | `''` | The first name of the user. |
| `lastName` | `string` | | `''` | The last name of the user. |
| `gravatarHash` | `string` || | The hashed string of the user gravatar. |
| `isHighlighted` | `boolean` | | `false` | Enhances the appearance of the avatar. |
| `size` | `union`<br/>Possible values:<br/>`'s' , 'm' , 'l'` | | `'s'` | The size of the rendered avatar. |
| `color` | `union`<br/>Possible values:<br/>`'accent' , 'purple' , 'turquoise', 'brown'` | | `'accent'` | The color of the rendered avatar. |
| `icon` | `ReactElement` | | | an `<Icon />` component |
1 change: 1 addition & 0 deletions packages/components/avatar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@commercetools-uikit/utils": "16.7.5",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"lodash": "4.17.21",
"prop-types": "15.8.1"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion packages/components/avatar/src/avatar.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import Avatar from './avatar';
import { render } from '../../../../test/test-utils';
import { PlusBoldIcon } from '@commercetools-uikit/icons';
import { render, screen } from '../../../../test/test-utils';

const createTestProps = (customProps) => ({
firstName: '',
lastName: '',
gravatarHash: '20c9c1b252b46ab49d6f7a4cee9c3e68',
isHighlighted: false,
size: 'l',
color: 'accent',
icon: <PlusBoldIcon data-testid="icon" />,
...customProps,
});

Expand All @@ -17,4 +20,8 @@ describe('Avatar', () => {

expect(container.querySelector("[data-foo='bar']")).toBeInTheDocument();
});
it('should render icon', () => {
render(<Avatar {...props} />);
expect(screen.getByTestId('icon')).toBeInTheDocument();
});
});
14 changes: 14 additions & 0 deletions packages/components/avatar/src/avatar.story.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { createElement } from 'react';
import { storiesOf } from '@storybook/react';
import { withKnobs, text, boolean, select } from '@storybook/addon-knobs/react';
import Section from '../../../../docs/.storybook/decorators/section';
import * as icons from '../../icons';
import Avatar from './avatar';
import Readme from '../README.md';

const iconNames = [null, ...Object.keys(icons)];
const selectedIcon = () => {
const icon = select('icon', iconNames, iconNames[0]);
return icon && createElement(icons[select('icon', iconNames)]);
};

storiesOf('Components|Avatar', module)
.addDecorator(withKnobs)
.addParameters({
Expand All @@ -20,6 +28,12 @@ storiesOf('Components|Avatar', module)
gravatarHash={text('gravatarHash', '')}
isHighlighted={boolean('isHighlighted', false)}
size={select('size', ['s', 'm', 'l'], 'l')}
icon={selectedIcon()}
color={select(
'color',
['accent', 'purple', 'turquoise', 'brown'],
'accent'
)}
/>
</Section>
));
93 changes: 93 additions & 0 deletions packages/components/avatar/src/avatar.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { designTokens } from '@commercetools-uikit/design-system';
import { css } from '@emotion/react';
import startCase from 'lodash/startCase';
import { TAvatarProps } from './avatar';

const fontSizeMap = {
s: {
narrow: designTokens.fontSize30,
wide: designTokens.fontSize10,
},
m: {
narrow: designTokens.fontSize50,
wide: designTokens.fontSize30,
},
l: {
narrow: designTokens.fontSize90,
wide: designTokens.fontSize80,
},
};

const widthSizeMap = {
s: '32px',
m: '40px',
l: '100px',
};

const colorsMap = {
accent: {
normal: designTokens.colorAccent90,
highlighted: designTokens.colorAccent95,
},
purple: {
normal: designTokens.colorPurple90,
highlighted: designTokens.colorPurple95,
},
turquoise: {
normal: designTokens.colorTurquoise90,
highlighted: designTokens.colorTurquoise95,
},
brown: {
normal: designTokens.colorBrown90,
highlighted: designTokens.colorBrown95,
},
};

export const getFontSize = (
avatarInitials: string,
avatarSize: TAvatarProps['size']
) => {
const widthCode = avatarInitials.length > 1 ? 'wide' : 'narrow';
return fontSizeMap[avatarSize][widthCode];
};

export const getWidthSize = (avatarSize: TAvatarProps['size']) =>
widthSizeMap[avatarSize];

export const getBackgroundColor = (
avatarColor: TAvatarProps['color'] = 'accent',
isHighlighted: TAvatarProps['isHighlighted']
) => colorsMap[avatarColor][isHighlighted ? 'highlighted' : 'normal'];

export const getForegroundColor = (avatarColor: TAvatarProps['color']) => {
return avatarColor === 'accent'
? designTokens.colorAccent40
: designTokens[
`color${startCase(avatarColor)}50` as keyof typeof designTokens
];
};

export const getAvatarStyles = (avatarProps: TAvatarProps) => {
const avatarSize = getWidthSize(avatarProps.size);
const backgroundColor = getBackgroundColor(
avatarProps.color,
avatarProps.isHighlighted
);
const foregroundColor = getForegroundColor(avatarProps.color);
return css`
align-items: center;
background-color: ${backgroundColor};
border-radius: 100%;
font-size: ${designTokens.fontSizeDefault};
font-weight: ${designTokens.fontWeight600};
color: ${foregroundColor};
display: flex;
justify-content: center;
overflow: hidden;
position: relative;
fill: ${foregroundColor};
height: ${avatarSize};
width: ${avatarSize};
`;
};
Loading

1 comment on commit fbd12e2

@vercel
Copy link

@vercel vercel bot commented on fbd12e2 Oct 20, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.