diff --git a/.changeset/good-beans-knock.md b/.changeset/good-beans-knock.md new file mode 100644 index 0000000000..91de53c128 --- /dev/null +++ b/.changeset/good-beans-knock.md @@ -0,0 +1,5 @@ +--- +'@backstage/core-components': patch +--- + +Don't set the background color on an Avatar component that has a picture. diff --git a/packages/core-components/src/components/Avatar/Avatar.test.tsx b/packages/core-components/src/components/Avatar/Avatar.test.tsx index 1c08d57679..d7bbf712fe 100644 --- a/packages/core-components/src/components/Avatar/Avatar.test.tsx +++ b/packages/core-components/src/components/Avatar/Avatar.test.tsx @@ -17,6 +17,7 @@ import { render } from '@testing-library/react'; import React from 'react'; import { Avatar } from './Avatar'; +import { stringToColor } from './utils'; describe('', () => { it('renders without exploding', async () => { @@ -24,4 +25,24 @@ describe('', () => { expect(getByText('JD')).toBeInTheDocument(); }); + + it('generates a background color', async () => { + const bgcolor = stringToColor('John Doe'); + const { getByText } = render(); + expect(getByText('JD')).toHaveStyle(`background-color: ${bgcolor}`); + }); + + it('does not generate a background color when a picture is given', async () => { + const bgcolor = stringToColor('John Doe'); + const { getByAltText } = render( + , + ); + + expect(getByAltText('John Doe')).not.toHaveStyle( + `background-color: ${bgcolor}`, + ); + }); }); diff --git a/packages/core-components/src/components/Avatar/Avatar.tsx b/packages/core-components/src/components/Avatar/Avatar.tsx index 304ee574d2..af8dabf554 100644 --- a/packages/core-components/src/components/Avatar/Avatar.tsx +++ b/packages/core-components/src/components/Avatar/Avatar.tsx @@ -67,15 +67,23 @@ export interface AvatarProps { export function Avatar(props: AvatarProps) { const { displayName, picture, customStyles } = props; const classes = useStyles(); + let styles = { ...customStyles }; + // We only calculate the background color if there's not an avatar + // picture. If there is a picture, it might have a transparent + // background and we don't know whether the calculated background + // color will clash. + if (!picture) { + styles = { + backgroundColor: stringToColor(displayName || ''), + ...customStyles, + }; + } return ( {displayName && extractInitials(displayName)}