Git Product home page Git Product logo

react-styled-components-for-designers's Introduction

React and Styled Components for Designers

This repo contains three example approaches (or phases) for building modern static layouts using React.js and Styled Components. The first mimics traditional approaches to layout and css, with a single component being mounted containing all of the HTML laid out and styled via a stylesheet. The second phase takes the first a bit further and breaks the HTML apart into individual files / components, showing how a flat file is broken up into multiple files. The third phase modifies the second by introducing common Styled Components patterns for handling CSS. The first example is typically what is built first; once the layout is responsive and looks right then the second and third phases become production work as markup and CSS is moved from the main file into individual component-files one-by-one.

From a design-to-engineering hand-off perspective the first example approach is typically sufficient. However, the second and third examples are included so that the reader may gain a greater insight into how components finally appear in real-world applications.

Building upon create-react-app and VSCode as a development environment, this project aims to make the barrier of entry as low as possible while providing for the fastest possible iteration times. Also included is an excellent 12-column grid-system based on Bootstrap.

Requisites

A basic understanding of HTML + CSS.

First Time Installation

  1. Install VSCode
  2. Install VSCode shell command code : [Cmd + Shift + P] and search for shell.
  3. Install Node.js: https://nodejs.org/en/download/
  4. Then open your terminal and enter
git clone [email protected]:damassi/react-styled-components-for-designers.git
cd react-styled-components-for-designers
npm install -g yarn
yarn install
yarn start

This should launch a web browser pointed at http://localhost:3000, as well as VSCode. Changes to source-files will be immediately reflected in the browser, without a refresh.

Back in VSCode, you should see this pop up:

screen shot 2018-01-28 at 4 28 11 pm

Be sure to click 'Install All' and once that's complete run cmd+shift+p > reload window to reload with new extensions installed. Plugins include Prettier, an automatic code formatter so you don't have to worry about coding style, as well as ESLint, which will check your code for errors as you type:

screen shot 2018-01-28 at 4 24 59 pm

You can open this panel by selecting View > Problems from the menu bar.

Once the one-time-only setup takes place all that's needed to start coding is yarn start. Toggle working examples by modifying the number located here and save.

A Quick Primer on React and Styled Components

React

React is a popular way to build user interfaces and aims to be as simple as possible. For example:

function App() {
  return (
    <div>
      <h1>Name</h1>
      <ul>
        <li>Hello</li>
        <li>How</li>
        <li>Are<li>
        <li>You?</li>
      </ul>
    </div>
  )
}

is a React component. Look familiar? Yup :) just regular html. And further, individual components can be combined to better describe page structure:

function Header() {
  return <h1>Name</h1>
}

function Description() {
  return (
    <ul>
      <li>Hello</li>
      <li>How</li>
      <li>Are</li>
      <li>You?</li>
    </ul>
  )
}

function App() {
  return (
    <div>
      <Header />
      <Description />
    </div>
  )
}

If you need to pass data into your component, you can do so with props:

function Header(props) {
  return <h1>{props.name}</h1>
}

function Description(props) {
  return <p>{props.description}</p>
}

function App() {
  return (
    <div>
      <Header name="Will Burroughs" />
      <Description description="The place behind the pines" />
    </div>
  )
}

When rendered in a web browser, this will output

<div>
  <h1>Will Burroughs</h1>
  <p>The place behind the pines</p>
</div>

Styled Components

Building on our above example, lets add some styles. Before Styled Components, developers would typically use CSS classes to style markup:

function App() {
  return (
    <div>
      <div className='name'>
        Leif the Cat
      </div>
      <div className='location'>
        Seattle, WA
      </div>
    </div>
  )
}

// styles.css

.name {
  font-size: 20px;
  font-weight: bold;
}

.location {
  text-align: center;
}

With Styled Components, you can write your CSS directly in your JavaScript -- an innovation made possible by the recent addition of template literals to the JavaScript lanaguage.

While the full extent of what's possible will not be covered here, in short template literals allow for an easy way to mix strings and data together (note the back tick characters in the third introduction variable):

const name = 'Chris'
const location = 'Seattle'
const introduction = `Hello my name is ${name} and I live in ${location}.`

And a full example:

import styled from 'styled-components'

const Container = styled.div`
  padding: 20px;
`

const Name = styled.div`
  font-size: 20px;
  font-weight: bold;
`

const Location = styled.div`
  text-align: center;
`

function App() {
  return (
    <Container>
      <Name>Leif the Cat</Name>
      <Location>Seattle, WA</Location>
    </Container>
  )
}

Breaking this apart a bit, what Styled Components do is allow you to assign a variable to an HTML element (in the above example its a div, but can be anything) and then pass in styles between the back-ticks. You then use this variable as a React component. Not only does this markup read well semantically, but it allows one to easily share common properties between styles:

const colors = {
  darkGrey: '#333',
  lightGrey: '#ccc'
}

const Name = styled.div`
  color: ${colors.darkGrey};
  font-size: 20px;
`

const Description = styled.div`
  color: ${colors.lightGrey};
`

function App() {
  return (
    <div>
      <Name>Leif the Cat</Name>
      <Description>The best kitty</Description>
    </div>
  )
}
...

Combining all of these things together makes for a flexible codebase as more and more things are broken apart into reusable, modular components that can be shared throughout. It also makes the development experience more convenient as the markup and corresponding styles are co-located side-by-side in the same file.


(This project was bootstrapped with Create React App)


react-styled-components-for-designers's People

Contributors

briansw avatar damassi avatar orta avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

orta dbmedialab

react-styled-components-for-designers's Issues

Step 4: State + Props?

Just walked through this with Owen (super helpful, thanks!) and we thought it would be nice to tease state+props as a step 4 since as a designer, there might be some animation that depends on state.

First run: Got error

I just ran:

git clone [email protected]:damassi/react-styled-components-for-designers.git
cd react-styled-components-for-designers
npm install -g yarn
yarn install
yarn start

and got:

screen shot 2018-01-29 at 1 16 03 pm

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.