Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-avatars-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add text placeholder support to Avatar when no image source is provided.
11 changes: 11 additions & 0 deletions packages/react/src/Avatar/Avatar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
/* stylelint-disable-next-line primer/box-shadow */
box-shadow: 0 0 0 1px var(--avatar-borderColor);

&:where([data-placeholder]) {
display: inline-flex;
align-items: center;
justify-content: center;
background: var(--bgColor-muted);
color: var(--fgColor-muted);
font-size: calc(var(--avatarSize-regular) * 0.4);
font-weight: var(--base-text-weight-semibold);
white-space: nowrap;
}

&:where([data-square]) {
/* stylelint-disable-next-line primer/borders */
border-radius: clamp(4px, calc(var(--avatarSize-regular) - 24px), var(--borderRadius-medium));
Expand Down
25 changes: 25 additions & 0 deletions packages/react/src/Avatar/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ describe('Avatar', () => {
expect(avatar).toHaveAttribute('src', 'primer.png')
})

it('renders a text placeholder when src is omitted', () => {
render(<Avatar placeholder="NS" size={40} data-testid="avatar" />)
const avatar = screen.getByTestId('avatar')
expect(avatar).toHaveTextContent('NS')
expect(avatar).toHaveAttribute('role', 'img')
expect(avatar).toHaveAttribute('aria-label', 'NS')
expect(avatar).toHaveAttribute('data-placeholder')
expect(avatar).toHaveAttribute('data-component', 'Avatar')
expect(avatar).not.toHaveAttribute('src')
})

it('prefers the image when both src and placeholder are provided', () => {
render(<Avatar src="primer.png" placeholder="NS" alt="Naveed" data-testid="avatar" />)
const avatar = screen.getByTestId('avatar')
expect(avatar.tagName).toBe('IMG')
expect(avatar).toHaveAttribute('src', 'primer.png')
expect(avatar).toHaveAttribute('alt', 'Naveed')
expect(avatar).not.toHaveAttribute('data-placeholder')
})

it('uses alt text as the accessible name for a placeholder when provided', () => {
render(<Avatar placeholder="NS" alt="Naveed Sajjad" data-testid="avatar" />)
expect(screen.getByTestId('avatar')).toHaveAttribute('aria-label', 'Naveed Sajjad')
})

it('should support the `style` prop without overriding internal styles', () => {
render(
<Avatar
Expand Down
67 changes: 43 additions & 24 deletions packages/react/src/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ export type AvatarProps = {
size?: number | ResponsiveValue<number>
/** Sets the shape of the avatar to a square if true. If false, the avatar will be circular. */
square?: boolean
/** URL of the avatar image. */
src: string
/** URL of the avatar image. When provided, the image takes precedence over the placeholder. */
src?: string
/** Text to display when no image URL is provided, such as a user's initials. */
placeholder?: string
/** Provide alt text when the Avatar is used without the user's name next to it. */
alt?: string
/** Additional class name. */
className?: string
} & React.ComponentPropsWithoutRef<'img'>
} & Omit<React.ComponentPropsWithoutRef<'img'>, 'src'>

const Avatar = React.forwardRef<HTMLImageElement, AvatarProps>(function Avatar(
{alt = '', size = DEFAULT_AVATAR_SIZE, square = false, className, style, ...rest},
const Avatar = React.forwardRef<HTMLElement, AvatarProps>(function Avatar(
{alt = '', size = DEFAULT_AVATAR_SIZE, square = false, placeholder, src, className, style, ...rest},
ref,
) {
const isResponsive = isResponsiveValue(size)
Expand All @@ -34,26 +36,43 @@ const Avatar = React.forwardRef<HTMLImageElement, AvatarProps>(function Avatar(
cssSizeVars['--avatarSize-regular'] = `${size}px`
}

const sharedProps = {
'data-component': 'Avatar',
className: clsx(className, classes.Avatar),
'data-responsive': isResponsive ? '' : undefined,
'data-square': square ? '' : undefined,
style: style
? {
...cssSizeVars,
...style,
}
: (cssSizeVars as React.CSSProperties),
...rest,
}

if (src) {
return (
<img
{...sharedProps}
ref={ref as React.Ref<HTMLImageElement>}
alt={alt}
width={isResponsive ? undefined : size}
height={isResponsive ? undefined : size}
src={src}
/>
)
}

return (
<img
data-component="Avatar"
className={clsx(className, classes.Avatar)}
ref={ref}
alt={alt}
data-responsive={isResponsive ? '' : undefined}
data-square={square ? '' : undefined}
width={isResponsive ? undefined : size}
height={isResponsive ? undefined : size}
style={
style
? {
...cssSizeVars,
...style,
}
: (cssSizeVars as React.CSSProperties)
}
{...rest}
/>
<span
{...sharedProps}
ref={ref as React.Ref<HTMLSpanElement>}
role="img"
aria-label={alt || placeholder || undefined}
data-placeholder=""
>
{placeholder}
</span>
)
})

Expand Down
Loading