Skip to content
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

Avatar background color handling based on initials #753

Merged
merged 4 commits into from
Aug 30, 2023

Conversation

sgraczyk
Copy link

@sgraczyk sgraczyk commented Aug 29, 2023

Resolves: Slack thread

Description

It was decided that background color of the Avatar should have a default value if it uses text type. It uses a basic algorithm that makes sure that a set of 10 colors are used, and for the exact same initials the color will be always the same.

❗️ Attention: This change brings some sort of breaking change as it will apply new color in for avatars using text type without color provided. By default it was grey.

I've also enabled the possibility of using CSS variable for color prop as earlier it was impossible due to contrast calculation requiring direct values. It was solved by using window.getComputedStyle

image

Storybook

https://feature-avatar-default-backgrounds--613a8e945a5665003a05113b.chromatic.com/?path=/story/components-avatar--colors

Checklist

Obligatory:

  • Self review (use this as your final check for proposed changes before requesting the review)
  • Add reviewers (livechat/design-system)
  • Add correct label
  • Assign pull request with the correct issue

@sgraczyk sgraczyk added feature New feature or request WIP Work in progress labels Aug 29, 2023
@sgraczyk sgraczyk requested a review from a team August 29, 2023 18:44
@sgraczyk sgraczyk removed the WIP Work in progress label Aug 30, 2023
@sgraczyk sgraczyk requested a review from vladko-uxds August 30, 2023 07:54
@sgraczyk sgraczyk changed the title Add Avatar background color handling based on initials Avatar background color handling based on initials Aug 30, 2023
@sgraczyk sgraczyk merged commit 801e638 into main Aug 30, 2023
@sgraczyk sgraczyk deleted the feature/avatar-default-backgrounds branch August 30, 2023 11:24
});

describe('getInitials', () => {
it('should return initials for a full name', () => {
Copy link
Member

Choose a reason for hiding this comment

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

It used to be possible to set emoji as customer name, consider checking if such unusual input is handled

Copy link
Author

Choose a reason for hiding this comment

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

I think it's not handled in here. I believe there is such an implementation in LiveChat Web

export function getInitials(name = ''): string {
  const isEmojiInName = EMOJI_REGEX.test(name);
  const finalName = name ? name : '';
  let initials = '';

  initials = finalName
    .split(' ')
    .map((el) => el.charAt(0))
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    .filter((el) => isAlphaNumeric(removeDiacritics(el)))
    .join('')
    .substring(0, 2)
    .toUpperCase();

  // fallback for emoji-only and non-alpha-numeric characters
  if (!initials) {
    initials = isEmojiInName
      ? finalName.match(EMOJI_REGEX)?.[0] || ''
      : finalName.toLocaleUpperCase().trim().substring(0, 2);
  }

  return initials;
}

@vladko-uxds Do we want to support emojis somehow in Design System?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants