All Projects → mbasso → React Cssom

mbasso / React Cssom

Licence: mit
Css selector for React Components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Cssom

React Styling Hoc
Механизм темизации для React-компонентов, написанных с использованием CSS модулей.
Stars: ✭ 25 (-56.14%)
Mutual labels:  style, css-modules
React Flexbox Grid
A set of React components implementing flexboxgrid with the power of CSS Modules.
Stars: ✭ 2,858 (+4914.04%)
Mutual labels:  react-components, css-modules
CustomWebCheckbox
An example of a make checkbox design on the web.
Stars: ✭ 12 (-78.95%)
Mutual labels:  style, css-selector
React Atlas
Composable React components with CSS Modules.
Stars: ✭ 36 (-36.84%)
Mutual labels:  react-components, css-modules
Electron Basic Ui Layout
🐙 Common UI layout for an Electron/React app
Stars: ✭ 51 (-10.53%)
Mutual labels:  style
Push Starter
React Redux Starter with SSR 🤖
Stars: ✭ 43 (-24.56%)
Mutual labels:  css-modules
React Bulma Components
React components for Bulma framework
Stars: ✭ 1,015 (+1680.7%)
Mutual labels:  react-components
Should Enzyme
Useful functions for testing React Components with Enzyme.
Stars: ✭ 41 (-28.07%)
Mutual labels:  react-components
Deep Viz
A React component library, providing concise and beautiful diversity charts with Canvas, SVG, E-map, WebGL, Dom, based on data visualization experience and commercial data display practice.
Stars: ✭ 55 (-3.51%)
Mutual labels:  react-components
React Boilerplate
Production-ready boilerplate for building universal web apps with React and Redux
Stars: ✭ 53 (-7.02%)
Mutual labels:  css-modules
React Bscroll
inspired by betterscroll, and fullfilled by React
Stars: ✭ 49 (-14.04%)
Mutual labels:  react-components
H5ui
Lightweight, elegant open source mobile UI style library.
Stars: ✭ 44 (-22.81%)
Mutual labels:  style
Paragon
💎 Accessible components done right.
Stars: ✭ 52 (-8.77%)
Mutual labels:  react-components
React Redux Starter Kit
My best-practices-included universal frontend starter kit
Stars: ✭ 1,018 (+1685.96%)
Mutual labels:  css-modules
React Form Controlled
Intuitive react forms for building powerful applications.
Stars: ✭ 53 (-7.02%)
Mutual labels:  react-components
Axiom React
Axiom - Brandwatch design system and React pattern library
Stars: ✭ 41 (-28.07%)
Mutual labels:  react-components
Nord Highlightjs
An arctic, north-bluish clean and elegant highlight.js theme.
Stars: ✭ 49 (-14.04%)
Mutual labels:  style
Ripple Without Js
Create Material Design ripple effect in your HTML without using a single line of JS.
Stars: ✭ 53 (-7.02%)
Mutual labels:  css-selector
Vitaminjs
🍍 The build toolchain against JavaScript Fatigue
Stars: ✭ 48 (-15.79%)
Mutual labels:  css-modules
Modest
CSS selectors for HTML5 Parser myhtml
Stars: ✭ 47 (-17.54%)
Mutual labels:  css-selector

react-cssom

Build Status npm version npm downloads Coverage Status Join the chat at https://gitter.im/mbasso/react-cssom

Css selector for React Components

Preview

Motivation

Styling components is a critical point when we decide to develop in React. There are a lot of ways in which we can do this but all of these have pros and cons. In some situations this means having a strong connection with the source code, that is not so good. With ReactCSSOM we have tried to develop a system that allows us to separate js and css.

Using ReactCSSOM means:

  • Scoped styles based on component's structure
  • Each Component has its own style.css
  • No extra inline styles, use them only when and where you really need, this means better performance
  • No limitations to CSS power
  • Easy to use with CSS Preprocessors (no more stuff needed)
  • Highly expressive
  • Lightweight, only 88 lines of js

Installation

You can install react-cssom using npm:

npm install --save react-cssom

If you aren't using npm in your project, you can include ReactCSSOM using UMD build in the dist folder with <script> tag.

Usage

Basic

Once you have installed react-cssom, supposing a CommonJS environment, you can import it in your index file, before ReactDOM.render and App import.

import React from 'react';
import ReactDOM from 'react-dom';
import ReactCSSOM from 'react-cssom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

Now you can use react-cssom as you can see in the gif above, just like normal css, but with a special selector for React Components. You can load styles in the way that you prefer but is important to keep in mind that selectors must be in this form:

.ComponentName

For example, this is a valid one .⚛App. So, pay attention that components' names and css selectors always match. This is particular important if you have css-modules that modifies the original names, or code obfuscation.

The first ones for example need a syntax like this:

:global .ComponentName {
	/* styles here */
}

And the second one, considering for example a minification process with webpack's UglifyJsPlugin (see here for more information), need a component with a displayName attribute like this:

class ComponentName extends React.Component {

	static propTypes = {
		// propTypes...
	}

	// this
	static displayName = 'ComponentName';

	render() {
		// render here...
	}
}

// or this
// ComponentName.displayName = 'ComponentName';

Please, note also that if you are using a functional stateless component, you have to wrap it with our function to make it work. This function accepts the component as parameter of the first function and its name as parameter of its return:

import ReactCSSOM from 'react-cssom';

const Foo = ReactCSSOM.inject((props) => (
	<div>
		Hello {props.name}
	</div>
))('Foo');

Adapting based on props

If you want to set styles based on props, you can do it in 2 ways:

  • Setting a specific class, maybe using css-modules, as we can see here:
import styles from './Button.css';

export default class Button extends React.Component {

	static displayName = 'Button';

	render() {
		return (
			<button
				className={this.props.primary ? styles.primary : styles.default}
			>
				Click me
			</button>
		)
	}
}

and here is the corresponding css, note the global selector:

:global .Button {
	height: 50px;
	width: 100px;
}

.primary {
	background-color: blue;
}

.primary:global.Button {
	color: yellow;
}

.default {
	background-color: grey;
}

.default:global.Button {
	color: black;
}
  • Setting inline styles, as we can see in this example:
class Button extends React.Component {

	static displayName = 'Button';

	render() {
		return (
			<button
				style={{
					backgroundColor: this.props.primary ? 'blue' : 'black',
				}}
			>
				Click me
			</button>
		)
	}
}

Change Log

This project adheres to Semantic Versioning.
Every release, along with the migration instructions, is documented on the Github Releases page.

Authors

Matteo Basso

Copyright and License

Copyright (c) 2016, Matteo Basso.

react-cssom source code is licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].