Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of @emotion/react over @emotion/css for a React project?

If I install emotion/css then the API is nice and clear:

package.json:

"dependencies": {
  "@emotion/css": "^11.1.3",

React component:

import React from "react";
import { css } from "@emotion/css";

const someStyle = css`
    display: none;
`

function MyComponent() {
  return (
    <div className={someStyle} />
  );
}

However according to the docs you should install @emotion/react: https://emotion.sh/docs/introduction

package.json:

"dependencies": {
  "@emotion/react": "^11.4.1",

Now the API is a lot more messy:

import React from "react";
/** @jsx jsx */
import { jsx, css } from "@emotion/react";

const someStyle = css`
  display: none;
`;

function MyComponent() {
  return (
    <div css={someStyle} />
  );
}

Not only do you need a comment /** @jsx jsx */, but you also have to import jsx even though it's not used. In my IDE this means I get a lint warning by my React import.

What is the benefit of the recommended way? Im tempted to ignore the documentation and do it the old way.

like image 563
Evanss Avatar asked Aug 14 '19 15:08

Evanss


People also ask

Why are emotions better than styled-components?

The biggest advantage of Emotion is its easily handled object styles for writing CSS. Take, for example, the case of styled-components, wherein the developer must create unique names for different components, all while avoiding identical naming styles.

What is emotion used for in React?

emotion provides a css prop that can accept nested selectors and media queries. It can support an object or a tagged template literal. The first example uses an object with style object properties and values.

Are emotions faster than styled-components?

Emotion. js performs slightly better than styled-components.

Is emotion fast css?

Emotion is a really performant and consistent CSS-in-JS library, in comparison with other libraries like styled-components. There's a lot of content and comparisons between those two libraries, saying that Emotion is 25x faster than styled-components, etc.

Which react package should I use for @emotion?

React. The “@emotion/react” package requires React and is recommended for users of that framework if possible. Best when using React with a build environment that can be configured. Similar to the style prop, but also has support for auto vendor-prefixing, nested selectors, and media queries. Allows developers to skip the styled API abstraction ...

How do I create a react app with emotion?

Start with using create-react-app to generate a React App and then install dependecies: Change into the new project directory: Next, install @emotion/react and @emotion/styled via npm: At this point, you will have a new React project with @emotion/react. emotion provides a css prop that can accept nested selectors and media queries.

What is emotion CSS?

Emotion is a library designed for writing css styles with JavaScript. It provides powerful and predictable style composition in addition to a great developer experience with features such as source maps, labels, and testing utilities. Both string and object styles are supported.

What is emotion in JS?

Emotion is a library for authoring and composing CSS rulesets in a performant way. Here's how to get started using it with Gatsby. In this episode, Adam talks to Mitchell Hamilton about writing your styles directly in your JavaScript components using the CSS-in-JS library Emotion. CSS in JS – is it good, is it bad?


1 Answers

The jsx pragma/import is required because the css prop is provided by a wrapper around React.createElement - without that transform being applied, the css prop would not work.

That said, including it in every file is tedious - that's why there is a babel plugin that does it for you.

{
  "presets": ["@emotion/babel-preset-css-prop"]
}

With regards to the benefits of using this method over the stock emotion package, the docs list some on the page you linked:

  • CSS prop support
    • Similar to the style prop but adds support for nested selectors, media queries, and auto-prefixing.
    • Allows developers to skip the styled API abstraction and style components and elements directly.
    • The css prop also accepts a function that is called with your theme as an argument allowing developers easy access to common and customizable values.
    • Reduces boilerplate when composing components and styled with emotion.
  • Server side rendering with zero configuration.
  • Theming works out of the box.
  • ESLint plugins available to ensure proper patterns and configuration are set.
like image 159
Joe Clay Avatar answered Nov 05 '22 21:11

Joe Clay