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] Memoize typography styles & mixins #7543

Merged
merged 9 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 (+ opinionated removals of several unused CSS functions that were exported from component style files only and not as top-level APIs) on EuiText, EuiTitle, and EuiLink components (+ typography mixins).

No UI or classNames should have changed/regressed as part of this PR.

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
  • Release checklist
    • [ ] 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

- no one's using it
- it isn't documented anywhere
- we should likely try to remove unnecessary style exports where we can, and encourage using components directly instead
- not being used anywhere in EUI or KIbana, just inline them

+ move internal `_colorCSS` util to bottom of file
- just write out the style manually 🤷 honestly, it might need a redesign at some point anyway

+ clean up styles and how they're written
@cee-chen cee-chen marked this pull request as ready for review February 23, 2024 18:34
@cee-chen cee-chen requested a review from a team as a code owner February 23, 2024 18:34
return euiNumberFormat(euiTheme);
};
export const useEuiNumberFormat = (): string =>
useEuiMemoizedStyles<any>(euiNumberFormat);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we could prevent using an any type here?
Could it make sense to enhance useEuiMemoizedStyles to return StylesMaps | string to support cases like useEuiNumberFormat? 🤔

type MemoizedStylesMap = WeakMap<Function, StylesMaps | string>;

const useEuiMemoizedStyles = <
  T extends (theme: UseEuiTheme) => StylesMaps | string
>(
  styleGenerator: T
): ReturnType<T> => {}

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how I feel about this. These utility functions return strings on purpose, and they should only be used in the css tagged template literal so that they can get processed, validated and injected in the runtime.

The only reason for them to be memoized is if they're passed directly to the css prop, not wrapped like this

css={css`
  ${useEuiNumberFormat()}
}`

because in this case, when the component rerenders, the styles will be recalculated anyway and memoization won't help much.

The css prop doesn't support passing a raw style string even though the Interpolation type allows it.

I believe that memoization here is unnecessary, and the responsibility to memoize customer styles lies on the consumer. If our goal is to provide one-line emotion utilities that are also memoized, we should probably also export useEuiNumberFormatCss() that does that exact thing and is meant to be used when the only thing you need is that single style (e.g., <p css={useEuiNumberFormatCss()}></p>)

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'm wondering if we could prevent using an any type here?

see my question here about where my Styles type was too restrictive 🥲 #7529 (comment)

In light of this conversation I think it was, and I should modify it.

These utility functions return strings on purpose

I don't know if I agree with that and I don't think our architecture was meant to be that deliberate to be honest with you. Some utilities return strings so they don't trigger additional appended Emotion classnames, some use the css template literal because they do want to insert additional classNames. TBH, I don't think Caroline really had a specific rhyme or reason for it.

Also, our Emotion useX hooks are also fairly arbitrary and not well planned out, they currently have zero usage outside of our own documentation and to be frank: I don't like the idea of maintaining them. I'd rather just get rid of them entirely if we have the option.

Copy link
Member

Choose a reason for hiding this comment

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

Also, our Emotion useX hooks are also fairly arbitrary and not well planned out, they currently have zero usage outside of our own documentation and to be frank: I don't like the idea of maintaining them. I'd rather just get rid of them entirely if we have the option.

In that case, maybe it's not worth adding memoization to them since the outputs of these functions vary, and they aren't really used externally. There's no need for adding memoization to small and rarely used utility styles, especially if we aren't sure we want to keep them in the code base

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fine with that, I'd also vote to just rip out useEuiNumberFormat in this PR / in our docs if no one objects. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tkajtoch is this PR good to merge on your end or do you have any further feedback/change requests?

@kibanamachine
Copy link

Preview staging links for this PR:

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

Copy link
Member

@tkajtoch tkajtoch left a comment

Choose a reason for hiding this comment

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

The changes look and work great!

@cee-chen
Copy link
Contributor Author

cee-chen commented Mar 7, 2024

Thanks y'all!

@cee-chen cee-chen merged commit 0d0130c into elastic:main Mar 7, 2024
6 of 7 checks passed
@cee-chen cee-chen deleted the emotion/memoize-typography-styles branch March 7, 2024 17:43
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