React uses emotion to write CSS code

React uses emotion to write CSS code

Introduction:

emotion is a JavaScript library. Using emotion, you can write CSS code in the same way as writing JS. After installing emotion in react, you can easily encapsulate and reuse CSS. After using emotion, the tags rendered by the browser will be added with a logo starting with css. As follows: The tags starting with css- in the screenshot are rendered using the emotion library.

The following will introduce the application of emotion in engineering from installation to use.

Installation of emotion:

yarn add @emotion/react
yarn add @emotion/styled

Add common CSS components:

1. Name the same as the component, starting with a capital letter
2. Styled is followed by the html tag

//Introduce emotion
import styled from "@emotion/styled";
// Create a CSS component using emotion const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
`;
//Use css components in html code:
<Container>
//html code</Container>

To add styles to an existing component:

1. Capitalize the first character of the variable name
2. Pass the existing component as a parameter to styled
3. Style code can add parameters

// Card is an existing component of antd const ShadowCard = styled(Card)`
    width: 40rem;
    min-height: 56rem;
    padding: 3.2rem 4rem;
    border-radius: 0.3rem;
    box-sizing: border-box;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 10px;
    text-align: center;
`;
// Imported image, used directly as a parameter import logo from "assets/logo.svg";

// Backticks can refer to the magic string passed in parameter const Header = styled.header`
background: url(${logo}) no-repeat center;
padding: 5rem 0;
background-size: 8rem;
width: 100%;
`;

Extract common CSS components

1. Before the backquote, all possible parameters that may be used should be listed to receive generic parameters.
2. Get the parameters passed in, use props to get them, such as props.between, you can use the function return value to assign the CSS attribute, the CSS attribute is not rendered, and the return value is undefined

justify-content: ${(props) => (props.between ? "space-between" : undefined)};

3. You can use css selector
4. When calling, use it like a normal js component, and pass the same parameters

//Call Row component <HeaderLeft gap={1}> 
//html code</HeaderLeft>
const HeaderLeft = styled(Row)``;


// Define the Row component export const Row = styled.div<{
  gap?: number | boolean;
  between?: Boolean;
  marginBottom?: number;
}>`
display: flex;
align-items: center;
justify-content: ${(props) => (props.between ? "space-between" : undefined)};

margin-bottom: ${(props) =>

props.marginBottom ? props.marginBottom + "px" : undefined};

> * {
margin-top: 0 !important;
margin-bottom: 0 !important;
margin-right: ${(props) =>

typeof props.gap === "number"
    ? props.gap + "rem"
    : props.gap
    ? "2rem"
    : undefined};
}
`;

Write emotion inline styles

1. Write the following code at the top of the component to inform the current component to use the emotion inline style

/* @jsxImportSource @emotion/react */

2. Inline style format: css={ /* Inline style code*/ }

<Form css={{ marginBottom: "2rem" }} layout={"inline"}>
// html code</Form>

The above is the introduction and use of emotion. (#^.^#)

The above is the details of how React uses emotion to write CSS code. For more information about how React uses emotion to write CSS code, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of two ways to dynamically change CSS styles in react
  • Solution to CSS reference path error based on react project packaging
  • React uses CSS to implement react animation function example
  • 7 ways to use CSS in React (the most complete summary)
  • An example of how to use CSS Modules in Create React App
  • Example code for using css modules in create-react-app
  • Detailed explanation of adding css modules, sasss and antd using create-react-app
  • ReactJs method of setting css style
  • How to write CSS elegantly with react

<<:  Detailed explanation of how to stop the Docker container from automatically exiting

>>:  CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

Recommend

Detailed explanation of Vue.js directive custom instructions

Customize a demo command The syntax of Vue custom...

Teach you to implement a simple promise step by step

Table of contents Step 1: Build the framework Ste...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

Centos8.3, docker deployment springboot project actual case analysis

introduction Currently, k8s is very popular, and ...

Linux implements the source code of the number guessing game

A simple Linux guessing game source code Game rul...

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

A brief discussion on React Component life cycle functions

What are the lifecycle functions of React compone...

How to fix the WeChat applet input jitter problem

Find the problem Let's look at the problem fi...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Detailed tutorial on compiling and installing MySQL 8.0.20 from source code

In the previous article, we introduced: MySQL8.0....

Summary of Kubernetes's application areas

Kubernetes is the leader in the container orchest...

Solve the abnormal error when building vue environment with webpack

Table of contents First, configure package.json T...

How to change the domestic image source for Docker

Configure the accelerator for the Docker daemon S...

Example code for implementing hollowing effect with CSS

Effect principle Mainly use CSS gradient to achie...