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

[EuAvatar] Avoid mutating the style prop #6251

Merged

Conversation

chandlerprall
Copy link
Contributor

@chandlerprall chandlerprall commented Sep 20, 2022

Summary

Closes #6249 by cloning the incoming style prop, if any, instead of keeping a direct reference to the object.

Checklist

- [ ] Checked in both light and dark modes
- [ ] Checked in mobile
- [ ] Checked in Chrome, Safari, Edge, and Firefox
- [ ] Props have proper autodocs and playground toggles
- [ ] Added documentation
- [ ] Checked Code Sandbox works for any docs examples
- [ ] Added or updated jest and cypress tests

  • Checked for breaking changes and labeled appropriately
    - [ ] Checked for accessibility including keyboard-only and screenreader modes
    - [ ] Updated the Figma library counterpart
  • A changelog entry exists and is marked appropriately

Comment on lines 144 to 147
// copying style into a new object for `avatarStyle` has two purposes:
// 1. `avatarStyle` is mutated later and this prevents that mutation from affecting the incoming prop
// 2. if `style` is undefined, this "defaults" an empty object
const avatarStyle: CSSProperties = { ...style };
Copy link
Member

@cee-chen cee-chen Sep 20, 2022

Choose a reason for hiding this comment

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

This is not a strongly held change request, but I don't think this level of comment detail is necessary. The idea of not mutating objects not defined by ourselves is a fairly standard one in JS at this point and in particular I think 2. doesn't add anything to the context of this change. Nor do I think it likely that someone will come in here and change it back to a mutated obj for any reason that would warrant adding this comment.

Also worth considering: instead of setting props in avatarStyle.x directly, we can assign avatarStyle to a let and have the property setting be spread operators later, e.g.

let avatarStyle = style;

// later
avatarStyle = { ...avatarStyle, color: textColor, backgroundColor: assignedColor };

// later
avatarStyle = { ...avatarStyle, backgroundImage: etc };

This is a pattern we use in several other components/places: 1, 2, 3

Its primary advantage (other than not potentially unnecessarily cloning style if no overrides exist) is not having to set the CSSProperties type definition, as TS automatically infers the overrides.

Copy link
Member

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

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

@thomheymann has requested this fix for 8.5 so gonna go ahead and approve as my comments just food for thought / not blocking!

@cee-chen cee-chen mentioned this pull request Sep 20, 2022
5 tasks
@chandlerprall
Copy link
Contributor Author

Applied your recommended changes

Copy link
Member

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6251/

@chandlerprall chandlerprall merged commit be9c29c into elastic:main Sep 20, 2022
@chandlerprall chandlerprall deleted the bug/6249-avatar-style-mutation branch September 20, 2022 21:37
@thomheymann
Copy link
Contributor

Amazing, thanks so much for fixing this so quickly!

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.

Avatar component mutates style prop
4 participants