Stylized components

Styling components is one of the new ways to use CSS in modern JavaScript. Its purpose is to become the successor of CSS modules, a way to write CSS whose scope is limited to a single component and will not leak to any other elements in the page

A brief history

Once upon a time, the Web was so simple that even CSS did not exist. We usetableAnd frame. wonderful time.

thenCSSBecome alive, after a period of time, people gradually realized that the framework can greatly help you, especially in the construction of grid and layout, Bootstrap and Foundation play an important role in it.

Preprocessor like thisSassOther help greatly slowed down the adoption of the framework and better organized the code, such asBoundary elementwithSMACSSUsed more and more, especially within the team.

Agreement is not a solution to all problems, and it is very complicated to remember, so in recent years, as more and more people adoptJavaScriptAnd to build the process in each front-end project, CSS found a way to enter JavaScript (CSS-in-JS).

New tools explore new ways to use CSS-in-JS, some of which are successful and increasingly popular:

  • Reaction style
  • jsxstyle
  • radium

And more.

Introduce stylized components

One of the most popular of these tools isStylized components.

It is the successor to beCSS module, This is a way to write CSS, its scope is limited to a single component, and will not leak to any other elements in the page.

(More information about CSS modulesHerewithHere)

Styling components allows you to write pure CSS in the component without worrying about class name conflicts.


Use installation style componentsnpmoryarn:

npm install styled-components
yarn add styled-components

That's it! Now, all you have to do is to add this import:

import styled from 'styled-components'

Your first styled component

along withstyledAfter the objects are imported, you can now start creating styled components. This is the first one:

const Button = styled.button`
  font-size: 1.5em;
  background-color: black;
  color: white;

ButtonIt isreactionPlay its biggest role.

We use the functionality of the style object (calledbuttonIn this case, some CSS properties are passed in itTemplate text.

This component can now be rendered in our container using regular React syntax:

render(<Button />)

Stylized components not only provide other functions, but can also be used to create other componentsbutton, Likesection,h1,inputAnd many others.

The syntax with backticks may be strange at first, but it is calledMarkup template, It is pure JavaScript, a way to pass parameters to a function.

Use props to customize components

When you pass some props to the style component, it will pass them toDOMThe node is installed.

For example, this is what we passedplaceholderwithtypePropsinputingredient:

const Input = styled.input`

render( <div> <Input placeholder="…" type=“text” /> </div> )

This will do what you want to insert these props as HTML attributes.

Props instead of being blindly passed toDOMIt can also be used to customize components based on prop values. This is an example:

const Button = styled.button`
  background: ${props => (props.primary ? 'black' : 'white')};
  color: ${props => (props.primary ? 'white' : 'black')};

render( <div> <Button>A normal button</Button> <Button>A normal button</Button> <Button primary>The primary button</Button> </div> )

Set upprimaryProp changes the color of the button.

Extend existing styled components

If you have a component and want to create a similar component with a slightly different style, you can useextend:

const Button = styled.button`
  color: black;

const WhiteButton = Button.extend </span><span style="color:#e6db74"> color: white; </span><span style="color:#e6db74">

render( <div> <Button>A black button, like all buttons</Button> <WhiteButton>A white button</WhiteButton> </div> )

This is normal CSS

In styling components, you can use CSS that you already know and love. This is pure CSS. It is neither pseudo CSS nor inline CSS, and it has its limitations.

You can use media queries,NestingAnd anything else you might need.

This is an example of a media query:

const Button = styled.button`
  color: green;
  @media screen and (max-width: 800px) {
    color: black;

Use vendor prefix

The styling component automatically adds all required vendor prefixes, so you don’t need to worry about this issue.

in conclusion

This is the introduction of "styled components"! These concepts will help you understand the concept and help you get started and run in this way of using CSS in JavaScript.

Download mine for freeResponse Handbook

More response tutorials: