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

[Emotion] Add HOC util for memoized styles + memoize EuiIcon styles #7544

Merged
merged 4 commits into from
Mar 7, 2024

Conversation

cee-chen
Copy link
Contributor

@cee-chen cee-chen commented Feb 23, 2024

Summary

This PR is a performance enhancement (+ minor code cleanup) on the EuiIcon component, and should not regress or change any output UI or generated classNames.

As always, I recommend code reviewing by commit.

QA

General checklist

  • Browser QA
    • Checked in both light and dark modes
    • Checked in Chrome, Safari, Edge, and Firefox
    • Checked for accessibility including keyboard-only and screenreader modes
      - [ ] Checked in mobile
  • Docs site QA - N/A
  • Code quality checklist
    • [ ] Added or updated jest and cypress tests Should be fine if tests pass
  • Release checklist - N/A
    • [ ] A changelog entry exists and is marked appropriately. - Skipping the changelog on these PRs as they shouldn't affect either end-users or consumers
  • Designer checklist - N/A

- remove `let: any` in favor of a conditional (still needs to be an obj spread, react DOM yells about the invalid property otherwise)

- remove `hideIconEmpty` and object in favor of simply using a prop
@cee-chen cee-chen force-pushed the emotion/memoize-icon-hoc branch from ed951a7 to 6b2adc0 Compare February 23, 2024 21:02
@cee-chen cee-chen marked this pull request as ready for review February 23, 2024 21:05
@cee-chen cee-chen requested a review from a team as a code owner February 23, 2024 21:05
@kibanamachine
Copy link

Preview staging links for this PR:

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

@mgadewoll
Copy link
Contributor

From what I can tell so far: LGTM! 🚀

@cee-chen
Copy link
Contributor Author

cee-chen commented Mar 4, 2024

Thanks Lene! Does the HOC wrapper itself make sense to you, as well as the new DRYed out getMemoizedStyles utility? To be honest, I wish there was a way to avoid HOCs (e.g. using a render prop instead) but I couldn't think of an easy way to do so.

Also, it would be nice to imagine us one day moving away from class components entirely, but I'm not sure we'll ever be able to do so given the complexity of some of our components.

@mgadewoll
Copy link
Contributor

@cee-chen Yes, to me the updates make sense. While HOCs add extra weight here, I don't think we have another way to add the memoization logic without introducing some separate way just for class components. And as the idea is to (hopefully) remove class components altogether in the future and we want to keep things DRY, it definitely seems better to reuse the hooks via HOCs, imho 👍

Component: React.ComponentType<T & WithEuiStylesMemoizerProps>
) => {
const componentName =
Component.displayName || Component.name || 'ComponentWithStylesMemoizer';
Copy link
Member

@tkajtoch tkajtoch Mar 5, 2024

Choose a reason for hiding this comment

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

This results in the HoC component using the same display name as the wrapped component, which is confusing in React DevTools and may seem like improperly nested icons.
Screenshot 2024-03-05 at 11 55 51

Could you please rename the HoC's displayName to match the recommended HoCName(WrappedComponentName) convention? In this case, that would be something like WithEuiStylesMemoizer(${Component.displayName || Component.name || 'Component'}). LMK what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a copy of what we're already doing in withEuiTheme:

export const withEuiTheme = <T extends {} = {}, U extends {} = {}>(
Component: React.ComponentType<T & WithEuiThemeProps<U>>
) => {
const componentName =
Component.displayName || Component.name || 'ComponentWithTheme';
const Render = (
props: Omit<T, keyof WithEuiThemeProps<U>>,
ref: React.Ref<Omit<T, keyof WithEuiThemeProps<U>>>
) => {
const theme = useEuiTheme<U>();
return <Component theme={theme} ref={ref} {...(props as T)} />;
};
const WithEuiTheme = forwardRef(Render);
WithEuiTheme.displayName = componentName;
return WithEuiTheme;

I'm reluctant to change it for that reason, and also primarily because I believe it will massively fuck up snapshots (which was likely the original reason for Greg doing this weird shenanigans in the first place). I don't love it, but since we're already doing it and I don't really feel like going through a massively painful set of Kibana upgrades, I'd rather not change it.

Copy link
Member

Choose a reason for hiding this comment

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

I get it, but that's a new HoC, so enzyme snapshots will be affected anyway, won't they? DOM snapshots should all be fine since they aren't aware of React vDOM structure

Copy link
Contributor Author

@cee-chen cee-chen Mar 6, 2024

Choose a reason for hiding this comment

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

I get it, but that's a new HoC, so enzyme snapshots will be affected anyway, won't they?

No, they won't, because:

  1. The .displayName is overridden to use the original component displayName
  2. We're replacing withEuiTheme with withEuiStylesMemoizer - that's the whole point of keeping the override the same

React DevTools is not the same as Enzyme's shallow/mounted renderer and I believe this code was originally written with Enzyme in mind (bleh).

Copy link
Member

Choose a reason for hiding this comment

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

aah gotcha, I missed the withEuiTheme diff part! I'm good leaving it as is then and hope enzyme snapshots will comply 🤣

@cee-chen cee-chen merged commit 5cc4ce8 into elastic:main Mar 7, 2024
8 checks passed
@cee-chen cee-chen deleted the emotion/memoize-icon-hoc branch March 7, 2024 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants