GithubHelp home page GithubHelp logo

styled-components's Introduction

Styled Components

Styled components let's you keep your CSS within scope and provides a lot of benefits like (copied from their Styled Component Docs):

  • Automatic critical CSS: styled-components keeps track of which components are rendered on a page and injects their styles and nothing else, fully automatically. Combined with code splitting, this means your users load the least amount of code necessary.

  • No class name bugs: styled-components generates unique class names for your styles. You never have to worry about duplication, overlap or misspellings.

  • Easier deletion of CSS: it can be hard to know whether a class name is used somewhere in your codebase. styled-components makes it obvious, as every bit of styling is tied to a specific component. If the component is unused (which tooling can detect) and gets deleted, all its styles get deleted with it.

  • Simple dynamic styling: adapting the styling of a component based on its props or a global theme is simple and intuitive without having to manually manage dozens of classes.

  • Painless maintenance: you never have to hunt across different files to find the styling affecting your component, so maintenance is a piece of cake no matter how big your codebase is.

  • Automatic vendor prefixing: write your CSS to the current standard and let styled-components handle the rest.

Getting Started

First let's install the package with yarn.

yarn add styled-components

Great, now let's import it and start playing around with it. The styled default export it provides us let's us create normal html tags using tag literals.

import React from "react"
import styled, { ThemeProvider } from "styled-components"

const Title = styled.h1`
color: blue
`


function App() {
  return (
    <div className="App">
      <Title>Hello</Title>
    </div>
  )
}

export default App

Great, next we'll be extending one component's styles and customizing it a bit.

import React from "react"
import styled, { ThemeProvider } from "styled-components"

const Title = styled.h1`
color: blue
`

// Extends Title
const UnderlinedTitle = styled(Title)`
  padding: 20px;
  text-decoration: underline;

function App() {
  return (
    <div className="App">
      <Title>Hello</Title>
      <UnderlinedTitle>My UnderlinedParagraph</UnderlinedTitle>
    </div>
  )
}

export default App
`

Alright, next -> You can also pass a function for the theme prop. This function will receive the parent theme, that is from another higher up the tree. This way themes themselves can be made contextual.

import React from "react"
import styled, { ThemeProvider } from "styled-components"

const Title = styled.h1`
  color: ${props => props.theme.fg};
  font-size: 100px;
  margin: 0;
  background: ${props => props.theme.bg};
`

// Extends Title
const UnderlinedTitle = styled(Title)`
  padding: 20px;
  text-decoration: underline;
`

// Define our `fg` and `bg` on the theme
const theme = {
  fg: "palevioletred",
  bg: "red"
}

function App() {
  return (
    <div className="App">
      <Title>Hello</Title>
      <ThemeProvider theme={theme}>
        <UnderlinedTitle>My UnderlinedParagraph</UnderlinedTitle>
      </ThemeProvider>
    </div>
  )
}

export default App

styled-components's People

Contributors

nugoo1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.