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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-inline-css's Issues

ID vs. Class?

Would it be worth adding an option for the output styles to prefix an ID (e.g. #Style-4n412lnmi) instead of a class (.Style-4n412lnmi)?

Scenario being, IDs will absolutely clobber any other specificity, which is normally bad, except in one instance: when you need to sandbox styles for a component.

Right now, to do this, you can either:

  • Wrap your component an in <iframe> & deal with 10x more problems.
  • Prefix your styles with an ID # and using something like normalize.css to reset all child elements.

This way, any existing styles on the page are pretty much guaranteed not to affect your component. This is ideal for when distributing a solution like:

https://stripe.com/docs/checkout

Interesting in your thoughts!

Error: Can't resolve create-react-class

I got this error today building a CRA app on Netlify:

Module not found: Error: Can't resolve 'create-react-class' in '/opt/build/repo/node_modules/react-inline-css/src'

Compiling with Babel plugin for SASS, LESS and prefixing

One major annoyance is the lack of automatic vendor prefixing of certain CSS attributes. It doesn't really make sense to add them at run-time. It would be beneficial to the performance to pre-compile the written CSS much like JSX. At that point it might be a small step to include a SASS/LESS hook so it's possible to make the inline CSS stylesheets shorter and more composed.

Isomorphic Support

Does this module support scss for an isomorphic application such as your react starterkit?

I get the message:

Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) <div id="InlineCss-0" data-reactid=".52
(server) <div id="InlineCss-1" data-reactid=".52

I have added the modules and the site renders correctly but the id being changed does not keep the client and server in sync...?

Is there anyway to solve this?

Support for media queries?

Hi, I noticed that when using this plugin, when I insert a media query into the css string:

@media all and (max-width: 600px) {
    & .myElement {...}
}

The output in the browser does not convert & to the namespace. Is there something I am missing here? Thanks! This is a great plugin, it's very useful.

React 15 Support

var assign     = React.__spread;

'Uncaught TypeError: assign is not a function' error when used with React 15.

Unhandled promise rejection error

I have followed your example for importing less files. And, I get this error: Unhandled promise rejection TypeError: stylesheet.replace is not a function

Integrate Autoprefixer

Any chance of running the CSS through autoprefixer before it's output into the DOM? That would be awesome!

From what I can tell, it's only async.

Local files not loading on Meteor

Images stored locally don't show.
<img src="images/notfound.jpg" className="bg" />
But those hosted remotely work just fine.
<img src="http://vectorpatterns.co.uk/wp-content/uploads/2012/06/greencirclepattern_thumb.png" className="bg" />

React.createClass is deprecated

React is starting to complain that this package still uses React.createClass, it should be replaced with an ES6 class or the create-react-class package.

Warning: React attempted to reuse markup in a container but the checksum was invalid issue

HI,

Thanks for this module first of all, new to react, it's helping me organise CSS quite well. Running into the following issue building a Universal (isomorphoc) js app though, I'm getting the following warning when I add InlineCss to a component:

Warning: React attempted to reuse markup in a container but the checksum was invalid. 
This generally means that you are using server rendering and the markup generated on 
the server was not what the client was expecting. React injected new markup to 
compensate which works but you have lost many of the benefits of server rendering. 
Instead, figure out why the markup being generated is different on the client or server:
(client) ><div id="InlineCss-0" data-reactid=".p7
(server) ><div id="InlineCss-1" data-reactid=".p7

I've narrowed it down to adding InlineCSS to the following render method:

render() {
   const listNumber = this.props.listNumber;

   return (
      <InlineCss stylesheet={TextInput.css()}>
         <div>
            <span className="list-number">{listNumber}.</span>
            <div className="question">{this.props.label} {this.props.required ? '*' : ''}</div>
            <input type="text" />
         </div>
       </InlineCss>
   );
}

Have you any advice on debugging this? Is there a problem with using InlineCss on nested components or repeated components ?

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.