question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[AvatarGroup] component interacts poorly with custom Avatars

See original GitHub issue

Due to something in the way AvatarGroup clones its children, it can’t be used with custom Avatar components, which greatly limits its utility.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

I have a custom Avatar component with fixed sizes. It also allows for wrapping with a link etc, but here’s a simplified example:

function MyAvatar = ({size = 'md', user}: Props) => {
  const classes = useStyles()
  const className = classes[size]
  const { name, imageUrl } = user
  return <MuiAvatar className={className} src={imageUrl} alt={name} />
}

I want to use it within an AvatarGroup like this:

<AvatarGroup>
  <MyAvatar user={user1} size="xl" />
  <MyAvatar user={user2} size="xl" />
</AvatarGroup>

However, when I do this, the xl className is not applied to the nested MuiAvatar.

Putting them side by side shows the difference:

  <MyAvatar size="xl" user={user1} linked={false} />
  <AvatarGroup>
    <MyAvatar size="xl" user={viewer} linked={false} />
  </AvatarGroup>

The MuiAvatar outside AvatarGroup gets : MuiAvatar-root MuiAvatar-circle makeStyles-xl-509

The MuiAvatar inside AvatarGroup gets: MuiAvatar-root MuiAvatar-circle MuiAvatarGroup-avatar

Here’s the full fragment

<div class="MuiAvatar-root MuiAvatar-circle makeStyles-xl-509">
  <img alt="Jamie Talbot" src="url" class="MuiAvatar-img">
</div>
<div class="MuiAvatarGroup-root">
  <div class="MuiAvatar-root MuiAvatar-circle MuiAvatarGroup-avatar" style="z-index: 2;">
    <img alt="Jamie Talbot" src="url" class="MuiAvatar-img">
  </div>
</div>

Expected Behavior 🤔

Both nested MuiAvatars should have the className makestyles-xl-509

Tech Version
Material-UI v4.9.11
React 16.13.1
Browser
TypeScript 3.8.3
etc.

Context

It’s probably due to the way the children are cloned, but I can’t actually see why the classNames for nested children would not be cloned too. 🤷🏼‍♂️

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
majelbstoatcommented, Apr 25, 2020

Ahh, I finally understand, and got it working. Thanks for taking so much time to help me get that right. For others similarly confused, the final component is:

const Avatar = ({ size = 'md', user, className, linked = true, ...other }: AvatarProps) => {
  const classes = useStyles()
  const username = user?.username || 'unknown'
  const name = user?.name || '?'
  const src = user?.avatar.url || undefined

  const Component = (
    <MuiAvatar className={`${className} ${classes[size]}`} src={src} alt={name} {...other} />
  )
  if (linked) {
    return (
      <Anchor route="profile" params={{ username }}>
        {Component}
      </Anchor>
    )
  }
  return Component
}

export type AvatarProps = {
  size?: Size
  user?: User
  linked?: boolean
} & MuiAvatarProps
1reaction
oliviertassinaricommented, Apr 24, 2020

What I mean is that you should concatenate the class name coming from the AvatarGroup with the new one you inject.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[AvatarGroup] component interacts poorly with custom Avatars
Current Behavior. I have a custom Avatar component with fixed sizes. It also allows for wrapping with a link etc, but here's a...
Read more >
MUI V5: Avatars / Profile pics in React (with Badges) - YouTube
... the Avatar component in Material UI V5. This component can act as a profile picture for your ReactJS components, as well as...
Read more >
Material UI v0 ListItem Alignment Issue when using Custom ...
I am trying to assign leftAvatar value via CustomAvatar component but it is not aligning as you can see in attached screenshot. Please...
Read more >
AvatarGroup API - Material UI - MUI
API reference docs for the React AvatarGroup component. Learn about the props, CSS, and other APIs of this exported module.
Read more >
Element: <oj-avatar>
JET components are implemented as custom HTML elements and programmatic access to these components is similar to interacting with regular ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found