My Six Methods for Using the Emotion Effectively #3165
mitchell-up
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have summarized methods and code examples for effectively using the Emotion. Furthermore, I have validated its effectiveness by measuring and comparing its performance against other methods. This process confirms whether it is a superior choice.
The performance measurement of each method is based on the
baseDuration
obtained using theReact.Profile
component, with measurements recorded in milliseconds (ms).1. Defining Styles Outside Components
The method involves storing style code in separate external variables rather than within tags inside components.
(Bad) Example: rendering 12 toggle buttons.
(Better) Example: rendering 12 toggle buttons.
When measured against the above code, it is observed that the method of defining styles externally renders 89.96% faster on average compared to defining them internally.
See Performance Comparison
2. Using CssProp Instead of Styled
Both
styled
from@emotion/styled
andcss
from@emotion/react
can be used to create styles in Emotion, but it's generally better to usecss
.css
compared tostyled
.styled
has additional overhead, especially when passing props for dynamic styling.css
, only the style-related code can be reused, and it can be used withReact.memo
,useMemo
, etc., to prevent unnecessary style recalculations.css
Similar to the example in Section 1, performance is measured by rendering 12 toggle buttons. We compare the performance of writing styles using styled and css with both Object format and Tagged Template Literal format.
Comparison with Object Styles
When styles are written as objects,
css
renders on average 66.67% faster thanstyled
.See Performance Comparison
Comparison with Template Literal Styles
When styles are written with Template Literals,
css
renders on average50%
faster thanstyled
.See Performance Comparison
3. Using Object Definition over Template Literals
In Emotion, both Tagged Template Literals like
css
and Object formats likecss({ color: 'blue' })
are supported, but using the Object format is recommended for the following reasons:The code example below also renders 12 toggle buttons, similar to Section 1.
(Bad) Example: Template Literal
(Good) Example: Template Literal
After conducting around 30 trials, the rendering speed of the code defining styles in Object format shows almost no performance difference compared to Literal format, with a difference of only 0.01ms.
See Performance Comparison
4. Using className Within a Single Component
Instead of applying cssProp to every tag where styles need to be applied, it's better to apply a single cssProp to the top-level parent tag within a single component and add classNames to the child tags to apply styles.
The code example below is a component that simply lists information.
(Bad) Separate Css
(Better) css with className
Applying className to each tag within a component is on average 60% faster than applying cssProp separately to each tag.
5. Use Css Variables or className for Dynamic Styling
Dynamic styling can be approached in two aspects:
01 Cases where UI itself dynamically changes, such as animations or transitions
As described in Emotion's Best Practice, using the
style
prop and css variables can efficiently handle parts that need to be dynamically processed.02 Cases where styles need to change based on props received from component users
In this scenario, although the component's styles are determined by the user, they don't change dynamically after the component is rendered. If possible, defining variations of the component in advance and applying them using className would lead to more efficient rendering. Furthermore, as shown in the example below, while it's faster even for simple color applications, it shines brighter in cases where various properties need to be changed.
Similar to Section 4, the code example below lists information, but it allows applying red, green, or blue color based on the
color
prop.(Bad) Dynamic
(Better) ClassName
Applying styles with className is on average 62.5% faster than applying them dynamically for dynamic styling.
6. Using JavaScript Constants and Css Variables Instead of Theme
It's better to use Css Variables like
var(--color-bg)
and JavaScript-defined constants rather than usingThemeProvider
,withTheme
,useTheme
provided by Emotion to accommodate Light and Dark themes.ThemeProvider
wrapping, passing theme as a function within cssProp, etc.).ThemeProvider
are recalculated for each component render.The code example below is an Article UI that requires applying various colors. The example assumes only one theme. (If you need to support multiple themes, you'll have to define colors for each theme or define corresponding Css Variables.)
(Bad) Using ThemeProvider
(Better) Using Constants
Applying colors using JavaScript constants is on average 55.56% faster than using Theme.
Conclusion
The methods summarized above will streamline decision-making and facilitate making better choices. Additionally, it's essential to bear in mind the trade-off relationship between developer experience and performance when it comes to CSS-in-JS. Therefore, when applying these methods, it's crucial to weigh the performance benefits against potential decreases in developer experience.
Feel free to leave any feedback, opinions, or additional insights regarding the methods outlined in this article in the comments section!
Beta Was this translation helpful? Give feedback.
All reactions