GithubHelp home page GithubHelp logo

automotola / react-css-components Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andreypopp/react-css-components

0.0 2.0 0.0 519 KB

Define React presentational components with CSS

Makefile 3.38% JavaScript 90.33% CSS 6.29%

react-css-components's Introduction

React CSS components

Join the chat at https://gitter.im/andreypopp/react-css-components Travis build status npm

Table of Contents

Motivation

Define React presentational components with CSS (or even SASS or Less if you like).

The implementation is based on CSS modules. In fact React CSS Components is just a thin API on top of CSS modules.

NOTE: The current implementation is based on Webpack but everything is ready to be ported onto other build systems (generic API is here just not yet documented). Raise an issue or better submit a PR if you have some ideas.

Installation & Usage

Install from npm:

% npm install react-css-components style-loader css-loader

Configure in webpack.config.js:

module.exports = {
  ...
  module: {
    loaders: [
      {
        test: /\.react.css$/,
        loader: 'react-css-components',
      }
    ]
  }
  ...
}

Now you can author React components in Styles.react.css:

Label {
  color: red;
}

Label:hover {
  color: white;
}

And consume them like regular React components:

import {Label} from './styles.react.css'

<Label /> // => <div class="<autogenerated classname>">...</div>

Base components

DOM components

By default React CSS Components produces styled <div /> components. You can change that by defining base: property:

FancyButton {
  base: button;
  color: red;
}

Now <FancyButton /> renders into <button />:

import {FancyButton} from './styles.react.css'

<FancyButton /> // => <button class="<autogenerated classname>">...</button>

Composite components

In fact any React components which accepts className props can be used as a base. That's means that React CSS Components can be used as theming tool for any UI library.

Example:

DangerButton {
  base: react-ui-library/components/Button;
  color: red;
}

Variants

Variants is a mechanism which allows to define styling variants for a component.

Named variants

You can define additional styling variants for your components:

Label {
  color: red;
}

Label:emphasis {
  font-weight: bold;
}

They are compiled as CSS classes which then can be controlled from JS via variant prop:

<Label variant={{emphasis: true}} /> // sets both classes with `color` and `font-weight`

JavaScript expressions

You can define variants which are conditionally applied if JS expression against props evaluates to a truthy value.

Example:

Label {
  color: red;
}

Label:prop(mode == "emphasis") {
  font-weight: bold;
}

Note that any free variable reference a member of props, thus in JS mode becomes props.mode in the example above.

They are compiled as CSS classes as well. JS expressions within prop(..) are used to determine if corresponding CSS classes should be applied to DOM:

<Label mode="emphasis" /> // sets both classes with `color` and `font-weight`

Customizing CSS loading

By default React CSS components loads CSS using style-loader!css-loader loader chain. That could be configured differently using loadCSS loader parameter.

This could be used to enable features such as CSS extraction, processing stylesheets with PostCSS/Autoprefixer or even authoring stylesheets with SASS or LESS.

CSS extraction

See the complete example which configures extract-text-webpack-plugin to extract stylesheets to a separate chunk.

Using with SASS/SCSS/LESS/Stylus/...

See the complete example which uses SASS/SCSS to create React components.

Using with PostCSS (including autoprefixer)

See the complete example which configures PostCSS with Autoprefixer to automatically add vendor prefixes to stylesheets.

react-css-components's People

Contributors

andreypopp avatar kacperkozak avatar shidhincr avatar

Watchers

 avatar  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.