Git Product home page Git Product logo

react-inline-css's Introduction

screenshot

React Inline CSS

Make your React components visually predictable. React Inline CSS allows you to write traditional CSS stylesheets in your components, automatically namespacing them for you.

Inspired by the SUIT CSS methodology.

Demo

Mao-mao-mao!

Example

You write:

var Profile = React.createClass({
	render: function () {
		return (
			<InlineCss stylesheet={`
				& .card {
					cursor: pointer;
					margin: 15px;
					padding: 15px;
					text-align: center;
					height: 200px;
				}
				& img {
					width: 130px;
					height: 130px;
				}
				& p {
					margin: 10px;
				}
				`}>
				<div className="card">
					<img src="mao.jpg" />
					<p>Mao</p>
				</div>
			</InlineCss>
		);
	}
});

You get namespaced CSS that works on sub-components (comparable to HTML5 <style scoped>):

<div id="InlineCss-1">
	<div class="card">
		<img src="mao.jpg">
		<p>Mao</p>
	</div>
	<style>
		#InlineCss-1 .card { 
		  cursor: pointer; 
		  margin: 15px; 
		  padding: 15px; 
		  text-align: center; 
		  height: 200px; 
		}
		#InlineCss-1 img { 
		  width: 130px; 
		  height: 130px; 
		}
		#InlineCss-1 p { 
		  margin: 10px; 
		}
	</style>
</div>

For a cascaded effect, see the index.html demo.

Installation

npm install --save react-inline-css react

Usage

Run npm run watch in your terminal and play with example/ to get a feel of react-inline-css.

SASS / LESS

You can override the & as the selector to the current component. This is useful if you want to require precompiled stylesheets from an external file. Here's an example with SASS loader for Webpack:

UserComponent.js

import React from "react";
import InlineCss from "react-inline-css";
const stylesheet = require("!raw!sass!./UserComponent.scss");

class UserComponent extends React.Component {
	render () {
		return (
			<InlineCss componentName="UserComponent" stylesheet={stylesheet}>
				<div className="facebook">Mao is no longer red!</div>
				<div className="google">Mao is no longer red!</div>
				<div className="twitter">Mao is no longer red!</div>
			</InlineCss>
		);
	}
};

UserComponent.scss

UserComponent {
	color: red;
	.facebook {
		color: blue;
	}
	.google {
		color: blue;
	}
	.twitter {
		color: green;
	}
}

result

screenshot

Community

Let's start one together! After you β˜…Star this project, follow me @Rygu on Twitter.

Contributors

License

BSD 3-Clause license. Copyright Β© 2015, Rick Wong. All rights reserved.

react-inline-css's People

Contributors

floriferous avatar mitchheddles avatar moret avatar rickwong avatar willbutler 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.