-
Notifications
You must be signed in to change notification settings - Fork 839
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
Conversation
- passes down a memoizer fn
- 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
ed951a7
to
6b2adc0
Compare
Preview staging links for this PR:
|
💚 Build Succeeded
|
From what I can tell so far: LGTM! 🚀 |
Thanks Lene! Does the HOC wrapper itself make sense to you, as well as the new DRYed out 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. |
@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'; |
There was a problem hiding this comment.
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.
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!
There was a problem hiding this comment.
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
:
eui/src/services/theme/hooks.tsx
Lines 67 to 84 in 829709e
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
- The
.displayName
is overridden to use the original componentdisplayName
- We're replacing
withEuiTheme
withwithEuiStylesMemoizer
- 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).
There was a problem hiding this comment.
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 🤣
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
- [ ] Checked in mobile[ ] Added or updated jest and cypress testsShould be fine if tests pass[ ] A changelog entry exists and is marked appropriately.- Skipping the changelog on these PRs as they shouldn't affect either end-users or consumers