Skip to content

Commit 7562cbb

Browse files
joshblackCopilot
andauthored
fix(Avatar): merge style when style prop is provided (#5963)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ccd6899 commit 7562cbb

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

.changeset/soft-cases-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Update Avatar component to correctly merge style when style prop is provided

packages/react/src/__tests__/Avatar.test.tsx renamed to packages/react/src/Avatar/Avatar.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import {Avatar} from '..'
33
import theme from '../theme'
44
import {px, render, behavesAsComponent, checkExports} from '../utils/testing'
5-
import {render as HTMLRender} from '@testing-library/react'
5+
import {render as HTMLRender, screen} from '@testing-library/react'
66
import axe from 'axe-core'
77

88
describe('Avatar', () => {
@@ -48,4 +48,21 @@ describe('Avatar', () => {
4848
it('respects margin props', () => {
4949
expect(render(<Avatar src="primer.png" alt="" sx={{m: 2}} />)).toHaveStyleRule('margin', px(theme.space[2]))
5050
})
51+
52+
it('should support the `style` prop without overriding internal styles', () => {
53+
HTMLRender(
54+
<Avatar
55+
data-testid="avatar"
56+
src="primer.png"
57+
style={{
58+
background: 'black',
59+
}}
60+
/>,
61+
)
62+
63+
expect(screen.getByTestId('avatar')).toHaveStyle({
64+
background: 'black',
65+
['--avatarSize-regular']: '20px',
66+
})
67+
})
5168
})

packages/react/src/Avatar/Avatar.tsx

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {clsx} from 'clsx'
22
import React from 'react'
3-
import Box from '../Box'
43
import type {SxProp} from '../sx'
54
import type {ResponsiveValue} from '../hooks/useResponsiveValue'
65
import {isResponsiveValue} from '../hooks/useResponsiveValue'
76
import {defaultSxProp} from '../utils/defaultSxProp'
7+
import {BoxWithFallback} from '../internal/components/BoxWithFallback'
88
import classes from './Avatar.module.css'
99

1010
export const DEFAULT_AVATAR_SIZE = 20
@@ -24,7 +24,7 @@ export type AvatarProps = {
2424
React.ComponentPropsWithoutRef<'img'>
2525

2626
const Avatar = React.forwardRef<HTMLImageElement, AvatarProps>(function Avatar(
27-
{alt = '', size = DEFAULT_AVATAR_SIZE, square = false, sx: sxProp = defaultSxProp, className, ...rest},
27+
{alt = '', size = DEFAULT_AVATAR_SIZE, square = false, sx: sxProp = defaultSxProp, className, style, ...rest},
2828
ref,
2929
) {
3030
const isResponsive = isResponsiveValue(size)
@@ -38,27 +38,9 @@ const Avatar = React.forwardRef<HTMLImageElement, AvatarProps>(function Avatar(
3838
cssSizeVars['--avatarSize-regular'] = `${size}px`
3939
}
4040

41-
if (sxProp !== defaultSxProp) {
42-
return (
43-
<Box
44-
as={'img'}
45-
data-component="Avatar"
46-
className={clsx(className, classes.Avatar)}
47-
ref={ref}
48-
alt={alt}
49-
data-responsive={isResponsive ? '' : undefined}
50-
data-square={square ? '' : undefined}
51-
width={isResponsive ? undefined : size}
52-
height={isResponsive ? undefined : size}
53-
style={cssSizeVars as React.CSSProperties}
54-
sx={sxProp}
55-
{...rest}
56-
/>
57-
)
58-
}
59-
6041
return (
61-
<img
42+
<BoxWithFallback
43+
as="img"
6244
data-component="Avatar"
6345
className={clsx(className, classes.Avatar)}
6446
ref={ref}
@@ -67,7 +49,15 @@ const Avatar = React.forwardRef<HTMLImageElement, AvatarProps>(function Avatar(
6749
data-square={square ? '' : undefined}
6850
width={isResponsive ? undefined : size}
6951
height={isResponsive ? undefined : size}
70-
style={cssSizeVars as React.CSSProperties}
52+
style={
53+
style
54+
? {
55+
...cssSizeVars,
56+
...style,
57+
}
58+
: (cssSizeVars as React.CSSProperties)
59+
}
60+
sx={sxProp}
7161
{...rest}
7262
/>
7363
)

0 commit comments

Comments
 (0)