Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AvatarStack opacity for children without sx prop #1751

Merged
merged 4 commits into from Dec 22, 2021

Conversation

talune
Copy link
Contributor

@talune talune commented Dec 17, 2021

What this changes

Resolves https://github.com/github/memex/issues/6311

This change adjusts how style rules are defined for .pc-AvatarItem so that children of AvatarStack display correctly regardless of if the child element has an sx prop.

Previously, opacity was defined in AvatarStackWrapper, but z-index was passed to the child's sx prop. If the child element didn't have the sx prop, then avatars were stacked in reverse order, with the least opaque on top, as can be seen in the storybook example here.

To resolve, I removed the z-index mapping from transformChildren, and instead defined the appropriate z-index explicitly in the same place as opacity -- in AvatarStackWrapper.

Screenshots

Before:
before-avatars
before-img

After
https://user-images.githubusercontent.com/6740550/146618418-8a415298-666d-4ca4-a7d9-a1aa358701f5.mov

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

.pc-AvatarItem elements need both opacity and z-index styles in order
to display properly. Since children of AvatarStack are not guaranteed
to have an sx prop, it can't reliably enforce these rules.
Instead, AvatarStackWrapper can define the styles.
@talune talune requested review from a team and mperrotti December 17, 2021 23:29
@changeset-bot
Copy link

changeset-bot bot commented Dec 17, 2021

🦋 Changeset detected

Latest commit: abcc056

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@talune talune added the skip changeset This change does not need a changelog label Dec 17, 2021
@talune talune self-assigned this Dec 17, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Dec 17, 2021

size-limit report 📦

Path Size
dist/browser.esm.js 58.91 KB (-0.07% 🔽)
dist/browser.umd.js 59.24 KB (-0.06% 🔽)

@talune talune removed the skip changeset This change does not need a changelog label Dec 17, 2021
@rezrah
Copy link
Contributor

rezrah commented Dec 20, 2021

@talune 👋 - Thanks for putting up this PR - looks great. Wanted to check something first. The storybook example appears to be incorrect, as the correct usage for AvatarStack is here: https://primer.style/react/AvatarStack

Converting the img in your original issue example to a nested Avatar should prevent this opacity issue from occurring. I think. Have you tried this approach instead?

Screenshot 2021-12-20 at 10 06 32

@talune
Copy link
Contributor Author

talune commented Dec 20, 2021

Thanks, @rezrah! Yes, you're correct that the storybook example looks off because the child elements are <img>s instead of <Avatar>s.

In my team's case, we were correctly using <Avatar> child elements, but we were inadvertently overriding the sx prop that AvatarStack maps to child elements here. We corrected our bug by augmenting the sx prop instead of replacing it.

I noticed the storybook bug while working on the above resolution, and decided to take a closer look. The storybook bug has the same root issue: <img>s don't support an sx prop. This means they don't receive the z-index rule, However, they do receive the opacity rule which isn't defined by the child sx prop.

I think it's a bit confusing to have the opacity and z-index rules defined in these two different ways, and it seemed pretty easy to resolve that by moving the z-index definition into the same block as the opacity rule.

You're absolutely right that the styles render correctly if one uses <Avatar> children without overriding the sx prop. But my hope with this change is to prevent future users from running into the same bug by simplifying how the styles are applied. However, no worries if this isn't the approach the team prefers!

Copy link
Contributor

@rezrah rezrah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@talune - thanks for the background on this issue. I saw your fix here, glad it's been resolved.

We encourage using Avatar over a native img inside AvatarStack, so we'll fix up the Story so that it doesn't confuse others in future. Apologies for any confusion that caused you too.

With regards to the PR itself, I've tested it for visual regression too and it seems okay 👍 . Co-locating the styles seems sensible, but I can't reproduce the bug around Avatar sx props being overridden, as it merges prop styles with z-index fine for me 🤔 . I do like how this makes the code and design logic easier to reason about though, and will therefore help us move this component to beta faster, so thanks for this contribution.

Edit: PR was missing a changeset, which is required to merge and trigger a new npm release. I've added one for you just now, so please go ahead and merge this PR when you're happy to proceed.

@talune
Copy link
Contributor Author

talune commented Dec 22, 2021

Thank you for the extra context and for adding the changeset, @rezrah! I was prompted to first merge in main & I will merge this PR once checks re-pass.

@talune talune merged commit 03584e0 into main Dec 22, 2021
@talune talune deleted the talune/avatar-stack-opacity branch December 22, 2021 17:34
@primer-css primer-css mentioned this pull request Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants