Skip to content

Commit f86eb30

Browse files
committed
refactor: More aria-hidden to AvatarImage
1 parent 1afc875 commit f86eb30

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/avatar/avatar.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,24 @@ describe('Avatar', () => {
267267

268268
expect(screen.queryByRole('img')).not.toBeInTheDocument()
269269
expect(screen.getByAltText('')).toHaveAttribute('src', 'avatar.png')
270-
expect(screen.getByTestId('avatar')).toHaveAttribute('aria-hidden', 'true')
270+
expect(screen.getByAltText('')).toHaveAttribute('aria-hidden', 'true')
271+
expect(screen.getByTestId('avatar')).not.toHaveAttribute('aria-hidden')
271272
})
272273

273274
it('supports decorative image avatars with empty alt text', () => {
274275
render(<Avatar size={36} name="Jane Doe" image="avatar.png" alt="" />)
275276

276277
expect(screen.queryByRole('img')).not.toBeInTheDocument()
277278
expect(screen.getByAltText('')).toHaveAttribute('src', 'avatar.png')
279+
expect(screen.getByAltText('')).toHaveAttribute('aria-hidden', 'true')
278280
})
279281

280282
it('supports decorative initials avatars with empty alt text', () => {
281283
render(<Avatar data-testid="avatar" size={36} name="Jane Doe" alt="" />)
282284

283285
expect(screen.queryByRole('img')).not.toBeInTheDocument()
284-
expect(screen.getByTestId('avatar')).toHaveAttribute('aria-hidden', 'true')
286+
expect(screen.getByText('JD')).toHaveAttribute('aria-hidden', 'true')
287+
expect(screen.getByTestId('avatar')).not.toHaveAttribute('aria-hidden')
285288
expect(screen.getByTestId('avatar')).toHaveTextContent('JD')
286289
})
287290

src/avatar/avatar.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const Avatar = polymorphicComponent<'div', AvatarOwnProps, 'omitClassName'>(func
100100
ref,
101101
) {
102102
const label = getAvatarLabel({ alt, name, 'aria-label': ariaLabel })
103-
const isDecorative = ariaHidden || label === ''
103+
const isDecorative = Boolean(ariaHidden ?? label === '')
104104

105105
return (
106106
<Box
@@ -113,7 +113,6 @@ const Avatar = polymorphicComponent<'div', AvatarOwnProps, 'omitClassName'>(func
113113
exceptionallySetClassName,
114114
)}
115115
data-testid={testId}
116-
aria-hidden={isDecorative || undefined}
117116
display="inlineFlex"
118117
alignItems="center"
119118
justifyContent="center"
@@ -130,6 +129,7 @@ const Avatar = polymorphicComponent<'div', AvatarOwnProps, 'omitClassName'>(func
130129
name={name}
131130
image={image}
132131
label={label}
132+
aria-hidden={isDecorative}
133133
/>
134134
</Box>
135135
)
@@ -148,9 +148,10 @@ type AvatarImageProps = {
148148
name?: string
149149
image?: AvatarImageProp
150150
label?: string
151+
'aria-hidden'?: boolean
151152
}
152153

153-
function AvatarImage({ size, name, image, label }: AvatarImageProps) {
154+
function AvatarImage({ size, name, image, label, 'aria-hidden': ariaHidden }: AvatarImageProps) {
154155
const imageSources = getSources(image, size)
155156
const [failedImageSources, setFailedImageSources] = React.useState<string[]>([])
156157
const availableImageSources = getAvailableImageSources(imageSources, failedImageSources)
@@ -165,6 +166,7 @@ function AvatarImage({ size, name, image, label }: AvatarImageProps) {
165166
srcSet={availableImageSources.srcSet}
166167
sizes={availableImageSources.sizes}
167168
alt={label}
169+
aria-hidden={ariaHidden}
168170
onError={(event) => {
169171
const failedSource = getFailedImageSource(
170172
availableImageSources,
@@ -189,6 +191,7 @@ function AvatarImage({ size, name, image, label }: AvatarImageProps) {
189191
)}
190192
role={label ? 'img' : undefined}
191193
aria-label={label}
194+
aria-hidden={ariaHidden}
192195
>
193196
{initials}
194197
</div>

0 commit comments

Comments
 (0)